11import assert from "node:assert/strict" ;
2- import { execFileSync } from "node:child_process" ;
2+ import { execFileSync , spawnSync } from "node:child_process" ;
33import fs from "node:fs" ;
44import os from "node:os" ;
55import path from "node:path" ;
@@ -332,6 +332,10 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
332332 " process.exit(0);" ,
333333 "}" ,
334334 "if (args[0] === 'pr' && args[1] === 'merge') {" ,
335+ " if (process.env.FAKE_GH_MERGE_RACE === '1') {" ,
336+ " process.stderr.write('base branch was modified; review and try the merge again\\n');" ,
337+ " process.exit(1);" ,
338+ " }" ,
335339 " fs.writeFileSync(process.env.FAKE_GH_MERGED_FILE, '1');" ,
336340 " process.exit(0);" ,
337341 "}" ,
@@ -382,11 +386,7 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
382386 const alreadyMergedReport = JSON . parse ( fs . readFileSync ( alreadyMergedPath , "utf8" ) ) ;
383387 alreadyMergedReport . actions [ 0 ] . commit = "d" . repeat ( 40 ) ;
384388 fs . writeFileSync ( alreadyMergedPath , JSON . stringify ( alreadyMergedReport , null , 2 ) ) ;
385- execFileSync ( process . execPath , [ "dist/repair/post-flight.js" , jobPath , resultPath ] , {
386- cwd : repoRoot ,
387- env,
388- stdio : "pipe" ,
389- } ) ;
389+ runPostFlight ( jobPath , resultPath , env , 1 ) ;
390390 const alreadyMerged = JSON . parse ( fs . readFileSync ( reportPath , "utf8" ) ) ;
391391 assert . equal ( alreadyMerged . actions [ 0 ] ?. status , "blocked" ) ;
392392 assert . equal (
@@ -405,11 +405,7 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
405405 staleAfterMerged . actions [ 0 ] . merge_preflight . validated_head_sha = staleHead ;
406406 fs . writeFileSync ( fixReportPath , JSON . stringify ( staleAfterMerged , null , 2 ) ) ;
407407
408- execFileSync ( process . execPath , [ "dist/repair/post-flight.js" , jobPath , resultPath ] , {
409- cwd : repoRoot ,
410- env,
411- stdio : "pipe" ,
412- } ) ;
408+ runPostFlight ( jobPath , resultPath , env , 1 ) ;
413409 const stalePostFlight = JSON . parse ( fs . readFileSync ( reportPath , "utf8" ) ) ;
414410 assert . equal ( stalePostFlight . actions [ 0 ] ?. status , "blocked" ) ;
415411 assert . equal (
@@ -429,11 +425,7 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
429425 fs . writeFileSync ( fixReportPath , JSON . stringify ( staleBaseReport , null , 2 ) ) ;
430426 fs . rmSync ( reportPath , { force : true } ) ;
431427 fs . rmSync ( viewCountPath , { force : true } ) ;
432- execFileSync ( process . execPath , [ "dist/repair/post-flight.js" , jobPath , resultPath ] , {
433- cwd : repoRoot ,
434- env,
435- stdio : "pipe" ,
436- } ) ;
428+ runPostFlight ( jobPath , resultPath , env , 1 ) ;
437429 const staleBasePostFlight = JSON . parse ( fs . readFileSync ( reportPath , "utf8" ) ) ;
438430 assert . equal ( staleBasePostFlight . actions [ 0 ] ?. status , "blocked" ) ;
439431 assert . equal (
@@ -445,11 +437,7 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
445437 writeMergeReports ( runDir , resultPath ) ;
446438 fs . rmSync ( reportPath , { force : true } ) ;
447439 fs . rmSync ( viewCountPath , { force : true } ) ;
448- execFileSync ( process . execPath , [ "dist/repair/post-flight.js" , jobPath , resultPath ] , {
449- cwd : repoRoot ,
450- env,
451- stdio : "pipe" ,
452- } ) ;
440+ runPostFlight ( jobPath , resultPath , env , 1 ) ;
453441 const unboundReport = JSON . parse ( fs . readFileSync ( reportPath , "utf8" ) ) ;
454442 assert . equal ( unboundReport . actions [ 0 ] ?. status , "blocked" ) ;
455443 assert . equal (
@@ -462,11 +450,12 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
462450 fs . rmSync ( reportPath , { force : true } ) ;
463451 fs . rmSync ( viewCountPath , { force : true } ) ;
464452 fs . rmSync ( commentsCountPath , { force : true } ) ;
465- execFileSync ( process . execPath , [ "dist/repair/post-flight.js" , jobPath , resultPath ] , {
466- cwd : repoRoot ,
467- env : { ...env , FAKE_GH_STRICT_BASE : "1" , FAKE_GH_LATE_SECURITY : "1" } ,
468- stdio : "pipe" ,
469- } ) ;
453+ runPostFlight (
454+ jobPath ,
455+ resultPath ,
456+ { ...env , FAKE_GH_STRICT_BASE : "1" , FAKE_GH_LATE_SECURITY : "1" } ,
457+ 1 ,
458+ ) ;
470459 const lateSecurityReport = JSON . parse ( fs . readFileSync ( reportPath , "utf8" ) ) ;
471460 assert . equal ( lateSecurityReport . actions [ 0 ] ?. status , "blocked" ) ;
472461 assert . equal (
@@ -475,6 +464,23 @@ test("merge post-flight requires exact proof and server-enforced strict base bin
475464 ) ;
476465 assert . equal ( fs . existsSync ( mergeFlagPath ) , false ) ;
477466
467+ writeMergeReports ( runDir , resultPath ) ;
468+ fs . rmSync ( reportPath , { force : true } ) ;
469+ fs . rmSync ( viewCountPath , { force : true } ) ;
470+ fs . rmSync ( commentsCountPath , { force : true } ) ;
471+ runPostFlight (
472+ jobPath ,
473+ resultPath ,
474+ { ...env , FAKE_GH_STRICT_BASE : "1" , FAKE_GH_MERGE_RACE : "1" } ,
475+ 1 ,
476+ ) ;
477+ const mergeRaceReport = JSON . parse ( fs . readFileSync ( reportPath , "utf8" ) ) ;
478+ assert . equal ( mergeRaceReport . outcome , "requeue" ) ;
479+ assert . equal ( mergeRaceReport . actions [ 0 ] ?. status , "blocked" ) ;
480+ assert . equal ( mergeRaceReport . actions [ 0 ] ?. retry_recommended , true ) ;
481+ assert . match ( mergeRaceReport . actions [ 0 ] ?. reason , / m e r g e a t t e m p t n e e d s b r a n c h r e f r e s h / ) ;
482+ assert . equal ( fs . existsSync ( mergeFlagPath ) , false ) ;
483+
478484 writeMergeReports ( runDir , resultPath ) ;
479485 fs . rmSync ( reportPath , { force : true } ) ;
480486 fs . rmSync ( viewCountPath , { force : true } ) ;
@@ -595,7 +601,7 @@ test("post-flight keeps no-timestamp pending duplicate checks visible", () => {
595601 }
596602} ) ;
597603
598- test ( "post-flight exports a blocked report even when the command exits successfully " , ( ) => {
604+ test ( "post-flight exports a blocked report before exiting unsuccessfully " , ( ) => {
599605 const tmp = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "clawsweeper-post-flight-" ) ) ;
600606 const jobPath = path . join ( tmp , "job.md" ) ;
601607 const runDir = path . join ( tmp , "run" ) ;
@@ -620,16 +626,17 @@ test("post-flight exports a blocked report even when the command exits successfu
620626 ) ;
621627
622628 try {
623- execFileSync ( process . execPath , [ "dist/repair/post-flight.js" , jobPath , resultPath ] , {
624- cwd : repoRoot ,
625- env : {
629+ runPostFlight (
630+ jobPath ,
631+ resultPath ,
632+ {
626633 ...process . env ,
627634 CLAWSWEEPER_ALLOW_EXECUTE : "1" ,
628635 CLAWSWEEPER_ALLOWED_OWNER : "openclaw" ,
629636 GITHUB_OUTPUT : outputPath ,
630637 } ,
631- stdio : "pipe" ,
632- } ) ;
638+ 1 ,
639+ ) ;
633640
634641 const report = JSON . parse ( fs . readFileSync ( reportPath , "utf8" ) ) ;
635642 assert . equal ( report . outcome , "blocked" ) ;
@@ -640,6 +647,22 @@ test("post-flight exports a blocked report even when the command exits successfu
640647 }
641648} ) ;
642649
650+ function runPostFlight (
651+ jobPath : string ,
652+ resultPath : string ,
653+ env : NodeJS . ProcessEnv ,
654+ expectedStatus : number ,
655+ ) {
656+ const child = spawnSync ( process . execPath , [ "dist/repair/post-flight.js" , jobPath , resultPath ] , {
657+ cwd : repoRoot ,
658+ env,
659+ encoding : "utf8" ,
660+ stdio : "pipe" ,
661+ } ) ;
662+ assert . equal ( child . status , expectedStatus , child . stderr || child . stdout ) ;
663+ return child ;
664+ }
665+
643666function writeIssueImplementationJob ( jobPath : string ) {
644667 fs . writeFileSync (
645668 jobPath ,
0 commit comments