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
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import type { SleepingAgentSessionRecord } from '../../../shared/agent-session-resume'
import { makePaneKey } from '../../../shared/stable-pane-id'
import { useAppStore } from '@/store'
import { makeWorktree, TEST_REPO } from '../store/slices/worktrees-slice-test-fixtures'
import { resumeSleepingAgentSessionsForWorktree } from './resume-sleeping-agent-session'

const initialAppStoreState = useAppStore.getState()
const LEAF_ID = '11111111-1111-4111-8111-111111111111'
const OTHER_LEAF_ID = '22222222-2222-4222-8222-222222222222'
const SSH_PTY_ID = 'ssh:ssh-target-1@@pty-3'

afterEach(() => {
vi.unstubAllGlobals()
useAppStore.setState(initialAppStoreState, true)
})

function setStoreState(partial: Record<string, unknown>): void {
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: fixtures set only the slices the wake sweep reads; the rest stay at the real initial state.
useAppStore.setState(partial as Partial<ReturnType<typeof useAppStore.getState>>)
}

function makeRecord(
overrides: Partial<SleepingAgentSessionRecord> = {}
): SleepingAgentSessionRecord {
return {
paneKey: makePaneKey('tab-1', LEAF_ID),
tabId: 'tab-1',
worktreeId: 'wt-1',
agent: 'claude',
providerSession: { key: 'session_id', id: 'sess-1' },
prompt: 'finish the task',
state: 'working',
origin: 'quit',
capturedAt: 1,
updatedAt: 1,
...overrides
}
}

function makeTerminalTab(id: string, worktreeId: string): Record<string, unknown> {
return {
id,
ptyId: null,
worktreeId,
title: 'shell',
customTitle: null,
color: null,
sortOrder: 0,
createdAt: 1
}
}

function makeLayout(leafId: string, ptyId: string | null): Record<string, unknown> {
return {
root: { type: 'leaf', leafId },
activeLeafId: leafId,
expandedLeafId: null,
ptyIdsByLeafId: ptyId ? { [leafId]: ptyId } : {}
}
}

describe('resumeSleepingAgentSessionsForWorktree — SSH panes and stale worktree ids', () => {
it('treats a leaf bound to an ssh pty as pane-owned while its target is disconnected', () => {
const record = makeRecord()
setStoreState({
activeWorktreeId: 'wt-other',
tabsByWorktree: { 'wt-1': [makeTerminalTab('tab-1', 'wt-1')] },
terminalLayoutsByTabId: { 'tab-1': makeLayout(LEAF_ID, SSH_PTY_ID) },
ptyIdsByTabId: {},
sshConnectionStates: new Map([['ssh-target-1', { status: 'disconnected' }]]),
sleepingAgentSessionsByPaneKey: { [record.paneKey]: record }
})

const launched = resumeSleepingAgentSessionsForWorktree('wt-1')

expect(launched).toBe(0)
expect(useAppStore.getState().sleepingAgentSessionsByPaneKey[record.paneKey]).toBe(record)
})

it('resumes a leaf whose ssh pty is gone while its target is connected', () => {
const record = makeRecord({ origin: 'worktree-sleep' })
setStoreState({
activeWorktreeId: 'wt-other',
tabsByWorktree: { 'wt-1': [makeTerminalTab('tab-1', 'wt-1')] },
terminalLayoutsByTabId: { 'tab-1': makeLayout(LEAF_ID, SSH_PTY_ID) },
ptyIdsByTabId: {},
sshConnectionStates: new Map([['ssh-target-1', { status: 'connected' }]]),
sleepingAgentSessionsByPaneKey: { [record.paneKey]: record }
})

const launched = resumeSleepingAgentSessionsForWorktree('wt-1')

expect(launched).toBe(1)
})

it("does not credit a split leaf with another leaf's ssh pty", () => {
const record = makeRecord()
setStoreState({
activeWorktreeId: 'wt-other',
tabsByWorktree: { 'wt-1': [{ ...makeTerminalTab('tab-1', 'wt-1'), ptyId: SSH_PTY_ID }] },
terminalLayoutsByTabId: {
'tab-1': {
root: {
type: 'split',
direction: 'vertical',
first: { type: 'leaf', leafId: OTHER_LEAF_ID },
second: { type: 'leaf', leafId: LEAF_ID }
},
activeLeafId: OTHER_LEAF_ID,
expandedLeafId: null,
ptyIdsByLeafId: {}
}
},
ptyIdsByTabId: {},
sshConnectionStates: new Map([['ssh-target-1', { status: 'disconnected' }]]),
sleepingAgentSessionsByPaneKey: { [record.paneKey]: record }
})

const launched = resumeSleepingAgentSessionsForWorktree('wt-1')

expect(launched).toBe(1)
})

it('still resumes when the preserved pane has no pty binding and cannot own recovery', () => {
const record = makeRecord()
setStoreState({
activeWorktreeId: 'wt-other',
tabsByWorktree: { 'wt-1': [makeTerminalTab('tab-1', 'wt-1')] },
terminalLayoutsByTabId: { 'tab-1': makeLayout(LEAF_ID, null) },
ptyIdsByTabId: {},
sleepingAgentSessionsByPaneKey: { [record.paneKey]: record }
})

const launched = resumeSleepingAgentSessionsForWorktree('wt-1')

expect(launched).toBe(1)
})

it('finds the preserved tab when the record worktreeId is stale', () => {
const record = makeRecord()
setStoreState({
activeWorktreeId: 'wt-other',
tabsByWorktree: { 'wt-1': [], 'wt-2': [makeTerminalTab('tab-1', 'wt-2')] },
terminalLayoutsByTabId: { 'tab-1': makeLayout(LEAF_ID, SSH_PTY_ID) },
ptyIdsByTabId: {},
sleepingAgentSessionsByPaneKey: { [record.paneKey]: record }
})

const launched = resumeSleepingAgentSessionsForWorktree('wt-1')

expect(launched).toBe(0)
expect(useAppStore.getState().sleepingAgentSessionsByPaneKey[record.paneKey]).toBe(record)
})

it('clears a record whose provider session is owned by a record in another worktree', () => {
const stale = makeRecord({ providerSession: { key: 'session_id', id: 'sess-9' } })
const owned = makeRecord({
paneKey: makePaneKey('tab-2', OTHER_LEAF_ID),
tabId: 'tab-2',
worktreeId: 'wt-2',
providerSession: { key: 'session_id', id: 'sess-9' }
})
setStoreState({
activeWorktreeId: 'wt-other',
tabsByWorktree: { 'wt-1': [], 'wt-2': [makeTerminalTab('tab-2', 'wt-2')] },
terminalLayoutsByTabId: { 'tab-2': makeLayout(OTHER_LEAF_ID, SSH_PTY_ID) },
ptyIdsByTabId: {},
sleepingAgentSessionsByPaneKey: { [stale.paneKey]: stale, [owned.paneKey]: owned }
})

const launched = resumeSleepingAgentSessionsForWorktree('wt-1')

expect(launched).toBe(0)
const records = useAppStore.getState().sleepingAgentSessionsByPaneKey
expect(records[stale.paneKey]).toBeUndefined()
expect(records[owned.paneKey]).toBe(owned)
})

// The other record's host moved, so it can't resume the session; letting it claim the session
// would delete the only record that can, and nothing would resume.
it('ignores a claim from a record whose worktree now routes to another host', () => {
const remotePath = '/home/neil/projects/wt-2'
const remoteWorktreeId = `repo-2::${remotePath}`
const local = makeRecord({ providerSession: { key: 'session_id', id: 'sess-9' } })
const moved = makeRecord({
paneKey: makePaneKey('tab-2', OTHER_LEAF_ID),
tabId: 'tab-2',
worktreeId: remoteWorktreeId,
connectionId: null,
providerSession: { key: 'session_id', id: 'sess-9' }
})
setStoreState({
activeWorktreeId: 'wt-other',
repos: [
{ ...TEST_REPO, id: 'repo-2', path: '/home/neil/projects', connectionId: 'ssh-target-1' }
],
worktreesByRepo: {
'repo-2': [
makeWorktree({
id: remoteWorktreeId,
repoId: 'repo-2',
path: remotePath,
hostId: 'ssh:ssh-target-1'
})
]
},
tabsByWorktree: {
'wt-1': [],
[remoteWorktreeId]: [makeTerminalTab('tab-2', remoteWorktreeId)]
},
terminalLayoutsByTabId: { 'tab-2': makeLayout(OTHER_LEAF_ID, SSH_PTY_ID) },
ptyIdsByTabId: {},
sleepingAgentSessionsByPaneKey: { [local.paneKey]: local, [moved.paneKey]: moved }
})

expect(resumeSleepingAgentSessionsForWorktree('wt-1')).toBe(1)
})

it('ignores a claim from a folder workspace whose host moved since capture', () => {
const folderWorkspaceId = 'folder:fw-1'
const local = makeRecord({ providerSession: { key: 'session_id', id: 'sess-9' } })
const moved = makeRecord({
paneKey: makePaneKey('tab-2', OTHER_LEAF_ID),
tabId: 'tab-2',
worktreeId: folderWorkspaceId,
connectionId: null,
providerSession: { key: 'session_id', id: 'sess-9' }
})
setStoreState({
activeWorktreeId: 'wt-other',
folderWorkspaces: [{ id: 'fw-1', connectionId: 'ssh-target-1', diffComments: [] }],
tabsByWorktree: {
'wt-1': [],
[folderWorkspaceId]: [makeTerminalTab('tab-2', folderWorkspaceId)]
},
terminalLayoutsByTabId: { 'tab-2': makeLayout(OTHER_LEAF_ID, SSH_PTY_ID) },
ptyIdsByTabId: {},
sleepingAgentSessionsByPaneKey: { [local.paneKey]: local, [moved.paneKey]: moved }
})

expect(resumeSleepingAgentSessionsForWorktree('wt-1')).toBe(1)
})

// Matching ids on two machines (a copied state dir) are two sessions, not one. Here only the
// catalog names this record's machine and only the capture stamp names the other's.
it('ignores a claim from a pane on a different machine than this record', () => {
const localPath = '/Users/neil/projects/wt-1'
const localWorktreeId = `repo-1::${localPath}`
const local = makeRecord({
paneKey: makePaneKey('tab-1', LEAF_ID),
worktreeId: localWorktreeId,
providerSession: { key: 'session_id', id: 'sess-9' }
})
const remote = makeRecord({
paneKey: makePaneKey('tab-2', OTHER_LEAF_ID),
tabId: 'tab-2',
worktreeId: 'wt-2',
connectionId: 'ssh-target-1',
providerSession: { key: 'session_id', id: 'sess-9' }
})
setStoreState({
activeWorktreeId: 'wt-other',
tabsByWorktree: { [localWorktreeId]: [], 'wt-2': [makeTerminalTab('tab-2', 'wt-2')] },
terminalLayoutsByTabId: { 'tab-2': makeLayout(OTHER_LEAF_ID, SSH_PTY_ID) },
ptyIdsByTabId: {},
repos: [{ ...TEST_REPO, id: 'repo-1', path: '/Users/neil/projects' }],
worktreesByRepo: {
'repo-1': [
makeWorktree({ id: localWorktreeId, repoId: 'repo-1', path: localPath, hostId: 'local' })
]
},
sleepingAgentSessionsByPaneKey: { [local.paneKey]: local, [remote.paneKey]: remote }
})

expect(resumeSleepingAgentSessionsForWorktree(localWorktreeId)).toBe(1)
})

it('does not let a finished note in another worktree clear an active record', () => {
const active = makeRecord({ providerSession: { key: 'session_id', id: 'sess-9' } })
const finished = makeRecord({
paneKey: makePaneKey('tab-2', OTHER_LEAF_ID),
tabId: 'tab-2',
worktreeId: 'wt-2',
origin: 'live',
state: 'done',
providerSession: { key: 'session_id', id: 'sess-9' }
})
setStoreState({
activeWorktreeId: 'wt-other',
tabsByWorktree: { 'wt-1': [], 'wt-2': [makeTerminalTab('tab-2', 'wt-2')] },
terminalLayoutsByTabId: { 'tab-2': makeLayout(OTHER_LEAF_ID, null) },
ptyIdsByTabId: {},
sleepingAgentSessionsByPaneKey: { [active.paneKey]: active, [finished.paneKey]: finished }
})

const launched = resumeSleepingAgentSessionsForWorktree('wt-1')

expect(launched).toBe(1)
})
})
30 changes: 29 additions & 1 deletion src/renderer/src/lib/resume-sleeping-agent-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
type UnhydratedHostMirror
} from './host-mirrored-pane-liveness'
import { parkUntilHostMirrorHandleLands } from './host-mirror-handle-gap-wait'
import { sleepingRecordNamesAnotherExecutionHost } from './sleeping-record-execution-host-scope'
import {
sleepingRecordHostAllowsSessionClaim,
sleepingRecordNamesAnotherExecutionHost
} from './sleeping-record-execution-host-scope'
import { resolveWorkspaceTerminalHostAuthority } from './workspace-terminal-host-authority'
import { parkUntilHostSessionMirrorHydrates } from '@/runtime/host-session-mirror-hydration'

Expand Down Expand Up @@ -175,6 +178,24 @@ function activeOrQueuedResumeClaimsProviderSession(
return false
}

function providerSessionOwnedElsewhere(
record: SleepingAgentSessionRecord,
state: ReturnType<typeof useAppStore.getState>
): boolean {
// Same-worktree duplicates are handled by claim keys; finished notes never claim a session, and
// neither does a record whose host moved or that names a different machine than this record.
return Object.values(state.sleepingAgentSessionsByPaneKey).some(
(other) =>
other.worktreeId !== record.worktreeId &&
other.agent === record.agent &&
!isInvalidWorktreeActivationRecord(other) &&
!activationTreatsNoteAsFinished(other) &&
sleepingRecordHostAllowsSessionClaim(other, record, state) &&
agentProviderSessionsEqual(record.agent, other.providerSession, record.providerSession) &&
recordPaneIsOwnedByPreservedPane(other, state)
)
}

// Why: an interrupted turn is still resumable — `claude --resume` reopens the transcript at the
// prompt — so discarding those records only stranded the session across wake and restart.
function isInvalidWorktreeActivationRecord(record: SleepingAgentSessionRecord): boolean {
Expand Down Expand Up @@ -323,6 +344,13 @@ export function resumeSleepingAgentSessionsForWorktree(
if (isHostMirroredSleepingRecord(record)) {
continue
}
// Why: claim keys are worktree-scoped, but a provider session is global —
// a record whose stored worktreeId went stale must not relaunch a session
// that a pane tracked by another record still owns.
if (providerSessionOwnedElsewhere(record, currentState)) {
state.clearSleepingAgentSession(record.paneKey)
continue
}
if (launchSleepingAgentSession(record, options)) {
launched += 1
freshlyLaunchedClaimKeys.add(claimKey)
Expand Down
Loading
Loading