source dump of claude code
at main 65 lines 2.4 kB view raw
1import { feature } from 'bun:bundle' 2import type { PendingClassifierCheck } from '../../../types/permissions.js' 3import { logError } from '../../../utils/log.js' 4import type { PermissionDecision } from '../../../utils/permissions/PermissionResult.js' 5import type { PermissionUpdate } from '../../../utils/permissions/PermissionUpdateSchema.js' 6import type { PermissionContext } from '../PermissionContext.js' 7 8type CoordinatorPermissionParams = { 9 ctx: PermissionContext 10 pendingClassifierCheck?: PendingClassifierCheck | undefined 11 updatedInput: Record<string, unknown> | undefined 12 suggestions: PermissionUpdate[] | undefined 13 permissionMode: string | undefined 14} 15 16/** 17 * Handles the coordinator worker permission flow. 18 * 19 * For coordinator workers, automated checks (hooks and classifier) are 20 * awaited sequentially before falling through to the interactive dialog. 21 * 22 * Returns a PermissionDecision if the automated checks resolved the 23 * permission, or null if the caller should fall through to the 24 * interactive dialog. 25 */ 26async function handleCoordinatorPermission( 27 params: CoordinatorPermissionParams, 28): Promise<PermissionDecision | null> { 29 const { ctx, updatedInput, suggestions, permissionMode } = params 30 31 try { 32 // 1. Try permission hooks first (fast, local) 33 const hookResult = await ctx.runHooks( 34 permissionMode, 35 suggestions, 36 updatedInput, 37 ) 38 if (hookResult) return hookResult 39 40 // 2. Try classifier (slow, inference -- bash only) 41 const classifierResult = feature('BASH_CLASSIFIER') 42 ? await ctx.tryClassifier?.(params.pendingClassifierCheck, updatedInput) 43 : null 44 if (classifierResult) { 45 return classifierResult 46 } 47 } catch (error) { 48 // If automated checks fail unexpectedly, fall through to show the dialog 49 // so the user can decide manually. Non-Error throws get a context prefix 50 // so the log is traceable — intentionally NOT toError(), which would drop 51 // the prefix. 52 if (error instanceof Error) { 53 logError(error) 54 } else { 55 logError(new Error(`Automated permission check failed: ${String(error)}`)) 56 } 57 } 58 59 // 3. Neither resolved (or checks failed) -- fall through to dialog below. 60 // Hooks already ran, classifier already consumed. 61 return null 62} 63 64export { handleCoordinatorPermission } 65export type { CoordinatorPermissionParams }