TypeScript
perf(github): revalidate pull request stack reads with ETags (#22078)
Julius Marminge0ea5e5a53ead
diff --git a/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts b/packages/source-control-github/src/server/GitHubPullRequestApi.test.tsindex 705be8fb556..1b8d98ac4dc 100644--- a/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts+++ b/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts@@ -1119,6 +1119,46 @@ layer("GitHubPullRequestApi.layer", (it) => {}),);+ it.effect("revalidates a polled stack, per credential, so an unchanged one costs no quota", () =>+ Effect.gen(function* () {+ const listing = encodeJson([+ {+ number: 4,+ url: "https://api.github.com/repos/acme/poll/stacks/4",+ base: { ref: "main" },+ pull_requests: [+ { number: 9, head: { ref: "feat/nine" }, state: "open", merged_at: null },+ ],+ },+ ]);+ const read = Effect.flatMap(GitHubPullRequestApi.GitHubPullRequestApi, (cli) =>+ cli.getPullRequestStack({+ cwd: "/w",+ repository: "acme/poll",+ host: "github.com",+ number: 9,+ }),+ );+ mockedExecute.mockReturnValueOnce(+ Effect.succeed(output(listing, false, false, { etag: 'W/"v1"' })),+ );+ const first = yield* read;+ mockedExecute.mockReturnValueOnce(Effect.succeed(output("", false, false, {}, 304)));+ const second = yield* read;+ mockedCredential.mockImplementationOnce(() =>+ Effect.succeed({ token: Redacted.make("other"), fingerprint: "github.com:other" }),+ );+ mockedExecute.mockReturnValueOnce(Effect.succeed(output(listing)));+ yield* read;++ assert.deepStrictEqual(second, first);+ expect(callAt(0)).not.toHaveProperty("ifNoneMatch");+ expect(callAt(1)).toMatchObject({ ifNoneMatch: 'W/"v1"' });+ // Another account never revalidates the first account's answer.+ expect(callAt(2)).not.toHaveProperty("ifNoneMatch");+ }),+ );+it.effect("fetches layer titles only when the caller asks for stack details", () =>Effect.gen(function* () {const minimal = {diff --git a/packages/source-control-github/src/server/GitHubPullRequestApi.ts b/packages/source-control-github/src/server/GitHubPullRequestApi.tsindex 4eb3aec349c..937601d9461 100644--- a/packages/source-control-github/src/server/GitHubPullRequestApi.ts+++ b/packages/source-control-github/src/server/GitHubPullRequestApi.ts@@ -1,6 +1,7 @@import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage";import { KnownWorkflowRuns, makeChecksRevalidator } from "./gitHubConditionalChecks.ts";import { runGitHubStackAction, type GitHubStackActionError } from "./githubStackActions.ts";+import * as Cache from "effect/Cache";import * as Context from "effect/Context";import * as Clock from "effect/Clock";import * as Duration from "effect/Duration";@@ -874,6 +875,14 @@ const PullRequestGraphQLGroup = GraphQLGroup.merge(/** States in which `gh pr merge --auto` merges at once instead of arming auto-merge. */const IMMEDIATELY_MERGEABLE = new Set(["CLEAN", "HAS_HOOKS", "UNSTABLE"]);+interface RestReadInput<A> {+ readonly cwd: string;+ readonly host: string;+ readonly operation: string;+ readonly path: string;+ readonly decode: (raw: string) => Result.Result<A, unknown>;+}+/** @public Service construction is part of the canonical Effect module API. */export const make = Effect.gen(function* () {const api = yield* GitHubRest.GitHubRest;@@ -1133,13 +1142,7 @@ export const make = Effect.gen(function* () {};/** A REST read whose JSON answer is decoded the same way. */- const restRead = <A>(input: {- readonly cwd: string;- readonly host: string;- readonly operation: string;- readonly path: string;- readonly decode: (raw: string) => Result.Result<A, unknown>;- }): Effect.Effect<A, GitHubPullRequestApiError> =>+ const restRead = <A>(input: RestReadInput<A>): Effect.Effect<A, GitHubPullRequestApiError> =>api.rest({ host: input.host, operation: input.operation, path: input.path }).pipe(@@ -1148,6 +1151,49 @@ export const make = Effect.gen(function* () {),);+ // The last ETag and body of each polled REST read, so an unchanged answer comes back as a+ // 304, which GitHub does not charge. Keyed by the credential the request is pinned to, so a+ // validator never revalidates another account's body. A 304 writes the entry back, so one+ // that is still polled outlives its time to live.+ const validators = yield* Cache.make({+ capacity: 512,+ timeToLive: "1 hour",+ lookup: (_key: string) =>+ Effect.succeed(Option.none<{ readonly etag: string; readonly body: string }>()),+ });+ const revalidatedRestRead = Effect.fn("GitHubPullRequestApi.revalidatedRestRead")(function* <A>(+ input: RestReadInput<A>,+ ) {+ const host = GitHubRequestPolicy.normalizeHost(input.host);+ const { token, fingerprint } = yield* api.credential(host);+ const key = `${fingerprint}\0${host}\0${input.path}`;+ const previous = yield* Cache.get(validators, key);+ const response = yield* api+ .rest({+ host,+ operation: input.operation,+ path: input.path,+ ...(Option.isSome(previous) ? { ifNoneMatch: previous.value.etag } : {}),+ })+ .pipe(+ Effect.provideService(GitHubRequestPolicy.PinnedGitHubCredential, {+ host,+ token,+ credentialFingerprint: fingerprint,+ }),+ );+ if (response.status === 304 && Option.isSome(previous)) {+ yield* Cache.set(validators, key, previous);+ return yield* decodeWith(input.cwd, input.operation, input.decode, previous.value.body);+ }+ const decoded = yield* decodeWith(input.cwd, input.operation, input.decode, response.body);+ const etag = response.headers["etag"]?.trim();+ yield* etag+ ? Cache.set(validators, key, Option.some({ etag, body: response.body }))+ : Cache.invalidate(validators, key);+ return decoded;+ });+const diffFilesPage = (input: {readonly cwd: string;readonly repository: string;@@ -1915,7 +1961,8 @@ export const make = Effect.gen(function* () {getPullRequestStack: (input) => {const { owner, name } = parseRepositorySelector(input.repository);- return restRead({+ // Polled by the pull request sync for every linked pull request, so revalidate it.+ return revalidatedRestRead({cwd: input.cwd,host: input.host,operation: "getPullRequestStack",