@@ -13,6 +13,33 @@ const DEFAULT_IGNORED_CHECKS = [
1313] ;
1414const TRANSIENT_CANCELLED_CHECKS = new Set ( [ "real behavior proof" ] ) ;
1515
16+ export function dispatchClaimLookupKeys ( entry : LooseRecord ) {
17+ const keys : string [ ] = [ ] ;
18+ const commentId = String ( entry . comment_id ?? "" ) . trim ( ) ;
19+ const commentUpdatedAt = String ( entry . comment_updated_at ?? "" ) . trim ( ) ;
20+ if ( commentId && commentUpdatedAt ) keys . push ( `comment:${ commentId } :${ commentUpdatedAt } ` ) ;
21+ const idempotencyKey = String ( entry . idempotency_key ?? "" ) . trim ( ) ;
22+ if ( idempotencyKey ) keys . push ( `idempotency:${ idempotencyKey } ` ) ;
23+ return keys ;
24+ }
25+
26+ export function dispatchReceiptKeyMaterial ( entry : LooseRecord , claim : LooseRecord | null ) {
27+ const idempotencyKey = String ( entry . idempotency_key ?? entry . comment_version_key ?? "unknown" ) ;
28+ if ( entry . automation_source !== "repair_loop_label_sweep" ) return idempotencyKey ;
29+ const attempt = String (
30+ claim ?. processed_at ?? entry . processed_at ?? entry . comment_updated_at ?? "unknown-attempt" ,
31+ ) ;
32+ return `${ idempotencyKey } :${ attempt } ` ;
33+ }
34+
35+ export function hasSuccessfulDispatchExecutionJob ( jobs : LooseRecord [ ] , requiredJobName : string ) {
36+ return jobs . some (
37+ ( job ) =>
38+ String ( job . name ?? "" ) === requiredJobName &&
39+ String ( job . conclusion ?? "" ) . toLowerCase ( ) === "success" ,
40+ ) ;
41+ }
42+
1643export function summarizeChecks ( checks : LooseRecord [ ] ) {
1744 const ignored = ignoredCheckNames ( ) ;
1845 const latestChecks = latestCheckRuns ( checks ) ;
@@ -115,6 +142,49 @@ export function shouldSuppressProcessedCommentVersion(entry: LooseRecord) {
115142 return true ;
116143}
117144
145+ export function dispatchClaimDecision ( {
146+ claim,
147+ runs,
148+ expectedTitle,
149+ nowMs = Date . now ( ) ,
150+ graceMs = 300_000 ,
151+ } : {
152+ claim : LooseRecord | null ;
153+ runs : LooseRecord [ ] ;
154+ expectedTitle : string ;
155+ nowMs ?: number ;
156+ graceMs ?: number ;
157+ } ) {
158+ if ( ! claim ) return { action : "dispatch" , run : null } ;
159+ const normalizedGraceMs = Number . isFinite ( graceMs ) ? Math . max ( 0 , graceMs ) : 300_000 ;
160+ const claimedAtMs = Date . parse ( String ( claim . processed_at ?? "" ) ) ;
161+ const matchingRuns = runs . filter ( ( run ) => {
162+ if ( String ( run . display_title ?? run . displayTitle ?? "" ) !== expectedTitle ) return false ;
163+ const createdAtMs = Date . parse ( String ( run . created_at ?? run . createdAt ?? "" ) ) ;
164+ return (
165+ Number . isFinite ( claimedAtMs ) &&
166+ Number . isFinite ( createdAtMs ) &&
167+ createdAtMs >= claimedAtMs - 5_000
168+ ) ;
169+ } ) ;
170+ const successfulRun = matchingRuns . find (
171+ ( run ) =>
172+ String ( run . conclusion ?? "" ) . toLowerCase ( ) === "success" &&
173+ run . dispatch_execution_verified !== false ,
174+ ) ;
175+ if ( successfulRun ) return { action : "recover" , run : successfulRun } ;
176+ const activeRun = matchingRuns . find ( ( run ) =>
177+ [ "queued" , "in_progress" , "waiting" , "pending" , "requested" ] . includes (
178+ String ( run . status ?? "" ) . toLowerCase ( ) ,
179+ ) ,
180+ ) ;
181+ if ( activeRun ) return { action : "wait" , run : null } ;
182+ if ( Number . isFinite ( claimedAtMs ) && nowMs - claimedAtMs >= normalizedGraceMs ) {
183+ return { action : "dispatch" , run : null } ;
184+ }
185+ return { action : "wait" , run : null } ;
186+ }
187+
118188export function sortCommentsForRouting ( comments : LooseRecord [ ] ) {
119189 return [ ...comments ] . sort ( ( left : LooseRecord , right : LooseRecord ) => {
120190 const leftTime = commentRoutingTime ( left ) ;
@@ -244,7 +314,9 @@ export function readLedger(file: JsonValue) {
244314
245315export function appendLedger ( current : LooseRecord , entries : LooseRecord [ ] ) {
246316 const compact = entries
247- . filter ( ( entry : JsonValue ) => [ "executed" , "skipped" , "waiting" ] . includes ( entry . status ) )
317+ . filter ( ( entry : JsonValue ) =>
318+ [ "claimed" , "executed" , "skipped" , "waiting" ] . includes ( entry . status ) ,
319+ )
248320 . filter ( ( entry : JsonValue ) => ! isNoopSkip ( entry ) )
249321 . map ( ( entry : JsonValue ) => {
250322 const actions = compactLedgerActions ( entry . actions ) ;
@@ -271,7 +343,7 @@ export function appendLedger(current: LooseRecord, entries: LooseRecord[]) {
271343 expected_head_sha : entry . expected_head_sha ?? null ,
272344 finding_id : entry . finding_id ?? null ,
273345 status : entry . status ,
274- processed_at : new Date ( ) . toISOString ( ) ,
346+ processed_at : entry . processed_at ?? new Date ( ) . toISOString ( ) ,
275347 target : entry . target
276348 ? {
277349 kind : entry . target . kind ,
@@ -313,10 +385,20 @@ function isNoopSkip(entry: LooseRecord) {
313385}
314386
315387function stableLedgerEntry ( entry : LooseRecord ) {
316- return JSON . stringify ( { ...entry , processed_at : null } ) ;
388+ return JSON . stringify ( {
389+ ...entry ,
390+ processed_at : entry . status === "claimed" ? entry . processed_at : null ,
391+ } ) ;
317392}
318393
319394function ledgerEntryKey ( entry : LooseRecord ) {
395+ if (
396+ ! entry . comment_version_key &&
397+ entry . automation_source === "repair_loop_label_sweep" &&
398+ entry . idempotency_key
399+ ) {
400+ return `idempotency:${ entry . idempotency_key } ` ;
401+ }
320402 return (
321403 entry . comment_version_key ??
322404 `${ entry . comment_id ?? "unknown" } :${ entry . comment_updated_at ?? "unknown" } `
0 commit comments