Files
archived-orca/src/main/runtime/runtime-managed-worktree-queries.ts
T
Neil 15717a4e53 Add CLI commands to manage and find workspace references (#26807)
* Add CLI commands to manage and find workspace references

* Fix reference review findings: source-context twins, legacy link collapse, closed chats, old-host errors

- Keep same-URL links with different source contexts separate; CLI add/remove no longer drops a twin
- Restore merging a bare legacy link into its single matching URL link (no duplicate PR rows, no resurrection on older-client removal)
- Carry absorbed data through collapse chains; resolve repoId conflicts deterministically
- Identity: ignore www., case-insensitive GitHub/GitLab/Gitea paths (stored URL keeps case), no Jira match on git hosts; find accepts self-hosted issue URLs
- reference find: skip closed native chats, require a session id for linked, include an explicitly named archived workspace, warm-cache fallback for --worktree
- Selector parity: collapse same-host duplicate paths, issue: ignores unlinked rows
- CLI: clear update-host error for hosts without reference.*, local flag checks before host probe, help text fixes

* Fix CI: add reference rm alias for verb policy; expect linkedItems in worktree ps summary

* Simplify reference CLI: share identifier/legacy-match helpers, drop redundant dedupe
2026-10-09 22:22:01 -07:00

311 lines
13 KiB
TypeScript

import type { DetectedWorktreeListResult, Worktree } from '../../shared/worktree/types'
import {
getLocalWorktreeCatalogVersion,
localWorktreeCatalogVersionAt
} from '../local-worktree-scan-generation'
import type { Repo } from '../../shared/repo-types'
import type { RuntimeWorktreeListResult } from '../../shared/runtime-types'
import { getRepoExecutionHostId, type ExecutionHostId } from '../../shared/execution-host'
import { buildWorktreeListingPage, listingKnownHostIds } from './worktree-listing-host-scope'
import { readWorktreeMetaForHost } from '../persistence/host-qualified-worktree-meta'
import { getRepoOwnedWorktreeMeta } from '../worktree-metadata-ownership'
import type { WorktreeMeta } from '../../shared/worktree/meta-types'
import { isFolderRepo } from '../../shared/repo-kind'
import {
applyMetadataFallbackVisibility,
buildKnownOrcaWorkspaceLayouts,
isLegacyRepoForExternalWorktreeVisibility,
toDetectedWorktree
} from '../../shared/worktree/ownership'
import { projectResolvedWorktreeLineage } from '../../shared/resolved-worktree-lineage'
import {
createWorktreeVisibilitySourceMatcher,
resolveCustomWorktreeVisibilitySources,
type WorktreeVisibilitySourceMatcher
} from '../../shared/worktree/visibility-sources'
import { mergeWorktree } from '../ipc/worktree-logic'
import { pruneLineageForMissingRepoWorktrees } from '../worktree-lineage-pruning'
import { pruneMetadataMissingFromAuthoritativeLocalScan } from '../ipc/worktrees/listing/authoritative-local-worktree-metadata-pruning'
import type { Store } from '../persistence'
import type { RuntimeStore } from './runtime-store-contract'
import { getWorkspaceAttachments } from '../../shared/workspace-attachments'
import type { RuntimeWorktreeScanResult } from './repo-worktree-resolution-scan'
import { scanRuntimeWorktreesUntilNotOvertaken } from './runtime-witnessed-worktree-scan'
import { listRuntimeFolderWorkspaces } from './runtime-worktree-filesystem'
import type { ResolvedWorktree } from './runtime-worktree-path-identity'
import { resolveConfiguredWorktreeBasePaths } from '../../shared/worktree/configured-worktree-base-path'
import { getRetiredNameRegistryForRepo } from '../worktree-name-retirement'
type Dependencies = {
getStore(): RuntimeStore | null
listResolved(): Promise<ResolvedWorktree[]>
resolveRepo(selector: string): Promise<Repo>
selectRepos(selector: string): Repo[]
scanRepo(repo: Repo): Promise<RuntimeWorktreeScanResult>
/** Hosts this runtime has repos or workspaces on, so a host with no rows is still named. */
listKnownHostIds(): Iterable<ExecutionHostId>
}
export class RuntimeManagedWorktreeQueries {
constructor(private readonly deps: Dependencies) {}
async list(
repoSelector: string | undefined,
limit: number,
sourceDefaultsSupported = true
): Promise<RuntimeWorktreeListResult> {
if (!Number.isInteger(limit) || limit <= 0) {
throw new Error('invalid_limit')
}
const resolved = await this.deps.listResolved()
const scopedRepo = repoSelector ? await this.deps.resolveRepo(repoSelector) : null
const pathsByRepo = new Map<string, string[]>()
for (const worktree of resolved) {
const paths = pathsByRepo.get(worktree.repoId) ?? []
paths.push(worktree.path)
pathsByRepo.set(worktree.repoId, paths)
}
const visibilityDefaults = this.visibilityDefaults(sourceDefaultsSupported)
const matchers = new Map(
(this.deps.getStore()?.getRepos() ?? []).map((repo) => [
repo.id,
createWorktreeVisibilitySourceMatcher(
[repo.path, ...(pathsByRepo.get(repo.id) ?? [])],
resolveCustomWorktreeVisibilitySources(repo, visibilityDefaults),
resolveConfiguredWorktreeBasePaths(repo)
)
])
)
const worktrees = resolved.filter(
(worktree) =>
(!scopedRepo || worktree.repoId === scopedRepo.id) &&
this.isVisible(worktree, matchers.get(worktree.repoId), sourceDefaultsSupported)
)
// See `listingKnownHostIds`: a scoped listing must still name the host it was asked about.
const knownHostIds = listingKnownHostIds(scopedRepo, () => this.deps.listKnownHostIds())
const page = buildWorktreeListingPage(worktrees, limit, knownHostIds)
return {
...page,
worktrees: page.worktrees.map((worktree) => ({
...worktree,
linkedItems: getWorkspaceAttachments(worktree)
}))
}
}
resolveRepoForConnection(selector: string, connectionId?: string | null): Promise<Repo> {
if (connectionId === undefined) {
return this.deps.resolveRepo(selector)
}
const wanted = connectionId?.trim() || null
const matches = this.deps
.selectRepos(selector)
.filter((repo) => (repo.connectionId?.trim() || null) === wanted)
if (matches.length !== 1) {
throw new Error(matches.length > 1 ? 'selector_ambiguous' : 'repo_not_found')
}
return Promise.resolve(matches[0])
}
async listDetected(
repo: Repo,
sourceDefaultsSupported = true
): Promise<DetectedWorktreeListResult> {
const store = this.deps.getStore()
if (!store) {
throw new Error('runtime_unavailable')
}
const settings = store.getSettings()
const visibilityDefaults = this.visibilityDefaults(sourceDefaultsSupported)
const visibilitySettings = { ...settings, worktreeVisibilityDefaults: visibilityDefaults }
if (isFolderRepo(repo)) {
const worktrees = listRuntimeFolderWorkspaces(store, repo)
const metaById = store.getAllWorktreeMeta()
const repoOwnerCount = store.getRepos().filter((candidate) => candidate.id === repo.id).length
const matcher = createWorktreeVisibilitySourceMatcher(
[repo.path, ...worktrees.map((worktree) => worktree.path)],
resolveCustomWorktreeVisibilitySources(repo, visibilityDefaults),
resolveConfiguredWorktreeBasePaths(repo)
)
const detected = worktrees.map((worktree) =>
this.toDetected(
repo,
worktree,
matcher,
sourceDefaultsSupported,
visibilitySettings,
getRepoOwnedWorktreeMeta(repo, worktree.id, metaById, repoOwnerCount) ?? null
)
)
return {
repoId: repo.id,
authoritative: true,
source: 'git',
catalogVersion: getLocalWorktreeCatalogVersion(repo.id),
worktrees: projectResolvedWorktreeLineage(detected, store.getAllWorktreeLineage?.() ?? {})
}
}
const scan = await scanRuntimeWorktreesUntilNotOvertaken(store, repo, (target) =>
this.deps.scanRepo(target)
)
// Why a still-overtaken scan is published non-authoritative rather than rejected: this method
// has no stale reply, and a thrown error makes the client drop the repo's rows. Non-authoritative
// rows purge nothing and light no indicator; the overtaking mutation's own change event, sent
// after its generation bump, brings the listing that reflects it. Nothing is pruned from rows
// that omit a worktree created under the scan.
if (scan.ok && !scan.superseded) {
// Why the runtime sweeps too: the desktop listing that used to own this runs off `ipcMain`,
// so a headless host -- which has no renderer -- never pruned its own repos' rows (#17776).
if (scan.metadataPruneExpectation) {
await pruneMetadataMissingFromAuthoritativeLocalScan({
store: store as unknown as Store,
repo,
gitWorktrees: scan.worktrees,
scan: scan.metadataPruneExpectation,
scanGeneration: scan.scanGeneration
})
}
pruneLineageForMissingRepoWorktrees(store as unknown as Store, repo, scan.worktrees)
}
const matcher = createWorktreeVisibilitySourceMatcher(
[repo.path, ...scan.worktrees.map((worktree) => worktree.path)],
resolveCustomWorktreeVisibilitySources(repo, visibilityDefaults),
resolveConfiguredWorktreeBasePaths(repo)
)
const expectedHostId = getRepoExecutionHostId(repo)
const repoOwnerCount = store.getRepos().filter((candidate) => candidate.id === repo.id).length
const metaById = store.getAllWorktreeMeta()
const detected = scan.worktrees.map((gitWorktree) => {
const id = `${repo.id}::${gitWorktree.path}`
const meta =
readWorktreeMetaForHost(store as unknown as Store, id, expectedHostId) ??
getRepoOwnedWorktreeMeta(repo, id, metaById, repoOwnerCount)
const worktree = {
...mergeWorktree(repo.id, gitWorktree, meta, repo.displayName),
hostId: repoOwnerCount === 1 ? (meta?.hostId ?? expectedHostId) : expectedHostId
}
const result = this.toDetected(
repo,
worktree,
matcher,
sourceDefaultsSupported,
visibilitySettings,
meta ?? null
)
return scan.ok ? result : applyMetadataFallbackVisibility(result)
})
return {
repoId: repo.id,
authoritative: scan.ok && !scan.superseded,
source: scan.ok ? 'git' : 'metadata-fallback',
// Why the scan's generation: the rows describe the catalog as of when the scan began.
catalogVersion: localWorktreeCatalogVersionAt(scan.scanGeneration),
worktrees: projectResolvedWorktreeLineage(detected, store.getAllWorktreeLineage?.() ?? {})
}
}
isVisible(
worktree: Worktree,
matcher?: WorktreeVisibilitySourceMatcher,
sourceDefaultsSupported = true,
providedSettings?: ReturnType<RuntimeStore['getSettings']>
): boolean {
const repo = this.deps.getStore()?.getRepo(worktree.repoId)
return repo
? this.toDetected(repo, worktree, matcher, sourceDefaultsSupported, providedSettings).visible
: true
}
buildVisibilityMatchers(
worktrees: readonly Worktree[],
sourceDefaultsSupported = true,
providedSettings?: ReturnType<RuntimeStore['getSettings']>
): Map<string, WorktreeVisibilitySourceMatcher> {
const checkoutPathsByRepoId = new Map<string, string[]>()
for (const worktree of worktrees) {
const checkoutPaths = checkoutPathsByRepoId.get(worktree.repoId) ?? []
checkoutPaths.push(worktree.path)
checkoutPathsByRepoId.set(worktree.repoId, checkoutPaths)
}
const visibilityDefaults = this.visibilityDefaults(sourceDefaultsSupported, providedSettings)
return new Map(
(this.deps.getStore()?.getRepos() ?? [])
.filter((repo) => checkoutPathsByRepoId.has(repo.id))
.map((repo) => [
repo.id,
createWorktreeVisibilitySourceMatcher(
[repo.path, ...(checkoutPathsByRepoId.get(repo.id) ?? [])],
resolveCustomWorktreeVisibilitySources(repo, visibilityDefaults),
resolveConfiguredWorktreeBasePaths(repo)
)
])
)
}
private toDetected(
repo: Repo,
worktree: Worktree,
matcher?: WorktreeVisibilitySourceMatcher,
sourceDefaultsSupported = true,
providedSettings?: ReturnType<RuntimeStore['getSettings']>,
providedMeta?: WorktreeMeta | null
) {
const store = this.deps.getStore()
const settings = providedSettings ?? store?.getSettings()
if (!settings) {
return {
...worktree,
ownership: 'unknown-legacy' as const,
selectedCheckout: false,
visible: true
}
}
const visibilityDefaults = this.visibilityDefaults(sourceDefaultsSupported, settings)
return toDetectedWorktree({
repo,
worktree,
meta:
providedMeta === undefined
? store?.getWorktreeMeta(worktree.id)
: (providedMeta ?? undefined),
settings: { ...settings, worktreeVisibilityDefaults: visibilityDefaults },
knownOrcaLayouts: buildKnownOrcaWorkspaceLayouts(settings, repo),
isLegacyRepoForVisibility: isLegacyRepoForExternalWorktreeVisibility(repo),
worktreeVisibilitySourceMatcher: matcher
})
}
async listRetiredNames(repoSelector: string): Promise<{
retiredNamesByRepo: Record<string, readonly string[]>
retiredNameTiersByRepo: Record<string, number>
}> {
const store = this.deps.getStore()
if (!store?.getRetiredWorktreeNameRegistry || !store.mergeRetiredWorktreeNames) {
return { retiredNamesByRepo: {}, retiredNameTiersByRepo: {} }
}
const repo = await this.deps.resolveRepo(repoSelector)
const settings = store.getSettings()
const registry = await getRetiredNameRegistryForRepo(
store as never,
repo,
store.getRepos(),
settings
)
return {
retiredNamesByRepo: { [repo.id]: registry.names },
retiredNameTiersByRepo: { [repo.id]: registry.exhaustedTiers }
}
}
private visibilityDefaults(
sourceDefaultsSupported: boolean,
providedSettings?: ReturnType<RuntimeStore['getSettings']>
) {
const defaults =
providedSettings !== undefined
? providedSettings.worktreeVisibilityDefaults
: this.deps.getStore()?.getSettings().worktreeVisibilityDefaults
return sourceDefaultsSupported || !defaults ? defaults : { external: defaults.external }
}
}