Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fd89072
feat(worktree): add claudeAccountId to Worktree/WorktreeMeta data model
srs-adamr Jun 30, 2026
c946176
feat(claude-accounts): inject per-worktree pinned account at PTY spawn
srs-adamr Jun 30, 2026
aee4e55
feat(claude-accounts): seed scoped macOS Keychain for injected accounts
srs-adamr Jun 30, 2026
d35001b
feat(worktree): plumb claudeAccountId through worktree creation
srs-adamr Jun 30, 2026
e5c4296
feat(worktree): add account selector to the create-worktree modal
srs-adamr Jun 30, 2026
63eae83
feat(worktree): add Assign Account submenu to the worktree context menu
srs-adamr Jun 30, 2026
30f70c2
fix(claude-accounts): honor per-worktree WSL account binding + warn o…
srs-adamr Jun 30, 2026
23caa44
fix(test): add missing getWorktreeMeta stub to pty.test.ts store mocks
srs-adamr Jun 30, 2026
096cae2
feat(claude-accounts): exempt injected per-worktree launches from the…
srs-adamr Jun 30, 2026
3a60eaa
fix(worktree): prevent Agent combobox overflow when the Account selec…
srs-adamr Jun 30, 2026
d24a873
fix(worktree): align Account field baseline with the Agent field in t…
srs-adamr Jun 30, 2026
21d675d
fix: address CodeRabbit review on per-worktree account binding
srs-adamr Jul 2, 2026
ec0fd8c
fix: correct default-WSL account matching + guard injected Keychain r…
srs-adamr Jul 2, 2026
f0431c3
test: restore scoped-keychain mock impl safely instead of forced non-…
srs-adamr Jul 2, 2026
1f4bca0
test(pty): stub getWorktreeMeta in the SSH identity-mismatch store mock
srs-adamr Jul 7, 2026
a3c99cc
fix: isolate per-worktree Claude accounts safely
Jul 12, 2026
6f9df30
fix: preserve Claude auth mode on reattach
Jul 12, 2026
4508863
fix: retain shared Claude account ownership
Jul 12, 2026
d2eada8
fix: close Claude account ownership races
Jul 12, 2026
66b8b7c
fix: batch Claude account pin updates
Jul 13, 2026
6f4afe4
fix: preserve runtime Claude account assignments
Jul 13, 2026
c08f06b
fix: preserve account pin update compatibility
Jul 13, 2026
4b22097
fix: refresh cleared Claude account pins
Jul 13, 2026
3f4e593
fix: bound legacy worktree metadata updates
Jul 13, 2026
a809046
Merge remote-tracking branch 'origin/main' into review/pr-6963-final
Jul 13, 2026
7e71015
fix: preserve shared Claude reattach ownership
Jul 13, 2026
92bf227
fix: require preserved Claude session reattach
Jul 13, 2026
442540c
fix: verify preserved PTY ownership across providers
Jul 13, 2026
8e03911
fix: require unique preserved PTY owner
Jul 13, 2026
9a9a98b
fix: retain Claude ownership across duplicate exits
Jul 13, 2026
6a97159
fix: coalesce duplicate Claude PTY exits
Jul 13, 2026
5f5cff8
fix: scope Claude exit proof to provider generation
Jul 13, 2026
7dea782
fix: batch Claude exit ownership verification
Jul 13, 2026
478f3bf
fix: exclude outgoing Claude account selection races
Jul 13, 2026
8e7a9f0
Merge remote-tracking branch 'origin/main' into review/pr-6963-final
Jul 13, 2026
94ae918
fix: reconcile delayed Claude provider exits
Jul 13, 2026
adb5904
perf: coalesce Claude exit reconciliation timers
Jul 13, 2026
b058385
fix: retry replacement-provider Claude exits
Jul 13, 2026
7ae59f1
fix: preserve Claude exit retries across windows
Jul 13, 2026
4707863
perf: coalesce Claude inventory across windows
Jul 13, 2026
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
206 changes: 204 additions & 2 deletions src/main/claude-accounts/live-pty-gate.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
attachClaudeLivePtyPersistence,
beginManagedClaudeAccountMutation,
beginClaudeAuthSwitch,
confirmSeededClaudeLivePtys,
endClaudeAuthSwitch,
endManagedClaudeAccountMutation,
getLiveInjectedClaudePtyAccountId,
getLiveSharedClaudePtyAccountId,
hasLiveSharedClaudePtysForAccount,
hasLiveInjectedClaudePtysForAccount,
hasLiveClaudePtys,
isClaudeAuthSwitchInProgress,
markClaudePtyExited,
markClaudePtySpawned,
seedLiveClaudePtysFromPersistence
markInjectedClaudePtySpawned,
releaseInjectedClaudeAccountLaunch,
releaseSharedClaudeAccountLaunch,
reserveInjectedClaudeAccountLaunch,
reserveSharedClaudeAccountLaunch,
seedLiveClaudePtysFromPersistence,
seedLiveInjectedClaudePtysFromPersistence
} from './live-pty-gate'

describe('Claude live PTY gate', () => {
afterEach(() => {
markClaudePtyExited('live-claude-pty')
markClaudePtyExited('seeded-pty-1')
markClaudePtyExited('seeded-pty-2')
markClaudePtyExited('injected-pty')
confirmSeededClaudeLivePtys([])
attachClaudeLivePtyPersistence(null)
endManagedClaudeAccountMutation('account-a')
endClaudeAuthSwitch()
})

Expand Down Expand Up @@ -77,6 +91,27 @@ describe('Claude live PTY gate', () => {
expect(hasLiveClaudePtys()).toBe(true)
})

it('preserves shared launch-account ownership across restart seeding', () => {
seedLiveClaudePtysFromPersistence(
['seeded-pty-1'],
[{ sessionId: 'seeded-pty-1', accountId: 'account-a' }]
)

expect(getLiveSharedClaudePtyAccountId('seeded-pty-1')).toBe('account-a')
expect(hasLiveSharedClaudePtysForAccount('account-a')).toBe(true)
expect(hasLiveSharedClaudePtysForAccount('account-b')).toBe(false)
expect(() => reserveInjectedClaudeAccountLaunch('account-a')).toThrow(
'already in use by a global terminal'
)
})

it('treats legacy shared sessions with unknown accounts conservatively', () => {
seedLiveClaudePtysFromPersistence(['seeded-pty-1'])

expect(hasLiveSharedClaudePtysForAccount('account-a')).toBe(true)
expect(hasLiveSharedClaudePtysForAccount('account-b')).toBe(true)
})

it('persists spawns and exits when persistence is attached', () => {
const addClaudeLivePtySessionId = vi.fn()
const removeClaudeLivePtySessionId = vi.fn()
Expand All @@ -86,9 +121,176 @@ describe('Claude live PTY gate', () => {
})

markClaudePtySpawned('live-claude-pty')
expect(addClaudeLivePtySessionId).toHaveBeenCalledWith('live-claude-pty')
expect(addClaudeLivePtySessionId).toHaveBeenCalledWith('live-claude-pty', null)

markClaudePtyExited('live-claude-pty')
expect(removeClaudeLivePtySessionId).toHaveBeenCalledWith('live-claude-pty')
})

it('tracks injected PTYs by account without closing the shared auth gate', () => {
const addClaudeLivePtyAccountBinding = vi.fn()
const removeClaudeLivePtyAccountBinding = vi.fn()
attachClaudeLivePtyPersistence({
addClaudeLivePtySessionId: vi.fn(),
removeClaudeLivePtySessionId: vi.fn(),
addClaudeLivePtyAccountBinding,
removeClaudeLivePtyAccountBinding
})

markInjectedClaudePtySpawned('injected-pty', 'account-a')

expect(hasLiveClaudePtys()).toBe(false)
expect(hasLiveInjectedClaudePtysForAccount('account-a')).toBe(true)
expect(hasLiveInjectedClaudePtysForAccount('account-b')).toBe(false)
expect(addClaudeLivePtyAccountBinding).toHaveBeenCalledWith('injected-pty', 'account-a')

markClaudePtyExited('injected-pty')
expect(hasLiveInjectedClaudePtysForAccount('account-a')).toBe(false)
expect(removeClaudeLivePtyAccountBinding).toHaveBeenCalledWith('injected-pty')
})

it('keeps injected account ownership across restart reconciliation', () => {
seedLiveInjectedClaudePtysFromPersistence([
{ sessionId: 'injected-pty', accountId: 'account-a' }
])

confirmSeededClaudeLivePtys(['injected-pty'])

expect(hasLiveInjectedClaudePtysForAccount('account-a')).toBe(true)
})

it('reserves account ownership while an injected PTY spawn is pending', () => {
const reservationId = reserveInjectedClaudeAccountLaunch('account-a')
expect(hasLiveInjectedClaudePtysForAccount('account-a')).toBe(true)

releaseInjectedClaudeAccountLaunch(reservationId)
expect(hasLiveInjectedClaudePtysForAccount('account-a')).toBe(false)
})

it('does not expire a reservation while a legitimate launch is still pending', () => {
vi.useFakeTimers()
const reservationId = reserveInjectedClaudeAccountLaunch('account-a')

vi.advanceTimersByTime(120_000)

expect(hasLiveInjectedClaudePtysForAccount('account-a')).toBe(true)
releaseInjectedClaudeAccountLaunch(reservationId)
})

it('atomically consumes a matching reservation into live ownership', () => {
const reservationId = reserveInjectedClaudeAccountLaunch('account-a')

markInjectedClaudePtySpawned('injected-pty', 'account-a', reservationId)

expect(getLiveInjectedClaudePtyAccountId('injected-pty')).toBe('account-a')
markClaudePtyExited('injected-pty')
expect(hasLiveInjectedClaudePtysForAccount('account-a')).toBe(false)
})

it('rolls back live ownership when durable binding persistence fails', () => {
const reservationId = reserveInjectedClaudeAccountLaunch('account-a')
attachClaudeLivePtyPersistence({
addClaudeLivePtySessionId: vi.fn(),
removeClaudeLivePtySessionId: vi.fn(),
addClaudeLivePtyAccountBinding: vi.fn(() => {
throw new Error('disk full')
})
})

expect(() => markInjectedClaudePtySpawned('injected-pty', 'account-a', reservationId)).toThrow(
'disk full'
)
expect(getLiveInjectedClaudePtyAccountId('injected-pty')).toBeNull()
expect(hasLiveInjectedClaudePtysForAccount('account-a')).toBe(false)
})

it('rolls back shared ownership when durable account persistence fails', () => {
const reservationId = reserveSharedClaudeAccountLaunch('account-a')
attachClaudeLivePtyPersistence({
addClaudeLivePtySessionId: vi.fn(() => {
throw new Error('disk full')
}),
removeClaudeLivePtySessionId: vi.fn()
})

expect(() => markClaudePtySpawned('live-claude-pty', 'account-a', reservationId)).toThrow(
'disk full'
)
expect(hasLiveClaudePtys()).toBe(false)
expect(hasLiveSharedClaudePtysForAccount('account-a')).toBe(false)
})

it('never overwrites a surviving PTY binding with a later worktree pin', () => {
markInjectedClaudePtySpawned('injected-pty', 'account-a')

expect(() => markInjectedClaudePtySpawned('injected-pty', 'account-b')).toThrow(
'cannot change its assigned account'
)
expect(getLiveInjectedClaudePtyAccountId('injected-pty')).toBe('account-a')
})

it('excludes new launches for the full managed-account mutation', () => {
beginManagedClaudeAccountMutation('account-a')

expect(() => reserveInjectedClaudeAccountLaunch('account-a')).toThrow('being changed')

endManagedClaudeAccountMutation('account-a')
const reservationId = reserveInjectedClaudeAccountLaunch('account-a')
releaseInjectedClaudeAccountLaunch(reservationId)
})

it('owns a shared account from preparation until the PTY enters the live gate', () => {
const reservationId = reserveSharedClaudeAccountLaunch('account-a')
try {
expect(() => reserveInjectedClaudeAccountLaunch('account-a')).toThrow(
'being launched globally'
)
expect(() => beginManagedClaudeAccountMutation('account-a')).toThrow('in use')
expect(() => beginClaudeAuthSwitch()).toThrow('global Claude terminal is starting')

markClaudePtySpawned('live-claude-pty', 'account-a', reservationId)
beginClaudeAuthSwitch()
expect(isClaudeAuthSwitchInProgress()).toBe(true)
} finally {
releaseSharedClaudeAccountLaunch(reservationId)
}
})

it('prevents shared preparation from racing an injected reservation', () => {
const reservationId = reserveInjectedClaudeAccountLaunch('account-a')
try {
expect(() => reserveSharedClaudeAccountLaunch('account-a')).toThrow('assigned worktree')
} finally {
releaseInjectedClaudeAccountLaunch(reservationId)
}
})

it('protects a system-default shared launch even without a managed account id', () => {
const reservationId = reserveSharedClaudeAccountLaunch(null)
try {
expect(() => beginClaudeAuthSwitch()).toThrow('global Claude terminal is starting')
expect(() => reserveInjectedClaudeAccountLaunch('account-a')).toThrow(
'being launched globally'
)
expect(() => beginManagedClaudeAccountMutation('account-a')).toThrow('in use')
} finally {
releaseSharedClaudeAccountLaunch(reservationId)
}
})

it('prevents unknown shared preparation from racing account-specific ownership', () => {
const reservationId = reserveInjectedClaudeAccountLaunch('account-a')
try {
expect(() => reserveSharedClaudeAccountLaunch(null)).toThrow('assigned worktree')
} finally {
releaseInjectedClaudeAccountLaunch(reservationId)
}

beginManagedClaudeAccountMutation('account-a')
try {
expect(() => reserveSharedClaudeAccountLaunch(null)).toThrow('being changed')
} finally {
endManagedClaudeAccountMutation('account-a')
}
})
})
Loading
Loading