The best way to control Claude Code, Codex and more from any computer or phone
fix(github): expand pull request context from the merge base (#23638)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Victor Vogel committed
Oct 30, 2026 at 15:55 UTC
2b98f54fc05256b7158bb23fd8f1ab4c4112059e
2 files changed
+62
-1
packages/source-control-github/src/server/GitHubPullRequestApi.test.ts
+31
@@ -574,6 +574,7 @@ const checkContextsPage = (
574
});
575
576
const pullRequestRefs = encodeJson({ base: { sha: "a1b2c3d" }, head: { sha: "b1c2d3e" } });
577
+const pullRequestMergeBase = encodeJson({ merge_base_commit: { sha: "c1d2e3f" } });
578
579
const crossRepositoryDetail = (headRefOid = "abc123") =>
580
coreResponse({
@@ -2856,6 +2857,34 @@ layer("GitHubPullRequestApi.layer", (it) => {
2857
}),
2858
);
2859
2860
+ it.effect("expands a pull request file from its merge base, not the moved base tip", () =>
2861
+ Effect.gen(function* () {
2862
+ const answers: Record<string, string> = {
2863
+ "repos/acme/web/pulls/7": pullRequestRefs,
2864
+ "repos/acme/web/compare/a1b2c3d...b1c2d3e?per_page=1&page=2": pullRequestMergeBase,
2865
+ "repos/acme/web/contents/src/a.ts?ref=a1b2c3d": "base tip\n",
2866
+ "repos/acme/web/contents/src/a.ts?ref=c1d2e3f": "merge base\n",
2867
+ "repos/acme/web/contents/src/a.ts?ref=b1c2d3e": "head\n",
2868
+ };
2869
+ mockedExecute.mockImplementation((call) =>
2870
+ Effect.succeed(output(call.kind === "rest" ? (answers[call.path] ?? "") : "")),
2871
+ );
2872
+ const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
2873
+
2874
+ const contents = yield* cli.getPullRequestDiffFileContents({
2875
+ cwd: "/w",
2876
+ repository: "acme/web",
2877
+ host: "github.com",
2878
+ number: 7,
2879
+ changeType: "change",
2880
+ oldPath: "src/a.ts",
2881
+ newPath: "src/a.ts",
2882
+ });
2883
+
2884
+ expect(contents).toEqual({ oldContents: "merge base\n", newContents: "head\n" });
2885
+ }),
2886
+ );
2887
+
2888
it.effect("reports unusable diff revisions as a structured error", () =>
2889
Effect.gen(function* () {
2890
mockedExecute.mockReturnValueOnce(Effect.succeed(output("not-a-sha\tstill-not-a-sha\n")));
@@ -2885,6 +2914,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
2914
it.effect("reports undecodable diff file contents as binary", () =>
2915
Effect.gen(function* () {
2916
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestRefs)));
2917
+ mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestMergeBase)));
2918
mockedExecute.mockReturnValueOnce(Effect.succeed(output("binary�contents", false, true)));
2919
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
2920
@@ -2911,6 +2941,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
2941
it.effect("returns valid text containing a literal replacement character", () =>
2942
Effect.gen(function* () {
2943
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestRefs)));
2944
+ mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestMergeBase)));
2945
mockedExecute.mockReturnValueOnce(Effect.succeed(output("before�after")));
2946
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
2947
packages/source-control-github/src/server/GitHubPullRequestApi.ts
+31
-1
@@ -863,6 +863,13 @@ const decodeRevisionRefs = Schema.decodeUnknownOption(
863
),
864
);
865
866
+/** Where a pull request's head forked off its base, the commit its patch is computed from. */
867
+const decodeMergeBase = Schema.decodeUnknownOption(
868
+ Schema.fromJsonString(
869
+ Schema.Struct({ merge_base_commit: Schema.Struct({ sha: Schema.String }) }),
870
+ ),
871
+);
872
+
873
const GRAPHQL_MERGE_METHODS = {
874
merge: "MERGE",
875
squash: "SQUASH",
@@ -1299,6 +1306,29 @@ export const make = Effect.gen(function* () {
1306
...(input.commit === undefined ? {} : { commit: input.commit }),
1307
});
1308
}
1309
+ // A pull request's patch runs from the merge base, and the base branch may have moved on
1310
+ // since, so its old side is read there. Page 2 of a compare leaves out the file list; a
1311
+ // failed read falls back to the base branch tip.
1312
+ const oldRef =
1313
+ input.commit === undefined && input.changeType !== "new"
1314
+ ? yield* api
1315
+ .rest({
1316
+ host: input.host,
1317
+ operation: "getPullRequestDiffFileContents",
1318
+ path: `repos/${owner}/${name}/compare/${baseRef}...${headRef}?per_page=1&page=2`,
1319
+ timeout: DIFF_TIMEOUT,
1320
+ })
1321
+ .pipe(
1322
+ Effect.map((response) =>
1323
+ decodeMergeBase(response.body).pipe(
1324
+ Option.map((compare) => compare.merge_base_commit.sha),
1325
+ Option.filter(isCommitSha),
1326
+ Option.getOrElse(() => baseRef ?? ""),
1327
+ ),
1328
+ ),
1329
+ Effect.orElseSucceed(() => baseRef ?? ""),
1330
+ )
1331
+ : (baseRef ?? "");
1332
1333
const readFile = (revision: string, filePath: string) =>
1334
api
@@ -1329,7 +1359,7 @@ export const make = Effect.gen(function* () {
1359
1360
const [oldContents, newContents] = yield* Effect.all(
1361
[
1332
- input.changeType === "new" ? Effect.succeed("") : readFile(baseRef ?? "", input.oldPath),
1362
+ input.changeType === "new" ? Effect.succeed("") : readFile(oldRef, input.oldPath),
1363
input.changeType === "deleted" ? Effect.succeed("") : readFile(headRef, input.newPath),
1364
],
1365
{ concurrency: 2 },