Skip to content

Commit 28a3153

Browse files
rwollCopilot
andcommitted
fix: scope Assisted approvals to permission callbacks
Remove tool-call-level approval caching so a managed approval cannot authorize a later unmanaged permission request. Cover delayed events and missing callback verdicts, and preserve sandbox and managed one-shot guards. Consolidate the duplicate AgentService import inherited from main that was blocking the PR ESLint check. 🤖 Authored with GitHub Copilot on behalf of @rwoll. Co-authored-by: Copilot <[email protected]>
1 parent b39b916 commit 28a3153

4 files changed

Lines changed: 95 additions & 72 deletions

File tree

‎.github/skills/policy-and-managed-settings/sdk-runtime-policy.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Do not add a VS Code `policy:` merely to mirror runtime policy. Do not add a Git
2828
- New VS Code/AHP code transports managed rules opaquely and does not parse or match them.
2929
- Raw `managedApprovalRequired` preserves managed provenance and forbids reusable grants and unrelated host auto-approval shortcuts.
3030
- In Assisted mode, only an approving judge verdict on a derived `commands`, `read`, `write`, `url`, or `workflow` prompt with the human requirement cleared may approve a managed ask once. Missing or unsupported derived prompts remain human-required.
31+
- Assisted approvals use only the current permission callback's derived verdict, never an event-cached verdict keyed by tool call. A tool call can encounter multiple distinct permission requests; missing callback verdicts cannot grant Assisted approval.
3132
- Permission authorization never widens sandbox access.
3233
- Validate rule grammar through the real SDK/runtime boundary.
3334

‎src/vs/platform/agentHost/node/agentService.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ import { AgentSessionRegistry, IRegisteredSession, IStoredRegisteredSession } fr
8181
import { IAgentHostSessionStateStore, SessionStateDeletedError, SessionStateMirrorFlush, sessionStatePatch, type SessionStateValuesPrefetch } from '../common/agentHostSessionStateStore.js';
8282
import { getSessionStateValueKey } from './agentHostSessionStateMirror.js';
8383
import { IAgentHostGitService, META_DIFF_BASE_BRANCH, resolveDefaultBaseBranchName, tryResolvePrimaryWorktreeRoot } from '../common/agentHostGitService.js';
84-
import { resolveBranchChangesetScopeForOwner, resolveBranchChangesetScopeForSource, resolveGitHubStateFolder } from './agentHostBranchChangesetScope.js';
84+
import { resolveBranchChangesetScopeForOwner, resolveBranchChangesetScopeForSource, resolveChangesetOwnerScope, resolveGitHubStateFolder } from './agentHostBranchChangesetScope.js';
8585
import { IAgentHostSubscriptionService, resolveAgentHostSession } from '../common/agentHostSubscriptionService.js';
8686
import { deriveLegacyDefaultChat } from '../common/agentHostLegacyDefaultChat.js';
8787
import { IAgentHostChatOwnershipService, usesStandardSessionUris, type IChatOwnerResolution } from './agentHostChatOwnershipService.js';
@@ -122,7 +122,6 @@ import type { IAgentHostSessionLifecycleCandidate } from './agentHostSessionLife
122122
import { IAgentHostCheckpointService } from '../common/agentHostCheckpointService.js';
123123
import { IAgentHostReviewService } from '../common/agentHostReviewService.js';
124124
import { AgentHostChangesetCoordinator } from './agentHostChangesetCoordinator.js';
125-
import { resolveChangesetOwnerScope } from './agentHostBranchChangesetScope.js';
126125
import { IAgentHostCompletions } from './agentHostCompletions.js';
127126
import { AgentHostSkillCompletionProvider } from './agentHostSkillCompletionProvider.js';
128127
import { SessionServerToolName } from '../common/serverToolNames.js';

‎src/vs/platform/agentHost/node/copilot/copilotAgentSession.ts‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { createLocalModelTurnExecutionMeta, isAiCreditsStatus, type UsageInfoMeta } from '../../common/meta/agentUsageMeta.js';
77
import type { ILocalTurnExecution } from '../../common/localModel.js';
88
import type { ILocalModelDirectRequestWait } from './localModelBridge/localModelProvider.js';
9-
import type { AttributedPermissionResult, CopilotSession, CurrentToolMetadata, ElicitationContext, ElicitationFieldValue, ElicitationResult, ElicitationSchema, ElicitationSchemaField, ExitPlanModeCompletedData, ExitPlanModeRequest, ExitPlanModeResult, JsonValue, MessageOptions, PermissionDecisionSource, PermissionHandler, PermissionMode, PermissionAssistedApproval, PermissionRequest, PermissionRequestResult, PermissionResult, SessionConfig, SessionEvent, SessionEventPayload, SessionHooks, SessionMode as CopilotSdkMode, Tool, ToolInvocation, ToolResultObject, McpServerStatus as SdkMcpServerStatus } from '@github/copilot-sdk';
9+
import type { AttributedPermissionResult, CopilotSession, CurrentToolMetadata, ElicitationContext, ElicitationFieldValue, ElicitationResult, ElicitationSchema, ElicitationSchemaField, ExitPlanModeCompletedData, ExitPlanModeRequest, ExitPlanModeResult, JsonValue, MessageOptions, PermissionDecisionSource, PermissionHandler, PermissionMode, PermissionRequest, PermissionRequestResult, PermissionResult, SessionConfig, SessionEvent, SessionEventPayload, SessionHooks, SessionMode as CopilotSdkMode, Tool, ToolInvocation, ToolResultObject, McpServerStatus as SdkMcpServerStatus } from '@github/copilot-sdk';
1010
import { attributePermissionResult, isPermissionDeniedKind, permissionResultToConfirmKind } from './copilotPermissionAttribution.js';
1111
import { realpath as fsRealpath } from 'fs';
1212
import { cp, rm } from 'fs/promises';
@@ -1164,8 +1164,6 @@ export class CopilotAgentSession extends Disposable {
11641164
private _refreshDetachedBackgroundShells = false;
11651165
private readonly _backgroundShellRefresh = this._register(new RunOnceScheduler(() => this._refreshBackgroundTasks(true), 5000));
11661166
private readonly _unroutableSubagentToolCallIds = new Set<string>();
1167-
private readonly _autoApprovals = new Map<string, PermissionAssistedApproval | null>();
1168-
private readonly _pendingAutoApprovals = new PendingRequestRegistry<PermissionAssistedApproval | undefined>();
11691167
/** Correlates tool execution with the SDK permission lifecycle for `chat.toolApproval` telemetry. */
11701168
private readonly _toolApprovalRecords = new Map<string, {
11711169
permissionRequested: boolean;
@@ -6463,7 +6461,7 @@ export class CopilotAgentSession extends Disposable {
64636461
&& (promptRequest.kind === 'commands' || promptRequest.kind === 'read' || promptRequest.kind === 'write' || promptRequest.kind === 'url' || promptRequest.kind === 'workflow')
64646462
&& promptRequest.managedApprovalRequired !== true);
64656463
const autoApproval = canUseAssistedApproval && this._lastAppliedPermissionMode === 'assisted'
6466-
? managedApprovalRequired ? promptRequest?.assistedApproval : await this._takeAutoApproval(toolCallId)
6464+
? promptRequest?.assistedApproval
64676465
: undefined;
64686466
const recommendation = autoApproval?.recommendation;
64696467
if (recommendation === 'approve' && !requestSandboxBypass) {
@@ -6911,22 +6909,6 @@ export class CopilotAgentSession extends Disposable {
69116909
}
69126910
}
69136911

6914-
private async _takeAutoApproval(toolCallId: string): Promise<PermissionAssistedApproval | undefined> {
6915-
if (this._autoApprovals.has(toolCallId)) {
6916-
const autoApproval = this._autoApprovals.get(toolCallId) ?? undefined;
6917-
this._autoApprovals.delete(toolCallId);
6918-
return autoApproval;
6919-
}
6920-
return this._pendingAutoApprovals.register(toolCallId);
6921-
}
6922-
6923-
private _recordAutoApproval(toolCallId: string, autoApproval: PermissionAssistedApproval | undefined): void {
6924-
if (this._pendingAutoApprovals.respond(toolCallId, autoApproval)) {
6925-
return;
6926-
}
6927-
this._autoApprovals.set(toolCallId, autoApproval ?? null);
6928-
}
6929-
69306912
syncPermissionMode(source: 'config-change' | 'turn-start', recorder?: IAgentProviderSendStageRecorder): Promise<void> {
69316913
return measureAgentProviderOperation(recorder, 'permission', async () => {
69326914
const { mode, configuredLevel } = this._approvalPolicy.resolveSelection(this._getApprovalInputs());
@@ -7952,13 +7934,11 @@ export class CopilotAgentSession extends Disposable {
79527934
this._completeSamplingRequest(e.data.requestId);
79537935
}));
79547936

7955-
// TODO@connor4312: Remove this correlation once the SDK permission callback includes auto-approval data.
79567937
store.add(wrapper.onPermissionRequested(e => {
79577938
const toolCallId = e.data.permissionRequest.toolCallId;
79587939
if (!toolCallId) {
79597940
return;
79607941
}
7961-
this._recordAutoApproval(toolCallId, e.data.promptRequest?.assistedApproval);
79627942
const existing = this._toolApprovalRecords.get(toolCallId);
79637943
const permissionRequest = e.data.permissionRequest as { requestSandboxBypass?: boolean; toolName?: string };
79647944
if (permissionRequest.requestSandboxBypass && !e.data.resolvedByHook) {
@@ -8285,9 +8265,7 @@ export class CopilotAgentSession extends Disposable {
82858265
this._logService.info(`[Copilot:${sessionId}] Tool completed: ${e.data.toolCallId}`);
82868266
this._reportToolApprovalIfNoPermission(e.data.toolCallId);
82878267
this._activeToolCalls.delete(e.data.toolCallId);
8288-
this._autoApprovals.delete(e.data.toolCallId);
82898268
this._toolApprovalRecords.delete(e.data.toolCallId);
8290-
this._pendingAutoApprovals.respond(e.data.toolCallId, undefined);
82918269
if (!parentToolCallId && !e.agentId && (tracked.turnId !== this._turnId || this._shouldDropLateRootTurnEvent('tool.execution_complete', true))) {
82928270
return;
82938271
}
@@ -10929,7 +10907,6 @@ export class CopilotAgentSession extends Disposable {
1092910907
* Cancels every pending interaction for abort and dispose. This completes synchronously before any awaiter resumes, so ordering is not significant.
1093010908
*/
1093110909
private _cancelAllPendingInteractions(): void {
10932-
this._cancelPendingAutoApprovals();
1093310910
this._denyPendingPermissions();
1093410911
this._cancelPendingUserInputs();
1093510912
this._cancelPendingElicitations();
@@ -10939,11 +10916,6 @@ export class CopilotAgentSession extends Disposable {
1093910916
this._cancelPendingClientToolCalls();
1094010917
}
1094110918

10942-
private _cancelPendingAutoApprovals(): void {
10943-
this._pendingAutoApprovals.denyAll(undefined);
10944-
this._autoApprovals.clear();
10945-
}
10946-
1094710919
private _denyPendingPermissions(): void {
1094810920
for (const [toolCallId] of this._pendingPermissions.entries()) {
1094910921
this._deletePendingEditContent(toolCallId);

‎src/vs/platform/agentHost/test/node/copilotAgentSession.test.ts‎

Lines changed: 91 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11125,14 +11125,17 @@ suite('CopilotAgentSession', () => {
1112511125
});
1112611126
});
1112711127

11128-
test('Approve When Safe honors approve recommendations without prompting', async () => {
11129-
const { session, runtime, mockSession, signals } = await createAgentSession(disposables, {
11128+
test('Approve When Safe honors callback approve recommendations without an event or prompting', async () => {
11129+
const { session, runtime, signals } = await createAgentSession(disposables, {
1113011130
configValues: { [SessionConfigKey.AutoApprove]: 'assisted' },
1113111131
});
1113211132
await session.syncPermissionMode('turn-start');
11133-
mockSession.fire('permission.requested', {
11134-
requestId: 'request-1',
11135-
permissionRequest: { kind: 'read', path: '/workspace/src/file.ts', intention: 'Read the file', toolCallId: 'tc-assisted' },
11133+
const result = await runtime.handlePermissionRequest({
11134+
kind: 'read',
11135+
path: '/workspace/src/file.ts',
11136+
toolCallId: 'tc-assisted',
11137+
}, {
11138+
sessionId: session.sessionId,
1113611139
promptRequest: {
1113711140
kind: 'path',
1113811141
accessKind: 'read',
@@ -11142,12 +11145,6 @@ suite('CopilotAgentSession', () => {
1114211145
},
1114311146
});
1114411147

11145-
const result = await runtime.handlePermissionRequest({
11146-
kind: 'read',
11147-
path: '/workspace/src/file.ts',
11148-
toolCallId: 'tc-assisted',
11149-
});
11150-
1115111148
assert.deepStrictEqual({ result, signals }, {
1115211149
result: { kind: 'approve-once' },
1115311150
signals: [],
@@ -11185,6 +11182,61 @@ suite('CopilotAgentSession', () => {
1118511182
});
1118611183
}
1118711184

11185+
for (const scenario of ['requireApproval', 'missing-verdict', 'missing-context'] as const) {
11186+
test(`managed Assisted approval does not leak into an unmanaged ${scenario} callback before its event`, async () => {
11187+
const { session, runtime, mockSession, signals } = await createAgentSession(disposables, {
11188+
configValues: { [SessionConfigKey.AutoApprove]: 'assisted' },
11189+
});
11190+
await session.syncPermissionMode('turn-start');
11191+
const toolCallId = 'tc-assisted-multiple-permissions';
11192+
const managedRequest: PermissionRequest = {
11193+
kind: 'read', path: '/workspace/first.ts', intention: 'Read the first file', toolCallId, managedApprovalRequired: true,
11194+
};
11195+
const approvedPrompt: Parameters<PermissionHandler>[1]['promptRequest'] = {
11196+
...managedRequest,
11197+
managedApprovalRequired: undefined,
11198+
assistedApproval: { recommendation: 'approve', reason: 'Explicit user request' },
11199+
};
11200+
mockSession.fire('permission.requested', {
11201+
requestId: 'request-managed',
11202+
permissionRequest: managedRequest,
11203+
promptRequest: approvedPrompt,
11204+
});
11205+
const first = await runtime.handlePermissionRequest(managedRequest, { sessionId: session.sessionId, promptRequest: approvedPrompt });
11206+
11207+
const unmanagedRequest: PermissionRequest = {
11208+
kind: 'read', path: '/workspace/second.ts', intention: 'Read a different file', toolCallId,
11209+
};
11210+
const confirmationPrompt: Parameters<PermissionHandler>[1]['promptRequest'] = {
11211+
...unmanagedRequest,
11212+
assistedApproval: { recommendation: 'requireApproval', reason: 'Needs confirmation' },
11213+
};
11214+
const invocation = scenario === 'missing-context' ? undefined : {
11215+
sessionId: session.sessionId,
11216+
promptRequest: scenario === 'missing-verdict' ? { ...confirmationPrompt, assistedApproval: undefined } : confirmationPrompt,
11217+
};
11218+
const second = runtime.handlePermissionRequest(unmanagedRequest, invocation);
11219+
await timeout(0);
11220+
const confirmationsBeforeEvent = signals.filter(signal => signal.kind === 'pending_confirmation').length;
11221+
mockSession.fire('permission.requested', {
11222+
requestId: 'request-unmanaged',
11223+
permissionRequest: unmanagedRequest,
11224+
promptRequest: confirmationPrompt,
11225+
});
11226+
11227+
const responded = session.respondToPermissionRequest(toolCallId, false);
11228+
assert.deepStrictEqual({
11229+
results: [first, await second],
11230+
confirmationsBeforeEvent,
11231+
responded,
11232+
}, {
11233+
results: [{ kind: 'approve-once' }, { kind: 'reject', feedback: 'The user denied permission.' }],
11234+
confirmationsBeforeEvent: 1,
11235+
responded: true,
11236+
});
11237+
});
11238+
}
11239+
1118811240
for (const scenario of ['human-required', 'requireApproval', 'missing-verdict', 'unsupported-kind', 'sandbox-bypass', 'manual', 'allow-all'] as const) {
1118911241
test(`managed Assisted callback remains conservative for ${scenario}`, async () => {
1119011242
const { session, runtime, signals, waitForSignal } = await createAgentSession(disposables, {
@@ -11338,42 +11390,36 @@ suite('CopilotAgentSession', () => {
1133811390
}
1133911391
});
1134011392

11341-
test('Approve When Safe correlates a recommendation event that arrives after the permission callback', async () => {
11393+
test('Approve When Safe uses the callback recommendation before its event arrives', async () => {
1134211394
const { session, runtime, mockSession, signals } = await createAgentSession(disposables, {
1134311395
configValues: { [SessionConfigKey.AutoApprove]: 'assisted' },
1134411396
});
1134511397
await session.syncPermissionMode('turn-start');
1134611398

11347-
const resultPromise = runtime.handleAttributedPermissionRequest({
11399+
const request: PermissionRequest = {
1134811400
kind: 'write',
1134911401
fileName: '/workspace/package.json',
1135011402
intention: 'Edit file',
1135111403
diff: 'diff',
11404+
canOfferSessionApproval: true,
1135211405
toolCallId: 'tc-assisted-late-event',
11406+
};
11407+
const promptRequest: Parameters<PermissionHandler>[1]['promptRequest'] = {
11408+
...request,
11409+
assistedApproval: { recommendation: 'approve', reason: 'Matches the request' },
11410+
};
11411+
const result = await runtime.handleAttributedPermissionRequest(request, {
11412+
sessionId: session.sessionId,
11413+
promptRequest,
1135311414
});
1135411415
mockSession.fire('permission.requested', {
1135511416
requestId: 'request-late-event',
11356-
permissionRequest: {
11357-
kind: 'write',
11358-
fileName: '/workspace/package.json',
11359-
intention: 'Edit file',
11360-
diff: 'diff',
11361-
canOfferSessionApproval: true,
11362-
toolCallId: 'tc-assisted-late-event',
11363-
},
11364-
promptRequest: {
11365-
kind: 'write',
11366-
fileName: '/workspace/package.json',
11367-
intention: 'Edit file',
11368-
diff: 'diff',
11369-
canOfferSessionApproval: true,
11370-
toolCallId: 'tc-assisted-late-event',
11371-
assistedApproval: { recommendation: 'approve', reason: 'Matches the request' },
11372-
},
11417+
permissionRequest: request,
11418+
promptRequest,
1137311419
});
1137411420

1137511421
assert.deepStrictEqual({
11376-
result: await resultPromise,
11422+
result,
1137711423
signals,
1137811424
}, {
1137911425
result: { kind: 'attributed', result: { kind: 'approve-once' }, decisionContext: { source: 'assisted_approval', surface: 'sdk', outcome: 'auto_approved' } },
@@ -11386,7 +11432,7 @@ suite('CopilotAgentSession', () => {
1138611432
configValues: { [SessionConfigKey.AutoApprove]: 'assisted' },
1138711433
});
1138811434
await session.syncPermissionMode('turn-start');
11389-
mockSession.fire('permission.requested', {
11435+
const permissionEvent: SessionEventPayload<'permission.requested'>['data'] = {
1139011436
requestId: 'request-2',
1139111437
permissionRequest: { kind: 'read', path: '/workspace/src/file.ts', intention: 'Read the file', toolCallId: 'tc-assisted-prompt' },
1139211438
promptRequest: {
@@ -11396,13 +11442,14 @@ suite('CopilotAgentSession', () => {
1139611442
toolCallId: 'tc-assisted-prompt',
1139711443
assistedApproval: { recommendation: 'requireApproval', reason: 'Needs confirmation' },
1139811444
},
11399-
});
11445+
};
11446+
mockSession.fire('permission.requested', permissionEvent);
1140011447

1140111448
const resultPromise = runtime.handlePermissionRequest({
1140211449
kind: 'read',
1140311450
path: '/workspace/src/file.ts',
1140411451
toolCallId: 'tc-assisted-prompt',
11405-
});
11452+
}, { sessionId: session.sessionId, promptRequest: permissionEvent.promptRequest });
1140611453
const confirmation = await waitForSignal(signal => signal.kind === 'pending_confirmation');
1140711454
assert.deepStrictEqual(confirmation.kind === 'pending_confirmation' ? confirmation.state.riskAssessment : undefined, {
1140811455
kind: ToolCallRiskAssessmentKind.Judge,
@@ -11420,7 +11467,7 @@ suite('CopilotAgentSession', () => {
1142011467
configValues: { [SessionConfigKey.AutoApprove]: 'assisted' },
1142111468
});
1142211469
await session.syncPermissionMode('turn-start');
11423-
mockSession.fire('permission.requested', {
11470+
const permissionEvent: SessionEventPayload<'permission.requested'>['data'] = {
1142411471
requestId: 'request-3',
1142511472
permissionRequest: {
1142611473
kind: 'shell',
@@ -11443,14 +11490,15 @@ suite('CopilotAgentSession', () => {
1144311490
toolCallId: 'tc-assisted-bypass',
1144411491
assistedApproval: { recommendation: 'approve', reason: 'Incorrect recommendation' },
1144511492
},
11446-
});
11493+
};
11494+
mockSession.fire('permission.requested', permissionEvent);
1144711495

1144811496
const resultPromise = runtime.handlePermissionRequest({
1144911497
kind: 'shell',
1145011498
toolCallId: 'tc-assisted-bypass',
1145111499
fullCommandText: 'curl https://example.com',
1145211500
requestSandboxBypass: true,
11453-
});
11501+
}, { sessionId: session.sessionId, promptRequest: permissionEvent.promptRequest });
1145411502
const confirmation = await waitForSignal(signal => signal.kind === 'pending_confirmation');
1145511503
assert.deepStrictEqual(confirmation.kind === 'pending_confirmation' ? confirmation.state.riskAssessment : undefined, {
1145611504
kind: ToolCallRiskAssessmentKind.Judge,
@@ -22163,28 +22211,31 @@ Use the attached image as context.
2216322211
arguments: { file: 'test.ts' },
2216422212
} as SessionEventPayload<'tool.execution_start'>['data']);
2216522213

22166-
mockSession.fire('permission.requested', {
22214+
const permissionEvent: SessionEventPayload<'permission.requested'>['data'] = {
2216722215
requestId: 'permission-assisted',
2216822216
permissionRequest: {
2216922217
kind: 'custom-tool',
2217022218
toolCallId: 'tc-assisted',
2217122219
toolName: 'my_tool',
22220+
toolDescription: '',
2217222221
},
2217322222
promptRequest: {
2217422223
kind: 'custom-tool',
2217522224
toolCallId: 'tc-assisted',
2217622225
toolName: 'my_tool',
22226+
toolDescription: '',
2217722227
assistedApproval: {
2217822228
recommendation: 'approve',
2217922229
reason: 'The requested browser navigation is safe.',
2218022230
},
2218122231
},
22182-
} as SessionEventPayload<'permission.requested'>['data']);
22232+
};
22233+
mockSession.fire('permission.requested', permissionEvent);
2218322234
const permissionResult = await runtime.handlePermissionRequest({
2218422235
kind: 'custom-tool',
2218522236
toolCallId: 'tc-assisted',
2218622237
toolName: 'my_tool',
22187-
});
22238+
}, { sessionId: session.sessionId, promptRequest: permissionEvent.promptRequest });
2218822239
const readySignal = signals.find((s): s is IAgentToolPendingConfirmationSignal => s.kind === 'pending_confirmation');
2218922240
const readyState = readySignal?.state;
2219022241
const readyToolInput = getInlineToolInput(readyState?.toolInput);

0 commit comments

Comments
 (0)