11import assert from "node:assert/strict" ;
22import { execFileSync , spawn , spawnSync } from "node:child_process" ;
3+ import crypto from "node:crypto" ;
34import fs from "node:fs" ;
45import os from "node:os" ;
56import path from "node:path" ;
@@ -15,6 +16,7 @@ const helperRoot = path.dirname(fileURLToPath(import.meta.url));
1516export const AUTOMERGE_E2E_SCENARIOS = [
1617 "approve-intent-persistence" ,
1718 "completed-verdict-resume" ,
19+ "completed-verdict-source-drift" ,
1820 "dependency-setup-mutation" ,
1921 "happy-path" ,
2022 "pending-checks" ,
@@ -257,6 +259,7 @@ export function runAutomergeE2E({
257259 if (
258260 scenario === "approve-intent-persistence" ||
259261 scenario === "completed-verdict-resume" ||
262+ scenario === "completed-verdict-source-drift" ||
260263 scenario === "resume-intent-persistence"
261264 ) {
262265 const activeJob = path . join (
@@ -313,6 +316,41 @@ export function runAutomergeE2E({
313316 attemptId : String ( verdictHandoff . client_payload . attempt_id ) ,
314317 } ) ;
315318 completedVerdictAlreadyRouted = true ;
319+ } else if ( scenario === "completed-verdict-source-drift" ) {
320+ const beforeCommand = JSON . parse ( fs . readFileSync ( statePath , "utf8" ) ) ;
321+ const staleVerdictId = addExactHeadVerdict (
322+ statePath ,
323+ repairedHead ,
324+ fixtureSourceRevision ( beforeCommand ) ,
325+ ) ;
326+ const commandId = addMaintainerAutomergeCommand ( statePath ) ;
327+ runCommentRouterExact ( runtimeRoot , baseEnv , artifacts , "08-comment-router-source-drift" , {
328+ commentId : commandId ,
329+ } ) ;
330+ const refreshedState = JSON . parse ( fs . readFileSync ( statePath , "utf8" ) ) ;
331+ assert . equal (
332+ refreshedState . dispatches . some (
333+ ( dispatch ) =>
334+ dispatch . event_type === "clawsweeper_comment" &&
335+ dispatch . client_payload ?. comment_id === String ( staleVerdictId ) ,
336+ ) ,
337+ false ,
338+ "a source-stale exact-head verdict must not be replayed" ,
339+ ) ;
340+ assert . equal (
341+ refreshedState . dispatches . some ( ( dispatch ) => dispatch . event_type === "clawsweeper_item" ) ,
342+ true ,
343+ "source drift must queue a fresh exact-head review" ,
344+ ) ;
345+ const freshVerdictId = addExactHeadVerdict (
346+ statePath ,
347+ repairedHead ,
348+ fixtureSourceRevision ( refreshedState ) ,
349+ ) ;
350+ runCommentRouterExact ( runtimeRoot , baseEnv , artifacts , "10-comment-router-fresh-verdict" , {
351+ commentId : freshVerdictId ,
352+ } ) ;
353+ completedVerdictAlreadyRouted = true ;
316354 } else if ( scenario === "resume-intent-persistence" ) {
317355 addMaintainerAutomergeCommand ( statePath ) ;
318356 runCommentRouter ( runtimeRoot , baseEnv , artifacts , "08-comment-router-resume-command" ) ;
@@ -579,12 +617,12 @@ function createCandidateRuntime(root, candidateRoot) {
579617 return runtime ;
580618}
581619
582- function addExactHeadVerdict ( statePath , headSha ) {
620+ function addExactHeadVerdict ( statePath , headSha , sourceRevision = null ) {
583621 const state = JSON . parse ( fs . readFileSync ( statePath , "utf8" ) ) ;
584622 const now = new Date ( ) . toISOString ( ) ;
585623 state . comments . push ( {
586624 id : state . nextCommentId ++ ,
587- body : `ClawSweeper review passed.\n<!-- clawsweeper-verdict:pass item=${ state . pr . number } sha=${ headSha } reviewed_at=${ now } -->` ,
625+ body : `ClawSweeper review passed.\n<!-- clawsweeper-verdict:pass item=${ state . pr . number } sha=${ headSha } ${ sourceRevision ? ` source_revision= ${ sourceRevision } ` : "" } reviewed_at=${ now } -->` ,
588626 issue_url : `https://api.github.com/repos/${ state . repo } /issues/${ state . pr . number } ` ,
589627 html_url : `https://github.com/${ state . repo } /pull/${ state . pr . number } #issuecomment-${ state . nextCommentId - 1 } ` ,
590628 user : { id : 1 , login : "clawsweeper[bot]" } ,
@@ -596,6 +634,41 @@ function addExactHeadVerdict(statePath, headSha) {
596634 return state . nextCommentId - 1 ;
597635}
598636
637+ function fixtureSourceRevision ( state ) {
638+ const snapshot = {
639+ title : state . pr . title ,
640+ body : state . pr . body ,
641+ labels : state . pr . labels
642+ . map ( ( label ) => String ( label ) . trim ( ) . toLowerCase ( ) )
643+ . filter ( ( label ) => ! label . startsWith ( "clawsweeper:" ) )
644+ . sort ( ) ,
645+ comments : state . comments
646+ . filter (
647+ ( comment ) =>
648+ ! [
649+ "clawsweeper" ,
650+ "clawsweeper[bot]" ,
651+ "openclaw-clawsweeper" ,
652+ "openclaw-clawsweeper[bot]" ,
653+ ] . includes (
654+ String ( comment . user ?. login ?? "" )
655+ . trim ( )
656+ . toLowerCase ( ) ,
657+ ) ,
658+ )
659+ . map ( ( comment ) => ( {
660+ id : String ( comment . id ?? "" ) ,
661+ author : String ( comment . user ?. login ?? "" ) ,
662+ body : String ( comment . body ?? "" ) ,
663+ updated_at : String ( comment . updated_at ?? comment . created_at ?? "" ) ,
664+ } ) )
665+ . sort ( ( left , right ) =>
666+ `${ left . id } :${ left . updated_at } ` . localeCompare ( `${ right . id } :${ right . updated_at } ` ) ,
667+ ) ,
668+ } ;
669+ return crypto . createHash ( "sha256" ) . update ( JSON . stringify ( snapshot ) ) . digest ( "hex" ) ;
670+ }
671+
599672function addMaintainerAutomergeCommand ( statePath ) {
600673 const state = JSON . parse ( fs . readFileSync ( statePath , "utf8" ) ) ;
601674 addFixtureComment ( statePath , {
0 commit comments