diff --git a/packages/data-provider/src/code/approval.spec.ts b/packages/data-provider/src/code/approval.spec.ts new file mode 100644 index 0000000000..476cc807bb --- /dev/null +++ b/packages/data-provider/src/code/approval.spec.ts @@ -0,0 +1,141 @@ +import type { CodeApprovalConstraints } from './approval'; +import { + CODE_APPROVAL_MODES, + CodeApprovalModeError, + getAllowedCodeApprovalModes, + resolveCodeApprovalMode, + resolveCodePermissionDecision, +} from './approval'; + +const constraints: CodeApprovalConstraints = { + environment: 'attached', + allowedModes: CODE_APPROVAL_MODES, + configSchema: { + permissions: { + fileWrite: { allowed: ['ask', 'allow', 'deny'], default: 'ask' }, + commandExecution: { allowed: ['ask', 'allow', 'deny'], default: 'ask' }, + }, + }, +}; + +describe('conversation code approval constraints', () => { + test('defaults to asking without enabling unattended execution', () => { + expect(getAllowedCodeApprovalModes({ environment: 'managed' })).toEqual(['ask']); + expect(getAllowedCodeApprovalModes({ environment: 'attached' })).toEqual(['ask']); + expect(resolveCodeApprovalMode(undefined, constraints)).toBeUndefined(); + }); + + test('requires callers to identify attached environments before applying machine policy', () => { + expect( + getAllowedCodeApprovalModes({ + environment: 'managed', + allowedModes: CODE_APPROVAL_MODES, + }), + ).toEqual(CODE_APPROVAL_MODES); + expect( + getAllowedCodeApprovalModes({ + ...constraints, + configSchema: { + permissions: { + fileWrite: { allowed: ['ask'], default: 'ask' }, + }, + }, + }), + ).toEqual(['ask']); + }); + + test('honors endpoint disablement and an explicit empty allowlist', () => { + expect(getAllowedCodeApprovalModes({ ...constraints, enabled: false })).toEqual([]); + expect(getAllowedCodeApprovalModes({ ...constraints, allowedModes: [] })).toEqual([]); + }); + + test('intersects deployment choices with machine policy in stable UI order', () => { + expect(getAllowedCodeApprovalModes(constraints)).toEqual(CODE_APPROVAL_MODES); + expect( + getAllowedCodeApprovalModes({ + ...constraints, + configSchema: { + permissions: { fileWrite: constraints.configSchema?.permissions?.fileWrite }, + }, + }), + ).toEqual(['ask', 'acceptEdits']); + expect(getAllowedCodeApprovalModes({ ...constraints, configSchema: undefined })).toEqual([ + 'ask', + ]); + expect( + getAllowedCodeApprovalModes({ ...constraints, allowedModes: ['acceptEdits', 'ask'] }), + ).toEqual(['ask', 'acceptEdits']); + }); + + test('never loosens an effective machine deny', () => { + expect( + getAllowedCodeApprovalModes({ + ...constraints, + settings: { permissions: { fileWrite: 'deny' } }, + }), + ).toEqual(['ask']); + expect( + getAllowedCodeApprovalModes({ + ...constraints, + settings: { permissions: { commandExecution: 'deny' } }, + }), + ).toEqual(['ask', 'acceptEdits']); + }); + + test('does not trust settings values excluded by the current schema', () => { + expect( + getAllowedCodeApprovalModes({ + ...constraints, + settings: { permissions: { fileWrite: 'allow', commandExecution: 'allow' } }, + configSchema: { + permissions: { + fileWrite: { allowed: ['ask'], default: 'ask' }, + commandExecution: { allowed: ['ask'], default: 'ask' }, + }, + }, + }), + ).toEqual(['ask']); + }); + + test.each(['autoApprove', 'bypass', 'fullAccess', '', {}, ['ask'], true, 1])( + 'rejects invalid request state: %j', + (value) => { + expect(() => resolveCodeApprovalMode(value, constraints)).toThrow(CodeApprovalModeError); + }, + ); + + test('rejects an unvalidated runtime mode before permission lookup', () => { + expect(() => + resolveCodePermissionDecision({ + mode: 'fullAccess' as never, + category: 'fileWrite', + decision: 'ask', + }), + ).toThrow(CodeApprovalModeError); + }); + + test('revalidates a once-permitted selection after a policy restriction', () => { + expect(resolveCodeApprovalMode('acceptEdits', constraints)).toBe('acceptEdits'); + expect(() => + resolveCodeApprovalMode('acceptEdits', { ...constraints, allowedModes: ['ask'] }), + ).toThrow(CodeApprovalModeError); + }); + + test.each(CODE_APPROVAL_MODES)('preserves deny under %s', (mode) => { + for (const category of ['fileWrite', 'commandExecution'] as const) { + expect(resolveCodePermissionDecision({ mode, category, decision: 'deny' })).toBe('deny'); + } + }); + + test('accept edits asks for commands and does not mutate its input', () => { + const before = JSON.stringify(constraints); + const mode = resolveCodeApprovalMode('acceptEdits', constraints); + expect(resolveCodePermissionDecision({ mode, category: 'fileWrite', decision: 'ask' })).toBe( + 'allow', + ); + expect( + resolveCodePermissionDecision({ mode, category: 'commandExecution', decision: 'allow' }), + ).toBe('ask'); + expect(JSON.stringify(constraints)).toBe(before); + }); +}); diff --git a/packages/data-provider/src/code/approval.ts b/packages/data-provider/src/code/approval.ts new file mode 100644 index 0000000000..e157093494 --- /dev/null +++ b/packages/data-provider/src/code/approval.ts @@ -0,0 +1,86 @@ +import type { + CodeEnvironmentPermissionDecision, + CodeEnvironmentUserConfigSchema, + CodeEnvironmentUserSettings, +} from '../config'; + +export const CODE_APPROVAL_MODES = ['ask', 'acceptEdits'] as const; +export type CodeApprovalMode = (typeof CODE_APPROVAL_MODES)[number]; + +type CodePermissions = Required>; + +const MODE_PERMISSIONS: Record = { + ask: { fileWrite: 'ask', commandExecution: 'ask' }, + acceptEdits: { fileWrite: 'allow', commandExecution: 'ask' }, +}; + +export type CodeApprovalConstraints = { + environment: 'attached' | 'managed'; + enabled?: boolean; + allowedModes?: readonly CodeApprovalMode[]; + configSchema?: CodeEnvironmentUserConfigSchema; + settings?: CodeEnvironmentUserSettings; +}; + +/** Omitted deployment configuration never grants unattended execution. */ +export function getAllowedCodeApprovalModes({ + enabled, + allowedModes, + configSchema, + settings, + environment, +}: CodeApprovalConstraints): CodeApprovalMode[] { + if (enabled === false) return []; + const permitted = new Set(allowedModes ?? ['ask']); + return CODE_APPROVAL_MODES.filter((mode) => { + if (!permitted.has(mode)) return false; + if (environment === 'managed') return true; + for (const category of ['fileWrite', 'commandExecution'] as const) { + if (MODE_PERMISSIONS[mode][category] !== 'allow') continue; + const field = configSchema?.permissions?.[category]; + const configured = settings?.permissions?.[category]; + const effective = + configured != null && field?.allowed.includes(configured) === true + ? configured + : (field?.default ?? 'ask'); + if (effective === 'deny' || field?.allowed.includes('allow') !== true) return false; + } + return true; + }); +} + +export class CodeApprovalModeError extends Error { + readonly code = 'CODE_APPROVAL_MODE_NOT_ALLOWED'; + + constructor() { + super('The selected code approval mode is not permitted by the current policy.'); + this.name = 'CodeApprovalModeError'; + } +} + +/** Validate untrusted request state again at admission, including after policy changes. */ +export function resolveCodeApprovalMode( + requested: unknown, + constraints: CodeApprovalConstraints, +): CodeApprovalMode | undefined { + if (requested == null) return undefined; + const allowed = getAllowedCodeApprovalModes(constraints); + const selected = allowed.find((mode) => mode === requested); + if (selected == null) throw new CodeApprovalModeError(); + return selected; +} + +/** Apply a turn preference without modifying machine settings or overriding an existing deny. */ +export function resolveCodePermissionDecision({ + mode, + category, + decision, +}: { + mode?: CodeApprovalMode; + category: keyof CodePermissions; + decision: CodeEnvironmentPermissionDecision; +}): CodeEnvironmentPermissionDecision { + if (mode == null || decision === 'deny') return decision; + if (MODE_PERMISSIONS[mode] == null) throw new CodeApprovalModeError(); + return MODE_PERMISSIONS[mode][category]; +} diff --git a/packages/data-provider/src/index.ts b/packages/data-provider/src/index.ts index 2da796ae7d..992a329dba 100644 --- a/packages/data-provider/src/index.ts +++ b/packages/data-provider/src/index.ts @@ -72,3 +72,4 @@ export * from './agentToolOptions'; /* code-execution sandbox */ export * from './codeEnvRef'; export * from './code/worker'; +export * from './code/approval';