Files
orca/src/shared/external-worktree-inbox.ts
T
Brennan BensonandGldywn 6ac39b7331 fix(worktrees): make hidden agent worktrees recoverable from the visibility dialog (#13652)
* fix(worktrees): make hidden agent worktrees recoverable from the visibility dialog

A discovered agent scratch worktree (.claude/worktrees, .gsd-workspaces)
was a one-way door: the non-Orca visibility toggle never reveals scratch
by design (#9388), the inbox never announces it, and the dialog listed
nothing — so once hidden it was unreachable from the UI while sitting on
disk. The per-path import exception has outranked the hidden rule all
along; no surface offered it.

The dialog now refetches an authoritative list on open (a stale snapshot
must not read as 'nothing hidden'), lists hidden importable worktrees,
and offers a per-row Show wired to the existing inbox import action,
which already merges the import + baseline and rolls back on a failed
refresh. When the list cannot be read the dialog says so and offers a
retry instead of claiming the repo has nothing.

No new settings, schema, or persistence: recovery rides entirely on
importedExternalWorktreePaths, which every host already stores and
validates. The repo-wide toggle is untouched and still never reveals
scratch.

Fix #10324

Co-authored-by: Gldywn <14254051+Gldywn@users.noreply.github.com>

* fix(worktrees): honest scan states and race-safe row actions in the visibility dialog

- row Show stays disabled until the open-time authoritative scan settles;
  a click mid-scan could join the pre-write refetch and read success off a
  list computed before the import landed, a silent no-op on slow hosts
- checking/failed indicators follow the scan state alone, so a warm older
  snapshot cannot present stale rows as current with no failure indication
- ownership-neutral section copy: non-scratch rows are listed too when the
  repo-wide switch is off

* fix(worktrees): close the retry race window and clear stale failure state

- Try again is locked while a row import is in flight; a retry scan
  started before the import's write lands can absorb the import's own
  refetch and report success off a pre-import list
- a successful row import (which requires a successful authoritative
  refetch) clears an earlier failed open-time scan instead of leaving a
  contradictory alert over the refreshed list
- zh: 智能体 for agent (代理 reads as network proxy); polite live region
  on the checking hint

* fix(worktrees): serialize visibility dialog actions

* fix(worktrees): clarify persistent visibility policy

* fix(worktrees): clarify hidden worktree list

* Explain hidden worktree defaults

* Show agent worktrees with Always show

* fix(worktrees): bound visibility dialog state and rendering

* fix(worktrees): preserve visibility mutation fences across dismissal

* fix(worktrees): scope visibility mutations by host

---------

Co-authored-by: Gldywn <14254051+Gldywn@users.noreply.github.com>
2026-08-12 18:19:33 -07:00

144 lines
4.4 KiB
TypeScript

import { normalizeRuntimePathForComparison } from './cross-platform-path'
import type { DetectedWorktree, DetectedWorktreeListResult, Repo } from './types'
import {
effectiveExternalWorktreeVisibility,
isLegacyRepoForExternalWorktreeVisibility
} from './external-worktree-visibility'
export function normalizeExternalWorktreeInboxPath(path: string): string {
return normalizeRuntimePathForComparison(path)
}
export function areExternalWorktreeInboxPathsEqual(leftPath: string, rightPath: string): boolean {
return (
normalizeExternalWorktreeInboxPath(leftPath) === normalizeExternalWorktreeInboxPath(rightPath)
)
}
export function mergeExternalWorktreeInboxPaths(
existing: readonly string[] | undefined,
additions: readonly string[]
): string[] {
const seen = new Set((existing ?? []).map((path) => normalizeExternalWorktreeInboxPath(path)))
const merged = [...(existing ?? [])]
for (const path of additions) {
const normalized = normalizeExternalWorktreeInboxPath(path)
if (!normalized || seen.has(normalized)) {
continue
}
seen.add(normalized)
merged.push(path)
}
return merged
}
export function getHiddenExternalWorktrees(
detected: DetectedWorktreeListResult | undefined
): DetectedWorktree[] {
if (detected?.authoritative !== true) {
return []
}
return detected.worktrees.filter(
(worktree) => !worktree.visible && isUserFacingExternalWorktree(worktree)
)
}
export function getVisibleExternalWorktrees(
detected: DetectedWorktreeListResult | undefined
): DetectedWorktree[] {
if (detected?.authoritative !== true) {
return []
}
return detected.worktrees.filter(
(worktree) => worktree.visible && isUserFacingExternalWorktree(worktree)
)
}
function isUserFacingExternalWorktree(worktree: DetectedWorktree): boolean {
// Why: agent plumbing stays outside the discovery inbox even when its separate visibility policy shows it.
return (
!worktree.selectedCheckout &&
worktree.ownership !== 'orca-managed' &&
worktree.ownership !== 'agent-scratch'
)
}
// Why: per-path recovery remains available while either repo visibility policy is off.
function isImportableExternalWorktree(worktree: DetectedWorktree): boolean {
return !worktree.selectedCheckout && worktree.ownership !== 'orca-managed'
}
export function getHiddenImportableExternalWorktrees(
detected: DetectedWorktreeListResult | undefined
): DetectedWorktree[] {
if (detected?.authoritative !== true) {
return []
}
return detected.worktrees.filter(
(worktree) => !worktree.visible && isImportableExternalWorktree(worktree)
)
}
export function getVisibleNonOrcaWorktrees(
detected: DetectedWorktreeListResult | undefined
): DetectedWorktree[] {
if (detected?.authoritative !== true) {
return []
}
return detected.worktrees.filter(
(worktree) =>
worktree.visible && !worktree.selectedCheckout && worktree.ownership !== 'orca-managed'
)
}
export function isExternalWorktreeDiscoverySuppressed(
repo: Pick<Repo, 'externalWorktreeDiscoverySuppressedAt'>
): boolean {
return typeof repo.externalWorktreeDiscoverySuppressedAt === 'number'
}
export function hasCompletedInitialExternalWorktreeImportPrompt(
repo: Pick<Repo, 'externalWorktreeVisibilityPromptDismissedAt'>
): boolean {
return typeof repo.externalWorktreeVisibilityPromptDismissedAt === 'number'
}
export function shouldOfferNewExternalWorktreeInbox(repo: Repo): boolean {
if (isExternalWorktreeDiscoverySuppressed(repo)) {
return false
}
if (!hasCompletedInitialExternalWorktreeImportPrompt(repo)) {
return false
}
return (
effectiveExternalWorktreeVisibility(repo, isLegacyRepoForExternalWorktreeVisibility(repo)) ===
'hide'
)
}
export function getNewExternalWorktreeInboxWorktrees(
detected: DetectedWorktreeListResult | undefined,
repo: Repo
): DetectedWorktree[] {
if (!shouldOfferNewExternalWorktreeInbox(repo)) {
return []
}
const baseline = new Set(
(repo.externalWorktreeInboxBaselinePaths ?? []).map((path) =>
normalizeExternalWorktreeInboxPath(path)
)
)
return getHiddenExternalWorktrees(detected).filter(
(worktree) => !baseline.has(normalizeExternalWorktreeInboxPath(worktree.path))
)
}
export function isExplicitlyImportedExternalWorktreePath(
worktreePath: string,
repo: { importedExternalWorktreePaths?: readonly string[] }
): boolean {
return (repo.importedExternalWorktreePaths ?? []).some((path) =>
areExternalWorktreeInboxPathsEqual(path, worktreePath)
)
}