@@ -74,6 +74,7 @@ function ruleMatches(
7474 serverName : string | undefined ,
7575 currentApprovalMode : ApprovalMode ,
7676 toolAnnotations ?: Record < string , unknown > ,
77+ subagent ?: string ,
7778) : boolean {
7879 // Check if rule applies to current approval mode
7980 if ( rule . modes && rule . modes . length > 0 ) {
@@ -82,6 +83,13 @@ function ruleMatches(
8283 }
8384 }
8485
86+ // Check subagent if specified (only for PolicyRule, SafetyCheckerRule doesn't have it)
87+ if ( 'subagent' in rule && rule . subagent ) {
88+ if ( rule . subagent !== subagent ) {
89+ return false ;
90+ }
91+ }
92+
8593 // Strictly enforce mcpName identity if the rule dictates it
8694 if ( rule . mcpName ) {
8795 if ( rule . mcpName === '*' ) {
@@ -203,6 +211,7 @@ export class PolicyEngine {
203211 allowRedirection ?: boolean ,
204212 rule ?: PolicyRule ,
205213 toolAnnotations ?: Record < string , unknown > ,
214+ subagent ?: string ,
206215 ) : Promise < CheckResult > {
207216 if ( ! command ) {
208217 return {
@@ -294,6 +303,7 @@ export class PolicyEngine {
294303 { name : toolName , args : { command : subCmd , dir_path } } ,
295304 serverName ,
296305 toolAnnotations ,
306+ subagent ,
297307 ) ;
298308
299309 // subResult.decision is already filtered through applyNonInteractiveMode by this.check()
@@ -352,6 +362,7 @@ export class PolicyEngine {
352362 toolCall : FunctionCall ,
353363 serverName : string | undefined ,
354364 toolAnnotations ?: Record < string , unknown > ,
365+ subagent ?: string ,
355366 ) : Promise < CheckResult > {
356367 // Case 1: Metadata injection is the primary and safest way to identify an MCP server.
357368 // If we have explicit `_serverName` metadata (usually injected by tool-registry for active tools), use it.
@@ -419,6 +430,7 @@ export class PolicyEngine {
419430 serverName ,
420431 this . approvalMode ,
421432 toolAnnotations ,
433+ subagent ,
422434 ) ,
423435 ) ;
424436
@@ -437,6 +449,7 @@ export class PolicyEngine {
437449 rule . allowRedirection ,
438450 rule ,
439451 toolAnnotations ,
452+ subagent ,
440453 ) ;
441454 decision = shellResult . decision ;
442455 if ( shellResult . rule ) {
@@ -463,9 +476,10 @@ export class PolicyEngine {
463476 this . defaultDecision ,
464477 serverName ,
465478 shellDirPath ,
466- undefined ,
479+ false ,
467480 undefined ,
468481 toolAnnotations ,
482+ subagent ,
469483 ) ;
470484 decision = shellResult . decision ;
471485 matchedRule = shellResult . rule ;
@@ -485,6 +499,7 @@ export class PolicyEngine {
485499 serverName ,
486500 this . approvalMode ,
487501 toolAnnotations ,
502+ subagent ,
488503 )
489504 ) {
490505 debugLogger . debug (
0 commit comments