Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/renderer/src/components/landing-preflight-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useEffect, useMemo } from 'react'
import { useAppStore } from '../store'
import { installWindowVisibilityInterval } from '@/lib/window-visibility-interval'
import {
isConnectedRuntimeHostState,
runtimeHostConnectionStateForEntry
} from '@/runtime/runtime-host-connection-state'
import {
getLandingPreflightIssues,
hasGitHubBackedProject,
Expand All @@ -18,10 +22,13 @@ export function useLandingPreflightRuntime(): { preflightIssues: PreflightIssue[
return 'local'
}
const runtimeStatus = s.runtimeStatusByEnvironmentId.get(environmentId)
// Why the shared verdict and not `entry.status`: an unverifiable probe nulls it while the
// transport is still up, and reading that as unreachable discarded the whole preflight
// result for a host that never went away (docs/reference/ssh-execution-boundary.md).
const reachability = runtimeStatus
? runtimeStatus.status === null
? 'unreachable'
: 'reachable'
? isConnectedRuntimeHostState(runtimeHostConnectionStateForEntry(runtimeStatus))
? 'reachable'
: 'unreachable'
: 'unknown'
return `${environmentId}:${runtimeStatus?.connectionGeneration ?? 0}:${reachability}`
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// @vitest-environment happy-dom

import { act, cleanup, renderHook } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { RuntimeHostStatusSnapshot } from '../../../shared/runtime-host-status'
import type { RuntimeStatus } from '../../../shared/runtime-types'
import { useAppStore } from '../store'
import type { RuntimeEnvironmentStatus } from '../store/slices/runtime-status-types'
import type { AppState } from '../store/types'
import { useLandingPreflightRuntime } from './landing-preflight-runtime'

const ENVIRONMENT_ID = 'environment-a'
const initialState = useAppStore.getInitialState()
const invalidate = vi.fn()
const refresh = vi.fn(async () => {})

function makeStatus(): RuntimeStatus {
return {
runtimeId: 'runtime-a',
rendererGraphEpoch: 0,
graphStatus: 'ready',
authoritativeWindowId: null,
liveTabCount: 0,
liveLeafCount: 0
}
}

function snapshot(overrides: Partial<RuntimeHostStatusSnapshot> = {}): RuntimeHostStatusSnapshot {
return {
environmentId: ENVIRONMENT_ID,
pairingRevision: 1,
sequence: 1,
checkedAt: 1,
status: makeStatus(),
verification: 'verified',
transport: 'ready',
...overrides
}
}

function setStatusEntry(entry: RuntimeEnvironmentStatus): void {
useAppStore.setState({
runtimeStatusByEnvironmentId: new Map([[ENVIRONMENT_ID, entry]])
})
}

describe('landing preflight under an unverifiable host probe', () => {
beforeEach(() => {
invalidate.mockClear()
refresh.mockClear()
useAppStore.setState(
{
...initialState,
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the hook under test reads only activeRuntimeEnvironmentId; the rest of GlobalSettings never reaches it.
settings: { activeRuntimeEnvironmentId: ENVIRONMENT_ID } as AppState['settings'],
invalidatePreflightStatus: invalidate,
refreshPreflightStatus: refresh
},
true
)
setStatusEntry({ status: makeStatus(), snapshot: snapshot(), checkedAt: 1 })
})

afterEach(() => {
cleanup()
useAppStore.setState(initialState, true)
})

it('keeps preflight state when a ready host answers an unverifiable probe', () => {
renderHook(() => useLandingPreflightRuntime())
expect(invalidate).not.toHaveBeenCalled()

act(() => {
setStatusEntry({
status: null,
snapshot: snapshot({ sequence: 2, checkedAt: 2, verification: 'unavailable' }),
checkedAt: 2
})
})

expect(invalidate).not.toHaveBeenCalled()
})

it('still discards preflight state once the transport goes down', () => {
renderHook(() => useLandingPreflightRuntime())

act(() => {
setStatusEntry({
status: null,
snapshot: snapshot({
sequence: 2,
checkedAt: 2,
verification: 'unavailable',
transport: 'disconnected'
}),
checkedAt: 2
})
})

expect(invalidate).toHaveBeenCalled()
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { getRuntimeEnvironmentRevision } from '@/runtime/runtime-environment-revision'
import {
isConnectedRuntimeHostState,
runtimeHostConnectionStateForEntry
} from '@/runtime/runtime-host-connection-state'
import { getEnvironmentSshStateGeneration } from '@/store/slices/runtime-environment-ssh'
import { getRuntimeEnvironmentConnectionGeneration } from '@/store/slices/runtime-status'
import type { AppState } from '../../store/types'
Expand All @@ -21,19 +25,34 @@ export function getRuntimeClientEventEnvironmentIds(
ids.add(activeEnvironmentId)
}
for (const environment of state.runtimeEnvironments ?? []) {
if (state.runtimeStatusByEnvironmentId?.get(environment.id)?.status) {
if (isRuntimeHostStillInContact(state, environment.id)) {
ids.add(environment.id)
}
}
return [...ids]
}

/**
* Why the shared verdict and not `entry.status`: an unverifiable probe nulls `entry.status`
* while the transport stays up and the host keeps delivering. Reading that as "gone" dropped
* the client-event subscription and fired the disconnect edge on a live host. Contact is lost
* only once the transport itself says so (docs/reference/ssh-execution-boundary.md).
*/
function isRuntimeHostStillInContact(
state: RuntimeEnvironmentStoreSyncState,
environmentId: string
): boolean {
return isConnectedRuntimeHostState(
runtimeHostConnectionStateForEntry(state.runtimeStatusByEnvironmentId?.get(environmentId))
)
}

export function getReachableRuntimeEnvironmentIds(
state: RuntimeEnvironmentStoreSyncState
): string[] {
const ids: string[] = []
for (const [environmentId, status] of state.runtimeStatusByEnvironmentId ?? []) {
if (status?.status) {
for (const environmentId of state.runtimeStatusByEnvironmentId?.keys() ?? []) {
if (isRuntimeHostStillInContact(state, environmentId)) {
ids.push(environmentId)
}
}
Expand Down
149 changes: 149 additions & 0 deletions src/renderer/src/lib/runtime-host-unverifiable-probe-readers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { describe, expect, it } from 'vitest'
import type { RuntimeHostStatusSnapshot } from '../../../shared/runtime-host-status'
import type { RuntimeStatus } from '../../../shared/runtime-types'
import {
getReachableRuntimeEnvironmentIds,
getRuntimeClientEventEnvironmentIds
} from '@/hooks/ipc-events/runtime-environment-subscription-selection'
import type { RuntimeEnvironmentStoreSyncState } from '@/hooks/ipc-events/runtime-environment-subscription-selection'
import {
selectRuntimeAwareSshError,
selectRuntimeAwareSshStatus
} from '@/store/slices/runtime-environment-ssh-selectors'

const ENVIRONMENT_ID = 'environment-a'
const VERIFIED_STATUS: RuntimeStatus = {
runtimeId: 'runtime-a',
rendererGraphEpoch: 0,
graphStatus: 'ready',
authoritativeWindowId: null,
liveTabCount: 0,
liveLeafCount: 0
}

function snapshot(overrides: Partial<RuntimeHostStatusSnapshot> = {}): RuntimeHostStatusSnapshot {
return {
environmentId: ENVIRONMENT_ID,
pairingRevision: 1,
sequence: 2,
checkedAt: 2,
status: VERIFIED_STATUS,
verification: 'verified',
transport: 'ready',
...overrides
}
}

/** The defect shape: the host answered once, its transport is still up, the last probe did not answer. */
function unverifiableWhileReady(): {
status: null
checkedAt: number
snapshot: RuntimeHostStatusSnapshot
} {
return { status: null, checkedAt: 2, snapshot: snapshot({ verification: 'unavailable' }) }
}

function transportDown(): { status: null; checkedAt: number; snapshot: RuntimeHostStatusSnapshot } {
return {
status: null,
checkedAt: 2,
snapshot: snapshot({ verification: 'unavailable', transport: 'disconnected' })
}
}

function syncState(
entry: {
status: RuntimeStatus | null
checkedAt: number
snapshot?: RuntimeHostStatusSnapshot
} | null
): RuntimeEnvironmentStoreSyncState {
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the readers under test consult only the four fields below; the rest of AppState never reaches them.
return {
runtimeEnvironments: [{ id: ENVIRONMENT_ID, createdAt: 1 }],
runtimeStatusByEnvironmentId: entry ? new Map([[ENVIRONMENT_ID, entry]]) : new Map(),
settings: { activeRuntimeEnvironmentId: null },
sshStateByEnvironment: new Map()
} as unknown as RuntimeEnvironmentStoreSyncState
}

describe('runtime client-event subscription selection', () => {
it('keeps a host whose transport is ready but whose last probe went unverifiable', () => {
expect(getRuntimeClientEventEnvironmentIds(syncState(unverifiableWhileReady()))).toEqual([
ENVIRONMENT_ID
])
})

it('keeps that host in the reachable set, so no spurious disconnect edge fires', () => {
expect(getReachableRuntimeEnvironmentIds(syncState(unverifiableWhileReady()))).toEqual([
ENVIRONMENT_ID
])
})

it('still drops a host whose transport went down', () => {
expect(getRuntimeClientEventEnvironmentIds(syncState(transportDown()))).toEqual([])
expect(getReachableRuntimeEnvironmentIds(syncState(transportDown()))).toEqual([])
})

it('still drops a retired host and one that never answered', () => {
const retired = { status: null, checkedAt: 2, snapshot: snapshot({ retired: true }) }
expect(getRuntimeClientEventEnvironmentIds(syncState(retired))).toEqual([])
expect(
getRuntimeClientEventEnvironmentIds(
syncState({
status: null,
checkedAt: 0,
snapshot: snapshot({ status: null, verification: 'checking' })
})
)
).toEqual([])
expect(getRuntimeClientEventEnvironmentIds(syncState(null))).toEqual([])
})

it('keeps a verified host', () => {
expect(
getRuntimeClientEventEnvironmentIds(
syncState({ status: VERIFIED_STATUS, checkedAt: 2, snapshot: snapshot() })
)
).toEqual([ENVIRONMENT_ID])
})
})

describe('runtime-aware SSH selectors', () => {
function sshState(entry: {
status: RuntimeStatus | null
checkedAt: number
snapshot?: RuntimeHostStatusSnapshot
}) {
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the selectors read only the SSH maps and the status map built below.
return {
sshConnectionStates: new Map(),
sshTargetLabels: new Map(),
removedSshTargetLabels: new Map(),
sshTargetsHydrated: true,
sshStateByEnvironment: new Map([
[
ENVIRONMENT_ID,
{
targetsHydrated: true,
connectionStates: new Map([['target-a', { status: 'connected', error: 'boom' }]]),
targetLabels: new Map([['target-a', 'Target A']]),
removedTargetLabels: new Map()
}
]
]),
runtimeStatusByEnvironmentId: new Map([[ENVIRONMENT_ID, entry]])
} as unknown as Parameters<typeof selectRuntimeAwareSshStatus>[0]
}

it('keeps reporting a mirrored SSH target while the host probe is unverifiable', () => {
const state = sshState(unverifiableWhileReady())
expect(selectRuntimeAwareSshStatus(state, ENVIRONMENT_ID, 'target-a')).toBe('connected')
expect(selectRuntimeAwareSshError(state, ENVIRONMENT_ID, 'target-a')).toBe('boom')
})

it('still withholds SSH state once the transport is down', () => {
const state = sshState(transportDown())
expect(selectRuntimeAwareSshStatus(state, ENVIRONMENT_ID, 'target-a')).toBeNull()
})
})
18 changes: 15 additions & 3 deletions src/renderer/src/lib/runtime-session-mirror-targets.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { describe, expect, it } from 'vitest'
import type { RuntimeStatus } from '../../../shared/runtime-types'
import { getReachableRuntimeSessionMirrorTargets } from './runtime-session-mirror-targets'

function makeStatus(runtimeId: string): RuntimeStatus {
return {
runtimeId,
rendererGraphEpoch: 0,
graphStatus: 'ready',
authoritativeWindowId: null,
liveTabCount: 0,
liveLeafCount: 0
}
}

const environments = [
{
id: 'online-env',
Expand All @@ -24,7 +36,7 @@ describe('getReachableRuntimeSessionMirrorTargets', () => {
[
'online-env',
{
status: { runtimeId: 'runtime-online' },
status: makeStatus('runtime-online'),
connectionGeneration: 3
}
],
Expand Down Expand Up @@ -55,7 +67,7 @@ describe('getReachableRuntimeSessionMirrorTargets', () => {
settings: { activeRuntimeEnvironmentId: 'missing-env' },
runtimeEnvironments: environments,
runtimeStatusByEnvironmentId: new Map([
['missing-env', { status: { runtimeId: 'runtime-missing' } }]
['missing-env', { status: makeStatus('runtime-missing') }]
])
})
).toEqual([])
Expand All @@ -67,7 +79,7 @@ describe('getReachableRuntimeSessionMirrorTargets', () => {
settings: { activeRuntimeEnvironmentId: 'offline-env' },
runtimeEnvironments: environments,
runtimeStatusByEnvironmentId: new Map([
['offline-env', { status: { runtimeId: 'runtime-recovered' } }]
['offline-env', { status: makeStatus('runtime-recovered') }]
])
})
).toEqual([
Expand Down
Loading
Loading