@@ -2,6 +2,7 @@ import type {
22 RegressionAssessment ,
33 RegressionProvenanceCandidate ,
44 RegressionSupportingEvidence ,
5+ SuspectedRegressionProvenance ,
56 VerifiedRegressionProvenance ,
67} from "./clawsweeper-types.js" ;
78
@@ -20,11 +21,17 @@ const regressionSupportingEvidence = new Set<RegressionSupportingEvidence>([
2021
2122export interface RegressionProvenanceVerifierDependencies {
2223 fetchPull : ( repo : string , number : number ) => unknown ;
24+ fetchPullDiff : ( repo : string , number : number ) => string ;
2325 runGit : ( args : string [ ] , options : { cwd : string ; env : NodeJS . ProcessEnv } ) => string ;
2426}
2527
2628export interface VerifyRegressionProvenanceOptions {
27- candidate : RegressionProvenanceCandidate | VerifiedRegressionProvenance | null | undefined ;
29+ candidate :
30+ | RegressionProvenanceCandidate
31+ | VerifiedRegressionProvenance
32+ | SuspectedRegressionProvenance
33+ | null
34+ | undefined ;
2835 item : { repo : string ; number : number } ;
2936 checkoutDir : string ;
3037 targetBranch : string | undefined ;
@@ -33,25 +40,37 @@ export interface VerifyRegressionProvenanceOptions {
3340
3441type VerifiedPullMetadata = {
3542 mergedAt : string ;
43+ headSha : string ;
3644} ;
3745
3846/**
3947 * Independently verifies the only public predecessor-provenance form we
40- * support: a reviewed source line blames exactly to a PR's merge commit.
48+ * support: a reviewed source line either blames exactly to a PR's merge
49+ * commit, or retains a cautiously labeled non-causal relationship after the
50+ * conservative rewrite-equivalence checks below.
4151 *
4252 * This module never accepts a command from model output. It executes only the
4353 * fixed read-only Git commands below, after rejecting malformed candidates.
4454 */
4555export function createRegressionProvenanceVerifier ( {
4656 fetchPull,
57+ fetchPullDiff,
4758 runGit,
4859} : RegressionProvenanceVerifierDependencies ) {
49- function verify ( options : VerifyRegressionProvenanceOptions ) : VerifiedRegressionProvenance | null {
60+ function verify (
61+ options : VerifyRegressionProvenanceOptions ,
62+ ) : VerifiedRegressionProvenance | SuspectedRegressionProvenance | null {
5063 const candidate = normalizeCandidate ( options . candidate , options . item ) ;
5164 const reviewedCommitShas = options . reviewedCommitShas
5265 . map ( ( sha ) => fullSha ( sha ?? "" ) )
5366 . filter ( ( sha ) : sha is string => sha !== null ) ;
54- if ( ! candidate || ! isSafeTargetBranch ( options . targetBranch ) || ! reviewedCommitShas . length ) {
67+ const reviewedBaseCommitSha = fullSha ( options . reviewedCommitShas [ 0 ] ?? "" ) ;
68+ if (
69+ ! candidate ||
70+ ! isSafeTargetBranch ( options . targetBranch ) ||
71+ ! reviewedCommitShas . length ||
72+ ! reviewedBaseCommitSha
73+ ) {
5574 return null ;
5675 }
5776
@@ -87,13 +106,54 @@ export function createRegressionProvenanceVerifier({
87106 ] ,
88107 { cwd : options . checkoutDir , env } ,
89108 ) ;
90- if ( hasBlameBoundary ( blame ) || blamedSha ( blame ) !== candidate . mergeCommitSha ) return null ;
109+ const sourceCommitSha = blamedSha ( blame ) ;
110+ if ( hasBlameBoundary ( blame ) || ! sourceCommitSha ) return null ;
111+
112+ const sourceAuthor = blamedAuthor ( blame ) ;
113+
114+ if ( sourceCommitSha !== candidate . mergeCommitSha ) {
115+ if ( ! sourceAuthor || ! isSafeSourceAuthor ( sourceAuthor ) ) return null ;
116+ runGit ( [ "merge-base" , "--is-ancestor" , sourceCommitSha , reviewedBaseCommitSha ] , {
117+ cwd : options . checkoutDir ,
118+ env,
119+ } ) ;
120+ let rewriteEquivalent = false ;
121+ try {
122+ rewriteEquivalent = isConservativeRewriteEquivalent ( {
123+ candidate,
124+ pull,
125+ sourceCommitSha,
126+ sourceAuthor,
127+ pullDiff : fetchPullDiff ( candidate . repo , candidate . pullRequestNumber ) ,
128+ checkoutDir : options . checkoutDir ,
129+ env,
130+ runGit,
131+ } ) ;
132+ } catch {
133+ // The local blame result remains useful even when the optional
134+ // canonical-PR diff cannot be fetched or compared. Fail closed only
135+ // for the related-PR association.
136+ }
137+ return {
138+ evidenceType : rewriteEquivalent ? "rewrite_equivalent" : "source_line" ,
139+ sourceCommitSha,
140+ sourceAuthor,
141+ sourcePath : candidate . sourcePath ,
142+ sourceLine : candidate . sourceLine ,
143+ relatedPullRequestNumber : rewriteEquivalent ? candidate . pullRequestNumber : null ,
144+ relatedPullRequestUrl : rewriteEquivalent ? candidate . pullRequestUrl : null ,
145+ relatedRepo : rewriteEquivalent ? candidate . repo : null ,
146+ } ;
147+ }
91148
92149 return {
93150 ...candidate ,
94151 evidenceType : "blame_to_merge_commit" ,
95152 mergedAt : pull . mergedAt ,
96153 reviewedCommitSha : checkoutHeadSha ,
154+ ...( sourceAuthor && isSafeSourceAuthor ( sourceAuthor )
155+ ? { sourceCommitSha : candidate . mergeCommitSha , sourceAuthor }
156+ : { } ) ,
97157 } ;
98158 } catch {
99159 // Metadata, checkout history, and tracked-path failures are unknown, not
@@ -116,13 +176,72 @@ export function isVerifiedRegressionProvenance(
116176 typeof candidate . mergedAt === "string" &&
117177 isIsoTimestamp ( candidate . mergedAt ) &&
118178 typeof candidate . reviewedCommitSha === "string" &&
119- fullSha ( candidate . reviewedCommitSha ) !== null
179+ fullSha ( candidate . reviewedCommitSha ) !== null &&
180+ ( ( candidate . sourceCommitSha === undefined && candidate . sourceAuthor === undefined ) ||
181+ ( typeof candidate . sourceCommitSha === "string" &&
182+ fullSha ( candidate . sourceCommitSha ) !== null &&
183+ fullSha ( candidate . sourceCommitSha ) === fullSha ( candidate . mergeCommitSha ?? "" ) &&
184+ typeof candidate . sourceAuthor === "string" &&
185+ isSafeSourceAuthor ( candidate . sourceAuthor ) ) )
120186 ) ;
121187}
122188
123- export function regressionProvenancePublicLine ( value : unknown ) : string | null {
124- if ( ! isVerifiedRegressionProvenance ( value ) ) return null ;
125- return `Verified regression provenance: [#${ value . pullRequestNumber } ](${ value . pullRequestUrl } ) introduced the reviewed source line (blame-to-merge-commit; \`${ value . mergeCommitSha . slice ( 0 , 12 ) } \`).` ;
189+ export function regressionProvenancePublicLine (
190+ value : unknown ,
191+ regressionAssessment ?: unknown ,
192+ ) : string | null {
193+ if ( isVerifiedRegressionProvenance ( value ) ) {
194+ const sourceCommitSha = fullSha ( value . sourceCommitSha ?? value . mergeCommitSha ) ! ;
195+ const sourceAuthor = value . sourceAuthor
196+ ? markdownText ( value . sourceAuthor )
197+ : "source author not recorded in this legacy report" ;
198+ return `Regression provenance — verified: source commit \`${ sourceCommitSha . slice ( 0 , 12 ) } \` by ${ sourceAuthor } ; canonical PR [#${ value . pullRequestNumber } ](${ value . pullRequestUrl } ) (blame-to-merge-commit).` ;
199+ }
200+ if ( ! isSuspectedRegressionProvenance ( value ) ) return null ;
201+ if ( ! isRegressionAssessment ( regressionAssessment ) ) return null ;
202+ const related = value . relatedPullRequestUrl
203+ ? `safely related PR [#${ value . relatedPullRequestNumber } ](${ value . relatedPullRequestUrl } ) (rewrite-equivalent)`
204+ : "no PR verified" ;
205+ return `Regression provenance — suspected predecessor, not a causality claim: source commit \`${ value . sourceCommitSha . slice ( 0 , 12 ) } \` by ${ markdownText ( value . sourceAuthor ) } ; ${ related } .` ;
206+ }
207+
208+ export function isSuspectedRegressionProvenance (
209+ value : unknown ,
210+ ) : value is SuspectedRegressionProvenance {
211+ if ( ! value || typeof value !== "object" || Array . isArray ( value ) ) return false ;
212+ const candidate = value as Partial < SuspectedRegressionProvenance > ;
213+ const hasRelated =
214+ typeof candidate . relatedRepo === "string" &&
215+ repositoryPattern . test ( candidate . relatedRepo ) &&
216+ typeof candidate . relatedPullRequestNumber === "number" &&
217+ Number . isSafeInteger ( candidate . relatedPullRequestNumber ) &&
218+ candidate . relatedPullRequestNumber > 0 &&
219+ typeof candidate . relatedPullRequestUrl === "string" &&
220+ candidate . relatedPullRequestUrl ===
221+ `https://github.com/${ candidate . relatedRepo } /pull/${ candidate . relatedPullRequestNumber } ` ;
222+ return (
223+ ( candidate . evidenceType === "source_line" || candidate . evidenceType === "rewrite_equivalent" ) &&
224+ fullSha ( candidate . sourceCommitSha ?? "" ) !== null &&
225+ typeof candidate . sourceAuthor === "string" &&
226+ isSafeSourceAuthor ( candidate . sourceAuthor ) &&
227+ typeof candidate . sourcePath === "string" &&
228+ isSafeSourcePath ( candidate . sourcePath ) &&
229+ typeof candidate . sourceLine === "number" &&
230+ Number . isSafeInteger ( candidate . sourceLine ) &&
231+ candidate . sourceLine > 0 &&
232+ candidate . sourceLine <= MAX_SOURCE_LINE &&
233+ ( ( candidate . evidenceType === "source_line" &&
234+ candidate . relatedPullRequestNumber === null &&
235+ candidate . relatedPullRequestUrl === null &&
236+ candidate . relatedRepo === null ) ||
237+ ( candidate . evidenceType === "rewrite_equivalent" && hasRelated ) )
238+ ) ;
239+ }
240+
241+ export function isPublicRegressionProvenance (
242+ value : unknown ,
243+ ) : value is VerifiedRegressionProvenance | SuspectedRegressionProvenance {
244+ return isVerifiedRegressionProvenance ( value ) || isSuspectedRegressionProvenance ( value ) ;
126245}
127246
128247export function isRegressionAssessment ( value : unknown ) : value is RegressionAssessment {
@@ -140,14 +259,23 @@ export function isRegressionAssessment(value: unknown): value is RegressionAsses
140259 ) ;
141260}
142261
143- export function regressionAssessmentPublicLine ( value : unknown ) : string | null {
262+ export function regressionAssessmentPublicLine (
263+ value : unknown ,
264+ options : { predecessorAttributed ?: boolean } = { } ,
265+ ) : string | null {
144266 if ( ! isRegressionAssessment ( value ) ) return null ;
145267 const evidence = value . supportingEvidence . map ( regressionEvidenceLabel ) . join ( "; " ) ;
146- return `Possible regression — ${ value . confidence } (${ evidence } ). No predecessor PR is attributed.` ;
268+ const attribution = options . predecessorAttributed ? "" : " No predecessor PR is attributed." ;
269+ return `Possible regression — ${ value . confidence } (${ evidence } ).${ attribution } ` ;
147270}
148271
149272function normalizeCandidate (
150- value : RegressionProvenanceCandidate | VerifiedRegressionProvenance | null | undefined ,
273+ value :
274+ | RegressionProvenanceCandidate
275+ | VerifiedRegressionProvenance
276+ | SuspectedRegressionProvenance
277+ | null
278+ | undefined ,
151279 item : { repo : string ; number : number } ,
152280) : RegressionProvenanceCandidate | null {
153281 const candidate = normalizedCandidateFields ( value ) ;
@@ -216,7 +344,74 @@ function verifiedPullMetadata(
216344 ) {
217345 return null ;
218346 }
219- return { mergedAt : pull . merged_at } ;
347+ const head = pull . head ;
348+ const rawHeadSha =
349+ head && typeof head === "object" && ! Array . isArray ( head )
350+ ? ( head as Record < string , unknown > ) . sha
351+ : null ;
352+ const headSha = typeof rawHeadSha === "string" ? fullSha ( rawHeadSha ) : null ;
353+ if ( ! headSha ) return null ;
354+ return { mergedAt : pull . merged_at , headSha } ;
355+ }
356+
357+ function isConservativeRewriteEquivalent ( options : {
358+ candidate : RegressionProvenanceCandidate ;
359+ pull : VerifiedPullMetadata ;
360+ sourceCommitSha : string ;
361+ sourceAuthor : string ;
362+ pullDiff : string ;
363+ checkoutDir : string ;
364+ env : NodeJS . ProcessEnv ;
365+ runGit : RegressionProvenanceVerifierDependencies [ "runGit" ] ;
366+ } ) : boolean {
367+ try {
368+ const sourceSubject = options . runGit ( [ "show" , "-s" , "--format=%s" , options . sourceCommitSha ] , {
369+ cwd : options . checkoutDir ,
370+ env : options . env ,
371+ } ) ;
372+ if ( ! new RegExp ( `\\(#${ options . candidate . pullRequestNumber } \\)\\s*$` ) . test ( sourceSubject ) ) {
373+ return false ;
374+ }
375+ const sourceParents = options
376+ . runGit ( [ "show" , "-s" , "--format=%P" , options . sourceCommitSha ] , {
377+ cwd : options . checkoutDir ,
378+ env : options . env ,
379+ } )
380+ . trim ( )
381+ . split ( / \s + / ) ;
382+ if ( sourceParents . length !== 1 ) return false ;
383+ const diffArgs = ( parent : string , sha : string ) => [
384+ "diff" ,
385+ "--unified=3" ,
386+ "--no-renames" ,
387+ parent ,
388+ sha ,
389+ "--" ,
390+ ] ;
391+ const sourceDiff = options . runGit ( diffArgs ( sourceParents [ 0 ] ! , options . sourceCommitSha ) , {
392+ cwd : options . checkoutDir ,
393+ env : options . env ,
394+ } ) ;
395+ const normalizedSourceDiff = normalizedTextPatch ( sourceDiff ) ;
396+ const normalizedHeadDiff = normalizedTextPatch ( options . pullDiff ) ;
397+ return normalizedSourceDiff !== null && normalizedSourceDiff === normalizedHeadDiff ;
398+ } catch {
399+ return false ;
400+ }
401+ }
402+
403+ function normalizedTextPatch ( value : string ) : string | null {
404+ if ( ! value . trim ( ) || / (?: G I T b i n a r y p a t c h | B i n a r y f i l e s .* d i f f e r ) / . test ( value ) ) return null ;
405+ return value
406+ . split ( / \r ? \n / )
407+ . filter ( ( line ) => ! line . startsWith ( "index " ) )
408+ . map ( ( line ) => line . replace ( / ^ ( @ @ - \d + (?: , \d + ) ? \+ \d + (?: , \d + ) ? @ @ ) .* / , "$1" ) )
409+ . join ( "\n" )
410+ . trim ( ) ;
411+ }
412+
413+ function markdownText ( value : string ) : string {
414+ return value . replace ( / @ / g, "@\u200b" ) . replace ( / [ \\ ` * _ [ \] < > ] / g, "\\$&" ) ;
220415}
221416
222417function isSafeSourcePath ( path : string ) : boolean {
@@ -240,6 +435,17 @@ function hasControlCharacter(value: string): boolean {
240435 return false ;
241436}
242437
438+ function hasUnsafeUnicodeFormat ( value : string ) : boolean {
439+ return / [ \p{ Cf} \p{ Zl} \p{ Zp} ] / u. test ( value ) ;
440+ }
441+
442+ function isSafeSourceAuthor ( value : string ) : boolean {
443+ const author = value . trim ( ) ;
444+ return (
445+ author . length > 0 && ! hasUnsafeUnicodeFormat ( author ) && ! / [ ^ \s < > @ ] + @ [ ^ \s < > @ ] + / . test ( author )
446+ ) ;
447+ }
448+
243449function isSafeTargetBranch ( value : string | undefined ) : value is string {
244450 return (
245451 typeof value === "string" &&
@@ -260,6 +466,12 @@ function blamedSha(value: string): string | null {
260466 return fullSha ( sha ) ;
261467}
262468
469+ function blamedAuthor ( value : string ) : string | null {
470+ const match = / (?: ^ | \r ? \n ) a u t h o r ( .+ ) (?: \r ? \n | $ ) / . exec ( value ) ;
471+ const author = match ?. [ 1 ] ?. trim ( ) ?? "" ;
472+ return author && author . length <= 200 && ! hasControlCharacter ( author ) ? author : null ;
473+ }
474+
263475function hasBlameBoundary ( value : string ) : boolean {
264476 return / (?: ^ | \r ? \n ) b o u n d a r y (?: \r ? \n | $ ) / . test ( value ) ;
265477}
0 commit comments