| 414 | // For async agents, also set shouldAvoidPermissionPrompts since they can't show UI |
| 415 | const agentPermissionMode = agentDefinition.permissionMode |
| 416 | const agentGetAppState = () => { |
| 417 | const state = toolUseContext.getAppState() |
| 418 | let toolPermissionContext = state.toolPermissionContext |
| 419 | |
| 420 | // Override permission mode if agent defines one (unless parent is bypassPermissions, acceptEdits, or auto) |
| 421 | if ( |
| 422 | agentPermissionMode && |
| 423 | state.toolPermissionContext.mode !== 'bypassPermissions' && |
| 424 | state.toolPermissionContext.mode !== 'acceptEdits' && |
| 425 | !( |
| 426 | feature('TRANSCRIPT_CLASSIFIER') && |
| 427 | state.toolPermissionContext.mode === 'auto' |
| 428 | ) |
| 429 | ) { |
| 430 | toolPermissionContext = { |
| 431 | ...toolPermissionContext, |
| 432 | mode: agentPermissionMode, |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | // Set flag to auto-deny prompts for agents that can't show UI |
| 437 | // Use explicit canShowPermissionPrompts if provided, otherwise: |
| 438 | // - bubble mode: always show prompts (bubbles to parent terminal) |
| 439 | // - default: !isAsync (sync agents show prompts, async agents don't) |
| 440 | const shouldAvoidPrompts = |
| 441 | canShowPermissionPrompts !== undefined |
| 442 | ? !canShowPermissionPrompts |
| 443 | : agentPermissionMode === 'bubble' |
| 444 | ? false |
| 445 | : isAsync |
| 446 | if (shouldAvoidPrompts) { |
| 447 | toolPermissionContext = { |
| 448 | ...toolPermissionContext, |
| 449 | shouldAvoidPermissionPrompts: true, |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // For background agents that can show prompts, await automated checks |
| 454 | // (classifier, permission hooks) before showing the permission dialog. |
| 455 | // Since these are background agents, waiting is fine — the user should |
| 456 | // only be interrupted when automated checks can't resolve the permission. |
| 457 | // This applies to bubble mode (always) and explicit canShowPermissionPrompts. |
| 458 | if (isAsync && !shouldAvoidPrompts) { |
| 459 | toolPermissionContext = { |
| 460 | ...toolPermissionContext, |
| 461 | awaitAutomatedChecksBeforeDialog: true, |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // Scope tool permissions: when allowedTools is provided, use them as session rules. |
| 466 | // IMPORTANT: Preserve cliArg rules (from SDK's --allowedTools) since those are |
| 467 | // explicit permissions from the SDK consumer that should apply to all agents. |
| 468 | // Only clear session-level rules from the parent to prevent unintended leakage. |
| 469 | if (allowedTools !== undefined) { |
| 470 | toolPermissionContext = { |
| 471 | ...toolPermissionContext, |
| 472 | alwaysAllowRules: { |
| 473 | // Preserve SDK-level permissions from --allowedTools |