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
6 changes: 5 additions & 1 deletion src/renderer/src/app-shell/use-app-session-persistence.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useEffect } from 'react'
import { translate } from '@/i18n/i18n'
import { useAppStore } from '../store'
import { isDirectSshRemoteWorkspaceApplyInProgress } from '../hooks/remote-workspace-snapshot-apply'
import {
isDirectSshRemoteWorkspaceApplyInProgress,
onDirectSshRemoteWorkspaceApplyWindowClosed
} from '../hooks/remote-workspace-snapshot-apply'
import { createSessionWriteSubscriber } from '../lib/session-write-subscriber'
import { buildActiveViewUnloadPatch } from '../lib/active-view-persist'
import {
Expand Down Expand Up @@ -84,6 +87,7 @@ export function useAppSessionPersistence(): void {
return createSessionWriteSubscriber({
store: useAppStore,
shouldSchedulePersist: () => !isDirectSshRemoteWorkspaceApplyInProgress(),
subscribeToPersistGateOpen: onDirectSshRemoteWorkspaceApplyWindowClosed,
persist: ({ patch }) => {
const state = useAppStore.getState()
// Why: route each host's worktree-scoped slice to its own partition; return the local write so the remote-workspace upload chain below keeps its ordering.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
/**
* Drives the real apply path with the real session writer wired exactly as the app wires it.
*
* The write gate closes for the whole apply plus a 1s suppression tail that expires on a wall
* clock — no store update follows it. So a tab the user closes on an unrelated target during
* target A's reconnect has to be held and written when the tail ends, or the close and its
* tombstone are lost until the next unrelated mutation (a crash or kill in between loses both).
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type * as AgentStatusModule from '@/lib/agent-status'
import type { RemoteWorkspaceSnapshot } from '../../../shared/remote-workspace-types'
import type { DirectSshAuthority, SshProviderEpoch } from '../../../shared/ssh-types'
import { createTestStore, makeWorktree } from '../store/slices/store-test-helpers'
import {
createSessionWriteSubscriber,
type WorkspaceSessionWrite
} from '../lib/session-write-subscriber'
import {
applyDirectSshRemoteWorkspaceSnapshot,
isDirectSshRemoteWorkspaceApplyInProgress,
onDirectSshRemoteWorkspaceApplyWindowClosed
} from './remote-workspace-snapshot-apply'
import type { DirectSshSnapshotApplyToken } from './direct-ssh-reconnect-coordinator-types'

vi.mock('sonner', () => ({
toast: { info: vi.fn(), success: vi.fn(), error: vi.fn() }
}))
vi.mock('@/lib/agent-status', async (importOriginal) => {
const actual = await importOriginal<typeof AgentStatusModule>()
return {
...actual,
detectAgentStatusFromTitle: vi.fn().mockReturnValue(null)
}
})

const TARGET_A = 'ssh-target-a'
const PATH_A = '/srv/proj/target-a'
const WORKTREE_A = `repoA::${PATH_A}`
const WORKTREE_B = 'repoB::/srv/other/target-b'

const authority: DirectSshAuthority = {
targetId: TARGET_A,
providerEpoch: 'provider-epoch-1' as SshProviderEpoch,
connectionGeneration: 1
}

type TestStore = ReturnType<typeof createTestStore>

function token(snapshotRevision: number): DirectSshSnapshotApplyToken {
return {
authority,
catalogRevision: 0,
repoFingerprint: 'fp',
authorityRequirement: 'required',
snapshotRevision,
outcome: 'complete'
}
}

function snapshot(revision: number): RemoteWorkspaceSnapshot {
return {
namespace: 'workspace',
revision,
updatedAt: revision,
schemaVersion: 1,
session: {
activeWorktreePath: PATH_A,
activeTabId: 'tab-a',
tabsByWorktreePath: {
[PATH_A]: [
{
id: 'tab-a',
worktreePath: PATH_A,
ptyId: 'pty-tab-a',
title: 'Terminal 1',
customTitle: null,
color: null,
sortOrder: 0,
createdAt: 1
}
]
},
terminalLayoutsByTabId: {},
activeWorktreePathsOnShutdown: [],
activeTabIdByWorktreePath: { [PATH_A]: 'tab-a' },
remoteSessionIdsByTabId: { 'tab-a': 'pty-tab-a' },
lastVisitedAtByWorktreePath: { [PATH_A]: revision },
defaultTerminalTabsAppliedByWorktreePath: { [PATH_A]: true }
}
} satisfies RemoteWorkspaceSnapshot
}

function seedCatalog(store: TestStore): void {
store.setState({
worktreesByRepo: {
repoA: [
makeWorktree({
id: WORKTREE_A,
repoId: 'repoA',
path: PATH_A,
hostId: `ssh:${TARGET_A}`
} as never)
]
},
repos: [
{
id: 'repoA',
path: '/srv/proj',
displayName: 'Proj',
badgeColor: '#000',
addedAt: 0,
connectionId: TARGET_A
} as never
],
reconnectPersistedTerminals: (async () => {}) as never,
markRemoteWorkspaceHydrated: (() => {}) as never,
setRemoteWorkspaceSyncStatus: (() => {}) as never
})
}

/** A second target's tab the user closes while target A is mid-apply. */
function closeUnrelatedTargetTab(store: TestStore): void {
store.setState({
tabsByWorktree: { ...store.getState().tabsByWorktree, [WORKTREE_B]: [] },
closedTerminalTabTombstonesByTabId: {
'tab-b': { closedAt: Date.now(), worktreeId: WORKTREE_B }
}
})
}

/** Everything here is timer-driven, so fake timers make the tail deterministic and instant. */
describe('session writes deferred by a direct-SSH apply', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

async function applyTargetA(store: TestStore, onApplied: () => void): Promise<void> {
await applyDirectSshRemoteWorkspaceSnapshot({
store,
snapshot: snapshot(1),
token: token(1),
arrival: 1,
isArrivalCurrent: () => true,
isPreparationTokenCurrent: () => true,
waitForWorkspaceSessionReady: async () => true,
// Runs inside the apply, which is when the write gate is shut.
finalizeHydratedTerminals: () => {
onApplied()
return 0
}
})
}

function seedBothTargets(store: TestStore): void {
seedCatalog(store)
store.setState({
tabsByWorktree: {
[WORKTREE_B]: [
{
id: 'tab-b',
ptyId: 'pty-tab-b',
worktreeId: WORKTREE_B,
title: 'Other host',
customTitle: null,
color: null,
sortOrder: 0,
createdAt: 1
} as never
]
}
})
}

it('lands an unrelated target\u2019s tab close once the suppression tail expires', async () => {
const store = createTestStore()
seedBothTargets(store)

const persist = vi.fn<(payload: WorkspaceSessionWrite) => void>()
const cleanup = createSessionWriteSubscriber({
store,
persist,
shouldSchedulePersist: () => !isDirectSshRemoteWorkspaceApplyInProgress(),
subscribeToPersistGateOpen: onDirectSshRemoteWorkspaceApplyWindowClosed
})
try {
store.setState({ workspaceSessionReady: true, hydrationSucceeded: true })
vi.advanceTimersByTime(200)
expect(persist).toHaveBeenCalled()
persist.mockClear()

await applyTargetA(store, () => closeUnrelatedTargetTab(store))

expect(
isDirectSshRemoteWorkspaceApplyInProgress(),
'the suppression tail should still be holding writes here'
).toBe(true)
expect(persist).not.toHaveBeenCalled()

// Tail (1000ms) + the notice's 1ms margin + the writer's 150ms debounce.
vi.advanceTimersByTime(1_200)

expect(persist, 'the close made during the apply never reached disk').toHaveBeenCalledTimes(1)
const patch = persist.mock.calls[0][0].patch
expect(patch.closedTerminalTabTombstonesByTabId?.['tab-b']?.worktreeId).toBe(WORKTREE_B)
expect(patch.tabsByWorktree?.[WORKTREE_B]).toEqual([])
expect(patch.tabsByWorktree?.[WORKTREE_A]).toHaveLength(1)
} finally {
cleanup()
}
})

it('still wakes the deferred write when the wall clock steps back during the tail', async () => {
const store = createTestStore()
seedBothTargets(store)
const startedAt = Date.now()

const persist = vi.fn<(payload: WorkspaceSessionWrite) => void>()
const cleanup = createSessionWriteSubscriber({
store,
persist,
shouldSchedulePersist: () => !isDirectSshRemoteWorkspaceApplyInProgress(),
subscribeToPersistGateOpen: onDirectSshRemoteWorkspaceApplyWindowClosed
})
try {
store.setState({ workspaceSessionReady: true, hydrationSucceeded: true })
vi.advanceTimersByTime(200)
persist.mockClear()

await applyTargetA(store, () => closeUnrelatedTargetTab(store))

// The notice's delay is wall-clock arithmetic on a monotonic timer, so an NTP step back
// leaves the deadline in the future when it fires. It must wait, not give up.
vi.setSystemTime(startedAt - 5_000)
vi.advanceTimersByTime(1_200)
expect(persist, 'the notice fired before its own deadline').not.toHaveBeenCalled()

vi.advanceTimersByTime(5_200)

expect(persist, 'a clock step back stranded the deferred write').toHaveBeenCalledTimes(1)
expect(
persist.mock.calls[0][0].patch.closedTerminalTabTombstonesByTabId?.['tab-b']
).toBeDefined()
} finally {
cleanup()
}
})
})
40 changes: 40 additions & 0 deletions src/renderer/src/hooks/remote-workspace-snapshot-apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,45 @@ export function isDirectSshRemoteWorkspaceApplyInProgress(): boolean {
return snapshotApplyDepth > 0 || Date.now() < snapshotWriteSuppressUntil
}

const applyWindowCloseListeners = new Set<() => void>()

/**
* Fires once after the write-suppression tail expires. Why: the tail ends on a wall clock with no
* store update behind it, so a session write deferred during an apply has nothing else to wake it.
*/
export function onDirectSshRemoteWorkspaceApplyWindowClosed(listener: () => void): () => void {
applyWindowCloseListeners.add(listener)
return () => {
applyWindowCloseListeners.delete(listener)
}
}

/**
* One timer in flight per apply. It never polls: the tail deadline only moves forward, and each
* re-arm waits exactly the time still left on it, so the wait converges instead of spinning.
*/
function scheduleApplyWindowClosedNotice(): void {
// +1ms because the gate compares `Date.now() < suppressUntil`; fire strictly past the deadline.
const delayMs = Math.max(0, snapshotWriteSuppressUntil - Date.now()) + 1
setTimeout(() => {
// An apply still in flight schedules the next notice from its own `finally`.
if (snapshotApplyDepth > 0) {
return
}
// Why re-arm rather than return: `delayMs` is wall-clock arithmetic handed to a monotonic
// timer, so a clock step back (NTP) can leave the deadline in the future when this fires.
// Dropping the notice there would strand every write deferred in this window.
if (Date.now() < snapshotWriteSuppressUntil) {
scheduleApplyWindowClosedNotice()
return
}
// Safe to iterate live: Set iteration tolerates a listener unsubscribing itself.
for (const listener of applyWindowCloseListeners) {
listener()
}
}, delayMs)
}

type RemoteWorkspaceSnapshotApplyInput = {
store: Pick<StoreApi<AppState>, 'getState'>
snapshot: RemoteWorkspaceSnapshot
Expand Down Expand Up @@ -195,5 +234,6 @@ export async function applyDirectSshRemoteWorkspaceSnapshot({
} finally {
snapshotWriteSuppressUntil = Date.now() + REMOTE_WORKSPACE_SNAPSHOT_WRITE_SUPPRESS_MS
snapshotApplyDepth -= 1
scheduleApplyWindowClosedNotice()
}
}
Loading