The best way to control Claude Code, Codex and more from any computer or phone
perf(github): revalidate pull request stack reads with ETags (#22078)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 19, 2026 at 19:36 UTC
0ea5e5a53eadc4b57d16533333014e06abd56e8c
2 files changed
+95
-8
packages/source-control-github/src/server/GitHubPullRequestApi.test.ts
+40
@@ -1119,6 +1119,46 @@ layer("GitHubPullRequestApi.layer", (it) => {
1119
}),
1120
);
1121
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
+
1162
it.effect("fetches layer titles only when the caller asks for stack details", () =>
1163
Effect.gen(function* () {
1164
const minimal = {
packages/source-control-github/src/server/GitHubPullRequestApi.ts
+55
-8
@@ -1,6 +1,7 @@
1
import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage";
2
import { KnownWorkflowRuns, makeChecksRevalidator } from "./gitHubConditionalChecks.ts";
3
import { runGitHubStackAction, type GitHubStackActionError } from "./githubStackActions.ts";
4
+import * as Cache from "effect/Cache";
5
import * as Context from "effect/Context";
6
import * as Clock from "effect/Clock";
7
import * as Duration from "effect/Duration";
@@ -874,6 +875,14 @@ const PullRequestGraphQLGroup = GraphQLGroup.merge(
875
/** States in which `gh pr merge --auto` merges at once instead of arming auto-merge. */
876
const IMMEDIATELY_MERGEABLE = new Set(["CLEAN", "HAS_HOOKS", "UNSTABLE"]);
877
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
+
886
/** @public Service construction is part of the canonical Effect module API. */
887
export const make = Effect.gen(function* () {
888
const api = yield* GitHubRest.GitHubRest;
@@ -1133,13 +1142,7 @@ export const make = Effect.gen(function* () {
1142
};
1143
1144
/** 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> =>
1146
api
1147
.rest({ host: input.host, operation: input.operation, path: input.path })
1148
.pipe(
@@ -1148,6 +1151,49 @@ export const make = Effect.gen(function* () {
1151
),
1152
);
1153
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
+
1197
const diffFilesPage = (input: {
1198
readonly cwd: string;
1199
readonly repository: string;
@@ -1915,7 +1961,8 @@ export const make = Effect.gen(function* () {
1961
1962
getPullRequestStack: (input) => {
1963
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({
1966
cwd: input.cwd,
1967
host: input.host,
1968
operation: "getPullRequestStack",