@@ -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