@@ -44,6 +44,15 @@ function switchBranch(path: string, branch: string): void {
4444 } ) ;
4545}
4646
47+ function initBranchOnlyRepo ( path : string , branch : string ) : void {
48+ mkdirSync ( path , { recursive : true } ) ;
49+ Bun . spawnSync ( {
50+ cmd : [ "git" , "init" , "-b" , branch ] ,
51+ cwd : path ,
52+ stdout : "ignore" ,
53+ } ) ;
54+ }
55+
4756// Set up state path for tests
4857let stateFilePath : string ;
4958const STATE_PATH_ENV = "DEVFLOW_MAIN_AGENT_WRITE_STATE" ;
@@ -242,8 +251,8 @@ describe("MainAgentWriteGuard", () => {
242251 } ) ;
243252 } ) ;
244253
245- describe ( "subagent sessions" , ( ) => {
246- it ( "blocks git push even for registered subagent " , ( ) => {
254+ describe ( "Codex worker sessions" , ( ) => {
255+ it ( "blocks git push even for registered Codex worker " , ( ) => {
247256 startSubagent ( "worker-1" ) ;
248257 expect (
249258 shouldBlockTool (
@@ -268,15 +277,15 @@ describe("MainAgentWriteGuard", () => {
268277 ) . not . toBeNull ( ) ;
269278 } ) ;
270279
271- it ( "allows registered subagent write" , ( ) => {
280+ it ( "allows registered Codex worker write" , ( ) => {
272281 startSubagent ( "worker-1" ) ;
273282 expect ( shouldBlockTool ( "Write" , { } , "worker-1" ) ) . toBeUndefined ( ) ;
274283 expect (
275284 shouldBlockTool ( "Bash" , { command : "git add README.md" } , "worker-1" ) ,
276285 ) . toBeUndefined ( ) ;
277286 } ) ;
278287
279- it ( "blocks after subagent stop" , ( ) => {
288+ it ( "blocks after Codex worker stop" , ( ) => {
280289 startSubagent ( "worker-1" ) ;
281290 stopSubagent ( "worker-1" ) ;
282291 expect ( shouldBlockTool ( "Write" , { } , "worker-1" ) ) . not . toBeNull ( ) ;
@@ -289,7 +298,7 @@ describe("MainAgentWriteGuard", () => {
289298 ) . not . toBeNull ( ) ;
290299 } ) ;
291300
292- it ( "multiple subagent sessions do not interfere" , ( ) => {
301+ it ( "multiple Codex worker sessions do not interfere" , ( ) => {
293302 startSubagent ( "worker-1" ) ;
294303 startSubagent ( "worker-2" ) ;
295304 expect ( shouldBlockTool ( "Write" , { } , "worker-1" ) ) . toBeUndefined ( ) ;
@@ -313,7 +322,7 @@ describe("MainAgentWriteGuard", () => {
313322 ) . toBeUndefined ( ) ;
314323 } ) ;
315324
316- it ( "allows unregistered shell write when nested agent declares subagent worker context" , ( ) => {
325+ it ( "allows unregistered shell write when adapter payload declares writable worker context" , ( ) => {
317326 expect (
318327 shouldBlockTool (
319328 "Bash" ,
@@ -418,8 +427,8 @@ describe("MainAgentWriteGuard", () => {
418427 } ) ;
419428 } ) ;
420429
421- describe ( "protected branch write for registered subagent " , ( ) => {
422- it ( "blocks registered subagent write on protected branch via workdir" , ( ) => {
430+ describe ( "protected branch write for registered Codex worker " , ( ) => {
431+ it ( "blocks registered Codex worker write on protected branch via workdir" , ( ) => {
423432 startSubagent ( "worker-1" ) ;
424433
425434 const root = join ( tempDir , "pb-root" ) ;
@@ -452,6 +461,36 @@ describe("MainAgentWriteGuard", () => {
452461 expect ( decision ?. reason ) . toInclude ( "cwd" ) ;
453462 expect ( decision ?. reason ) . toInclude ( root ) ;
454463 } ) ;
464+
465+ it ( "uses git -C cwd instead of parent cwd for registered worker git writes" , ( ) => {
466+ startSubagent ( "worker-1" ) ;
467+
468+ const root = join ( tempDir , "worker-nested-git-c-root" ) ;
469+ const allowedNested = join ( root , "allowed-nested" ) ;
470+ const blockedNested = join ( root , "blocked-nested" ) ;
471+ initBranchOnlyRepo ( root , "dev" ) ;
472+ initBranchOnlyRepo ( allowedNested , "codex/topic" ) ;
473+ initBranchOnlyRepo ( blockedNested , "dev" ) ;
474+
475+ expect (
476+ shouldBlockTool (
477+ "Bash" ,
478+ { command : `git -C "${ allowedNested } " add README.md` } ,
479+ "worker-1" ,
480+ root ,
481+ ) ,
482+ ) . toBeUndefined ( ) ;
483+
484+ const decision = shouldBlockTool (
485+ "Bash" ,
486+ { command : `git -C "${ blockedNested } " add README.md` } ,
487+ "worker-1" ,
488+ root ,
489+ ) ;
490+ expect ( decision ) . not . toBeNull ( ) ;
491+ expect ( decision ?. reason ) . toInclude ( "cwd" ) ;
492+ expect ( decision ?. reason ) . toInclude ( blockedNested ) ;
493+ } ) ;
455494 } ) ;
456495
457496 describe ( "hook manifest" , ( ) => {
0 commit comments