Files
orca/src/shared/external-worktree-visibility.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

77 lines
2.5 KiB
TypeScript

import { normalizeRuntimePathSeparators } from './cross-platform-path'
import type { ExternalWorktreeVisibility, Repo } from './types'
export const EXTERNAL_WORKTREE_VISIBILITY_ROLLOUT_AT = Date.UTC(2026, 4, 23)
export const UNKNOWN_EXTERNAL_WORKTREE_PARENT_PATH = 'Unknown location'
function trimRuntimePathTrailingSlash(value: string): string {
if (value === '/' || /^[A-Za-z]:\/$/.test(value)) {
return value
}
return value.replace(/\/+$/, '')
}
export function getExternalWorktreeParentPath(worktreePath: string | undefined): string {
if (!worktreePath) {
return UNKNOWN_EXTERNAL_WORKTREE_PARENT_PATH
}
const normalized = trimRuntimePathTrailingSlash(normalizeRuntimePathSeparators(worktreePath))
if (!normalized) {
return UNKNOWN_EXTERNAL_WORKTREE_PARENT_PATH
}
if (normalized.startsWith('//')) {
const parts = normalized.slice(2).split('/').filter(Boolean)
if (parts.length < 2) {
return UNKNOWN_EXTERNAL_WORKTREE_PARENT_PATH
}
if (parts.length === 2) {
return `//${parts[0]}/${parts[1]}`
}
return `//${parts.slice(0, -1).join('/')}`
}
const lastSeparatorIndex = normalized.lastIndexOf('/')
if (lastSeparatorIndex === -1) {
return UNKNOWN_EXTERNAL_WORKTREE_PARENT_PATH
}
if (lastSeparatorIndex === 0) {
return '/'
}
if (/^[A-Za-z]:\/$/.test(normalized)) {
return normalized
}
if (/^[A-Za-z]:\/[^/]+$/.test(normalized)) {
return `${normalized.slice(0, 2)}/`
}
return normalized.slice(0, lastSeparatorIndex)
}
export function isLegacyRepoForExternalWorktreeVisibility(repo: Repo): boolean {
if (typeof repo.externalWorktreeVisibilityLegacy === 'boolean') {
return repo.externalWorktreeVisibilityLegacy
}
if (repo.externalWorktreeVisibility === undefined) {
return true
}
if (!Number.isFinite(repo.addedAt)) {
return true
}
return repo.addedAt < EXTERNAL_WORKTREE_VISIBILITY_ROLLOUT_AT
}
export function effectiveExternalWorktreeVisibility(
repo: Pick<Repo, 'externalWorktreeVisibility'>,
isLegacyRepoForVisibility: boolean
): ExternalWorktreeVisibility {
if (repo.externalWorktreeVisibility) {
return repo.externalWorktreeVisibility
}
return isLegacyRepoForVisibility ? 'show' : 'hide'
}
export function effectiveAgentWorktreeVisibility(
repo: Pick<Repo, 'agentWorktreeVisibility'>
): ExternalWorktreeVisibility {
// Why: scratch worktrees have always defaulted hidden, including for legacy repos.
return repo.agentWorktreeVisibility === 'show' ? 'show' : 'hide'
}