1- import { readFileSync , writeFileSync } from "node:fs" ;
1+ import { existsSync , readFileSync , writeFileSync } from "node:fs" ;
22import { join } from "node:path" ;
33import { ACTION_EVENT_REASON_CODES , ACTION_EVENT_STATUSES } from "./action-ledger.js" ;
44import type { Args } from "./clawsweeper-args.js" ;
@@ -53,6 +53,21 @@ function reviewStartLeaseCommentUpdatedAt(
5353 return undefined ;
5454}
5555
56+ export function localExactBootstrapReviewCommentBody (
57+ markdown : string ,
58+ item : Pick < Item , "repo" | "number" > ,
59+ frontMatterValue : ( markdown : string , key : string ) => string | undefined ,
60+ renderReviewCommentFromReport : ( markdown : string , reason : "none" ) => string ,
61+ ) : string {
62+ if (
63+ frontMatterValue ( markdown , "repository" ) ?. toLowerCase ( ) !== item . repo . toLowerCase ( ) ||
64+ frontMatterValue ( markdown , "number" ) !== String ( item . number )
65+ ) {
66+ return "" ;
67+ }
68+ return renderReviewCommentFromReport ( markdown , "none" ) ;
69+ }
70+
5671export function restoreVerifiedMaintainerPullRequestAuthorAssociation (
5772 item : Pick < Item , "kind" | "author" | "authorAssociation" | "labels" > ,
5873 repositoryPermission : ( author : string ) => string | null ,
@@ -100,6 +115,7 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
100115 displayPath,
101116 enforceExpectedIssueSourceRevision,
102117 exactLocalReviewNoCandidateError,
118+ extractClawSweeperReviewCommentBody,
103119 existingReview,
104120 extractLatestClawSweeperReview,
105121 fetchIssueReviewComments,
@@ -115,6 +131,8 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
115131 itemContentDigest,
116132 itemSnapshotHash,
117133 liveClawSweeperReviewDigest,
134+ localExactReviewHistoryPath,
135+ localRangeHistoryApplies,
118136 makeTreeReadOnly,
119137 markdownFor,
120138 postReviewStartStatusComment,
@@ -125,6 +143,7 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
125143 recordReviewLogPublication,
126144 refreshRelatedItemsContext,
127145 replaceFrontMatterValue,
146+ renderReviewCommentFromReport,
128147 reportFileName,
129148 reportReviewFindings,
130149 restoreTreeModes,
@@ -163,6 +182,7 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
163182 expectedSourceRevision,
164183 allowClosed,
165184 localRangeData,
185+ localReviewHistoryPath,
166186 coordinationHeldPath,
167187 shardIndex,
168188 shardCount,
@@ -349,6 +369,32 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
349369 `[review] ${ new Date ( ) . toISOString ( ) } shard=${ shardIndex } /${ shardCount } start #${ item . number } (${ completed + 1 } /${ candidates . length } )` ,
350370 ) ;
351371 }
372+ const itemLocalReviewHistoryPath = localRangeData
373+ ? localReviewHistoryPath
374+ : localOnly
375+ ? localExactReviewHistoryPath ( artifactDir , item . repo , item . number )
376+ : null ;
377+ let previousLocalReviewCommentBody =
378+ itemLocalReviewHistoryPath && existsSync ( itemLocalReviewHistoryPath )
379+ ? readFileSync ( itemLocalReviewHistoryPath , "utf8" )
380+ : "" ;
381+ if (
382+ ! previousLocalReviewCommentBody &&
383+ localOnly &&
384+ ! localRangeData &&
385+ existsSync ( join ( artifactDir , reportFileName ( item . repo , item . number ) ) )
386+ ) {
387+ const bootstrapReport = readFileSync (
388+ join ( artifactDir , reportFileName ( item . repo , item . number ) ) ,
389+ "utf8" ,
390+ ) ;
391+ previousLocalReviewCommentBody = localExactBootstrapReviewCommentBody (
392+ bootstrapReport ,
393+ item ,
394+ frontMatterValue ,
395+ renderReviewCommentFromReport ,
396+ ) ;
397+ }
352398 const existingPriorReview = localRangeData ? null : existingReview ( item , itemsDir ) ;
353399 const lastFullReviewBulkFilerState = frontMatterValue (
354400 existingPriorReview ?. markdown ?? "" ,
@@ -735,6 +781,26 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
735781 reviewCacheDigest : true ,
736782 reviewCacheGitDir : openclawDir ,
737783 } ) ;
784+ if ( previousLocalReviewCommentBody ) {
785+ const previousLocalReview = extractClawSweeperReviewCommentBody (
786+ previousLocalReviewCommentBody ,
787+ ) ;
788+ const hasCompletedLocalReview =
789+ previousLocalReview . completedReviewCycles > 0 &&
790+ / ^ [ 0 - 9 a - f ] { 40 } $ / iu. test ( previousLocalReview . reviewedSha ?? "" ) ;
791+ const appliesToRange =
792+ ! localRangeData ||
793+ localRangeHistoryApplies (
794+ openclawDir ,
795+ previousLocalReview ?. reviewedSha ?? null ,
796+ localRangeData . headSha ,
797+ ) ;
798+ if ( hasCompletedLocalReview && appliesToRange ) {
799+ context . previousClawSweeperReview = previousLocalReview ;
800+ } else {
801+ previousLocalReviewCommentBody = "" ;
802+ }
803+ }
738804 if ( bulkFilerDetection . context ) context . bulkFiler = bulkFilerDetection . context ;
739805 const contextElapsedMs = Date . now ( ) - contextStartedAt ;
740806 const contextItemUpdatedAt = stringOrUndefined ( asRecord ( context . issue ) . updatedAt ) ;
@@ -1383,9 +1449,7 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
13831449 const action = reviewActionForDecision ( { item, decision, git, runtime } ) ;
13841450 structuralRecord = refreshStructuralRecordForVerdict ( ) ;
13851451 const reportPath = join ( artifactDir , reportFileName ( item . repo , item . number ) ) ;
1386- writeFileSync (
1387- reportPath ,
1388- markdownFor ( {
1452+ const reportMarkdown = markdownFor ( {
13891453 item,
13901454 context,
13911455 decision,
@@ -1404,9 +1468,23 @@ export function createReviewCommandWorkflow(dependencies: CreateReviewCommandWor
14041468 reviewLeaseCommentId : acquiredReviewLease . commentId ,
14051469 }
14061470 : { } ) ,
1407- } ) ,
1408- "utf8" ,
1409- ) ;
1471+ } ) ;
1472+ writeFileSync ( reportPath , reportMarkdown , "utf8" ) ;
1473+ if ( itemLocalReviewHistoryPath ) {
1474+ const nextLocalReviewCommentBody =
1475+ frontMatterValue ( reportMarkdown , "review_status" ) === "complete"
1476+ ? renderReviewCommentFromReport (
1477+ reportMarkdown ,
1478+ "none" ,
1479+ previousLocalReviewCommentBody
1480+ ? { previousReviewCommentBody : previousLocalReviewCommentBody }
1481+ : undefined ,
1482+ )
1483+ : previousLocalReviewCommentBody ;
1484+ if ( nextLocalReviewCommentBody ) {
1485+ writeFileSync ( itemLocalReviewHistoryPath , nextLocalReviewCommentBody , "utf8" ) ;
1486+ }
1487+ }
14101488 recordReviewLogPublication ( {
14111489 ledger : reviewLedger ,
14121490 item,
0 commit comments