mirror of
https://github.com/stablyai/orca.git
synced 2026-10-07 16:02:29 +00:00
* fix(memory): bound session and lifecycle registries * fix(memory): bound transient filesystem registries * fix(memory): cap path and locale caches * fix(memory): bound runtime recovery registries * fix(memory): bound host mirror gap verdicts * fix(memory): bound shell startup env cache * fix(memory): bound gitlab host context cache * fix(memory): release removed ssh generations * fix(memory): expire cloud refresh replay guards * fix(memory): release retired plugin generations * fix(memory): bound plugin log key retention * fix(memory): bound automation authority generations * fix(memory): bound native chat enrichment cache * fix(memory): bound web session tracking generations * fix(memory): bound codex credential absence paths * fix(memory): bound WSL canonical path cache * fix(memory): bound sparse checkout cache * fix(memory): bound shared directory cache * fix(memory): bound advertised URL scan snapshots * fix(memory): bound automation manager cache * fix(memory): bound web session reorder intents * fix(memory): bound web session focus intents * fix(memory): bound web session handoffs * fix(memory): bound automation dispatch tokens * fix(memory): bound host mirror waiters * fix(memory): bound retained session activity * fix(memory): bound retained session activity * fix(memory): bound web session close intents * fix(memory): bound cloud session cache * fix(memory): bound WSL home cache * fix(memory): bound SSH capability cache * fix(memory): bound trust grant cooldowns * fix(memory): bound WSL auth drain state * fix(memory): bound Linear workspace credential cache * fix(memory): bound local Git capability cache * fix(memory): bound WSL Git environment cache * fix(memory): bound WSL Git environment cache * fix(memory): bound WSL preflight cache * fix(memory): keep hot cache entries warm * fix(memory): preserve generation fences across eviction * fix(memory): close remaining eviction fences * fix(memory): align evicted upstream generations * fix(memory): trim successful capability probes * fix(auth): retain expired refresh replay evidence --------- Co-authored-by: m4air <m4air@Mac.localdomain>
147 lines
5.8 KiB
TypeScript
147 lines
5.8 KiB
TypeScript
import { stat } from 'node:fs/promises'
|
|
import { join } from 'node:path'
|
|
import { checkIgnoredPaths } from './check-ignored-paths'
|
|
import type { GitRuntimeOptions } from './git-runtime-options'
|
|
import { loadHooks } from '../hooks'
|
|
import type { Repo } from '../../shared/repo-types'
|
|
import { mapWithConcurrency } from '../../shared/map-with-concurrency'
|
|
|
|
// Why: a fresh worktree has no node_modules/.cache, and copying them is slow and
|
|
// duplicates disk; `orca.yaml` names the ones every worktree should share instead.
|
|
|
|
const CONFIGURED_SHARED_DIRECTORIES_CACHE_TTL_MS = 30_000
|
|
export const MAX_CONFIGURED_SHARED_DIRECTORIES_CACHE_ENTRIES = 512
|
|
// Why: resolving a worktree may list many generated directories; overlap
|
|
// independent local probes without flooding the filesystem threadpool.
|
|
const SHARED_DIRECTORY_STAT_CONCURRENCY = 8
|
|
const configuredSharedDirectoriesByRepoPath = new Map<
|
|
string,
|
|
{ directories: string[]; expiresAt: number }
|
|
>()
|
|
|
|
/** The configured `worktree.sharedDirectories` names, before any existence or
|
|
* gitignore filtering.
|
|
*
|
|
* Why deletion can't reuse the resolver below: a directory-only ignore rule
|
|
* (`node_modules/`) matches the primary's real directory but never the
|
|
* worktree's symlink, so Git reports that symlink as untracked. Removal has to
|
|
* tolerate and unlink it, and the resolver would have already dropped it.
|
|
*
|
|
* `readonly` because this is the cached array itself: a mutating caller would
|
|
* corrupt every later read for the rest of the TTL. Copying on return would fix
|
|
* that too, but this runs on the status-polling path — the type costs nothing. */
|
|
export function getConfiguredWorktreeSharedDirectories(repoPath: string): readonly string[] {
|
|
const cached = configuredSharedDirectoriesByRepoPath.get(repoPath)
|
|
const now = Date.now()
|
|
if (cached && cached.expiresAt > now) {
|
|
configuredSharedDirectoriesByRepoPath.delete(repoPath)
|
|
configuredSharedDirectoriesByRepoPath.set(repoPath, cached)
|
|
return cached.directories
|
|
}
|
|
const configured = loadHooks(repoPath)?.worktree?.sharedDirectories ?? []
|
|
configuredSharedDirectoriesByRepoPath.set(repoPath, {
|
|
directories: configured,
|
|
expiresAt: now + CONFIGURED_SHARED_DIRECTORIES_CACHE_TTL_MS
|
|
})
|
|
while (
|
|
configuredSharedDirectoriesByRepoPath.size > MAX_CONFIGURED_SHARED_DIRECTORIES_CACHE_ENTRIES
|
|
) {
|
|
const oldest = configuredSharedDirectoriesByRepoPath.keys().next()
|
|
if (oldest.done || oldest.value === repoPath) {
|
|
break
|
|
}
|
|
configuredSharedDirectoriesByRepoPath.delete(oldest.value)
|
|
}
|
|
return configured
|
|
}
|
|
|
|
/** Reset the process cache between tests. */
|
|
export function clearConfiguredWorktreeSharedDirectoriesCacheForTests(): void {
|
|
configuredSharedDirectoriesByRepoPath.clear()
|
|
}
|
|
|
|
export function getConfiguredWorktreeSharedDirectoriesCacheSizeForTests(): number {
|
|
return configuredSharedDirectoriesByRepoPath.size
|
|
}
|
|
|
|
/** Every path Orca may have symlinked into a worktree: the per-user Worktree
|
|
* Shared Paths setting plus the repo's `orca.yaml` shared directories.
|
|
*
|
|
* Callers pair this with `findExistingWorktreeSymlinkPaths`, which keeps only
|
|
* the entries that really are symlinks — so a configured name that the user
|
|
* happens to own as a regular file is never treated as one of ours. */
|
|
export function getWorktreeSharedLinkPaths(repo: Pick<Repo, 'path' | 'symlinkPaths'>): string[] {
|
|
return Array.from(
|
|
new Set([...(repo.symlinkPaths ?? []), ...getConfiguredWorktreeSharedDirectories(repo.path)])
|
|
)
|
|
}
|
|
|
|
/** Resolve `worktree.sharedDirectories` from the repo-root `orca.yaml` to
|
|
* concrete repo-relative directories to symlink into a new worktree.
|
|
*
|
|
* Only directories that exist in the primary checkout **and** are gitignored are
|
|
* returned: tracked directories are already materialized by the checkout, and
|
|
* sharing an unignored path would surface the link as a spurious worktree diff.
|
|
*
|
|
* Never throws — any read/parse/git failure resolves to `[]` so worktree
|
|
* creation is never blocked by this file. */
|
|
export async function resolveWorktreeSharedDirectories(
|
|
repoPath: string,
|
|
options: GitRuntimeOptions = {}
|
|
): Promise<string[]> {
|
|
try {
|
|
const configured = loadHooks(repoPath)?.worktree?.sharedDirectories ?? []
|
|
if (configured.length === 0) {
|
|
return []
|
|
}
|
|
|
|
// Keep only entries that exist as directories; a listed but absent path
|
|
// (node_modules before install) has nothing to share. The mapper retains
|
|
// configured order; warnings are emitted below in that same order.
|
|
const probes = await mapWithConcurrency(
|
|
configured,
|
|
SHARED_DIRECTORY_STAT_CONCURRENCY,
|
|
async (relativePath) => {
|
|
try {
|
|
return {
|
|
relativePath,
|
|
exists: true,
|
|
isDirectory: (await stat(join(repoPath, relativePath))).isDirectory()
|
|
}
|
|
} catch {
|
|
return { relativePath, exists: false, isDirectory: false }
|
|
}
|
|
}
|
|
)
|
|
const existing: string[] = []
|
|
for (const probe of probes) {
|
|
if (!probe.exists) {
|
|
continue
|
|
}
|
|
if (probe.isDirectory) {
|
|
existing.push(probe.relativePath)
|
|
} else {
|
|
console.warn(
|
|
`[worktree-shared-directories] Skipping "${probe.relativePath}": sharedDirectories entries must be directories`
|
|
)
|
|
}
|
|
}
|
|
if (existing.length === 0) {
|
|
return []
|
|
}
|
|
|
|
const ignored = new Set(await checkIgnoredPaths(repoPath, existing, options))
|
|
for (const relativePath of existing) {
|
|
if (!ignored.has(relativePath)) {
|
|
console.warn(
|
|
`[worktree-shared-directories] Skipping "${relativePath}": only gitignored directories can be shared`
|
|
)
|
|
}
|
|
}
|
|
return existing.filter((relativePath) => ignored.has(relativePath)).sort()
|
|
} catch (error) {
|
|
console.warn('[worktree-shared-directories] Failed to resolve shared directories:', error)
|
|
return []
|
|
}
|
|
}
|