Files
orca/src/shared/external-worktree-inbox.ts
T
Brennan Benson 83e2123582 Add global worktree visibility source defaults (#14276)
* Add global external worktree visibility defaults

* Expand global worktree visibility source defaults

* Fix host-scoped visibility settings races

* Fix global worktree visibility integration

* Enable source visibility defaults on mobile

* Polish external worktree settings navigation

* Clarify inherited worktree visibility settings

* feat(sidebar): replace the inherited-visibility switch with a Show/Hide picker

Each source row now shows a two-segment Show / Hide control preselected to the
global setting, and explains itself only where the project actually disagrees:
an "Overriding global setting: <value>" card names the value being ignored.
Picking the segment global already holds drops the override instead of pinning
a duplicate, so the same control both overrides and reverts, retiring the
separate "Use global" link. The dialog footer now lists every inheritable
source with its global value.

* fix(sidebar): preserve reset for matching visibility overrides
2026-08-14 12:15:58 -07:00

153 lines
4.7 KiB
TypeScript

import { normalizeRuntimePathForComparison } from './cross-platform-path'
import type { WorktreeVisibilityDefaults } from './global-settings-types'
import type { Repo } from './repo-types'
import type { DetectedWorktree, DetectedWorktreeListResult } from './worktree/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,
defaults?: WorktreeVisibilityDefaults
): boolean {
if (isExternalWorktreeDiscoverySuppressed(repo)) {
return false
}
if (!hasCompletedInitialExternalWorktreeImportPrompt(repo)) {
return false
}
return (
effectiveExternalWorktreeVisibility(
repo,
isLegacyRepoForExternalWorktreeVisibility(repo),
defaults
) === 'hide'
)
}
export function getNewExternalWorktreeInboxWorktrees(
detected: DetectedWorktreeListResult | undefined,
repo: Repo,
defaults?: WorktreeVisibilityDefaults
): DetectedWorktree[] {
if (!shouldOfferNewExternalWorktreeInbox(repo, defaults)) {
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)
)
}