@@ -394,7 +394,7 @@ func compareRangeMetadataLoss(a, b rangeMetadataLoss) int {
394394 }
395395}
396396
397- func measureOptionalSectionsLoss (original , trimmed optionalSectionsView ) int {
397+ func measureOptionalSectionsLoss (original , trimmed ReviewOptionalContext ) int {
398398 loss := 0
399399 if len (original .PreviousAttempts ) > 0 && len (trimmed .PreviousAttempts ) == 0 {
400400 loss ++
@@ -411,37 +411,45 @@ func measureOptionalSectionsLoss(original, trimmed optionalSectionsView) int {
411411 return loss
412412}
413413
414- func measureRangeMetadataLoss (original , trimmed rangePromptView ) rangeMetadataLoss {
414+ func measureRangeMetadataLoss (original , trimmed TemplateContext ) rangeMetadataLoss {
415+ if original .Review == nil || trimmed .Review == nil || original .Review .Subject .Range == nil || trimmed .Review .Subject .Range == nil {
416+ return rangeMetadataLoss {}
417+ }
415418 loss := rangeMetadataLoss {
416- RemovedEntries : len (original .Current . Entries ) - len (trimmed .Current .Entries ),
417- TrimmedOptional : measureOptionalSectionsLoss (original .Optional , trimmed .Optional ),
419+ RemovedEntries : len (original .Review . Subject . Range . Entries ) - len (trimmed .Review . Subject . Range .Entries ),
420+ TrimmedOptional : measureOptionalSectionsLoss (original .Review . Optional , trimmed . Review .Optional ),
418421 }
419- for i := range trimmed .Current .Entries {
420- if i >= len (original .Current .Entries ) {
422+ for i := range trimmed .Review . Subject . Range .Entries {
423+ if i >= len (original .Review . Subject . Range .Entries ) {
421424 break
422425 }
423- if original .Current . Entries [i ].Subject != "" && trimmed .Current .Entries [i ].Subject == "" {
426+ if original .Review . Subject . Range . Entries [i ].Subject != "" && trimmed .Review . Subject . Range .Entries [i ].Subject == "" {
424427 loss .BlankedSubject ++
425428 }
426429 }
427430 return loss
428431}
429432
430- func selectRichestRangePromptView (limit int , view rangePromptView , variants []diffSectionView ) (rangePromptView , error ) {
431- fallback := rangePromptView {Optional : view .Optional , Current : view .Current }
432- if len (variants ) > 0 {
433- fallback .Diff = variants [len (variants )- 1 ]
433+ func selectRichestRangePromptView (limit int , view TemplateContext , variants []diffSectionView ) (TemplateContext , error ) {
434+ fallback := view .Clone ()
435+ if len (variants ) > 0 && fallback .Review != nil {
436+ fallback .Review .Diff = DiffContext {Heading : variants [len (variants )- 1 ].Heading , Body : variants [len (variants )- 1 ].Body }
437+ fallback .Review .Fallback = fallbackContextFromDiffSection (variants [len (variants )- 1 ])
434438 }
435439 var (
436- best rangePromptView
440+ best TemplateContext
437441 bestLoss rangeMetadataLoss
438442 haveBest bool
439443 )
440444 for _ , variant := range variants {
441- candidate := rangePromptView {Optional : view .Optional , Current : view .Current , Diff : variant }
442- trimmed , body , err := trimRangePromptView (limit , candidate )
445+ candidate := view .Clone ()
446+ if candidate .Review != nil {
447+ candidate .Review .Diff = DiffContext {Heading : variant .Heading , Body : variant .Body }
448+ candidate .Review .Fallback = fallbackContextFromDiffSection (variant )
449+ }
450+ trimmed , body , err := trimRangePromptContext (limit , candidate )
443451 if err != nil {
444- return rangePromptView {}, err
452+ return TemplateContext {}, err
445453 }
446454 fallback = trimmed
447455 if len (body ) > limit {
@@ -658,16 +666,29 @@ func (b *Builder) buildRangePrompt(repoPath, rangeRef string, repoID int64, cont
658666 pathspecArgs := safeForMarkdown (git .FormatExcludeArgs (excludes ))
659667 if isCodexReviewAgent (agentName ) {
660668 variants := codexRangeInspectionFallbackVariants (rangeRef , pathspecArgs )
661- selectedView , err := selectRichestRangePromptView (bodyLimit , rangePromptView {
669+ selectedCtx , err := selectRichestRangePromptView (bodyLimit , templateContextFromRangeView ( rangePromptView {
662670 Optional : optional ,
663671 Current : currentView ,
664- }, variants )
672+ }) , variants )
665673 if err != nil {
666674 return "" , err
667675 }
668- optional = selectedView .Optional
669- currentView = selectedView .Current
670- diffView = selectedView .Diff
676+ if selectedCtx .Review != nil {
677+ optional = optionalSectionsView {
678+ ProjectGuidelines : buildProjectGuidelinesSectionView (selectedCtx .Review .Optional .ProjectGuidelinesBody ()),
679+ AdditionalContext : selectedCtx .Review .Optional .AdditionalContext ,
680+ PreviousReviews : previousReviewViewsFromTemplateContext (selectedCtx .Review .Optional .PreviousReviews ),
681+ PreviousAttempts : reviewAttemptViewsFromTemplateContext (selectedCtx .Review .Optional .PreviousAttempts ),
682+ }
683+ if selectedCtx .Review .Subject .Range != nil {
684+ entries := make ([]commitRangeEntryView , 0 , len (selectedCtx .Review .Subject .Range .Entries ))
685+ for _ , entry := range selectedCtx .Review .Subject .Range .Entries {
686+ entries = append (entries , commitRangeEntryView (entry ))
687+ }
688+ currentView = commitRangeSectionView {Count : selectedCtx .Review .Subject .Range .Count , Entries : entries }
689+ }
690+ diffView = diffSectionView {Heading : selectedCtx .Review .Diff .Heading , Body : selectedCtx .Review .Diff .Body , Fallback : selectedCtx .Review .Fallback .Rendered ()}
691+ }
671692 } else {
672693 fallback , err := renderGenericRangeFallback (renderShellCommand ("git" , "diff" , rangeRef ))
673694 if err != nil {
0 commit comments