(mode)
| 414 | }); |
| 415 | }, |
| 416 | onSetPermissionMode(mode) { |
| 417 | // Policy guards MUST fire before transitionPermissionMode — |
| 418 | // its internal auto-gate check is a defensive throw (with a |
| 419 | // setAutoModeActive(true) side-effect BEFORE the throw) rather |
| 420 | // than a graceful reject. Letting that throw escape would: |
| 421 | // (1) leave STATE.autoModeActive=true while the mode is |
| 422 | // unchanged (3-way invariant violation per src/CLAUDE.md) |
| 423 | // (2) fail to send a control_response → server kills WS |
| 424 | // These mirror print.ts handleSetPermissionMode; the bridge |
| 425 | // can't import the checks directly (bootstrap-isolation), so |
| 426 | // it relies on this verdict to emit the error response. |
| 427 | if (mode === 'bypassPermissions') { |
| 428 | if (isBypassPermissionsModeDisabled()) { |
| 429 | return { |
| 430 | ok: false, |
| 431 | error: 'Cannot set permission mode to bypassPermissions because it is disabled by settings or configuration' |
| 432 | }; |
| 433 | } |
| 434 | if (!store.getState().toolPermissionContext.isBypassPermissionsModeAvailable) { |
| 435 | return { |
| 436 | ok: false, |
| 437 | error: 'Cannot set permission mode to bypassPermissions because the session was not launched with --dangerously-skip-permissions' |
| 438 | }; |
| 439 | } |
| 440 | } |
| 441 | if (feature('TRANSCRIPT_CLASSIFIER') && mode === 'auto' && !isAutoModeGateEnabled()) { |
| 442 | const reason = getAutoModeUnavailableReason(); |
| 443 | return { |
| 444 | ok: false, |
| 445 | error: reason ? `Cannot set permission mode to auto: ${getAutoModeUnavailableNotification(reason)}` : 'Cannot set permission mode to auto' |
| 446 | }; |
| 447 | } |
| 448 | // Guards passed — apply via the centralized transition so |
| 449 | // prePlanMode stashing and auto-mode state sync all fire. |
| 450 | setAppState(prev_12 => { |
| 451 | const current = prev_12.toolPermissionContext.mode; |
| 452 | if (current === mode) return prev_12; |
| 453 | const next = transitionPermissionMode(current, mode, prev_12.toolPermissionContext); |
| 454 | return { |
| 455 | ...prev_12, |
| 456 | toolPermissionContext: { |
| 457 | ...next, |
| 458 | mode |
| 459 | } |
| 460 | }; |
| 461 | }); |
| 462 | // Recheck queued permission prompts now that mode changed. |
| 463 | setImmediate(() => { |
| 464 | getLeaderToolUseConfirmQueue()?.(currentQueue => { |
| 465 | currentQueue.forEach(item => { |
| 466 | void item.recheckPermission(); |
| 467 | }); |
| 468 | return currentQueue; |
| 469 | }); |
| 470 | }); |
| 471 | return { |
| 472 | ok: true |
| 473 | }; |
no test coverage detected