Files
orca/src/main/worktree-retirement-namespace.test.ts
T
Brennan Benson a7f1653415 fix(worktrees): keep retirement tombstones across project and SSH target re-add (#14917)
* fix(worktrees): keep retirement tombstones across project and SSH target re-add

Generated workspace names are retired so a name is never reissued onto a cwd
that still holds another workspace's Claude/Codex history. Two re-add paths
lost that record.

STA-4449 (local): retirement was stored only under `repo.id`. Removing a
project deletes that row and re-adding the same path mints a new id, so the
new repo starts with an empty registry. The on-disk backfill normally
re-seeds local repos, but it cannot recover a name whose only surviving
evidence is a Codex rollout JSONL — those are deliberately not scanned — so a
name spent under Codex with its workspace directory gone came back.

STA-4491 (SSH): the second, path-derived copy embedded the SSH target row id.
Row ids are minted fresh on every re-add, so `ssh:ssh-old:...` became
`ssh:ssh-new:...` and `reassignSshTargetId` migrated other carrier state but
not the retirement namespaces.

Keying the store on the namespace instead of `repo.id` was rejected in
`nestWorkspaces`, `worktreeBasePath` and `repo.path`, so a settings toggle
would orphan every retirement at once. `repo.id` therefore stays primary and
the path-derived namespace stays a mirror — a settings toggle loses the
mirror but keeps the repo row, a re-add loses the repo row but keeps the
mirror, and reads union both.

- Mirror local repos into the namespace too, not just remote ones.
- Key the namespace's host half on the SSH endpoint (host+port+username), the
  thing that actually decides which filesystem a path lands on, instead of the
  target row id. Reads also accept the pre-identity key so an upgrade keeps
  tombstones it already wrote, and `reassignSshTargetId` re-keys the rest.
- Cap the namespace map, which by design outlives the repos that wrote it and
  so has nothing to prune it per repo.

Endpoint identity is extracted from ssh-target-readoption.ts, which already
compared these fields for exactly the same reason, so re-adoption and
retirement cannot drift apart.

* fix(worktrees): copy shared SSH endpoint retirements instead of moving them

An endpoint identity is not owned by the target row that rotates: nothing
dedupes SSH targets by host|port|username, so a second live target can still
resolve to the same host. Moving the bucket stripped that target's tombstones
and reissued a path whose agent history is still on disk.

Row-id identities stay a move — reassignment leaves nothing pointing at them.

* fix(worktrees): carry retirement mirror across in-place SSH endpoint edits

Config sync rewrites host/port/username on the existing target row and a
runtime-owned target takes a fresh address from every provision, both keeping
the row id. No re-adoption runs, so nothing carried the endpoint-keyed mirror
across and it stranded — strictly worse than the pre-change key, which was the
row id and was invariant under these edits.

Also bound the map after a migration: a retained source bucket grows it, so the
cap has to be applied there too, and compare registries by membership rather
than size so an uncompacted destination cannot trade a folded name for a new
one and read as unchanged.

* fix(worktrees): skip retirement migration for on-demand runtime targets

An on-demand VM is discarded between provisions, so its fresh address reaches an
empty filesystem and a reissued name collides with nothing. Migrating there
would spend names against history that no longer exists, and because each
provision mints another address it would add a namespace bucket per run,
evicting the real tombstones of local and ordinary SSH repos.

* fix(worktrees): stop the namespace cap evicting what a migration just wrote

Two defects with one root cause. Assigning to an existing key leaves it in its
original insertion slot, so a merged destination kept the oldest position and the
trim deleted the bucket it had just enriched. Retained source buckets are older
than the destinations a copy appends, so at the cap the trim removed exactly the
sources the copy existed to keep — silently turning it back into a move. The trim
now exempts the keys the migration wrote or deliberately kept.

Also stop on-demand runtime workspaces writing namespace mirrors at all: each
provision reaches a discarded filesystem under a fresh address, so the entry can
never be read back and only spends a capped slot that a local or SSH project
needs. The repo-id row still records the name for the live session.

* fix(worktrees): re-insert migrated namespaces so the cap cannot undo a migration

Exempting keys from the trim protected them for that one call and no other. A
merged destination keeps its original insertion slot, so it sat at the front of
the eviction queue and the next unrelated retirement write dropped it — losing
both the migrated name and the name the destination already held, on a host that
had just been re-added.

Re-insert what the migration writes instead, the same discipline the ordinary
writer already follows, so insertion order reflects use. That also removes the
exemption, which could otherwise leave the map stuck at twice the cap until one
later write evicted the whole excess at once.

Corrects the runtime-gate comment as well: the mirror is unreadable after the
next provision, not immediately, so a remove/re-add inside one provision is a
real if narrow loss.

* fix(worktrees): refresh a retained namespace source even when its merge adds nothing

Replacing the trim exemption with re-insertion narrowed the protection: the
exemption covered every retained source, the re-insertion only covered sources
whose merge actually wrote. A copy whose destination already held the same names
was then neither re-inserted nor exempt, so the migration's own trim evicted the
shared source bucket ahead of hundreds of untouched ones — losing the tombstones
of a live sibling target still on that endpoint, which is what copying exists to
prevent.

A move's destination gets the same treatment: deleting the source makes it the
only remaining copy, so it has been used. Both are order-only and deliberately do
not set the changed flag, keeping an import that moved nothing from scheduling a
save.
2026-08-17 12:23:50 -07:00

327 lines
13 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import type { RetiredNameRegistry } from '../shared/worktree/retired-name-registry'
import {
MAX_RETIREMENT_NAMESPACES,
migrateRetirementNamespaceHostIdentity,
recordRetirementNamespaceRegistry,
retirementHostIdentity,
retirementNamespaceKey,
UNKNOWN_SSH_HOST_IDENTITY
} from './worktree-retirement-namespace'
function registry(...names: string[]): RetiredNameRegistry {
return { exhaustedTiers: 0, names }
}
const TARGET = {
configHost: 'builder',
host: 'builder.example.com',
port: 22,
username: 'dev'
}
describe('recordRetirementNamespaceRegistry', () => {
it('evicts the least recently recorded namespace once the map is full', () => {
const namespaces: Record<string, RetiredNameRegistry> = {}
for (let index = 0; index < MAX_RETIREMENT_NAMESPACES; index += 1) {
recordRetirementNamespaceRegistry(namespaces, `local:posix:/w/${index}`, registry('nautilus'))
}
recordRetirementNamespaceRegistry(namespaces, 'local:posix:/w/new', registry('seahorse'))
expect(Object.keys(namespaces)).toHaveLength(MAX_RETIREMENT_NAMESPACES)
expect(namespaces['local:posix:/w/0']).toBeUndefined()
expect(namespaces['local:posix:/w/1']).toEqual(registry('nautilus'))
expect(namespaces['local:posix:/w/new']).toEqual(registry('seahorse'))
})
it('refreshes a namespace it rewrites so an actively used repo is never the eviction victim', () => {
const namespaces: Record<string, RetiredNameRegistry> = {}
for (let index = 0; index < MAX_RETIREMENT_NAMESPACES; index += 1) {
recordRetirementNamespaceRegistry(namespaces, `local:posix:/w/${index}`, registry('nautilus'))
}
// The oldest key retires a second name, then a brand new namespace forces one eviction.
recordRetirementNamespaceRegistry(namespaces, 'local:posix:/w/0', registry('nautilus', 'orca'))
recordRetirementNamespaceRegistry(namespaces, 'local:posix:/w/new', registry('seahorse'))
expect(namespaces['local:posix:/w/0']).toEqual(registry('nautilus', 'orca'))
expect(namespaces['local:posix:/w/1']).toBeUndefined()
})
it('stays at one entry per namespace no matter how often it is rewritten', () => {
const namespaces: Record<string, RetiredNameRegistry> = {}
for (let index = 0; index < MAX_RETIREMENT_NAMESPACES * 2; index += 1) {
recordRetirementNamespaceRegistry(namespaces, 'local:posix:/w/a', registry(`name-${index}`))
}
expect(Object.keys(namespaces)).toEqual(['local:posix:/w/a'])
})
})
describe('migrateRetirementNamespaceHostIdentity', () => {
it('folds a migrated namespace into one the new identity already owns', () => {
const namespaces = {
'ssh:old-id:posix:/srv/a': registry('nautilus'),
'ssh:new|22|dev:posix:/srv/a': registry('seahorse')
}
expect(
migrateRetirementNamespaceHostIdentity(namespaces, {
moveFrom: ['ssh:old-id'],
to: 'ssh:new|22|dev'
})
).toBe(true)
expect(Object.keys(namespaces)).toEqual(['ssh:new|22|dev:posix:/srv/a'])
expect(namespaces['ssh:new|22|dev:posix:/srv/a'].names.toSorted()).toEqual([
'nautilus',
'seahorse'
])
})
it('keeps the source bucket when an endpoint identity moves, since a live target may share it', () => {
// A second target can still resolve to `old|22|dev`. Stripping its tombstones would reissue a
// path whose agent history is still on disk — the one outcome retirement exists to prevent.
const namespaces = {
'ssh:old|22|dev:posix:/srv/a': registry('nautilus')
}
expect(
migrateRetirementNamespaceHostIdentity(namespaces, {
copyFrom: ['ssh:old|22|dev'],
to: 'ssh:new|22|dev'
})
).toBe(true)
expect(Object.keys(namespaces).toSorted()).toEqual([
'ssh:new|22|dev:posix:/srv/a',
'ssh:old|22|dev:posix:/srv/a'
])
expect(namespaces['ssh:old|22|dev:posix:/srv/a'].names).toEqual(['nautilus'])
expect(namespaces['ssh:new|22|dev:posix:/srv/a'].names).toEqual(['nautilus'])
})
it('reports no change when a repeated copy adds nothing the destination lacks', () => {
// Re-import runs this on every add; a no-op copy must not schedule a save.
const namespaces = {
'ssh:old|22|dev:posix:/srv/a': registry('nautilus'),
'ssh:new|22|dev:posix:/srv/a': registry('nautilus')
}
expect(
migrateRetirementNamespaceHostIdentity(namespaces, {
copyFrom: ['ssh:old|22|dev'],
to: 'ssh:new|22|dev'
})
).toBe(false)
// Reporting "no change" must mean it left both buckets intact, not that it emptied them.
expect(Object.keys(namespaces).toSorted()).toEqual([
'ssh:new|22|dev:posix:/srv/a',
'ssh:old|22|dev:posix:/srv/a'
])
expect(namespaces['ssh:old|22|dev:posix:/srv/a'].names).toEqual(['nautilus'])
expect(namespaces['ssh:new|22|dev:posix:/srv/a'].names).toEqual(['nautilus'])
})
it('still copies a name the destination folded into its watermark', () => {
// A destination stored uncompacted trades a folded name for the incoming one, so comparing
// sizes alone would read as unchanged and drop the copy.
const namespaces = {
'ssh:old|22|dev:posix:/srv/a': registry('seahorse-2'),
'ssh:new|22|dev:posix:/srv/a': { exhaustedTiers: 1, names: ['nautilus'] }
}
expect(
migrateRetirementNamespaceHostIdentity(namespaces, {
copyFrom: ['ssh:old|22|dev'],
to: 'ssh:new|22|dev'
})
).toBe(true)
expect(namespaces['ssh:new|22|dev:posix:/srv/a'].names).toContain('seahorse-2')
})
it('keeps the namespace map within its cap by evicting bystanders, not the copy itself', () => {
// The cap must not turn a copy back into a move: the source buckets are older than the
// destinations just appended, so a naive oldest-first trim would delete exactly what the copy
// was retaining for a live sibling target.
// The source buckets are the OLDEST entries, so an oldest-first trim would take them first —
// which is precisely the copy collapsing back into a move.
const namespaces: Record<string, RetiredNameRegistry> = {
'ssh:old|22|dev:posix:/srv/a': registry('nautilus'),
'ssh:old|22|dev:posix:/srv/b': registry('manta')
}
for (let index = 0; index < MAX_RETIREMENT_NAMESPACES - 2; index += 1) {
namespaces[`local:posix:/w/${index}`] = registry('dolphin')
}
expect(
migrateRetirementNamespaceHostIdentity(namespaces, {
copyFrom: ['ssh:old|22|dev'],
to: 'ssh:new|22|dev'
})
).toBe(true)
expect(Object.keys(namespaces).length).toBe(MAX_RETIREMENT_NAMESPACES)
// Both halves of the copy survive; the two oldest bystanders are what give way.
expect(namespaces['ssh:old|22|dev:posix:/srv/a']).toBeDefined()
expect(namespaces['ssh:old|22|dev:posix:/srv/b']).toBeDefined()
expect(namespaces['ssh:new|22|dev:posix:/srv/a']).toBeDefined()
expect(namespaces['ssh:new|22|dev:posix:/srv/b']).toBeDefined()
expect(namespaces['local:posix:/w/0']).toBeUndefined()
expect(namespaces['local:posix:/w/1']).toBeUndefined()
expect(namespaces['local:posix:/w/2']).toBeDefined()
})
it('refreshes a retained source even when its own merge added nothing', () => {
// Mixed migration: /srv/a is a no-op because the destination already covers it, /srv/b writes and
// grows the map. The no-op source is still the bucket a live sibling on the old endpoint reads,
// so the trim must take a stale bystander instead of it.
const namespaces: Record<string, RetiredNameRegistry> = {
'ssh:old|22|dev:posix:/srv/a': registry('seahorse'),
'ssh:new|22|dev:posix:/srv/a': registry('seahorse'),
'ssh:old|22|dev:posix:/srv/b': registry('nautilus')
}
for (let index = 0; index < MAX_RETIREMENT_NAMESPACES - 3; index += 1) {
namespaces[`local:posix:/w/${index}`] = registry('dolphin')
}
migrateRetirementNamespaceHostIdentity(namespaces, {
copyFrom: ['ssh:old|22|dev'],
to: 'ssh:new|22|dev'
})
expect(Object.keys(namespaces).length).toBe(MAX_RETIREMENT_NAMESPACES)
expect(namespaces['ssh:old|22|dev:posix:/srv/a']).toBeDefined()
expect(namespaces['local:posix:/w/0']).toBeUndefined()
})
it('refreshes a move destination that the move just made the only copy', () => {
// The merge is a no-op, but deleting the source leaves this destination holding the sole copy —
// so it has been used and must not stay at the front of the eviction queue.
const namespaces: Record<string, RetiredNameRegistry> = {
'ssh:new|22|dev:posix:/srv/a': registry('seahorse'),
'local:posix:/w/0': registry('dolphin'),
'ssh:old-id:posix:/srv/a': registry('seahorse')
}
migrateRetirementNamespaceHostIdentity(namespaces, {
moveFrom: ['ssh:old-id'],
to: 'ssh:new|22|dev'
})
expect(Object.keys(namespaces).at(-1)).toBe('ssh:new|22|dev:posix:/srv/a')
})
it('leaves a merged destination newest, so the very next ordinary write cannot evict it', () => {
// Protecting the destination only inside the migration's own trim is not enough: the key keeps
// its original slot, so the next unrelated retirement write evicts it as "oldest" and undoes
// the migration along with the name the destination already held.
const namespaces: Record<string, RetiredNameRegistry> = {
'ssh:new|22|dev:posix:/srv/a': registry('seahorse')
}
for (let index = 0; index < MAX_RETIREMENT_NAMESPACES - 2; index += 1) {
namespaces[`local:posix:/w/${index}`] = registry('dolphin')
}
namespaces['ssh:old|22|dev:posix:/srv/a'] = registry('nautilus')
migrateRetirementNamespaceHostIdentity(namespaces, {
copyFrom: ['ssh:old|22|dev'],
to: 'ssh:new|22|dev'
})
recordRetirementNamespaceRegistry(namespaces, 'local:posix:/fresh', registry('manta'))
const destination = namespaces['ssh:new|22|dev:posix:/srv/a']
expect(destination).toBeDefined()
expect(destination.names.toSorted()).toEqual(['nautilus', 'seahorse'])
})
it('does not let the cap evict a destination it just merged into', () => {
// Assigning to an existing key leaves it in its original slot, so the merged destination is the
// OLDEST entry here and an unguarded oldest-first trim would drop the name it just absorbed.
const namespaces: Record<string, RetiredNameRegistry> = {
'ssh:new|22|dev:posix:/srv/a': registry('seahorse')
}
for (let index = 0; index < MAX_RETIREMENT_NAMESPACES - 2; index += 1) {
namespaces[`local:posix:/w/${index}`] = registry('dolphin')
}
namespaces['ssh:old|22|dev:posix:/srv/a'] = registry('nautilus')
namespaces['ssh:old|22|dev:posix:/srv/b'] = registry('manta')
expect(
migrateRetirementNamespaceHostIdentity(namespaces, {
copyFrom: ['ssh:old|22|dev'],
to: 'ssh:new|22|dev'
})
).toBe(true)
const destination = namespaces['ssh:new|22|dev:posix:/srv/a']
expect(destination).toBeDefined()
expect(destination.names.toSorted()).toEqual(['nautilus', 'seahorse'])
})
it('leaves an identity that merely shares a prefix with the old one alone', () => {
// `dev` must not swallow `dev2`: the separator is part of the prefix, not an afterthought.
const namespaces = {
'ssh:h|22|dev2:posix:/srv/a': registry('nautilus')
}
expect(
migrateRetirementNamespaceHostIdentity(namespaces, {
moveFrom: ['ssh:h|22|dev'],
to: 'ssh:h|22|other'
})
).toBe(false)
expect(Object.keys(namespaces)).toEqual(['ssh:h|22|dev2:posix:/srv/a'])
})
it('reports no change when there is nothing to move or the identity is unchanged', () => {
expect(
migrateRetirementNamespaceHostIdentity(undefined, { moveFrom: ['ssh:a'], to: 'ssh:b' })
).toBe(false)
expect(
migrateRetirementNamespaceHostIdentity(
{ 'ssh:a:posix:/srv': registry('nautilus') },
{ to: 'ssh:a' }
)
).toBe(false)
expect(
migrateRetirementNamespaceHostIdentity(
{ 'ssh:a:posix:/srv': registry('nautilus') },
{ moveFrom: ['ssh:a'], to: 'ssh:a' }
)
).toBe(false)
})
})
describe('retirementHostIdentity', () => {
it('resolves an SSH repo to the endpoint its target reaches, not the target row id', () => {
const identity = retirementHostIdentity({ connectionId: 'ssh-1' }, () => TARGET)
expect(identity).toBe('ssh:builder.example.com|22|dev')
expect(retirementHostIdentity({ connectionId: 'ssh-2' }, () => TARGET)).toBe(identity)
})
it('falls back to one shared bucket when the target row is gone', () => {
expect(retirementHostIdentity({ connectionId: 'ssh-1' }, () => undefined)).toBe(
UNKNOWN_SSH_HOST_IDENTITY
)
expect(retirementHostIdentity({ connectionId: 'ssh-1' })).toBe(UNKNOWN_SSH_HOST_IDENTITY)
})
it('leaves a non-SSH repo on its execution host id', () => {
expect(retirementHostIdentity({})).toBe('local')
})
})
describe('retirementNamespaceKey', () => {
it('keeps Windows paths case- and separator-insensitive without colliding with a POSIX path', () => {
expect(retirementNamespaceKey('local', 'C:\\Workspaces\\Probe')).toBe(
retirementNamespaceKey('local', 'C:/workspaces/probe')
)
expect(retirementNamespaceKey('local', 'C:\\workspaces\\probe')).not.toBe(
retirementNamespaceKey('local', '/workspaces/probe')
)
})
it('keeps POSIX paths case-sensitive, matching the filesystems they name', () => {
expect(retirementNamespaceKey('local', '/srv/Probe')).not.toBe(
retirementNamespaceKey('local', '/srv/probe')
)
})
})