@@ -41,6 +41,7 @@ import {
4141 resolveSkillTelemetrySourceValue ,
4242} from "../skills/loading/source.js" ;
4343import type { SkillSnapshot , SkillTelemetrySource } from "../skills/types.js" ;
44+ import { resolveSkillResearchToolApproval } from "../skills/workshop/policy.js" ;
4445import { isPlainObject } from "../utils.js" ;
4546import { adjustedParamsByToolCallId } from "./agent-tools.before-tool-call.state.js" ;
4647import { copyChannelAgentToolMeta , getChannelAgentToolMeta } from "./channel-tools.js" ;
@@ -603,6 +604,54 @@ export function cancelDeferredPluginToolApproval(
603604 notifyPluginApprovalResolution ( deferredApproval . approval , PluginApprovalResolutions . CANCELLED ) ;
604605}
605606
607+ async function resolveBeforeToolCallApprovalOutcome ( params : {
608+ result : PluginHookBeforeToolCallResult | undefined ;
609+ approvalMode ?: "request" | "report" | "defer" ;
610+ toolName : string ;
611+ toolCallId ?: string ;
612+ ctx ?: HookContext ;
613+ signal ?: AbortSignal ;
614+ baseParams : unknown ;
615+ } ) : Promise < HookOutcome | undefined > {
616+ const approval = params . result ?. requireApproval ;
617+ if ( ! approval ) {
618+ return undefined ;
619+ }
620+ if ( params . approvalMode === "defer" ) {
621+ return {
622+ blocked : false ,
623+ params : params . baseParams ,
624+ deferredApproval : {
625+ approval,
626+ toolName : params . toolName ,
627+ ...( params . toolCallId ? { toolCallId : params . toolCallId } : { } ) ,
628+ ...( params . ctx ? { ctx : params . ctx } : { } ) ,
629+ baseParams : params . baseParams ,
630+ overrideParams : params . result ?. params ,
631+ } ,
632+ } ;
633+ }
634+ if ( params . approvalMode === "report" ) {
635+ notifyPluginApprovalResolution ( approval , PluginApprovalResolutions . CANCELLED ) ;
636+ return {
637+ blocked : true ,
638+ kind : "failure" ,
639+ deniedReason : "plugin-approval" ,
640+ reason : approval . description || approval . title || "Plugin approval required" ,
641+ params : params . baseParams ,
642+ } ;
643+ }
644+ return await requestPluginToolApproval ( {
645+ approval,
646+ toolName : params . toolName ,
647+ ...( params . toolCallId ? { toolCallId : params . toolCallId } : { } ) ,
648+ ...( params . ctx ? { ctx : params . ctx } : { } ) ,
649+ signal : params . signal ,
650+ baseParams : params . baseParams ,
651+ overrideParams : params . result ?. params ,
652+ } ) ;
653+ }
654+
606655export function buildBlockedToolResult ( params : {
607656 reason : string ;
608657 deniedReason ?: HookBlockedReason ;
@@ -789,10 +838,15 @@ export async function runBeforeToolCallHook(args: {
789838 try {
790839 const hasBeforeToolCallHooks = hookRunner ?. hasHooks ( "before_tool_call" ) === true ;
791840 const shouldRunTrustedPolicies = hasTrustedToolPolicies ( ) ;
792- if ( ! shouldRunTrustedPolicies && ! hasBeforeToolCallHooks ) {
841+ const normalizedParams = isPlainObject ( params ) ? params : { } ;
842+ const corePolicyResult = resolveSkillResearchToolApproval ( {
843+ toolName,
844+ toolParams : normalizedParams ,
845+ ...( args . ctx ?. config ? { config : args . ctx . config } : { } ) ,
846+ } ) ;
847+ if ( ! corePolicyResult && ! shouldRunTrustedPolicies && ! hasBeforeToolCallHooks ) {
793848 return { blocked : false , params } ;
794849 }
795- const normalizedParams = isPlainObject ( params ) ? params : { } ;
796850 const deriveOptions =
797851 args . ctx ?. cwd || args . ctx ?. sandbox
798852 ? {
@@ -821,6 +875,18 @@ export async function runBeforeToolCallHook(args: {
821875 ...( args . ctx ?. channelId && { channelId : args . ctx . channelId } ) ,
822876 } ) ;
823877 const toolContext = buildToolContext ( toolIdentity ) ;
878+ const coreApprovalOutcome = await resolveBeforeToolCallApprovalOutcome ( {
879+ result : corePolicyResult ,
880+ approvalMode : args . approvalMode ,
881+ toolName,
882+ ...( args . toolCallId ? { toolCallId : args . toolCallId } : { } ) ,
883+ ...( args . ctx ? { ctx : args . ctx } : { } ) ,
884+ signal : args . signal ,
885+ baseParams : params ,
886+ } ) ;
887+ if ( coreApprovalOutcome ) {
888+ return coreApprovalOutcome ;
889+ }
824890 const trustedPolicyResult = shouldRunTrustedPolicies
825891 ? await runTrustedToolPolicies (
826892 {
@@ -869,45 +935,18 @@ export async function runBeforeToolCallHook(args: {
869935 } ;
870936 }
871937 if ( trustedPolicyResult ?. requireApproval ) {
872- if ( args . approvalMode === "defer" ) {
873- return {
874- blocked : false ,
875- params,
876- deferredApproval : {
877- approval : trustedPolicyResult . requireApproval ,
878- toolName,
879- ...( args . toolCallId ? { toolCallId : args . toolCallId } : { } ) ,
880- ...( args . ctx ? { ctx : args . ctx } : { } ) ,
881- baseParams : params ,
882- overrideParams : trustedPolicyResult . params ,
883- } ,
884- } ;
885- }
886- if ( args . approvalMode === "report" ) {
887- notifyPluginApprovalResolution (
888- trustedPolicyResult . requireApproval ,
889- PluginApprovalResolutions . CANCELLED ,
890- ) ;
891- return {
892- blocked : true ,
893- kind : "failure" ,
894- deniedReason : "plugin-approval" ,
895- reason :
896- trustedPolicyResult . requireApproval . description ||
897- trustedPolicyResult . requireApproval . title ||
898- "Plugin approval required" ,
899- params,
900- } ;
901- }
902- return await requestPluginToolApproval ( {
903- approval : trustedPolicyResult . requireApproval ,
938+ const approvalOutcome = await resolveBeforeToolCallApprovalOutcome ( {
939+ result : trustedPolicyResult ,
940+ approvalMode : args . approvalMode ,
904941 toolName,
905- toolCallId : args . toolCallId ,
906- ctx : args . ctx ,
942+ ... ( args . toolCallId ? { toolCallId : args . toolCallId } : { } ) ,
943+ ... ( args . ctx ? { ctx : args . ctx } : { } ) ,
907944 signal : args . signal ,
908945 baseParams : params ,
909- overrideParams : trustedPolicyResult . params ,
910946 } ) ;
947+ if ( approvalOutcome ) {
948+ return approvalOutcome ;
949+ }
911950 }
912951 const rawPolicyAdjustedParams = trustedPolicyResult ?. params ?? params ;
913952 const policyAdjustedParams = normalizeCodeModeExecBeforeHookParamsForToolKind ( {
@@ -953,45 +992,18 @@ export async function runBeforeToolCallHook(args: {
953992 }
954993
955994 if ( hookResult ?. requireApproval ) {
956- if ( args . approvalMode === "defer" ) {
957- return {
958- blocked : false ,
959- params : policyAdjustedParams ,
960- deferredApproval : {
961- approval : hookResult . requireApproval ,
962- toolName,
963- ...( args . toolCallId ? { toolCallId : args . toolCallId } : { } ) ,
964- ...( args . ctx ? { ctx : args . ctx } : { } ) ,
965- baseParams : policyAdjustedParams ,
966- overrideParams : hookResult . params ,
967- } ,
968- } ;
969- }
970- if ( args . approvalMode === "report" ) {
971- notifyPluginApprovalResolution (
972- hookResult . requireApproval ,
973- PluginApprovalResolutions . CANCELLED ,
974- ) ;
975- return {
976- blocked : true ,
977- kind : "failure" ,
978- deniedReason : "plugin-approval" ,
979- reason :
980- hookResult . requireApproval . description ||
981- hookResult . requireApproval . title ||
982- "Plugin approval required" ,
983- params : policyAdjustedParams ,
984- } ;
985- }
986- return await requestPluginToolApproval ( {
987- approval : hookResult . requireApproval ,
995+ const approvalOutcome = await resolveBeforeToolCallApprovalOutcome ( {
996+ result : hookResult ,
997+ approvalMode : args . approvalMode ,
988998 toolName,
989- toolCallId : args . toolCallId ,
990- ctx : args . ctx ,
999+ ... ( args . toolCallId ? { toolCallId : args . toolCallId } : { } ) ,
1000+ ... ( args . ctx ? { ctx : args . ctx } : { } ) ,
9911001 signal : args . signal ,
9921002 baseParams : policyAdjustedParams ,
993- overrideParams : hookResult . params ,
9941003 } ) ;
1004+ if ( approvalOutcome ) {
1005+ return approvalOutcome ;
1006+ }
9951007 }
9961008
9971009 if ( hookResult ?. params ) {
0 commit comments