Files
archived-orca/src/main/runtime/client/settings/runtime-client-agent-launch-rollback.test.ts
T
Neil 386afed0ca refactor(persistence): organize profile state and Store domains (#41106)
* refactor(persistence): organize profile state and Store domains

* fix(test): locate relocated profile state worker fixtures
2026-11-11 02:44:06 -08:00

102 lines
4.0 KiB
TypeScript

import { expect, it, vi } from 'vitest'
import {
fixture,
withReopenedProfileStore
} from '../../../persistence/loading-store/profile-writing/profile-state-delayed-authority-fixture'
import { RuntimeClientSettingsController } from './runtime-client-settings'
import {
agentPermissionChoices,
PERMISSION_CHOICE_AGENTS,
resolveAgentPermission
} from '../../../../shared/agent-launch/permissions/tui-agent-permission-choice'
vi.mock('../../../telemetry/client', () => ({ track: vi.fn() }))
vi.mock('../../../telemetry/cohort-classifier', () => ({
getCohortAtEmit: () => ({ nth_repo_added: 2 })
}))
vi.mock('../../../ssh/config/ssh-config-parser', () => ({
loadUserSshConfig: () => ({ hosts: [] }),
sshConfigHostsToTargets: () => []
}))
vi.mock('../../../claude/agent-view/agent-view-service', () => ({
getClaudeAgentViewService: () => ({ reconcile: async () => {} })
}))
vi.mock('../../../agent-hooks/installation/managed-agent-hook-controls', () => ({
applyAgentStatusHooksEnabled: vi.fn(async () => {})
}))
it.each([false, true])(
'durably removes every failed batch addition while preserving newer ownership (%s)',
async (newer) => {
const { store, authority, readState } = await fixture()
const controller = new RuntimeClientSettingsController(store)
store.updateSettings({
agentPermissionDefault: 'ask',
agentPermissionOverrides: {},
agentLaunchChoices: {}
})
await store.flushPendingOrThrowAsync()
const agents = PERMISSION_CHOICE_AGENTS.filter(
(agent) => !agentPermissionChoices(agent).includes('auto')
)
expect(agents.length).toBeGreaterThan(2)
const mutation = { type: 'permission', choice: 'full-access', withoutAuto: true } as const
authority.failNextWrite()
const gate = authority.pause()
const writing = controller.mutateAgentLaunch(mutation)
const refused = expect(writing).rejects.toThrow('profile_state_write_failed')
try {
await gate.started.promise
if (newer) {
// A generic writer owns this agent's entire newer tuple, independently of the batch.
store.updateSettings({
agentDefaultArgs: { ...store.getSettings().agentDefaultArgs, grok: '--debug' },
agentLaunchChoices: { grok: { model: 'opaque-current' } },
agentDefaultEnv: { ...store.getSettings().agentDefaultEnv, grok: { KEEP: 'newer' } },
agentPermissionOverrides: {
...store.getSettings().agentPermissionOverrides,
codex: 'own'
}
})
await controller.update({ machineName: 'newer-host' })
}
} finally {
gate.finish.resolve()
}
await refused
const check = (settings: ReturnType<typeof store.getSettings>) => {
for (const agent of agents) {
if (newer && agent === 'grok') {
expect(resolveAgentPermission(agent, settings).choice).toBe('full-access')
} else {
expect(settings.agentPermissionOverrides).not.toHaveProperty(agent)
expect(resolveAgentPermission(agent, settings).choice).toBe('ask')
}
}
if (newer) {
expect(settings).toMatchObject({
machineName: 'newer-host',
agentDefaultArgs: { grok: '--debug' },
agentLaunchChoices: { grok: { model: 'opaque-current' } },
agentDefaultEnv: { grok: { KEEP: 'newer' } },
agentPermissionOverrides: { codex: 'own' }
})
} else {
expect(settings.agentPermissionOverrides).toEqual({})
}
}
check(store.getSettings())
await store.flushPendingOrThrowAsync()
await withReopenedProfileStore(readState(), 'batch-rollback-reopen.json', (reopened) =>
check(reopened.getSettings())
)
// Reopen is deliberately before the legitimate retry can conceal a stale failed value.
await controller.mutateAgentLaunch(mutation)
await withReopenedProfileStore(readState(), 'batch-retry-reopen.json', (reopened) => {
for (const agent of agents) {
expect(resolveAgentPermission(agent, reopened.getSettings()).choice).toBe('full-access')
}
})
}
)