@@ -922,6 +922,130 @@ test("team plugin skips parallel enforcement on HEAD-less repos", async () => {
922922 } )
923923} )
924924
925+ test ( "guardrail delegation gates and quality hooks fire correctly" , async ( ) => {
926+ await withProfile ( async ( ) => {
927+ await using tmp = await tmpdir ( {
928+ git : true ,
929+ init : async ( dir ) => {
930+ await fs . mkdir ( path . join ( dir , "src" , "ui" ) , { recursive : true } )
931+ await fs . mkdir ( path . join ( dir , "src" , "api" ) , { recursive : true } )
932+ await Bun . write ( path . join ( dir , "src" , "app.ts" ) , "export const main = 1\n" )
933+ } ,
934+ } )
935+ const files = guard ( tmp . path )
936+
937+ await Instance . provide ( {
938+ directory : tmp . path ,
939+ fn : async ( ) => {
940+ // Initialize session to set up state
941+ const hook = ( await Plugin . list ( ) ) . find ( ( item ) => typeof item . event === "function" )
942+ await hook ?. event ?.( {
943+ event : {
944+ type : "session.created" ,
945+ properties : { sessionID : "session_delegation_test" } ,
946+ } ,
947+ } as any )
948+ await wait ( )
949+
950+ // 1. Verify delegation state fields are initialized
951+ let state = await Bun . file ( files . state ) . json ( )
952+ expect ( state . active_task_count ) . toBe ( 0 )
953+ expect ( state . session_cost ) . toBe ( 0 )
954+ expect ( state . consecutive_failures ) . toBe ( 0 )
955+ expect ( state . consecutive_fix_prs ) . toBe ( 0 )
956+ expect ( state . issue_verification_done ) . toBe ( false )
957+ expect ( state . edits_since_doc_reminder ) . toBe ( 0 )
958+
959+ // 2. Parallel execution gate: task tool increments active count
960+ await Plugin . trigger (
961+ "tool.execute.before" ,
962+ { tool : "task" , sessionID : "session_delegation_test" , callID : "call_task_1" } ,
963+ { args : { subagent_type : "explore" , prompt : "test" } } ,
964+ )
965+ state = await Bun . file ( files . state ) . json ( )
966+ expect ( state . active_task_count ) . toBe ( 1 )
967+
968+ // 3. Task completion decrements active count and tracks delegation
969+ await Plugin . trigger (
970+ "tool.execute.after" ,
971+ { tool : "task" , sessionID : "session_delegation_test" , callID : "call_task_1" , args : { subagent_type : "explore" , command : "" } } ,
972+ { title : "task" , output : "Result text here with enough content to pass" , metadata : { } } ,
973+ )
974+ state = await Bun . file ( files . state ) . json ( )
975+ expect ( state . active_task_count ) . toBe ( 0 )
976+ expect ( state . delegation_explore ) . toBe ( 1 )
977+
978+ // 4. Verify agent output check: short output triggers advisory
979+ const shortOut = { title : "task" , output : "" , metadata : { } }
980+ await Plugin . trigger (
981+ "tool.execute.after" ,
982+ { tool : "task" , sessionID : "session_delegation_test" , callID : "call_task_2" , args : { subagent_type : "review" , command : "" } } ,
983+ shortOut ,
984+ )
985+ expect ( shortOut . output ) . toContain ( "Agent output appears empty" )
986+
987+ // 5. Parallel execution gate: block at max
988+ for ( let i = 0 ; i < 5 ; i ++ ) {
989+ await Plugin . trigger (
990+ "tool.execute.before" ,
991+ { tool : "task" , sessionID : "session_delegation_test" , callID : `call_flood_${ i } ` } ,
992+ { args : { subagent_type : "explore" , prompt : "test" } } ,
993+ )
994+ }
995+ state = await Bun . file ( files . state ) . json ( )
996+ expect ( state . active_task_count ) . toBe ( 5 )
997+ await expect (
998+ Plugin . trigger (
999+ "tool.execute.before" ,
1000+ { tool : "task" , sessionID : "session_delegation_test" , callID : "call_flood_6" } ,
1001+ { args : { subagent_type : "explore" , prompt : "test" } } ,
1002+ ) ,
1003+ ) . rejects . toThrow ( "parallel task limit reached" )
1004+
1005+ // 6. Domain naming advisory: write to src/ui/ with non-PascalCase triggers event
1006+ await Plugin . trigger (
1007+ "tool.execute.before" ,
1008+ { tool : "write" , sessionID : "session_delegation_test" , callID : "call_domain" } ,
1009+ { args : { filePath : path . join ( tmp . path , "src" , "ui" , "bad_name.tsx" ) , content : "export default function Bad() {}" } } ,
1010+ )
1011+ const log = await Bun . file ( files . log ) . text ( )
1012+ expect ( log ) . toContain ( "domain_naming.mismatch" )
1013+
1014+ // 7. Endpoint dataflow advisory on API file edit
1015+ const endpointOut = { title : "edit" , output : "" , metadata : { } }
1016+ await Plugin . trigger (
1017+ "tool.execute.after" ,
1018+ { tool : "edit" , sessionID : "session_delegation_test" , callID : "call_endpoint" , args : { filePath : path . join ( tmp . path , "src" , "api" , "users.ts" ) , newString : "router.get('/users', handler)" } } ,
1019+ endpointOut ,
1020+ )
1021+ expect ( endpointOut . output ) . toContain ( "Endpoint modification detected" )
1022+
1023+ // 8. Tool failure recovery: consecutive failures
1024+ for ( let i = 0 ; i < 3 ; i ++ ) {
1025+ await Plugin . trigger (
1026+ "tool.execute.after" ,
1027+ { tool : "bash" , sessionID : "session_delegation_test" , callID : `call_fail_${ i } ` , args : { command : "npm build" } } ,
1028+ { title : "bash" , output : "Error: build failed with exception" , metadata : { } } ,
1029+ )
1030+ }
1031+ state = await Bun . file ( files . state ) . json ( )
1032+ expect ( state . consecutive_failures ) . toBe ( 3 )
1033+
1034+ // 9. Compaction context includes new delegation fields
1035+ const compact = await Plugin . trigger (
1036+ "experimental.session.compacting" ,
1037+ { sessionID : "session_delegation_test" } ,
1038+ { context : [ ] , prompt : undefined } ,
1039+ )
1040+ const ctx = compact . context . join ( "\n" )
1041+ expect ( ctx ) . toContain ( "Active tasks:" )
1042+ expect ( ctx ) . toContain ( "Session cost:" )
1043+ expect ( ctx ) . toContain ( "Consecutive failures: 3" )
1044+ } ,
1045+ } )
1046+ } )
1047+ } , 15000 )
1048+
9251049for ( const replay of Object . values ( replays ) ) {
9261050 it . live ( `guardrail replay keeps ${ replay . command } executable` , ( ) =>
9271051 run ( replay ) . pipe (
0 commit comments