Skip to content

Commit 0ea5e5a

Browse files
perf(github): revalidate pull request stack reads with ETags (pingdotgg#22078)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent e45ddea commit 0ea5e5a

2 files changed

Lines changed: 95 additions & 8 deletions

File tree

‎packages/source-control-github/src/server/GitHubPullRequestApi.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,46 @@ layer("GitHubPullRequestApi.layer", (it) => {
11191119
}),
11201120
);
11211121

1122+
it.effect("revalidates a polled stack, per credential, so an unchanged one costs no quota", () =>
1123+
Effect.gen(function* () {
1124+
const listing = encodeJson([
1125+
{
1126+
number: 4,
1127+
url: "https://api.github.com/repos/acme/poll/stacks/4",
1128+
base: { ref: "main" },
1129+
pull_requests: [
1130+
{ number: 9, head: { ref: "feat/nine" }, state: "open", merged_at: null },
1131+
],
1132+
},
1133+
]);
1134+
const read = Effect.flatMap(GitHubPullRequestApi.GitHubPullRequestApi, (cli) =>
1135+
cli.getPullRequestStack({
1136+
cwd: "/w",
1137+
repository: "acme/poll",
1138+
host: "github.com",
1139+
number: 9,
1140+
}),
1141+
);
1142+
mockedExecute.mockReturnValueOnce(
1143+
Effect.succeed(output(listing, false, false, { etag: 'W/"v1"' })),
1144+
);
1145+
const first = yield* read;
1146+
mockedExecute.mockReturnValueOnce(Effect.succeed(output("", false, false, {}, 304)));
1147+
const second = yield* read;
1148+
mockedCredential.mockImplementationOnce(() =>
1149+
Effect.succeed({ token: Redacted.make("other"), fingerprint: "github.com:other" }),
1150+
);
1151+
mockedExecute.mockReturnValueOnce(Effect.succeed(output(listing)));
1152+
yield* read;
1153+
1154+
assert.deepStrictEqual(second, first);
1155+
expect(callAt(0)).not.toHaveProperty("ifNoneMatch");
1156+
expect(callAt(1)).toMatchObject({ ifNoneMatch: 'W/"v1"' });
1157+
// Another account never revalidates the first account's answer.
1158+
expect(callAt(2)).not.toHaveProperty("ifNoneMatch");
1159+
}),
1160+
);
1161+
11221162
it.effect("fetches layer titles only when the caller asks for stack details", () =>
11231163
Effect.gen(function* () {
11241164
const minimal = {

‎packages/source-control-github/src/server/GitHubPullRequestApi.ts‎

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage";
22
import { KnownWorkflowRuns, makeChecksRevalidator } from "./gitHubConditionalChecks.ts";
33
import { runGitHubStackAction, type GitHubStackActionError } from "./githubStackActions.ts";
4+
import * as Cache from "effect/Cache";
45
import * as Context from "effect/Context";
56
import * as Clock from "effect/Clock";
67
import * 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. */
875876
const 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. */
878887
export 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

Comments
 (0)