diff --git a/src/main/index.ts b/src/main/index.ts index 3af46c372ea..041f461685f 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -35,8 +35,7 @@ import { prepareEarlyMainUiLanguage, translateMain } from './i18n/main-i18n' import { ProfileStateRecoveryRequiredError } from './persistence/profile-state/profile-state-recovery-required' import { getProfileUserDataPath } from './orca-profiles/profile-storage-paths' import { leaseAppImageGenerationForCurrentProcess } from './cli/appimage-generation-lease' -import { wakeRememberedWslHosts } from './wsl/wsl-host-early-wake' -import { wslHostsServing } from './wsl/wsl-host-gate' +import { deleteLegacyWslHostWakeList } from './wsl/wsl-host-early-wake' import { configureWslHostsGateFromDisk } from './wsl/wsl-host-gate-startup' import { ignoreInheritedWslHostsGateState } from '../shared/wsl-host-internal-gate' import { registerOrcaProtocolClient } from './startup/orca-protocol-registration' @@ -133,9 +132,9 @@ if (preflightReady) { void leaseAppImageGenerationForCurrentProcess() // Before any gate check: saved WSL work keeps a closed gate serving it. configureWslHostsGateFromDisk(app.getPath('userData'), app.getVersion()) - // Why this early: a cold distro takes seconds to boot; starting now overlaps it with app startup. - if (process.platform === 'win32' && wslHostsServing()) { - wakeRememberedWslHosts(app.getPath('userData')) + // A launch restore is passive (E9): it never boots a WSL host's distro, so drop the old wake list. + if (process.platform === 'win32') { + deleteLegacyWslHostWakeList(app.getPath('userData')) } app.on('open-url', (event, url) => { if (!isRoutedOrcaDeepLink(url)) { diff --git a/src/main/wsl/wsl-host-early-wake.test.ts b/src/main/wsl/wsl-host-early-wake.test.ts index 779d9e39b28..c897b5bac88 100644 --- a/src/main/wsl/wsl-host-early-wake.test.ts +++ b/src/main/wsl/wsl-host-early-wake.test.ts @@ -1,67 +1,27 @@ import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { writeWslHostDeployment } from '../../shared/wsl-host-deployment-store' -import { - forgetWslHostToWake, - forgetWslHostsToWake, - rememberWslHostsToWake, - wakeRememberedWslHosts -} from './wsl-host-early-wake' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { deleteLegacyWslHostWakeList } from './wsl-host-early-wake' -describe('WSL host early wake', () => { +describe('the old WSL host early-wake list', () => { let userDataPath: string + beforeEach(() => { - userDataPath = mkdtempSync(join(tmpdir(), 'orca-wsl-wake-')) - writeWslHostDeployment(userDataPath, 'wsl:ubuntu', { - kind: 'wsl', - distro: 'Ubuntu', - localPort: 41000, - remotePort: 6768, - environmentId: 'wsl:ubuntu', - createdAt: 1 - }) - }) - afterEach(() => rmSync(userDataPath, { recursive: true, force: true })) - - it('wakes only the remembered distros that are still WSL hosts', () => { - rememberWslHostsToWake(userDataPath, ['Ubuntu', 'Removed']) - const spawn = vi.fn() - expect(wakeRememberedWslHosts(userDataPath, spawn)).toEqual(['Ubuntu']) - expect(spawn).toHaveBeenCalledExactlyOnceWith('Ubuntu') + userDataPath = mkdtempSync(join(tmpdir(), 'orca-wsl-wake-list-')) }) - it('wakes nothing once the open work is gone', () => { - rememberWslHostsToWake(userDataPath, ['Ubuntu']) - rememberWslHostsToWake(userDataPath, []) - const spawn = vi.fn() - expect(wakeRememberedWslHosts(userDataPath, spawn)).toEqual([]) - expect(spawn).not.toHaveBeenCalled() + afterEach(() => { + rmSync(userDataPath, { recursive: true, force: true }) }) - it('wakes nothing without a list, or from an unreadable one', () => { - const spawn = vi.fn() - expect(wakeRememberedWslHosts(userDataPath, spawn)).toEqual([]) - writeFileSync(join(userDataPath, 'orca-wsl-host-wake.json'), '{"version":1,"distros":["a b"]}') - expect(wakeRememberedWslHosts(userDataPath, spawn)).toEqual([]) - expect(spawn).not.toHaveBeenCalled() - }) + it('is deleted at launch, so no build boots a distro from it again (E9)', () => { + const wakeList = join(userDataPath, 'orca-wsl-host-wake.json') + writeFileSync(wakeList, JSON.stringify({ version: 1, distros: ['Ubuntu'] })) - it('deletes the list when WSL hosts are turned off, so the next launch wakes nothing', () => { - rememberWslHostsToWake(userDataPath, ['Ubuntu']) - forgetWslHostsToWake(userDataPath) - expect(existsSync(join(userDataPath, 'orca-wsl-host-wake.json'))).toBe(false) - const spawn = vi.fn() - expect(wakeRememberedWslHosts(userDataPath, spawn)).toEqual([]) - // Nothing to delete is fine too. - expect(() => forgetWslHostsToWake(userDataPath)).not.toThrow() - }) + deleteLegacyWslHostWakeList(userDataPath) - it('forgets a removed host even while its record is still on disk', () => { - rememberWslHostsToWake(userDataPath, ['Ubuntu']) - forgetWslHostToWake(userDataPath, 'ubuntu') - const spawn = vi.fn() - expect(wakeRememberedWslHosts(userDataPath, spawn)).toEqual([]) + expect(existsSync(wakeList)).toBe(false) + expect(() => deleteLegacyWslHostWakeList(userDataPath)).not.toThrow() }) }) diff --git a/src/main/wsl/wsl-host-early-wake.ts b/src/main/wsl/wsl-host-early-wake.ts index 6a3829093f7..19eae8cd498 100644 --- a/src/main/wsl/wsl-host-early-wake.ts +++ b/src/main/wsl/wsl-host-early-wake.ts @@ -1,127 +1,18 @@ /** - * Boots the distro of the WSL host the last run had focused, at the very start of the next launch, - * so the boot (seconds on a slow PC) overlaps the app's own startup instead of following it. Only - * the focused host is listed (E9: a launch boots the focused host, never others with saved tabs). + * Older builds booted the last focused WSL host's distro at launch from this list. That broke E9 + * (a launch restore is passive), so nothing writes or reads the list now; a launch deletes a list + * an older build left behind. */ import { rmSync } from 'node:fs' import { join } from 'node:path' -import { z } from 'zod' -import { spawnProcess } from '@orca/process-host' -import { writeSecureJsonFileWithinLimit } from '../../shared/bounded-secure-json-file' -import { readNodeFileSyncWithinLimit } from '../../shared/node-bounded-file-reader' -import { readWslHostDeployments } from '../../shared/wsl-host-deployment-store' -import { buildWslExecArgs } from '../../shared/wsl-login-shell-command' -import { WslDistroNameSchema } from '../../shared/wsl-host-deployment-link' -import { resolveWslInteropSpawnCwd } from '../wsl-interop-spawn-directory' -import { wslDistroHolderKey } from './wsl-distro-holder-record' -import { resolveWslExecutablePath } from './wsl-executable-path' const WAKE_FILE = 'orca-wsl-host-wake.json' -const MAX_WAKE_FILE_BYTES = 16 * 1024 -const WakeListSchema = z.object({ version: z.literal(1), distros: z.array(WslDistroNameSchema) }) - -function wakeFile(userDataPath: string): string { - return join(userDataPath, WAKE_FILE) -} - -function readWakeList(userDataPath: string): string[] { +export function deleteLegacyWslHostWakeList(userDataPath: string): void { try { - const { buffer } = readNodeFileSyncWithinLimit(wakeFile(userDataPath), MAX_WAKE_FILE_BYTES) - return WakeListSchema.parse(JSON.parse(buffer.toString('utf8'))).distros - } catch { - return [] - } -} - -/** Saves the distro of the host the user is focused on (or none); called on each focus entry. */ -export function rememberWslHostsToWake(userDataPath: string, distros: readonly string[]): void { - const next = [...new Set(distros)].sort() - const current = readWakeList(userDataPath) - if (current.length === next.length && current.every((distro, i) => distro === next[i])) { - return - } - try { - writeSecureJsonFileWithinLimit( - wakeFile(userDataPath), - { version: 1, distros: next }, - MAX_WAKE_FILE_BYTES - ) + rmSync(join(userDataPath, WAKE_FILE), { force: true }) } catch (error) { - console.warn('[wsl-hosts] Could not save the distros to wake at next launch:', error) + // Nothing reads it any more; a leftover file is inert. + console.warn('[wsl-hosts] Could not delete the old early-wake list:', error) } } - -/** - * WSL hosts turned off: the file is the only thing the next launch reads (before settings load), - * so it goes now, synchronously. If it can't be deleted it is emptied instead, and if that fails - * too the failure is logged; a missing or unreadable file wakes nothing. - */ -export function forgetWslHostsToWake(userDataPath: string): void { - try { - rmSync(wakeFile(userDataPath), { force: true }) - } catch (error) { - console.warn('[wsl-hosts] Could not delete the early-wake list; emptying it:', error) - rememberWslHostsToWake(userDataPath, []) - } -} - -/** A removed host is never woken early, even before its record is gone. */ -export function forgetWslHostToWake(userDataPath: string, distro: string): void { - const key = wslDistroHolderKey(distro) - const remembered = readWakeList(userDataPath) - if (remembered.some((entry) => wslDistroHolderKey(entry) === key)) { - rememberWslHostsToWake( - userDataPath, - remembered.filter((entry) => wslDistroHolderKey(entry) !== key) - ) - } -} - -export type WslWakeSpawn = (distro: string) => void - -function spawnWake(distro: string): void { - const child = spawnProcess({ - program: resolveWslExecutablePath(), - args: buildWslExecArgs(distro, ['true']), - cwd: resolveWslInteropSpawnCwd(), - env: { ...process.env, WSL_UTF8: '1' }, - stdio: 'ignore' - }) - child.on('error', () => {}) - child.unref() -} - -/** - * Starts booting each remembered distro that is still a registered WSL host. Fire and forget: the - * connect that follows does the real work and reports any failure. - */ -export function wakeRememberedWslHosts( - userDataPath: string, - spawn: WslWakeSpawn = spawnWake -): string[] { - const remembered = readWakeList(userDataPath) - if (remembered.length === 0) { - return [] - } - let registered: Set - try { - // A host whose Remove is pending must not be booted: that would undo its stop's proof. - registered = new Set( - readWslHostDeployments(userDataPath) - .deployments.filter((record) => !record.pendingStop) - .map((record) => wslDistroHolderKey(record.distro)) - ) - } catch { - return [] - } - const woken = remembered.filter((distro) => registered.has(wslDistroHolderKey(distro))) - for (const distro of woken) { - try { - spawn(distro) - } catch (error) { - console.warn(`[wsl-hosts] Could not start waking ${distro}:`, error) - } - } - return woken -} diff --git a/src/main/wsl/wsl-host-pending-removal.test.ts b/src/main/wsl/wsl-host-pending-removal.test.ts index 23fb73a95fc..37f1a2bfc96 100644 --- a/src/main/wsl/wsl-host-pending-removal.test.ts +++ b/src/main/wsl/wsl-host-pending-removal.test.ts @@ -3,14 +3,10 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { listEnvironments } from '../../shared/runtime-environment-store' -import { - findWslHostDeployment, - setWslHostPendingStop -} from '../../shared/runtime-environment-wsl-host-store' +import { findWslHostDeployment } from '../../shared/runtime-environment-wsl-host-store' import type { WslHostStatus } from '../../shared/wsl-host-types' import { WslHostService, type WslHostConnector } from './wsl-host-service' import { UBUNTU, UBUNTU_ID, makeConnector } from './wsl-host-service-test-connector' -import { rememberWslHostsToWake, wakeRememberedWslHosts } from './wsl-host-early-wake' // E11: a Remove that can't prove the host's server stopped keeps the host, pending. describe('WslHostService pending removal', () => { @@ -231,32 +227,6 @@ describe('WslHostService pending removal', () => { expect(connector.connect).not.toHaveBeenCalled() }) - it('never boots a pending host early at launch', async () => { - const connector = makeConnector([UBUNTU]) - const service = makeService(connector) - await service.add({ distro: 'Ubuntu' }) - rememberWslHostsToWake(userDataPath, ['Ubuntu']) - // A Remove left pending by a build that still remembered the host. - setWslHostPendingStop(userDataPath, 'wsl:ubuntu', { since: 1, reason: 'busy', verdict: 'live' }) - const spawn = vi.fn() - - expect(wakeRememberedWslHosts(userDataPath, spawn)).toEqual([]) - expect(spawn).not.toHaveBeenCalled() - }) - - it('forgets the early wake of a host whose Remove ended pending', async () => { - const connector = makeConnector([UBUNTU]) - connector.stopServer.mockResolvedValueOnce({ stopped: false, reason: 'busy', verdict: 'live' }) - const service = makeService(connector) - await service.add({ distro: 'Ubuntu' }) - service.focusHost('wsl:ubuntu') - - await service.remove('wsl:ubuntu') - // Withdrawing the removal must not bring the early wake back by itself. - await service.connect('wsl:ubuntu', { withdrawPendingRemoval: true }) - expect(wakeRememberedWslHosts(userDataPath, vi.fn())).toEqual([]) - }) - it('a due automation never boots a host whose Remove is pending', async () => { const connector = makeConnector([UBUNTU]) connector.stopServer.mockResolvedValueOnce({ stopped: false, reason: 'busy', verdict: 'live' }) diff --git a/src/main/wsl/wsl-host-runtime-call.ts b/src/main/wsl/wsl-host-runtime-call.ts index 2c97ee3a62d..45bb029e988 100644 --- a/src/main/wsl/wsl-host-runtime-call.ts +++ b/src/main/wsl/wsl-host-runtime-call.ts @@ -20,6 +20,8 @@ export type WslHostRuntimeCallPort = { /** Hosts a confirmed stop parked: no runtime call reconnects them until the distro runs again. */ parked: Set isVisible: () => boolean + /** Bumped by every ready publish, so a read that a newer connect overtook is retried, not shown. */ + readyGeneration: () => number /** Shown as liveness shows a stop, so the host's panes offer Start. */ publishStopped: (record: WslHostDeploymentRecord) => void publishReady: (record: WslHostDeploymentRecord, connection: WslOrcadConnection) => void @@ -39,10 +41,15 @@ export async function ensureWslHostForRuntimeCall( if (!record || !connector) { return } + const generation = port.readyGeneration() const reachability = await connector.reachability(record.distro, environmentId) if (reachability === 'available') { return } + // A Start or New Terminal connected during the read: its 'stopped' would mark live terminals exited. + if (port.readyGeneration() !== generation) { + return ensureWslHostForRuntimeCall(environmentId, port) + } const refusal = wslHostRunRefusal(deps) if (refusal || port.removing.has(environmentId)) { throw new Error(refusal?.message ?? WSL_HOST_REMOVING_MESSAGE) diff --git a/src/main/wsl/wsl-host-service.test.ts b/src/main/wsl/wsl-host-service.test.ts index 4afa75676c2..56ee7fc62b3 100644 --- a/src/main/wsl/wsl-host-service.test.ts +++ b/src/main/wsl/wsl-host-service.test.ts @@ -15,7 +15,6 @@ import type { WslHostStatus } from '../../shared/wsl-host-types' import { OrcadHostUnsupportedError } from '../orcad/orcad-host-unavailable' import { RemoteNodeRuntimeSelfTestError } from '../ssh/orcad-remote-node-runtime-report' import { WslHostService, type WslHostConnector } from './wsl-host-service' -import { wakeRememberedWslHosts } from './wsl-host-early-wake' import { WslOrcadLaunchDeferredError } from './wsl-orcad-connection-errors' import { RemoteRuntimeClientError } from '../../shared/remote-runtime/remote-runtime-client-error' import { isRecoverableRemoteRuntimeConnectionError } from '../../shared/remote-runtime/remote-runtime-client-error-classification' @@ -398,7 +397,7 @@ describe('WslHostService', () => { expect(connector.connect).not.toHaveBeenCalled() }) - it('keeps a confirmed-stopped host down for polls until the user switches back to it (E9)', async () => { + it('keeps a confirmed-stopped host down for polls and for a switch back to it; only a running distro reconnects (E9)', async () => { const connector = makeConnector([UBUNTU]) const service = makeService(connector) await service.add({ distro: 'Ubuntu' }) @@ -414,16 +413,24 @@ describe('WslHostService', () => { // The focused host's polls get a refusal, not a boot, however often they come. await expect(service.ensureForRuntimeCall('wsl:ubuntu')).rejects.toThrow('Ubuntu was stopped') - service.setVisibleWork(['wsl:ubuntu']) - await expect(service.ensureForRuntimeCall('wsl:ubuntu')).rejects.toThrow('Ubuntu was stopped') expect(connector.connect).not.toHaveBeenCalled() - // Switching to one of its workspaces is the user acting on it. + // Switching back to one of its workspaces is navigation: it re-checks, never starts it. + service.focusHost('wsl:ubuntu') + await vi.waitFor(() => + expect(connector.reachability).toHaveBeenCalledWith('Ubuntu', 'wsl:ubuntu') + ) + expect(connector.connect).not.toHaveBeenCalled() + expect(published.at(-1)).toMatchObject({ environmentId: 'wsl:ubuntu', state: 'distro-stopped' }) + + // Once the distro runs (the user's Start or a terminal), a switch connects without booting. + connector.distro.state = 'running' service.focusHost('wsl:ubuntu') await vi.waitFor(() => expect(connector.connect).toHaveBeenCalledTimes(1)) + expect(connector.connect).toHaveBeenCalledWith('Ubuntu', { noBoot: true }) }) - it('focus connects without probing WSL while this process has no bridge, but polls still read the running list', async () => { + it('focus reads the running list before connecting, even with no bridge in this process', async () => { const connector = makeConnector([UBUNTU]) const service = makeService(connector) await service.add({ distro: 'Ubuntu' }) @@ -433,15 +440,8 @@ describe('WslHostService', () => { service.focusHost('wsl:ubuntu') await vi.waitFor(() => expect(connector.connect).toHaveBeenCalledTimes(1)) - expect(connector.reachability).not.toHaveBeenCalled() - - // A poll never takes that shortcut: it reads the running list and refuses a stopped distro. - await connector.disconnect('Ubuntu') - connector.distro.state = 'stopped' - connector.connect.mockClear() - await expect(service.ensureForRuntimeCall('wsl:ubuntu')).rejects.toThrow('Ubuntu was stopped') expect(connector.reachability).toHaveBeenCalledWith('Ubuntu', 'wsl:ubuntu') - expect(connector.connect).not.toHaveBeenCalled() + expect(connector.connect).toHaveBeenCalledWith('Ubuntu', { noBoot: true }) }) it('focus with a bridge still asks whether the host answers before connecting again', async () => { @@ -526,19 +526,49 @@ describe('WslHostService', () => { expect(connector.connect).not.toHaveBeenCalled() }) - it('a poll after a focus boot failed does not retry the boot', async () => { + it('a launch restore or switch to a stopped host never connects it and shows it stopped (E9)', async () => { const connector = makeConnector([UBUNTU]) const service = makeService(connector) await service.add({ distro: 'Ubuntu' }) - service.setVisibleWork(['wsl:ubuntu']) await connector.disconnect('Ubuntu') connector.distro.state = 'stopped' connector.connect.mockClear() - connector.connect.mockRejectedValueOnce(new Error('boot failed')) + + // The window reports the restored active workspace's host at launch, before any user act. service.focusHost('wsl:ubuntu') - await vi.waitFor(() => expect(connector.connect).toHaveBeenCalledTimes(1)) - await expect(service.ensureForRuntimeCall('wsl:ubuntu')).rejects.toThrow('Ubuntu was stopped') - expect(connector.connect).toHaveBeenCalledTimes(1) + + await vi.waitFor(() => + expect(published.at(-1)).toMatchObject({ + environmentId: 'wsl:ubuntu', + state: 'distro-stopped' + }) + ) + expect(connector.connect).not.toHaveBeenCalled() + }) + + it('a focus re-check overtaken by a user Start never publishes stopped over the live host', async () => { + const connector = makeConnector([UBUNTU]) + const service = makeService(connector) + await service.add({ distro: 'Ubuntu' }) + await connector.disconnect('Ubuntu') + connector.distro.state = 'stopped' + let finishRead: (state: 'stopped') => void = () => {} + connector.reachability.mockImplementationOnce( + () => new Promise((resolve) => (finishRead = resolve)) + ) + + service.focusHost('wsl:ubuntu') + await vi.waitFor(() => expect(connector.reachability).toHaveBeenCalled()) + // The user's Start boots and connects while the focus read is still out. + connector.distro.state = 'running' + await service.connect('wsl:ubuntu') + service.handleConnectionChange('Ubuntu', connection('Ubuntu'), { state: 'ready', census: null }) + finishRead('stopped') + + await vi.waitFor(() => expect(connector.reachability).toHaveBeenCalledTimes(2)) + await expect(service.ensureForRuntimeCall('wsl:ubuntu')).resolves.toBeUndefined() + expect(published.at(-1)).toMatchObject({ environmentId: 'wsl:ubuntu', state: 'ready' }) + expect(published.filter((status) => status.state === 'distro-stopped')).toEqual([]) }) it('never boots for a poll: not visible, or unconfirmed, is a refusal; lost contact never parks', async () => { @@ -713,36 +743,6 @@ describe('WslHostService', () => { expect(knownWhileAdding).toBe(true) }) - it('remembers only the focused host for an early boot next launch (E9)', async () => { - const connector = makeConnector([UBUNTU]) - const service = makeService(connector) - await service.add({ distro: 'Ubuntu' }) - const spawn = vi.fn() - // Open work alone is not remembered. - service.setVisibleWork(['wsl:ubuntu']) - expect(wakeRememberedWslHosts(userDataPath, spawn)).toEqual([]) - service.focusHost('wsl:ubuntu') - expect(wakeRememberedWslHosts(userDataPath, spawn)).toEqual(['Ubuntu']) - service.focusHost(null) - expect(wakeRememberedWslHosts(userDataPath, spawn)).toEqual([]) - service.focusHost('wsl:ubuntu') - enabled = false - await service.enabledChanged(false) - expect(wakeRememberedWslHosts(userDataPath, spawn)).toEqual([]) - expect(spawn).toHaveBeenCalledOnce() - }) - - it('forgets the early-wake host when it is removed', async () => { - const connector = makeConnector([UBUNTU]) - const service = makeService(connector) - await service.add({ distro: 'Ubuntu' }) - service.focusHost('wsl:ubuntu') - await service.remove('wsl:ubuntu') - // Re-registering the distro must not bring the old early wake back. - await service.add({ distro: 'Ubuntu' }) - expect(wakeRememberedWslHosts(userDataPath, vi.fn())).toEqual([]) - }) - it('keeps the startup holders of its hosts and releases orphans', async () => { const connector = makeConnector([UBUNTU]) const service = makeService(connector) diff --git a/src/main/wsl/wsl-host-service.ts b/src/main/wsl/wsl-host-service.ts index 3ef0b9d30cc..b31f8deae38 100644 --- a/src/main/wsl/wsl-host-service.ts +++ b/src/main/wsl/wsl-host-service.ts @@ -36,11 +36,6 @@ import { removeWslHostServer } from './wsl-host-removal' import type { WslOrcadConnectionStatus } from './wsl-orcad-connection-status' -import { - forgetWslHostToWake, - forgetWslHostsToWake, - rememberWslHostsToWake -} from './wsl-host-early-wake' export type { WslHostConnector, @@ -55,6 +50,7 @@ export class WslHostService { private visible = new Set() private readonly parked = new Set() private readonly removing = new Set() + private readonly readyGenerations = new Map() // Invalidate in-flight Adds when disabled so they cannot later publish ready hosts. private disableGeneration = 0 @@ -151,8 +147,6 @@ export class WslHostService { this.connector, record ).finally(() => this.removing.delete(environmentId)) - // Removed or pending, nothing may boot it early at the next launch. - forgetWslHostToWake(this.deps.userDataPath(), record.distro) if (!removal.removed) { this.setStatus(this.recordForEnvironment(environmentId) ?? record, { state: 'closed' }) return removal.serverStop @@ -175,7 +169,6 @@ export class WslHostService { return } this.disableGeneration += 1 - forgetWslHostsToWake(this.deps.userDataPath()) for (const record of this.records()) { await connector.disconnect(record.distro) await connector.releaseHolder(record.distro) @@ -209,14 +202,18 @@ export class WslHostService { } /** A runtime call (a poll, a fetch) never boots a distro (E9); see ensureWslHostForRuntimeCall. */ - async ensureForRuntimeCall(environmentId: string): Promise { + async ensureForRuntimeCall( + environmentId: string, + isVisible: () => boolean = () => this.visible.has(environmentId) + ): Promise { await ensureWslHostForRuntimeCall(environmentId, { record: this.recordForEnvironment(environmentId), connector: this.connector, deps: this.deps, removing: this.removing, parked: this.parked, - isVisible: () => this.visible.has(environmentId), + isVisible, + readyGeneration: () => this.readyGenerations.get(environmentId) ?? 0, publishStopped: (record) => this.setStatus(record, { state: 'distro-stopped', terminals: 'exited', recovering: false }), publishReady: (record, connection) => @@ -226,19 +223,20 @@ export class WslHostService { } /** - * The user switched to a workspace on this host (or launch restored it focused): the one - * non-explicit act that may boot its distro, and it lifts a park. + * A workspace on this host became active: the user switched to it, or a launch restored it. That + * is navigation, never a start (E9): it re-checks a park and connects only to a running distro; a + * stopped one is published stopped, so its panes show Start. Start and a new terminal boot it. */ focusHost(environmentId: string | null): void { const record = environmentId ? this.recordForEnvironment(environmentId) : null - const runnable = record !== null && this.mayWake(record) - // The next launch starts booting only this distro, early (none when off WSL or not runnable). - rememberWslHostsToWake(this.deps.userDataPath(), record && runnable ? [record.distro] : []) - if (record && runnable) { - this.wake(record).catch((error: unknown) => { - console.warn(`[wsl-hosts] Could not connect to ${record.distro}:`, error) - }) + if (!record || !this.mayWake(record)) { + return } + this.parked.delete(record.environmentId) + // The focused workspace's work is visible even before the window re-reports it. + this.ensureForRuntimeCall(record.environmentId, () => true).catch(() => { + // Refused (stopped, settling, unconfirmed): the published status tells the panes. + }) } /** E9: an enabled automation came due with the host off; boots the distro once, lifting a park. */ @@ -338,6 +336,10 @@ export class WslHostService { status: WslOrcadConnectionStatus, connection: WslOrcadConnection | null = null ): void { + if (status.state === 'ready') { + const generation = this.readyGenerations.get(record.environmentId) ?? 0 + this.readyGenerations.set(record.environmentId, generation + 1) + } this.board.set(toWslHostStatus(record, status, connection)) } diff --git a/src/renderer/src/hooks/ipc-events/wsl-host-ipc-bridge.ts b/src/renderer/src/hooks/ipc-events/wsl-host-ipc-bridge.ts index c47e8629420..da906ac48e9 100644 --- a/src/renderer/src/hooks/ipc-events/wsl-host-ipc-bridge.ts +++ b/src/renderer/src/hooks/ipc-events/wsl-host-ipc-bridge.ts @@ -103,7 +103,7 @@ export function registerWslHostIpcBridge(unsubs: (() => void)[]): void { } const [focusedHost] = selectWslHostsWithVisibleWork(state) const entry = focusedHost ? `${state.activeWorktreeId}\0${focusedHost}` : '' - // Switching to a workspace on a WSL host (or launch restoring one) is the user acting on it. + // A switch or launch restore onto a WSL host: main connects a running distro, never boots one. if (entry !== focusEntry) { focusEntry = entry if (typeof api.focusHost === 'function') {