@@ -2145,9 +2145,11 @@ function editValidatePrepareMerge({
21452145 } ) ;
21462146 }
21472147
2148- const firstCheckpoint = commitCheckpointIfNeeded ( {
2148+ const firstCheckpoint = commitRepairCheckpointIfNeeded ( {
2149+ fixArtifact,
21492150 targetDir,
21502151 message : fixArtifact . pr_title ,
2152+ phase : "initial" ,
21512153 trailers : mode === "replacement" ? coAuthorTrailers ( contributorCredits ) : [ ] ,
21522154 } ) ;
21532155 if ( firstCheckpoint ) {
@@ -2176,9 +2178,11 @@ function editValidatePrepareMerge({
21762178 baseBranch,
21772179 sourceHead : repairDeltaBaseHead ,
21782180 onReviewFix : ( reviewAttempt : JsonValue ) => {
2179- const checkpoint = commitCheckpointIfNeeded ( {
2181+ const checkpoint = commitRepairCheckpointIfNeeded ( {
2182+ fixArtifact,
21802183 targetDir,
21812184 message : `fix(clawsweeper): address review for ${ result . cluster_id } (${ reviewAttempt } )` ,
2185+ phase : `review-fix-${ reviewAttempt } ` ,
21822186 trailers : mode === "replacement" ? coAuthorTrailers ( contributorCredits ) : [ ] ,
21832187 } ) ;
21842188 if ( checkpoint ) {
@@ -2207,9 +2211,11 @@ function editValidatePrepareMerge({
22072211 headSha : currentHead ( targetDir ) ,
22082212 } ) ;
22092213 if ( sync . status === "already-current" ) break ;
2210- const checkpoint = commitCheckpointIfNeeded ( {
2214+ const checkpoint = commitRepairCheckpointIfNeeded ( {
2215+ fixArtifact,
22112216 targetDir,
22122217 message : `fix(clawsweeper): reconcile ${ result . cluster_id } with main (${ attempt } )` ,
2218+ phase : `base-sync-${ attempt } ` ,
22132219 trailers : mode === "replacement" ? coAuthorTrailers ( contributorCredits ) : [ ] ,
22142220 } ) ;
22152221 if ( checkpoint ) {
@@ -2227,9 +2233,11 @@ function editValidatePrepareMerge({
22272233 break ;
22282234 }
22292235 }
2230- const finalCheckpoint = commitCheckpointIfNeeded ( {
2236+ const finalCheckpoint = commitRepairCheckpointIfNeeded ( {
2237+ fixArtifact,
22312238 targetDir,
22322239 message : `fix(clawsweeper): finalize ${ result . cluster_id } ` ,
2240+ phase : "finalize" ,
22332241 trailers : mode === "replacement" ? coAuthorTrailers ( contributorCredits ) : [ ] ,
22342242 } ) ;
22352243 if ( finalCheckpoint ) {
@@ -3300,15 +3308,87 @@ function checkoutRecoverableReplacementBranch({
33003308 return { resumed : false , branch } ;
33013309}
33023310
3303- function commitCheckpointIfNeeded ( { targetDir, message, trailers = [ ] } : LooseRecord ) {
3304- if ( ! run ( "git" , [ "status" , "--porcelain" ] , { cwd : targetDir } ) . trim ( ) ) return "" ;
3311+ function commitRepairCheckpointIfNeeded ( {
3312+ fixArtifact,
3313+ targetDir,
3314+ message,
3315+ phase,
3316+ trailers = [ ] ,
3317+ } : LooseRecord ) {
3318+ const status = run ( "git" , [ "status" , "--porcelain" ] , { cwd : targetDir } ) . trim ( ) ;
3319+ if ( ! status ) return "" ;
3320+ enforceRepairCheckpointContract ( { fixArtifact, phase, status } ) ;
33053321 run ( "git" , [ "add" , "--all" ] , { cwd : targetDir } ) ;
33063322 const args = [ "commit" , "-m" , message ] ;
33073323 for ( const trailer of uniqueStrings ( trailers ) ) args . push ( "-m" , trailer ) ;
33083324 runGitNetwork ( args , targetDir ) ;
33093325 return run ( "git" , [ "rev-parse" , "HEAD" ] , { cwd : targetDir } ) . trim ( ) ;
33103326}
33113327
3328+ function enforceRepairCheckpointContract ( { fixArtifact, phase, status } : LooseRecord ) {
3329+ const mustTouch = repairCheckpointMustTouchFiles ( fixArtifact ) ;
3330+ if ( mustTouch . length === 0 ) return ;
3331+
3332+ const changedFiles = changedFilesFromPorcelainStatus ( String ( status ) ) ;
3333+ if (
3334+ changedFiles . some ( ( file ) =>
3335+ mustTouch . some ( ( expected ) => changedFileMatchesContract ( file , expected ) ) ,
3336+ )
3337+ ) {
3338+ return ;
3339+ }
3340+
3341+ throw new Error (
3342+ [
3343+ `repair checkpoint contract rejected ${ String ( phase || "checkpoint" ) } : no must_touch file changed` ,
3344+ `must_touch=${ mustTouch . join ( ", " ) } ` ,
3345+ `changed_files=${ changedFiles . join ( ", " ) || "none" } ` ,
3346+ ] . join ( "; " ) ,
3347+ ) ;
3348+ }
3349+
3350+ function repairCheckpointMustTouchFiles ( fixArtifact : LooseRecord ) : string [ ] {
3351+ const candidates = [
3352+ ...jsonStringArray ( fixArtifact . must_touch ) ,
3353+ ...jsonStringArray ( fixArtifact . must_touch_files ) ,
3354+ ] ;
3355+ return uniqueStrings ( candidates . map ( normalizeRepairContractPath ) . filter ( Boolean ) ) ;
3356+ }
3357+
3358+ function jsonStringArray ( value : JsonValue ) : string [ ] {
3359+ return Array . isArray ( value ) ? value . map ( ( entry ) => String ( entry ) ) : [ ] ;
3360+ }
3361+
3362+ function changedFilesFromPorcelainStatus ( status : string ) : string [ ] {
3363+ return uniqueStrings (
3364+ status
3365+ . split ( / \r ? \n / )
3366+ . flatMap ( ( line ) => porcelainChangedPaths ( line ) )
3367+ . map ( normalizeRepairContractPath )
3368+ . filter ( Boolean ) ,
3369+ ) ;
3370+ }
3371+
3372+ function porcelainChangedPaths ( line : string ) : string [ ] {
3373+ if ( line . length < 4 ) return [ ] ;
3374+ const body = line . slice ( 3 ) . trim ( ) ;
3375+ if ( ! body ) return [ ] ;
3376+ const renamed = body . split ( " -> " ) ;
3377+ return renamed . length === 2 ? [ renamed [ 1 ] ?? "" ] : [ body ] ;
3378+ }
3379+
3380+ function changedFileMatchesContract ( changedFile : string , expected : string ) {
3381+ return changedFile === expected || changedFile . startsWith ( `${ expected . replace ( / \/ $ / , "" ) } /` ) ;
3382+ }
3383+
3384+ function normalizeRepairContractPath ( value : JsonValue ) : string {
3385+ const pathValue = String ( value ?? "" ) . trim ( ) ;
3386+ if ( ! pathValue || pathValue . startsWith ( "/" ) || pathValue . includes ( "\0" ) ) return "" ;
3387+ if ( / [ ` $ ; & | < > ( ) [ \] { } * ? ~ ] / . test ( pathValue ) ) return "" ;
3388+ if ( pathValue . split ( / [ \\ / ] / ) . includes ( ".." ) ) return "" ;
3389+ return pathValue . replace ( / ^ \. \/ / , "" ) ;
3390+ }
3391+
33123392function pushRecoverableBranch ( { targetDir, branch } : LooseRecord ) {
33133393 assertIssueImplementationNotPaused ( ) ;
33143394 const remoteSha = remoteBranchSha ( { targetDir, branch } ) ;
0 commit comments