@devries-cameron20 / t3code-1 / commits / 23132f8338
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(github): pull requests missing refs/pull/N/head no longer show as not found (#25947)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Radu Stefan Popa committed Nov 12, 2026 at 01:04 UTC 23132f833888b70536f938a988da3599252f5f0b
5 files changed +236 -27
packages/source-control-github/src/server/GitHubPullRequestApi.test.ts
+114 -1
@@ -42,6 +42,12 @@ const refusedReviewer = {
42 message: "Resource not accessible by integration",
43 path: ["repository", "pullRequest", "reviewRequests", "nodes", 0, "requestedReviewer"],
44 };
45 +// What GitHub answers for a pull request that has no `refs/pull/<number>/head`.
46 +const unresolvedPullRef = {
47 + type: "NOT_FOUND",
48 + message: "Could not resolve head ref 'refs/pull/7/head'.",
49 + path: ["repository", "pullRequest", "baseRef", "compare"],
50 +};
51 const pullRequestInput = { cwd: "/w", repository: "acme/web", host: "github.com", number: 7 };
52 type PullRequestRead = (
53 input: typeof pullRequestInput,
@@ -2550,6 +2556,23 @@ layer("GitHubPullRequestApi.layer", (it) => {
2556 }),
2557 );
2558
2559 + it.effect.each([
2560 + { action: "merge", mutation: "mergePullRequest(" },
2561 + { action: "update-branch", mutation: "updatePullRequestBranch(" },
2562 + ] as const)("runs $action on a pull request GitHub has no pull ref for", ({ action, mutation }) =>
2563 + Effect.gen(function* () {
2564 + route([
2565 + "query PullRequestActionState",
2566 + { ...actionState({ baseRef: { compare: null } }), errors: [unresolvedPullRef] },
2567 + ]);
2568 + const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
2569 +
2570 + yield* cli.runPullRequestAction({ ...pullRequestInput, action });
2571 +
2572 + expect(variablesOf(mutation)).toHaveLength(1);
2573 + }),
2574 + );
2575 +
2576 it.effect("merges at once when auto-merge is asked of a pull request that is ready now", () =>
2577 Effect.gen(function* () {
2578 route(["query PullRequestActionState", actionState({ mergeStateStatus: "CLEAN" })]);
@@ -3461,7 +3484,12 @@ layer("GitHubPullRequestApi.layer", (it) => {
3484 }).pipe(Effect.asVoid, Effect.flip);
3485 expect(error._tag).toBe(tag ?? "GitHubApiResponseError");
3486 if (error._tag === "GitHubApiResponseError")
3464 - expect(error).toMatchObject({ status, githubErrors: errors.map((e) => e.message) });
3487 + expect(error).toMatchObject({
3488 + status,
3489 + githubErrors: errors.map((e) =>
3490 + "path" in e ? `${e.message} (at ${e.path.join(".")})` : e.message,
3491 + ),
3492 + });
3493 expect(mockedExecute).toHaveBeenCalledTimes(index + 1);
3494 }
3495 }),
@@ -3577,6 +3605,91 @@ layer("GitHubPullRequestApi.layer", (it) => {
3605 }),
3606 );
3607
3608 + const unresolvedCore = {
3609 + ...coreResponse({ baseRef: { compare: null } }),
3610 + errors: [unresolvedPullRef],
3611 + };
3612 + it.effect.each([
3613 + { description: "alone", answers: [unresolvedCore], requestedReviewers: [100] },
3614 + {
3615 + description: "beside a refused reviewer",
3616 + answers: [
3617 + { ...unresolvedCore, errors: [unresolvedPullRef, refusedReviewer] },
3618 + unresolvedCore,
3619 + ],
3620 + requestedReviewers: [100, 0],
3621 + },
3622 + ])(
3623 + "reads a pull request without a pull ref, its comparison unknown ($description)",
3624 + ({ answers, requestedReviewers }) =>
3625 + Effect.gen(function* () {
3626 + for (const answer of answers)
3627 + mockedExecute.mockReturnValueOnce(Effect.succeed(output(encodeJson(answer))));
3628 + const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
3629 + const detail = yield* cli.getPullRequestDetail(pullRequestInput);
3630 + expect(detail.number).toBe(7);
3631 + expect(detail.comparison).toBeNull();
3632 + expect(variablesOf("PullRequestCore").map((v) => v["requestedReviewers"])).toEqual(
3633 + requestedReviewers,
3634 + );
3635 + }),
3636 + );
3637 +
3638 + it.effect("fails a missing pull ref beside another error, in GitHub's words", () =>
3639 + Effect.gen(function* () {
3640 + const refusedChecks = {
3641 + type: "FORBIDDEN",
3642 + message: "Resource not accessible by integration",
3643 + path: ["repository", "pullRequest", "commits", "nodes", 0, "commit", "statusCheckRollup"],
3644 + };
3645 + mockedExecute.mockReturnValueOnce(
3646 + Effect.succeed(
3647 + output(encodeJson({ ...unresolvedCore, errors: [unresolvedPullRef, refusedChecks] })),
3648 + ),
3649 + );
3650 + const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
3651 + const error = yield* Effect.flip(cli.getPullRequestDetail(pullRequestInput));
3652 + expect(error).toMatchObject({
3653 + _tag: "GitHubApiResponseError",
3654 + status: 200,
3655 + githubErrors: [
3656 + "Could not resolve head ref 'refs/pull/7/head'. (at repository.pullRequest.baseRef.compare)",
3657 + "Resource not accessible by integration (at repository.pullRequest.commits.nodes.0.commit.statusCheckRollup)",
3658 + ],
3659 + });
3660 + }),
3661 + );
3662 +
3663 + it.effect("still reads a pull request GitHub cannot find as missing, in GitHub's words", () =>
3664 + Effect.gen(function* () {
3665 + const { data } = coreResponse();
3666 + mockedExecute.mockReturnValueOnce(
3667 + Effect.succeed(
3668 + output(
3669 + encodeJson({
3670 + data: { repository: { ...data.repository, pullRequest: null } },
3671 + errors: [
3672 + {
3673 + type: "NOT_FOUND",
3674 + message: "Could not resolve to a PullRequest with the number of 7.",
3675 + path: ["repository", "pullRequest"],
3676 + },
3677 + ],
3678 + }),
3679 + ),
3680 + ),
3681 + );
3682 + const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
3683 + const error = yield* Effect.flip(cli.getPullRequestDetail(pullRequestInput));
3684 + expect(error).toMatchObject({
3685 + _tag: "GitHubApiNotFoundError",
3686 + githubErrors: [
3687 + "Could not resolve to a PullRequest with the number of 7. (at repository.pullRequest)",
3688 + ],
3689 + });
3690 + }),
3691 + );
3692 +
3693 it.effect.each([false, true])(
3694 "continues after the core check page without reading it twice (reviewer refusal: %s)",
3695 (refused) =>
packages/source-control-github/src/server/GitHubPullRequestApi.ts
+61 -22
@@ -55,7 +55,11 @@ import {
55 PullRequestMutationsGraphQLGroup,
56 type PullRequestActionSummary,
57 } from "./pullRequestMutations.graphql.ts";
58 -import { PullRequestReadsGraphQLGroup } from "./pullRequestReads.graphql.ts";
58 +import {
59 + PullRequestActionState,
60 + PullRequestCore,
61 + PullRequestReadsGraphQLGroup,
62 +} from "./pullRequestReads.graphql.ts";
63 import { PullRequestReviewsGraphQLGroup } from "./pullRequestReviews.graphql.ts";
64 import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
65 import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
@@ -321,23 +325,54 @@ const isGraphQlClientError = (error: unknown): error is GraphQLClientError.Graph
325
326 const { isNotFoundOnly } = GitHubGraphQlClient;
327
328 +const isMissingPullRef = ({ path, type }: GraphQLClientError.GraphQLError): boolean =>
329 + type === "NOT_FOUND" && path?.join(".") === "repository.pullRequest.baseRef.compare";
330 +
331 const isRefusedRequestedReviewersOnly = (error: unknown): boolean =>
332 isGraphQlClientError(error) &&
333 error.reason._tag === "ResponseError" &&
334 error.reason.status === 200 &&
328 - error.reason.errors.every(
329 - ({ path, type }) =>
330 - type === "FORBIDDEN" &&
331 - path?.length === 6 &&
332 - path[0] === "repository" &&
333 - path[1] === "pullRequest" &&
334 - path[2] === "reviewRequests" &&
335 - path[3] === "nodes" &&
336 - typeof path[4] === "number" &&
337 - Number.isInteger(path[4]) &&
338 - path[4] >= 0 &&
339 - path[5] === "requestedReviewer",
340 - );
335 + // A missing pull ref can come along; the retried read tolerates it.
336 + error.reason.errors.every((graphQlError) => {
337 + const { path, type } = graphQlError;
338 + return (
339 + isMissingPullRef(graphQlError) ||
340 + (type === "FORBIDDEN" &&
341 + path?.length === 6 &&
342 + path[0] === "repository" &&
343 + path[1] === "pullRequest" &&
344 + path[2] === "reviewRequests" &&
345 + path[3] === "nodes" &&
346 + typeof path[4] === "number" &&
347 + Number.isInteger(path[4]) &&
348 + path[4] >= 0 &&
349 + path[5] === "requestedReviewer")
350 + );
351 + });
352 +
353 +const isMissingPullRefOnly = <E>(
354 + error: E,
355 +): error is E &
356 + GraphQLClientError.GraphQLClientError & { readonly reason: GraphQLClientError.ResponseError } =>
357 + isGraphQlClientError(error) &&
358 + error.reason._tag === "ResponseError" &&
359 + error.reason.status === 200 &&
360 + error.reason.errors.every(isMissingPullRef);
361 +
362 +/**
363 + * GitHub can lack `refs/pull/<number>/head` for a pull request it otherwise answers, and then
364 + * fails only the comparison against it. The rest of that answer stands, its comparison unknown.
365 + */
366 +const toleratingMissingPullRef =
367 + <A, R2>(decode: (data: unknown) => Effect.Effect<A, Schema.SchemaError, R2>) =>
368 + <E, R>(effect: Effect.Effect<A, E, R>) =>
369 + effect.pipe(
370 + Effect.catchIf(isMissingPullRefOnly, (error) =>
371 + decode(error.reason.data).pipe(Effect.mapError(() => error)),
372 + ),
373 + );
374 +const decodePullRequestCore = Schema.decodeUnknownEffect(PullRequestCore.result);
375 +const decodePullRequestActionState = Schema.decodeUnknownEffect(PullRequestActionState.result);
376
377 /** A large pull request can produce a multi-megabyte patch; past this it is truncated. */
378 const DIFF_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
@@ -1457,14 +1492,16 @@ export const make = Effect.gen(function* () {
1492 const { owner, name } = parseRepositorySelector(input.repository);
1493 const operation = "getPullRequestDetail";
1494 const read = (requestedReviewers: number) =>
1460 - client.PullRequestCore({
1461 - owner,
1462 - name,
1463 - number: input.number,
1464 - headRef: `refs/pull/${input.number}/head`,
1465 - required: asksRequired(input.host),
1466 - requestedReviewers,
1467 - });
1495 + client
1496 + .PullRequestCore({
1497 + owner,
1498 + name,
1499 + number: input.number,
1500 + headRef: `refs/pull/${input.number}/head`,
1501 + required: asksRequired(input.host),
1502 + requestedReviewers,
1503 + })
1504 + .pipe(toleratingMissingPullRef(decodePullRequestCore));
1505 return SourceControlRateLimit.Interactive.pipe(
1506 Effect.flatMap((allowReserve) =>
1507 readRequestedReviewers(input, 100, read).pipe(
@@ -1693,6 +1730,7 @@ export const make = Effect.gen(function* () {
1730 ? new GitHubRequestPolicy.GitHubApiNotFoundError({
1731 host: input.host,
1732 operation: input.operation,
1733 + githubErrors: GitHubGraphQlClient.describeErrors(error),
1734 })
1735 : readError(input.cwd, input.operation, error),
1736 ),
@@ -2637,6 +2675,7 @@ export const make = Effect.gen(function* () {
2675 headRef: `refs/pull/${input.number}/head`,
2676 })
2677 .pipe(
2678 + toleratingMissingPullRef(decodePullRequestActionState),
2679 graphql({ ...target, allowReserve: true }),
2680 Effect.map((result) => result.repository?.pullRequest ?? null),
2681 Effect.flatMap(found(target)),
packages/source-control-github/src/server/GitHubRequestPolicy.ts
+6 -1
@@ -69,12 +69,17 @@ export class GitHubApiNotFoundError extends Schema.TaggedError<GitHubApiNotFound
69 {
70 host: Schema.String,
71 operation: Schema.String,
72 + githubErrors: Schema.optionalKey(Schema.Array(Schema.String)),
73 /** The source of the credential the refused request carried, when known. */
74 credential: Schema.optionalKey(GitHubCredentials.GitHubCredentialSource),
75 },
76 ) {
77 override get message(): string {
77 - return "GitHub could not find the requested resource, or the credential cannot see it.";
78 + const missing =
79 + "GitHub could not find the requested resource, or the credential cannot see it.";
80 + return this.githubErrors !== undefined && this.githubErrors.length > 0
81 + ? `${missing} GitHub said: ${this.githubErrors.join("; ")}`
82 + : missing;
83 }
84 }
85
packages/source-control-github/src/server/gitHubGraphQlClient.test.ts
+32 -1
@@ -288,7 +288,9 @@ describe("call", () => {
288 const { layer } = harness(() =>
289 json({
290 data: { repository: null },
291 - errors: [{ type: "NOT_FOUND", message: "Could not resolve to a Repository" }],
291 + errors: [
292 + { type: "NOT_FOUND", path: ["repository"], message: "Could not resolve to a Repository" },
293 + ],
294 }),
295 );
296 return Effect.gen(function* () {
@@ -297,10 +299,39 @@ describe("call", () => {
299 _tag: "GitHubApiNotFoundError",
300 host: "github.com",
301 operation: "stats",
302 + githubErrors: ["Could not resolve to a Repository (at repository)"],
303 });
304 }).pipe(Effect.provide(layer));
305 });
306
307 + it.effect(
308 + "fails a NOT_FOUND below the pull request as GitHub's reason, not a missing one",
309 + () => {
310 + const { layer } = harness(() =>
311 + json({
312 + data: { repository: { pullRequest: stats } },
313 + errors: [
314 + {
315 + type: "NOT_FOUND",
316 + path: ["repository", "pullRequest", "baseRef", "compare"],
317 + message: "Could not resolve head ref 'refs/pull/7/head'.",
318 + },
319 + ],
320 + }),
321 + );
322 + return Effect.gen(function* () {
323 + const error = yield* Effect.flip(callStats());
324 + expect(error).toMatchObject({
325 + _tag: "GitHubApiResponseError",
326 + status: 200,
327 + githubErrors: [
328 + "Could not resolve head ref 'refs/pull/7/head'. (at repository.pullRequest.baseRef.compare)",
329 + ],
330 + });
331 + }).pipe(Effect.provide(layer));
332 + },
333 + );
334 +
335 it.effect(
336 "fails NOT_FOUND alongside another error as a failure carrying GitHub's reasons",
337 () => {
packages/source-control-github/src/server/gitHubGraphQlClient.ts
+23 -2
@@ -43,10 +43,31 @@ function queryHash(query: string): string {
43 return (hash >>> 0).toString(16).padStart(8, "0");
44 }
45
46 +/**
47 + * Whether every error is a NOT_FOUND on a lookup: a root field (with its list items),
48 + * `repository.pullRequest` or `repository.issueOrPullRequest`. A NOT_FOUND deeper down is one
49 + * field GitHub could not resolve.
50 + */
51 export const isNotFoundOnly = (error: unknown): boolean =>
52 GraphQLClientError.isGraphQLClientError(error) &&
53 error.reason._tag === "ResponseError" &&
49 - error.reason.errors.every((graphQlError) => graphQlError["type"] === "NOT_FOUND");
54 + error.reason.errors.every(
55 + ({ type, path }) =>
56 + type === "NOT_FOUND" &&
57 + (path === undefined ||
58 + path.slice(1).every((segment) => typeof segment === "number") ||
59 + (path.length === 2 &&
60 + path[0] === "repository" &&
61 + (path[1] === "pullRequest" || path[1] === "issueOrPullRequest"))),
62 + );
63 +
64 +/** GitHub's message for each error in an answer, with the field it names. */
65 +export const describeErrors = (error: GraphQLClientError.GraphQLClientError) =>
66 + error.reason._tag === "ResponseError"
67 + ? error.reason.errors.map(({ message, path }) =>
68 + path === undefined || path.length === 0 ? message : `${message} (at ${path.join(".")})`,
69 + )
70 + : [];
71
72 /** Share REST's credentials/slots/quota/pause; leave non-rate-limit errors to each lookup. */
73 export class GitHubGraphQlAuth extends GraphQLMiddleware.Service<
@@ -234,6 +255,7 @@ const fromClientError = (
255 const context = {
256 host: normalizeHost(target.host),
257 operation: target.operation,
258 + githubErrors: describeErrors(error),
259 };
260 const credential = Option.filter(
261 Headers.get(reason.headers ?? Headers.empty, CREDENTIAL_SOURCE_HEADER),
@@ -247,7 +269,6 @@ const fromClientError = (
269 : new GitHubRequestPolicy.GitHubApiResponseError({
270 ...context,
271 status: reason.status ?? 200,
250 - githubErrors: reason.errors.map((graphQlError) => graphQlError.message),
272 githubErrorTypes: reason.errors.map((graphQlError) =>
273 typeof graphQlError["type"] === "string" ? graphQlError["type"] : null,
274 ),