🗳️ feat: Define Conversation Code Approval Modes (#15685)

* feat: Define Conversation Code Approval Constraints

* style: Sort Code Approval Test Imports

* fix: Enforce Explicit Code Environment Policy
This commit is contained in:
Danny Avila 2026-09-06 22:56:51 -04:00 • committed by GitHub
parent 037cbf530d
commit 644e33ff37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 228 additions and 0 deletions

View file

@ -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);
});
});

View file

@ -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<NonNullable<CodeEnvironmentUserSettings['permissions']>>;
const MODE_PERMISSIONS: Record<CodeApprovalMode, CodePermissions> = {
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];
}

View file

@ -72,3 +72,4 @@ export * from './agentToolOptions';
/* code-execution sandbox */
export * from './codeEnvRef';
export * from './code/worker';
export * from './code/approval';