11import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage" ;
22import { KnownWorkflowRuns , makeChecksRevalidator } from "./gitHubConditionalChecks.ts" ;
33import { runGitHubStackAction , type GitHubStackActionError } from "./githubStackActions.ts" ;
4+ import * as Cache from "effect/Cache" ;
45import * as Context from "effect/Context" ;
56import * as Clock from "effect/Clock" ;
67import * as Duration from "effect/Duration" ;
@@ -874,6 +875,14 @@ const PullRequestGraphQLGroup = GraphQLGroup.merge(
874875/** States in which `gh pr merge --auto` merges at once instead of arming auto-merge. */
875876const IMMEDIATELY_MERGEABLE = new Set ( [ "CLEAN" , "HAS_HOOKS" , "UNSTABLE" ] ) ;
876877
878+ interface RestReadInput < A > {
879+ readonly cwd : string ;
880+ readonly host : string ;
881+ readonly operation : string ;
882+ readonly path : string ;
883+ readonly decode : ( raw : string ) => Result . Result < A , unknown > ;
884+ }
885+
877886/** @public Service construction is part of the canonical Effect module API. */
878887export const make = Effect . gen ( function * ( ) {
879888 const api = yield * GitHubRest . GitHubRest ;
@@ -1133,13 +1142,7 @@ export const make = Effect.gen(function* () {
11331142 } ;
11341143
11351144 /** A REST read whose JSON answer is decoded the same way. */
1136- const restRead = < A > ( input : {
1137- readonly cwd : string ;
1138- readonly host : string ;
1139- readonly operation : string ;
1140- readonly path : string ;
1141- readonly decode : ( raw : string ) => Result . Result < A , unknown > ;
1142- } ) : Effect . Effect < A , GitHubPullRequestApiError > =>
1145+ const restRead = < A > ( input : RestReadInput < A > ) : Effect . Effect < A , GitHubPullRequestApiError > =>
11431146 api
11441147 . rest ( { host : input . host , operation : input . operation , path : input . path } )
11451148 . pipe (
@@ -1148,6 +1151,49 @@ export const make = Effect.gen(function* () {
11481151 ) ,
11491152 ) ;
11501153
1154+ // The last ETag and body of each polled REST read, so an unchanged answer comes back as a
1155+ // 304, which GitHub does not charge. Keyed by the credential the request is pinned to, so a
1156+ // validator never revalidates another account's body. A 304 writes the entry back, so one
1157+ // that is still polled outlives its time to live.
1158+ const validators = yield * Cache . make ( {
1159+ capacity : 512 ,
1160+ timeToLive : "1 hour" ,
1161+ lookup : ( _key : string ) =>
1162+ Effect . succeed ( Option . none < { readonly etag : string ; readonly body : string } > ( ) ) ,
1163+ } ) ;
1164+ const revalidatedRestRead = Effect . fn ( "GitHubPullRequestApi.revalidatedRestRead" ) ( function * < A > (
1165+ input : RestReadInput < A > ,
1166+ ) {
1167+ const host = GitHubRequestPolicy . normalizeHost ( input . host ) ;
1168+ const { token, fingerprint } = yield * api . credential ( host ) ;
1169+ const key = `${ fingerprint } \0${ host } \0${ input . path } ` ;
1170+ const previous = yield * Cache . get ( validators , key ) ;
1171+ const response = yield * api
1172+ . rest ( {
1173+ host,
1174+ operation : input . operation ,
1175+ path : input . path ,
1176+ ...( Option . isSome ( previous ) ? { ifNoneMatch : previous . value . etag } : { } ) ,
1177+ } )
1178+ . pipe (
1179+ Effect . provideService ( GitHubRequestPolicy . PinnedGitHubCredential , {
1180+ host,
1181+ token,
1182+ credentialFingerprint : fingerprint ,
1183+ } ) ,
1184+ ) ;
1185+ if ( response . status === 304 && Option . isSome ( previous ) ) {
1186+ yield * Cache . set ( validators , key , previous ) ;
1187+ return yield * decodeWith ( input . cwd , input . operation , input . decode , previous . value . body ) ;
1188+ }
1189+ const decoded = yield * decodeWith ( input . cwd , input . operation , input . decode , response . body ) ;
1190+ const etag = response . headers [ "etag" ] ?. trim ( ) ;
1191+ yield * etag
1192+ ? Cache . set ( validators , key , Option . some ( { etag, body : response . body } ) )
1193+ : Cache . invalidate ( validators , key ) ;
1194+ return decoded ;
1195+ } ) ;
1196+
11511197 const diffFilesPage = ( input : {
11521198 readonly cwd : string ;
11531199 readonly repository : string ;
@@ -1915,7 +1961,8 @@ export const make = Effect.gen(function* () {
19151961
19161962 getPullRequestStack : ( input ) => {
19171963 const { owner, name } = parseRepositorySelector ( input . repository ) ;
1918- return restRead ( {
1964+ // Polled by the pull request sync for every linked pull request, so revalidate it.
1965+ return revalidatedRestRead ( {
19191966 cwd : input . cwd ,
19201967 host : input . host ,
19211968 operation : "getPullRequestStack" ,
0 commit comments