mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
fix(wsl-hosts): keep WSL drive answers through failures; clear unknown letters
- A failed lookup never downgrades a letter last known to map into WSL: the saved answer stays, the failure isn't persisted, and the mover sees it as unverifiable. - With an unreadable journal, the fence classifies each unknown drive letter once (bounded, never a letter last known as WSL) and unblocks it when it answers. - A stale WSL letter whose re-check times out keeps its last answer, so its projects are listed as unverifiable rather than dropped. - Future-dated answers count as unknown; unchanged answer sets aren't rewritten; stale temp files of the answers file are swept; a whole-distro start names the projects it left behind because their drive no longer points into the distro. Co-authored-by: Neil <neil@stably.ai>
This commit is contained in:
@@ -7,9 +7,9 @@ import type { WslMappedDrive } from '../../../shared/wsl-move/wsl-mapped-drive-p
|
||||
import {
|
||||
classifyWslMappedDrive,
|
||||
readWslMappedDriveShareNow,
|
||||
wslMappedDriveLastKnownShare,
|
||||
wslMappedDriveMatches
|
||||
} from '../../wsl/filesystem/wsl-mapped-drive-share'
|
||||
import { wslMappedDriveLastKnownShare } from '../../wsl/filesystem/wsl-mapped-drive-last-known'
|
||||
|
||||
export function wslMoveMappedDrivePorts(): Pick<
|
||||
WslMoveDriverDeps,
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
import {
|
||||
WSL_MOVE_FINISH_PENDING_DETAIL,
|
||||
type WslMoveDroppedProject,
|
||||
type WslMoveRefusal,
|
||||
type WslMoveRequest
|
||||
} from '../../../../shared/wsl-move/wsl-move-driver-types'
|
||||
@@ -17,6 +18,7 @@ import type { WslMappedDrive } from '../../../../shared/wsl-move/wsl-mapped-driv
|
||||
import {
|
||||
journaledWslMoveDrives,
|
||||
readWslMoveSourceDrives,
|
||||
wslMoveDroppedForDrives,
|
||||
wslMoveStartDriveRefusal
|
||||
} from './wsl-move-driver-source-drives'
|
||||
import { listWslMoveCutoverChain } from '../cutover-recovery/wsl-move-cutover-journal'
|
||||
@@ -50,6 +52,8 @@ export type WslMovePrepared = {
|
||||
exported: WslMoveManifestExport
|
||||
scope: WslMoveTerminalScope
|
||||
blockers: WslMoveGateBlocker[]
|
||||
/** A whole-distro start's previewed projects left because their drive now points elsewhere. */
|
||||
droppedForDrives?: WslMoveDroppedProject[]
|
||||
}
|
||||
|
||||
export function refusal(
|
||||
@@ -298,6 +302,9 @@ export async function prepareWslMove(
|
||||
if (changedDrives) {
|
||||
return changedDrives
|
||||
}
|
||||
const droppedForDrives = expectedMappedDrives
|
||||
? wslMoveDroppedForDrives(catalog, drives.mappedDrives, expectedMappedDrives, plan)
|
||||
: []
|
||||
const scope: WslMoveTerminalScope = {
|
||||
workspaceIds: exported.workspaceIds,
|
||||
workspaceKeys: new Set(exported.workspaceKeys)
|
||||
@@ -308,6 +315,7 @@ export async function prepareWslMove(
|
||||
plan,
|
||||
exported,
|
||||
scope,
|
||||
blockers: gate.verdict === 'blocked' ? gate.blockers : []
|
||||
blockers: gate.verdict === 'blocked' ? gate.blockers : [],
|
||||
...(droppedForDrives.length > 0 ? { droppedForDrives } : {})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
import {
|
||||
WSL_MOVE_MAPPED_DRIVE_UNVERIFIABLE_DETAIL,
|
||||
type WslMoveDroppedProject,
|
||||
type WslMoveRefusal,
|
||||
type WslMoveRequest
|
||||
} from '../../../../shared/wsl-move/wsl-move-driver-types'
|
||||
@@ -14,8 +15,10 @@ import {
|
||||
} from '../../../../shared/wsl-move/wsl-mapped-drive-paths'
|
||||
import {
|
||||
readWslMoveMappedDrives,
|
||||
wslMoveDriveLetterOf,
|
||||
wslMoveDriveLettersInUse
|
||||
} from '../manifest-scope/wsl-move-mapped-drives'
|
||||
import { isLocalWslMoveRow } from '../quiescence/wsl-move-scope-guard'
|
||||
import type { WslMoveScopeDrives, WslMoveScopeRefusal } from '../manifest-scope/wsl-move-scope'
|
||||
import { findWindowsGitRefusals } from '../manifest-scope/wsl-move-windows-git-worktrees'
|
||||
import type { WslMoveCatalogRows, WslMoveDriverDeps } from './wsl-move-driver-ports'
|
||||
@@ -110,3 +113,40 @@ export function wslMoveStartDriveRefusal(
|
||||
? null
|
||||
: { outcome: 'refused', code: 'wsl_move_scope_changed', detail: 'mapped-drives-changed' }
|
||||
}
|
||||
|
||||
/**
|
||||
* A whole-distro start's projects that the preview could show but this start leaves, because
|
||||
* their drive now points elsewhere (or no longer answers): named in the result, never silent.
|
||||
*/
|
||||
export function wslMoveDroppedForDrives(
|
||||
catalog: WslMoveCatalogRows,
|
||||
fresh: readonly WslMappedDrive[],
|
||||
expected: readonly WslMappedDrive[],
|
||||
moving: { repoIds: readonly string[]; folderWorkspaceIds: readonly string[] }
|
||||
): WslMoveDroppedProject[] {
|
||||
const changed = new Set(
|
||||
expected
|
||||
.filter((seen) => {
|
||||
const now = fresh.find((drive) => drive.letter === seen.letter)
|
||||
return !now || !sameWslMappedDrives([seen], [now])
|
||||
})
|
||||
.map((drive) => drive.letter.toUpperCase())
|
||||
)
|
||||
const onChanged = (row: { path: string; orcadRetained?: unknown }): boolean =>
|
||||
// A retained row was moved earlier; it isn't this start's to leave.
|
||||
!row.orcadRetained && changed.has(wslMoveDriveLetterOf(row.path) ?? '')
|
||||
return [
|
||||
...catalog.repos
|
||||
.filter((repo) => isLocalWslMoveRow(repo) && !moving.repoIds.includes(repo.id))
|
||||
.filter((repo) => onChanged(repo))
|
||||
.map((repo) => ({ kind: 'repo' as const, id: repo.id, name: repo.displayName })),
|
||||
...catalog.folderWorkspaces
|
||||
.filter(
|
||||
(folder) => isLocalWslMoveRow(folder) && !moving.folderWorkspaceIds.includes(folder.id)
|
||||
)
|
||||
.filter((folder) =>
|
||||
onChanged({ path: folder.folderPath, orcadRetained: folder.orcadRetained })
|
||||
)
|
||||
.map((folder) => ({ kind: 'folder' as const, id: folder.id, name: folder.name }))
|
||||
]
|
||||
}
|
||||
|
||||
+21
@@ -104,3 +104,24 @@ describe.each(['none', 'shown'] as const)('a whole-distro start with ids %s', (i
|
||||
expect(listWslMoveCutovers(rig.userDataPath).map((record) => record.version)).toContain(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('a whole-distro start whose previewed drive now points elsewhere', () => {
|
||||
it('moves the rest and names the projects it left', async () => {
|
||||
rig.world.repos = [
|
||||
project('repo-1', UNC),
|
||||
{ ...project('repo-w', 'W:\\home\\dev\\other'), displayName: 'other' }
|
||||
]
|
||||
const preview = await previewWslMove(rig.world.deps, { distro: 'Ubuntu' })
|
||||
expect(preview).toMatchObject({ outcome: 'ready', repoIds: ['repo-1', 'repo-w'] })
|
||||
shares = { W: { share: '\\\\wsl.localhost\\Debian' } }
|
||||
expect(
|
||||
await moveProjectsToWslHost(rig.world.deps, {
|
||||
distro: 'Ubuntu',
|
||||
mappedDrives: preview.outcome === 'ready' ? (preview.mappedDrives ?? []) : []
|
||||
})
|
||||
).toMatchObject({
|
||||
outcome: 'moved',
|
||||
droppedForMappedDrives: [{ kind: 'repo', id: 'repo-w', name: 'other' }]
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
import { resolve } from 'node:path'
|
||||
import { getRepoIdFromWorktreeId } from '../../../../shared/worktree/id'
|
||||
import { parseWorkspaceKey } from '../../../../shared/workspace/identity/workspace-scope'
|
||||
import { mayBeInWsl } from './wsl-move-unread-journal-folders'
|
||||
import {
|
||||
mayBeInWsl,
|
||||
resetUnreadJournalClassificationForTests
|
||||
} from './wsl-move-unread-journal-folders'
|
||||
import {
|
||||
getRepoExecutionHostId,
|
||||
LOCAL_EXECUTION_HOST_ID
|
||||
@@ -70,6 +73,7 @@ export function isWslMoveFenceLoaded(): boolean {
|
||||
}
|
||||
|
||||
export function resetWslMoveFenceForTests(): void {
|
||||
resetUnreadJournalClassificationForTests()
|
||||
stopFollowing?.()
|
||||
stopFollowing = null
|
||||
loaded = null
|
||||
|
||||
+19
-4
@@ -1,22 +1,37 @@
|
||||
/** What an unreadable WSL move journal still fences: folders its moves could have taken. */
|
||||
import { isWslUncPath } from '../../../../shared/file-access/wsl-paths'
|
||||
import { wslMappedDriveLastKnownShare } from '../../../wsl/filesystem/wsl-mapped-drive-share'
|
||||
import { wslMappedDriveLastKnownShare } from '../../../wsl/filesystem/wsl-mapped-drive-last-known'
|
||||
import { classifyWslMappedDrive } from '../../../wsl/filesystem/wsl-mapped-drive-share'
|
||||
|
||||
// Each unknown letter is classified once, so a local disk's projects unblock when it answers.
|
||||
const classifying = new Set<string>()
|
||||
|
||||
/**
|
||||
* Without the journal, any folder a move could have taken: a UNC path, or a drive letter not
|
||||
* known (from this launch or a saved answer) to point outside WSL. A moved `W:\` row has nothing
|
||||
* else marking it as moved.
|
||||
* else marking it as moved. Only for an unreadable journal, an unknown letter gets one bounded
|
||||
* lookup; a letter last known in WSL is never read, so no distro boots for it.
|
||||
*/
|
||||
export function mayBeInWsl(path: string): boolean {
|
||||
if (isWslUncPath(path)) {
|
||||
return true
|
||||
}
|
||||
const letter = /^([A-Za-z]):[\\/]/.exec(path)?.[1]
|
||||
const letter = /^([A-Za-z]):[\\/]/.exec(path)?.[1]?.toUpperCase()
|
||||
// Off Windows a drive letter names no share, so it's never in WSL.
|
||||
if (!letter || process.platform !== 'win32') {
|
||||
return false
|
||||
}
|
||||
// Never read here: it runs on background paths too. A user's preview or add learns the drive.
|
||||
const known = wslMappedDriveLastKnownShare(letter)
|
||||
if (!known && !classifying.has(letter)) {
|
||||
classifying.add(letter)
|
||||
// One that didn't settle may be asked again; a hung drive's lookup is shared, not repeated.
|
||||
void classifyWslMappedDrive(letter)
|
||||
.catch(() => null)
|
||||
.then((answer) => answer ?? classifying.delete(letter))
|
||||
}
|
||||
return !known || (known.share !== null && isWslUncPath(known.share))
|
||||
}
|
||||
|
||||
export function resetUnreadJournalClassificationForTests(): void {
|
||||
classifying.clear()
|
||||
}
|
||||
|
||||
@@ -158,11 +158,13 @@ async function runMove(
|
||||
detail: pauseDetail(quiesced.reason, quiesced.detail)
|
||||
})
|
||||
}
|
||||
try {
|
||||
return await moveQuiesced(deps, host, quiesced.cutover, prepared, running, slept)
|
||||
} catch (error) {
|
||||
return await putBack(deps, host, migrationId, describe(error))
|
||||
}
|
||||
const result = await moveQuiesced(deps, host, quiesced.cutover, prepared, running, slept).catch(
|
||||
(error: unknown) => putBack(deps, host, migrationId, describe(error))
|
||||
)
|
||||
// The projects the preview showed that this start left say why in the result.
|
||||
return result.outcome === 'moved' && prepared.droppedForDrives
|
||||
? { ...result, droppedForMappedDrives: prepared.droppedForDrives }
|
||||
: result
|
||||
} finally {
|
||||
slept.dispose()
|
||||
}
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
||||
import {
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readFileSync,
|
||||
rmSync,
|
||||
utimesSync,
|
||||
writeFileSync
|
||||
} from 'node:fs'
|
||||
import type * as NodeFs from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
@@ -22,10 +30,11 @@ import {
|
||||
wslMappedDriveAnswersPath
|
||||
} from './wsl-mapped-drive-answers-file'
|
||||
import {
|
||||
classifyWslMappedDrive,
|
||||
clearWslMappedDriveShareCacheForTests,
|
||||
readWslMappedDriveShareNow,
|
||||
wslMappedDriveLastKnownShare
|
||||
readWslMappedDriveShareNow
|
||||
} from './wsl-mapped-drive-share'
|
||||
import { wslMappedDriveLastKnownShare } from './wsl-mapped-drive-last-known'
|
||||
|
||||
let userDataPath: string
|
||||
beforeEach(() => {
|
||||
@@ -98,6 +107,78 @@ describe('saved mapped-drive answers', () => {
|
||||
}
|
||||
expect(isWslMoveProjectBlocked(store, { repoId: 'c' })).toBe(false)
|
||||
expect(isWslMoveProjectBlocked(store, { repoId: 'w' })).toBe(true)
|
||||
expect(realpathNative).not.toHaveBeenCalled()
|
||||
// C: is known from the file, so only the unknown W: is looked up.
|
||||
expect(realpathNative.mock.calls.map(([root]) => root)).toEqual(['W:\\'])
|
||||
})
|
||||
|
||||
it('keep a WSL answer through a failed lookup and a restart, and report it unverifiable', async () => {
|
||||
const answers = restart()
|
||||
await readWslMappedDriveShareNow('W', fake('\\\\wsl.localhost\\Ubuntu\\'))
|
||||
await answers.saved()
|
||||
const failing = {
|
||||
...fake(''),
|
||||
realpathRoot: async (): Promise<string> => {
|
||||
throw new Error('ENOENT')
|
||||
}
|
||||
}
|
||||
// The mover sees a WSL drive that fails to answer as unverifiable, not as mapping nowhere.
|
||||
await expect(readWslMappedDriveShareNow('W', failing)).resolves.toBe('unverifiable')
|
||||
await expect(classifyWslMappedDrive('W', { ...failing, now: () => 120_000 })).resolves.toEqual({
|
||||
share: '\\\\wsl.localhost\\Ubuntu'
|
||||
})
|
||||
await answers.saved()
|
||||
restart()
|
||||
expect(wslMappedDriveLastKnownShare('W')).toEqual({ share: '\\\\wsl.localhost\\Ubuntu' })
|
||||
writeUnreadableJournal()
|
||||
loadWslMoveFence(userDataPath)
|
||||
expect(isWslMoveProjectBlocked(storeOf({ w: 'W:\\home\\me\\app' }), { repoId: 'w' })).toBe(true)
|
||||
})
|
||||
|
||||
it('classify unknown letters once for an unreadable journal, never a saved WSL one', async () => {
|
||||
const answers = restart()
|
||||
await readWslMappedDriveShareNow('W', fake('\\\\wsl.localhost\\Ubuntu\\'))
|
||||
await answers.saved()
|
||||
restart()
|
||||
realpathNative.mockImplementation((root: string, done: (e: null, real: string) => void) =>
|
||||
done(null, root)
|
||||
)
|
||||
writeUnreadableJournal()
|
||||
loadWslMoveFence(userDataPath)
|
||||
const store = storeOf({ c: 'C:\\src\\site', w: 'W:\\home\\me\\app' })
|
||||
expect(isWslMoveProjectBlocked(store, { repoId: 'c' })).toBe(true)
|
||||
expect(isWslMoveProjectBlocked(store, { repoId: 'w' })).toBe(true)
|
||||
await vi.waitFor(() => expect(isWslMoveProjectBlocked(store, { repoId: 'c' })).toBe(false))
|
||||
expect(realpathNative.mock.calls.map(([root]) => root)).toEqual(['C:\\'])
|
||||
expect(isWslMoveProjectBlocked(store, { repoId: 'w' })).toBe(true)
|
||||
})
|
||||
|
||||
it('skip the write when only an answer’s time changed, and clear stale temporaries', async () => {
|
||||
const stale = `${wslMappedDriveAnswersPath(userDataPath)}.999999.1.abc.tmp`
|
||||
writeFileSync(stale, 'x')
|
||||
utimesSync(stale, 0, 0)
|
||||
const answers = restart()
|
||||
await answers.saved()
|
||||
expect(existsSync(stale)).toBe(false)
|
||||
await readWslMappedDriveShareNow('C', fake('C:\\'))
|
||||
await answers.saved()
|
||||
expect(existsSync(wslMappedDriveAnswersPath(userDataPath))).toBe(true)
|
||||
rmSync(wslMappedDriveAnswersPath(userDataPath))
|
||||
await readWslMappedDriveShareNow('C', { ...fake('C:\\'), now: () => 2_000 })
|
||||
await answers.saved()
|
||||
expect(existsSync(wslMappedDriveAnswersPath(userDataPath))).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
function writeUnreadableJournal(): void {
|
||||
mkdirSync(wslMoveJournalDirectory(userDataPath), { recursive: true })
|
||||
writeFileSync(
|
||||
join(wslMoveJournalDirectory(userDataPath), 'move-1.json'),
|
||||
JSON.stringify({ ...cutover('move-1', 'destination-committed'), version: 3 })
|
||||
)
|
||||
}
|
||||
|
||||
function storeOf(paths: Record<string, string>): {
|
||||
getRepo: (id: string) => { path: string } | undefined
|
||||
} {
|
||||
return { getRepo: (id) => (id in paths ? { path: paths[id] } : undefined) }
|
||||
}
|
||||
|
||||
@@ -7,12 +7,17 @@ import { join } from 'node:path'
|
||||
import { z } from 'zod'
|
||||
import { readNodeFileSyncWithinLimit } from '../../../shared/file-content/node-bounded-file-reader'
|
||||
import { parseWslUncPath } from '../../../shared/file-access/wsl-paths'
|
||||
import { durableWriteTempPath, writeFileDurable } from '../../filesystem/durable-file-write'
|
||||
import {
|
||||
durableWriteTempPath,
|
||||
removeStaleDurableWriteTempFiles,
|
||||
writeFileDurable
|
||||
} from '../../filesystem/durable-file-write'
|
||||
import {
|
||||
seedWslMappedDriveAnswers,
|
||||
setWslMappedDriveAnswerSink,
|
||||
wslMappedDriveAnswers,
|
||||
type WslMappedDriveAnswer
|
||||
} from './wsl-mapped-drive-share'
|
||||
} from './wsl-mapped-drive-last-known'
|
||||
|
||||
const FILE_NAME = 'wsl-mapped-drive-answers.json'
|
||||
const MAX_FILE_BYTES = 64 * 1024
|
||||
@@ -34,6 +39,14 @@ const AnswersSchema = z.object({
|
||||
.max(26)
|
||||
})
|
||||
|
||||
function sharesOf(answers: readonly WslMappedDriveAnswer[]): string {
|
||||
return JSON.stringify(
|
||||
[...answers]
|
||||
.sort((left, right) => left.letter.localeCompare(right.letter))
|
||||
.map(({ letter, share }) => [letter, share])
|
||||
)
|
||||
}
|
||||
|
||||
export function wslMappedDriveAnswersPath(userDataPath: string): string {
|
||||
return join(userDataPath, FILE_NAME)
|
||||
}
|
||||
@@ -53,9 +66,19 @@ export function loadWslMappedDriveAnswers(userDataPath: string): { saved: () =>
|
||||
} catch {
|
||||
// No answers saved yet, or unreadable: unknown letters stay unknown.
|
||||
}
|
||||
let writing: Promise<void> = Promise.resolve()
|
||||
// A write that died before its rename leaves a temporary; another instance's is spared.
|
||||
let writing: Promise<void> = removeStaleDurableWriteTempFiles(path, {
|
||||
minimumAgeMs: 60_000
|
||||
}).catch(() => {})
|
||||
let pending: WslMappedDriveAnswer[] | null = null
|
||||
let savedShares = sharesOf(wslMappedDriveAnswers())
|
||||
setWslMappedDriveAnswerSink((answers) => {
|
||||
// Only where letters point counts: a re-read that changed only the time isn't worth a write.
|
||||
const shares = sharesOf(answers)
|
||||
if (shares === savedShares) {
|
||||
return
|
||||
}
|
||||
savedShares = shares
|
||||
// Writes run one at a time; a burst of answers saves only the newest set.
|
||||
const queued = pending !== null
|
||||
pending = answers
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Each drive letter's last settled answer from any lookup: the WSL share it maps to, or nowhere.
|
||||
* Background paths read only these, never a drive (a mapped share's read can boot its distro).
|
||||
*/
|
||||
import { parseWslUncPath } from '../../../shared/file-access/wsl-paths'
|
||||
|
||||
/** A letter's settled answer: the share it maps to (null: nowhere), and when it was read. */
|
||||
export type WslMappedDriveAnswer = { letter: string; share: string | null; at: number }
|
||||
|
||||
const lastKnown = new Map<string, { share: string | null; at: number }>()
|
||||
// Told every changed answer, so a restart knows a drive without reading it.
|
||||
let answerSink: ((answers: WslMappedDriveAnswer[]) => void) | null = null
|
||||
|
||||
function isWslShare(share: string | null): share is string {
|
||||
return share !== null && parseWslUncPath(share) !== null
|
||||
}
|
||||
|
||||
function settle(key: string, share: string | null, at: number): void {
|
||||
lastKnown.set(key, { share, at })
|
||||
answerSink?.(wslMappedDriveAnswers())
|
||||
}
|
||||
|
||||
/** A lookup's answer: the drive root's final path, kept as a share only inside WSL. */
|
||||
export function rememberWslMappedDriveRead(key: string, real: string, at: number): string | null {
|
||||
const share = isWslShare(real) ? real.replace(/[\\/]+$/, '') : null
|
||||
settle(key, share, at)
|
||||
return share
|
||||
}
|
||||
|
||||
/**
|
||||
* A failed lookup: maps nowhere, unless the letter was last known in WSL. A WSL share that fails
|
||||
* to answer (its distro stopped, say) is not proof it moved, so that answer stands; true then.
|
||||
*/
|
||||
export function rememberWslMappedDriveFailure(key: string, at: number): boolean {
|
||||
if (isWslShare(lastKnown.get(key)?.share ?? null)) {
|
||||
return true
|
||||
}
|
||||
settle(key, null, at)
|
||||
return false
|
||||
}
|
||||
|
||||
export function setWslMappedDriveAnswerSink(
|
||||
sink: ((answers: WslMappedDriveAnswer[]) => void) | null
|
||||
): void {
|
||||
answerSink = sink
|
||||
}
|
||||
|
||||
export function wslMappedDriveAnswers(): WslMappedDriveAnswer[] {
|
||||
return [...lastKnown].map(([letter, known]) => ({ letter, ...known }))
|
||||
}
|
||||
|
||||
/** Seeds answers read from disk; one already settled in this run is newer and wins. */
|
||||
export function seedWslMappedDriveAnswers(answers: readonly WslMappedDriveAnswer[]): void {
|
||||
for (const { letter, share, at } of answers) {
|
||||
const key = letter.toUpperCase()
|
||||
if (!lastKnown.has(key)) {
|
||||
lastKnown.set(key, { share, at })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The letter's last answer, never read here; null before any lookup of it settled, when it is
|
||||
* older than `maxAgeMs`, or dated after `now` (a clock that moved back can't vouch for it).
|
||||
*/
|
||||
export function wslMappedDriveLastKnownShare(
|
||||
letter: string,
|
||||
maxAgeMs = Number.POSITIVE_INFINITY,
|
||||
now: number = Date.now()
|
||||
): { share: string | null } | null {
|
||||
const known = lastKnown.get(letter.toUpperCase())
|
||||
return known && known.at <= now && now - known.at < maxAgeMs ? { share: known.share } : null
|
||||
}
|
||||
|
||||
/** Whether the letter's last answer, of any age, maps into WSL. */
|
||||
export function isWslMappedDriveLastKnownInWsl(letter: string, now: number = Date.now()): boolean {
|
||||
return isWslShare(
|
||||
wslMappedDriveLastKnownShare(letter, Number.POSITIVE_INFINITY, now)?.share ?? null
|
||||
)
|
||||
}
|
||||
|
||||
export function clearWslMappedDriveLastKnownForTests(): void {
|
||||
lastKnown.clear()
|
||||
answerSink = null
|
||||
}
|
||||
@@ -3,10 +3,10 @@ import {
|
||||
classifyWslMappedDrive,
|
||||
clearWslMappedDriveShareCacheForTests,
|
||||
readWslMappedDriveShareNow,
|
||||
wslMappedDriveLastKnownShare,
|
||||
resolveWslMappedDrivePath,
|
||||
type WslMappedDriveShareDeps
|
||||
} from './wsl-mapped-drive-share'
|
||||
import { wslMappedDriveLastKnownShare } from './wsl-mapped-drive-last-known'
|
||||
|
||||
function deps(
|
||||
realpathRoot: WslMappedDriveShareDeps['realpathRoot'],
|
||||
@@ -252,4 +252,25 @@ describe('fresh reads and last-known answers', () => {
|
||||
})
|
||||
expect(realpathRoot).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('falls back to a stale WSL answer when re-classifying times out, so the fresh read decides', async () => {
|
||||
let now = 0
|
||||
await readWslMappedDriveShareNow(
|
||||
'W',
|
||||
deps(async () => '\\\\wsl.localhost\\Ubuntu\\', { now: () => now })
|
||||
)
|
||||
now = 120_000
|
||||
const hung = deps(() => new Promise<string>(() => {}), { now: () => now })
|
||||
await expect(classifyWslMappedDrive('W', hung)).resolves.toEqual({
|
||||
share: '\\\\wsl.localhost\\Ubuntu'
|
||||
})
|
||||
})
|
||||
|
||||
it('treats an answer dated after now as unknown', async () => {
|
||||
await readWslMappedDriveShareNow(
|
||||
'F',
|
||||
deps(async () => 'F:\\', { now: () => Date.now() + 60_000 })
|
||||
)
|
||||
expect(wslMappedDriveLastKnownShare('F')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,6 +6,13 @@ import { realpath } from 'node:fs'
|
||||
import { win32 } from 'node:path'
|
||||
import { normalizeRuntimePathForComparison } from '../../../shared/file-access/cross-platform-path'
|
||||
import { parseWslUncPath } from '../../../shared/file-access/wsl-paths'
|
||||
import {
|
||||
clearWslMappedDriveLastKnownForTests,
|
||||
isWslMappedDriveLastKnownInWsl,
|
||||
rememberWslMappedDriveFailure,
|
||||
rememberWslMappedDriveRead,
|
||||
wslMappedDriveLastKnownShare
|
||||
} from './wsl-mapped-drive-last-known'
|
||||
|
||||
const RESOLVE_TIMEOUT_MS = 2_000
|
||||
const CACHE_TTL_MS = 60_000
|
||||
@@ -23,8 +30,6 @@ export type WslMappedDriveShareDeps = {
|
||||
type CacheEntry = { settledAt: number | null; lookup: Promise<string | null> }
|
||||
|
||||
const cache = new Map<string, CacheEntry>()
|
||||
// Each letter's last settled answer from either lookup; background paths read only this.
|
||||
const lastKnown = new Map<string, { share: string | null; at: number }>()
|
||||
|
||||
const defaultDeps: WslMappedDriveShareDeps = {
|
||||
get platform() {
|
||||
@@ -61,11 +66,11 @@ function startDriveLookup(key: string, deps: WslMappedDriveShareDeps): CacheEntr
|
||||
entry.lookup = deps.realpathRoot(`${key}:\\`).then(
|
||||
(real) => {
|
||||
entry.settledAt = deps.now()
|
||||
return remember(key, real, deps.now())
|
||||
return rememberWslMappedDriveRead(key, real, deps.now())
|
||||
},
|
||||
() => {
|
||||
// Maps nowhere for now (no such drive, a disconnected share); the next add asks again.
|
||||
remember(key, null, deps.now())
|
||||
rememberWslMappedDriveFailure(key, deps.now())
|
||||
if (cache.get(key) === entry) {
|
||||
cache.delete(key)
|
||||
}
|
||||
@@ -106,40 +111,7 @@ export async function resolveWslMappedDrivePath(
|
||||
/** What a drive letter maps to right now; `unverifiable` when Windows didn't answer in time. */
|
||||
export type WslMappedDriveRead = { share: string | null } | 'unverifiable'
|
||||
|
||||
function remember(key: string, real: string | null, at: number): string | null {
|
||||
const share = real && parseWslUncPath(real) ? real.replace(/[\\/]+$/, '') : null
|
||||
lastKnown.set(key, { share, at })
|
||||
answerSink?.(wslMappedDriveAnswers())
|
||||
return share
|
||||
}
|
||||
|
||||
/** A letter's settled answer: the share it maps to (null: nowhere), and when it was read. */
|
||||
export type WslMappedDriveAnswer = { letter: string; share: string | null; at: number }
|
||||
|
||||
// Told every settled answer, so a restart knows a drive without reading it.
|
||||
let answerSink: ((answers: WslMappedDriveAnswer[]) => void) | null = null
|
||||
|
||||
export function setWslMappedDriveAnswerSink(
|
||||
sink: ((answers: WslMappedDriveAnswer[]) => void) | null
|
||||
): void {
|
||||
answerSink = sink
|
||||
}
|
||||
|
||||
export function wslMappedDriveAnswers(): WslMappedDriveAnswer[] {
|
||||
return [...lastKnown].map(([letter, known]) => ({ letter, ...known }))
|
||||
}
|
||||
|
||||
/** Seeds answers read from disk; one already settled in this run is newer and wins. */
|
||||
export function seedWslMappedDriveAnswers(answers: readonly WslMappedDriveAnswer[]): void {
|
||||
for (const { letter, share, at } of answers) {
|
||||
const key = letter.toUpperCase()
|
||||
if (!lastKnown.has(key)) {
|
||||
lastKnown.set(key, { share, at })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type FreshLookup = { serial: number; settled: Promise<{ share: string | null }> }
|
||||
type FreshLookup = { serial: number; settled: Promise<WslMappedDriveRead> }
|
||||
// Serials order fresh lookups, so a request can tell one started after it from an older one.
|
||||
let freshSerial = 0
|
||||
const freshLookups = new Map<string, FreshLookup>()
|
||||
@@ -148,9 +120,10 @@ function startFreshLookup(key: string, deps: WslMappedDriveShareDeps): FreshLook
|
||||
const entry: FreshLookup = {
|
||||
serial: ++freshSerial,
|
||||
settled: deps.realpathRoot(`${key}:\\`).then(
|
||||
(real) => ({ share: remember(key, real, deps.now()) }),
|
||||
// No such drive, or not one Windows can resolve: nothing is mapped there.
|
||||
() => ({ share: remember(key, null, deps.now()) })
|
||||
(real) => ({ share: rememberWslMappedDriveRead(key, real, deps.now()) }),
|
||||
// No such drive, or not one Windows can resolve: nowhere, unless last known in WSL.
|
||||
(): WslMappedDriveRead =>
|
||||
rememberWslMappedDriveFailure(key, deps.now()) ? 'unverifiable' : { share: null }
|
||||
)
|
||||
}
|
||||
freshLookups.set(key, entry)
|
||||
@@ -167,7 +140,7 @@ async function freshAnswer(
|
||||
key: string,
|
||||
deps: WslMappedDriveShareDeps,
|
||||
after: number
|
||||
): Promise<{ share: string | null }> {
|
||||
): Promise<WslMappedDriveRead> {
|
||||
for (;;) {
|
||||
const current = freshLookups.get(key)
|
||||
if (!current) {
|
||||
@@ -202,21 +175,9 @@ export async function readWslMappedDriveShareNow(
|
||||
}
|
||||
|
||||
/**
|
||||
* The letter's last answer, never read here; null before any lookup of it settled, or when the
|
||||
* answer is older than `maxAgeMs`. Background readers take any age: they never read a drive.
|
||||
*/
|
||||
export function wslMappedDriveLastKnownShare(
|
||||
letter: string,
|
||||
maxAgeMs = Number.POSITIVE_INFINITY,
|
||||
now: number = Date.now()
|
||||
): { share: string | null } | null {
|
||||
const known = lastKnown.get(letter.toUpperCase())
|
||||
return known && now - known.at < maxAgeMs ? { share: known.share } : null
|
||||
}
|
||||
|
||||
/**
|
||||
* The letter's last answer, classifying an unknown letter with one bounded, cached lookup; null
|
||||
* when that lookup did not settle in time.
|
||||
* The letter's last answer, classifying an unknown or stale letter with one bounded, cached
|
||||
* lookup. When that doesn't settle (or fails) and the letter was last known in WSL, that answer
|
||||
* of any age, so the caller's fresh read decides; null when nothing is known.
|
||||
*/
|
||||
export async function classifyWslMappedDrive(
|
||||
letter: string,
|
||||
@@ -231,7 +192,13 @@ export async function classifyWslMappedDrive(
|
||||
return known
|
||||
}
|
||||
await resolveDriveShare(letter, deps)
|
||||
return wslMappedDriveLastKnownShare(letter, CACHE_TTL_MS, deps.now())
|
||||
const settled = wslMappedDriveLastKnownShare(letter, CACHE_TTL_MS, deps.now())
|
||||
if (settled) {
|
||||
return settled
|
||||
}
|
||||
return isWslMappedDriveLastKnownInWsl(letter, deps.now())
|
||||
? wslMappedDriveLastKnownShare(letter, Number.POSITIVE_INFINITY, deps.now())
|
||||
: null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,7 +220,6 @@ export function wslMappedDriveMatches(
|
||||
|
||||
export function clearWslMappedDriveShareCacheForTests(): void {
|
||||
cache.clear()
|
||||
lastKnown.clear()
|
||||
answerSink = null
|
||||
clearWslMappedDriveLastKnownForTests()
|
||||
freshLookups.clear()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import '@testing-library/jest-dom/vitest'
|
||||
import { cleanup, render, screen } from '@testing-library/react'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { WslMoveResultBody } from './WslMoveResultBody'
|
||||
|
||||
afterEach(cleanup)
|
||||
|
||||
describe('WslMoveResultBody', () => {
|
||||
it('names the projects a move left because their drive points elsewhere now', () => {
|
||||
render(
|
||||
<WslMoveResultBody
|
||||
distro="Ubuntu"
|
||||
result={{
|
||||
outcome: 'moved',
|
||||
migrationId: 'move-1',
|
||||
phase: 'retained',
|
||||
reloadRequired: true,
|
||||
droppedForMappedDrives: [{ kind: 'repo', id: 'repo-w', name: 'other' }]
|
||||
}}
|
||||
blockers={[]}
|
||||
reload="idle"
|
||||
onReload={vi.fn()}
|
||||
onReview={vi.fn()}
|
||||
onClose={vi.fn()}
|
||||
/>
|
||||
)
|
||||
expect(screen.getByTestId('wsl-move-dropped-for-drives')).toHaveTextContent(
|
||||
'no longer points into Ubuntu: other'
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -107,6 +107,18 @@ export function WslMoveResultBody({
|
||||
{result.browserLoginsNotCopiedReason ? (
|
||||
<p>{wslMoveBrowserLoginsNotCopied(result.browserLoginsNotCopiedReason, t)}</p>
|
||||
) : null}
|
||||
{result.droppedForMappedDrives?.length ? (
|
||||
<p data-testid="wsl-move-dropped-for-drives">
|
||||
{t(
|
||||
'wslMove.result.droppedForMappedDrives',
|
||||
'Not moved, because their drive letter no longer points into {{distro}}: {{names}}. Check the drive, then move them again.',
|
||||
{
|
||||
distro,
|
||||
names: result.droppedForMappedDrives.map((project) => project.name).join(', ')
|
||||
}
|
||||
)}
|
||||
</p>
|
||||
) : null}
|
||||
<p className="text-muted-foreground">
|
||||
{t(
|
||||
'wslMove.result.undoHint',
|
||||
|
||||
@@ -24742,7 +24742,8 @@
|
||||
"review": "Back to projects",
|
||||
"browserLoginsHostUnknown": "Browser logins were not copied, because Orca could not identify the host’s browser profile. Sign in again in the host’s browser tabs.",
|
||||
"browserLoginsConflict": "Browser logins were not copied, because the host’s browser profile is already in use. Sign in again in the host’s browser tabs.",
|
||||
"browserLoginsNotCopied": "Browser logins were not copied to the host. Sign in again in the host’s browser tabs."
|
||||
"browserLoginsNotCopied": "Browser logins were not copied to the host. Sign in again in the host’s browser tabs.",
|
||||
"droppedForMappedDrives": "Not moved, because their drive letter no longer points into {{distro}}: {{names}}. Check the drive, then move them again."
|
||||
},
|
||||
"runtime": {
|
||||
"label": "Project runtime",
|
||||
|
||||
@@ -20,6 +20,9 @@ export type WslMoveRequest = {
|
||||
|
||||
export type WslMoveRowRef = { kind: 'repo' | 'folder'; id: string }
|
||||
|
||||
/** A project a whole-distro start left, because its drive letter now points elsewhere. */
|
||||
export type WslMoveDroppedProject = WslMoveRowRef & { name: string }
|
||||
|
||||
export type WslMoveBlockerKind =
|
||||
| 'agent-working'
|
||||
| 'process-running'
|
||||
@@ -122,6 +125,8 @@ export type WslMoveResult =
|
||||
automationsPending?: string[]
|
||||
/** Set by the browser-login copy when the host's browser starts signed out. */
|
||||
browserLoginsNotCopiedReason?: string
|
||||
/** Previewed projects this start left: their drive letter points elsewhere now. */
|
||||
droppedForMappedDrives?: WslMoveDroppedProject[]
|
||||
}
|
||||
| WslMoveRefusal
|
||||
| {
|
||||
|
||||
Reference in New Issue
Block a user