fix(permissions): harden auto disposal migration

This commit is contained in:
pku-xht committed 2026-09-04 12:21:56 +08:00
1 parent dae5844f93
commit 11c4fa1b0a
3 files changed
+96 -12

No files matched your search

@@ -1462,9 +1462,7 @@ describe('cancellation and integration teardown', () => {
const disposal = auto.dispose()
await until(() => adapter.requests[0]?.signal?.aborted === true)
expect(session.snapshotEvents().slice(-2).map(event => event.type)).toEqual([
'permission/preset', 'sandbox/mode',
])
expect(session.snapshotEvents().at(-1)?.type).toBe('sandbox/mode')
expect(ctx.permissionPresets.current(session)).not.toBe(AUTO_PRESET)
expect(() => { ctx.permissionPresets.set(session, AUTO_PRESET) }).toThrow(/integration is closing/)
const secondId = ToolCallId('second-dispose-call')
@@ -402,20 +402,27 @@ export class PermissionPresetService extends TypertRemoteService {
this.apply(session, name, (policy) => { setApprovalPolicy(session, policy) })
}
/** Apply one preset with the caller-selected live or initialization policy writer. */
/** Apply one preset; Auto teardown confines first so each durable prefix is safe. */
private apply(session: Session, name: string, setApproval: (policy: ApprovalPolicy) => void): void {
const spec = this.resolve(name)
if (name === AUTO_PRESET) this.autoAdmit?.()
if (this.current(session) !== name) {
session.append('permission/preset', { preset: name })
}
const current = this.current(session)
const knobs = this.permissionState(session)
if (spec.sandbox !== (knobs.sandbox ?? this.ctx.shell.sandboxMode)) {
setSandboxMode(session, spec.sandbox)
const updateKnobs = (): void => {
if (spec.sandbox !== (knobs.sandbox ?? this.ctx.shell.sandboxMode)) {
setSandboxMode(session, spec.sandbox)
}
if (spec.approval !== (knobs.approval ?? this.ctx.approval.config.policy ?? 'ask')) {
setApproval(spec.approval)
}
}
if (spec.approval !== (knobs.approval ?? this.ctx.approval.config.policy ?? 'ask')) {
setApproval(spec.approval)
if (current === AUTO_PRESET && name === 'read-only') {
updateKnobs()
session.append('permission/preset', { preset: name })
return
}
if (current !== name) session.append('permission/preset', { preset: name })
updateKnobs()
}
/**
@@ -1,6 +1,14 @@
import { describe, expect, it, vi } from 'vitest'
import { Context } from '@deepseek-ai/cordis'
import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session'
import SessionStore, {
Session,
SessionId,
type SessionEvent,
type SessionEventMap,
type SessionEventType,
type SurfaceEventType,
type SurfaceIntent,
} from '@deepseek-ai/dsh-session'
import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
import type { SandboxMode } from '@deepseek-ai/dsh-sandbox'
import type { ApprovalPolicy } from '@deepseek-ai/dsh-user-approval'
@@ -181,6 +189,77 @@ describe('PermissionPresetService', () => {
expect(session.snapshotEvents()).toEqual([])
})
it('makes every failed Auto-to-Read-Only prefix restart safely while other switches stay preset-first', async () => {
const config = { presets: {
'read-only': { sandbox: 'read-only', approval: 'ask' },
'workspace-write': { sandbox: 'workspace-write', approval: 'ask' },
'danger-full-access': { sandbox: 'danger-full-access', approval: 'never' },
} } satisfies Config
const prefixes = [
[['sandbox/mode', { mode: 'read-only' }]],
[
['sandbox/mode', { mode: 'read-only' }],
['approval/policy', { policy: 'ask' }],
],
[
['sandbox/mode', { mode: 'read-only' }],
['approval/policy', { policy: 'ask' }],
['permission/preset', { preset: 'read-only' }],
],
]
for (const [index, expectedPrefix] of prefixes.entries()) {
const ctx = await mounted({ config })
await mountAuto(ctx)
const source = freshSession(`auto-read-only-prefix-${index + 1}`)
ctx.permissionPresets.set(source, AUTO_PRESET)
const baselineLength = source.snapshotEvents().length
const append = source.append.bind(source)
let writes = 0
function appendThenFail<T extends SessionEventType>(
type: T,
data: SessionEventMap[T],
...opts: T extends SurfaceEventType ? [opts: SurfaceIntent<T>] : []
): SessionEvent<T> {
const event = append(type, data, ...opts)
writes += 1
if (writes === index + 1) throw new Error(`failed after migration write ${writes}`)
return event
}
vi.spyOn(source, 'append').mockImplementation(appendThenFail)
expect(() => ctx.permissionPresets.set(source, 'read-only'))
.toThrow(`failed after migration write ${index + 1}`)
expect(source.snapshotEvents().slice(baselineLength).map(event => [event.type, event.data]))
.toEqual(expectedPrefix)
const seed = source.snapshotEvents()
const restarted = await mounted({ config })
if (index < 2) {
expect(() => restarted.sessions.create(
SessionId(`auto-read-only-prefix-${index + 1}-without-auto`),
{ seed },
)).toThrow(/cannot restore preset "auto"/)
await mountAuto(restarted)
}
const resumed = restarted.sessions.create(
SessionId(`auto-read-only-prefix-${index + 1}-resumed`),
{ seed },
)
expect(restarted.sessionProjections.stateOf(resumed, 'permissions')).toMatchObject({
sandbox: 'read-only',
})
expect(restarted.permissionPresets.current(resumed)).not.toBe('danger-full-access')
}
const ordinary = await mounted({ config })
const session = freshSession('ordinary-preset-first')
ordinary.permissionPresets.set(session, 'danger-full-access')
expect(session.snapshotEvents().map(event => event.type)).toEqual([
'permission/preset', 'sandbox/mode', 'approval/policy',
])
})
it('current() derives from the effective knobs: composition defaults hit workspace-write, a switch hits its preset', async () => {
const ctx = await mounted()
const session = freshSession('sess-current')