diff --git a/src/main/ai-vault/session-scanner-scope-discovery.ts b/src/main/ai-vault/session-scanner-scope-discovery.ts index 5aa3a3c0f1b..719b9428ddb 100644 --- a/src/main/ai-vault/session-scanner-scope-discovery.ts +++ b/src/main/ai-vault/session-scanner-scope-discovery.ts @@ -25,7 +25,7 @@ const TRANSCRIPT_EXTENSIONS = new Set(['.jsonl']) // Bucket encodings are lossy; retain Claude’s cache and verify other transcripts individually. const PROJECT_DIR_CWD_CACHE_MAX = 2048 const projectDirCwdCache = new Map() -export type IssueSink = (issue: AiVaultScanIssue) => void +export type IssueSink = (issue: AiVaultScanIssue, error?: unknown) => void export function resetProjectDirCwdCacheForTests(): void { projectDirCwdCache.clear() @@ -39,7 +39,7 @@ export function recordRefusal( agent: AiVaultAgent ): void { if (!isMissingTranscriptPathError(error)) { - issues({ agent, path, ...scanIssueTextFromError(error) }) + issues({ agent, path, ...scanIssueTextFromError(error) }, error) } } diff --git a/src/main/workspaces/removal/retirement/worktree-retirement-discovery-wsl.test.ts b/src/main/workspaces/removal/retirement/worktree-retirement-discovery-wsl.test.ts index b915cd62a34..1f335efe069 100644 --- a/src/main/workspaces/removal/retirement/worktree-retirement-discovery-wsl.test.ts +++ b/src/main/workspaces/removal/retirement/worktree-retirement-discovery-wsl.test.ts @@ -1,9 +1,14 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { mkdir, mkdtemp, readdir, rm } from 'node:fs/promises' +import { mkdir, mkdtemp, readdir, rm, stat, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { MARINE_CREATURES } from '../../../../shared/marine-creatures' import { createRetiredNameLookup } from '../../../../shared/worktree/retired-name-registry' +import { encodeClaudeProjectPath } from '../../../ai-vault/scanners/claude/claude-project-dir-encoding' +import { + RETIREMENT_BACKFILL_RETRY_AFTER_FAILURE_MS, + runRetirementBackfillScan +} from './worktree-retirement-backfill-scan' const { runWslTranscriptFsTaskMock, getWslHomeAsyncMock } = vi.hoisted(() => ({ runWslTranscriptFsTaskMock: vi.fn(), @@ -26,6 +31,7 @@ const { ensureRetiredWorktreeNamesBackfilled } = await import('./worktree-name-r const { WslTranscriptFsError } = await import('../../../native-chat/wsl-transcript-fs-gate') const FIRST = MARINE_CREATURES[0].toLowerCase() +const SECOND = MARINE_CREATURES[1].toLowerCase() const DISTRO_ROOT = '\\\\wsl.localhost\\Ubuntu\\home\\ada\\orca\\workspaces' describe('retirement discovery on WSL', () => { @@ -164,6 +170,66 @@ describe('retirement discovery on WSL', () => { } }) + // A refused permission stays refused on retry; a slow route may answer next time. + it.each([ + ['a permission refusal', Object.assign(new Error('EACCES'), { code: 'EACCES' }), 1], + ['a slow route', new WslTranscriptFsError('timeout', 'filesystem access is slow'), 2] + ])( + 'after %s on a long-path transcript, a later create rescans only if retrying can help', + async (_case, refusal, expectedProbes) => { + const distro = '\\\\wsl.localhost\\Ubuntu' + const longRoot = `/${'a'.repeat(229)}` + const distroDir = await mkdtemp(join(tmpdir(), 'orca-wsl-distro-')) + const hostHome = await mkdtemp(join(tmpdir(), 'orca-wsl-host-home-')) + try { + const projects = join(distroDir, 'home', 'ada', '.claude', 'projects') + await mkdir(join(distroDir, longRoot), { recursive: true }) + await mkdir(join(distroDir, 'home', 'ada', 'orca', 'workspaces'), { recursive: true }) + await mkdir(join(projects, `-home-ada-orca-workspaces-${FIRST}`), { recursive: true }) + const hashed = join( + projects, + `${encodeClaudeProjectPath(`${longRoot}/${SECOND}`).slice(0, 200)}-1x2y3z` + ) + await mkdir(hashed, { recursive: true }) + await writeFile(join(hashed, 'session.jsonl'), '{}\n') + let probes = 0 + runWslTranscriptFsTaskMock.mockImplementation( + async (options: { operation: string; path: string }) => { + if (options.path.endsWith('session.jsonl')) { + probes += 1 + throw refusal + } + const local = join(distroDir, options.path.slice(distro.length).replaceAll('\\', '/')) + return options.operation === 'readdir' + ? readdir(local, { withFileTypes: true }) + : stat(local) + } + ) + let now = 0 + vi.spyOn(performance, 'now').mockImplementation(() => now) + const store = {} + const create = () => + runRetirementBackfillScan(store, 'ns', () => + discoverRetiredWorktreeNames({ + workspaceRoots: [DISTRO_ROOT, `${distro}${longRoot}`], + home: hostHome, + env: {}, + resolveWslHome: async () => `${distro}\\home\\ada` + }) + ) + + await expect(create()).resolves.toEqual(new Set([FIRST])) + now += RETIREMENT_BACKFILL_RETRY_AFTER_FAILURE_MS + 1 + await expect(create()).resolves.toEqual(new Set([FIRST])) + + expect(probes).toBe(expectedProbes) + } finally { + await rm(distroDir, { force: true, recursive: true }) + await rm(hostHome, { force: true, recursive: true }) + } + } + ) + it('reaches distro discovery from the real backfill, not only when called directly', async () => { const distroHome = await mkdtemp(join(tmpdir(), 'orca-wsl-backfill-home-')) try { diff --git a/src/main/workspaces/removal/retirement/worktree-retirement-discovery.ts b/src/main/workspaces/removal/retirement/worktree-retirement-discovery.ts index 56f3bbb5c8e..940318dc311 100644 --- a/src/main/workspaces/removal/retirement/worktree-retirement-discovery.ts +++ b/src/main/workspaces/removal/retirement/worktree-retirement-discovery.ts @@ -13,6 +13,8 @@ import { cwdBucketScopePrefixes } from '../../../ai-vault/session-scanner-scope-discovery' import { wslGatedReaddir, wslGatedStat } from '../../../native-chat/wsl-transcript-fs-access' +import { WslTranscriptFsError } from '../../../native-chat/wsl-transcript-fs-error' +import { mapWithConcurrency } from '../../../../shared/map-with-concurrency' import type { RetirementScanResult } from './worktree-retirement-backfill-scan' import { normalizeRetirableGeneratedName } from './worktree-name-retirement' import { getWslHomeAsync } from '../../../wsl/discovery/distro-catalog' @@ -49,21 +51,40 @@ export function extractBucketLeafCandidates( }) } +// Each probe lists a bucket and reads up to 3 transcripts. Host dirs overlap; the WSL gate still runs one scan task at a time. +const HASHED_BUCKET_PROBE_CONCURRENCY = 4 + +/** A permission refusal on one transcript or bucket stays refused on retry, so it must not keep the scan open. */ +function isPersistentFileRefusal(error: unknown): boolean { + const code = (error as NodeJS.ErrnoException | null)?.code + return !(error instanceof WslTranscriptFsError) && (code === 'EACCES' || code === 'EPERM') +} + /** A `-` bucket name has lost the workspace name; its transcript cwd still has it. */ async function hashedBucketLeafNames( projectDir: string, - roots: readonly string[], - onRefusal: () => void -): Promise { - const cwd = await cachedProjectDirCwd(projectDir, onRefusal, 'claude') + roots: readonly string[] +): Promise<{ names: string[]; refused: boolean }> { + let refused = false + const cwd = await cachedProjectDirCwd( + projectDir, + (_issue, error) => { + refused ||= !isPersistentFileRefusal(error) + }, + 'claude' + ) if (!cwd) { - return [] + // Unresolved like a bucket with no cwd; only a retryable refusal leaves the scan incomplete. + return { names: [], refused } } // Compare paths, not names: /repo/my-app and /repo/my/app share a bucket name. Fold case like the bucket match. const folded = cwd.toLowerCase() - return roots.flatMap( - (root) => relativePathInsideRoot(root.toLowerCase(), folded)?.split('/')[0] || [] - ) + return { + names: roots.flatMap( + (root) => relativePathInsideRoot(root.toLowerCase(), folded)?.split('/')[0] || [] + ), + refused + } } /** Share WSL scan admission/deadlines; report refusals without discarding names readable from other sources. */ @@ -191,17 +212,22 @@ export async function discoverRetiredWorktreeNames(args: { for (const source of bucketSources.sources) { const listing = await readDirectoryNames(source.projectsDir) refused ||= listing.refused + const hashedDirs: string[] = [] for (const bucket of listing.names) { const candidates = extractBucketLeafCandidates(bucket, source.encodedParents) leafNames.push(...candidates) // An in-scope hashed bucket always leaves a remainder (its hash), so it has candidates. if (candidates.length > 0 && mayBeHashedClaudeProjectBucketName(bucket)) { - const projectDir = join(source.projectsDir, bucket) - leafNames.push( - ...(await hashedBucketLeafNames(projectDir, source.roots, () => (refused = true))) - ) + hashedDirs.push(join(source.projectsDir, bucket)) } } + const probes = await mapWithConcurrency(hashedDirs, HASHED_BUCKET_PROBE_CONCURRENCY, (dir) => + hashedBucketLeafNames(dir, source.roots) + ) + for (const probe of probes) { + leafNames.push(...probe.names) + refused ||= probe.refused + } } return { names: collectRetiredNamesFromLeafNames(leafNames), complete: !refused }