The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
fix(server): PR guides work for pull requests on a fork's own repository (#39601)
Julius Marminge committed
Nov 20, 2026 at 15:03 UTC
d984b71385910bd575d4ed10ae6c0d1caf48f008
5 files changed
+124
-40
apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts
+24
-1
@@ -697,12 +697,33 @@ describe("pull request guide tools", () => {
697
}),
698
id: ProjectId.make("project-web"),
699
};
700
- const harness = yield* makeHarness({ projects: [makeProject(), other] });
700
+ // A fork's canonical key follows its upstream; its own repository is `origin`.
701
+ const fork = {
702
+ ...makeProject({
703
+ canonicalKey: "github.com/acme/web",
704
+ locator: {
705
+ source: "git-remote",
706
+ remoteName: "upstream",
707
+ remoteUrl: "https://github.com/acme/web.git",
708
+ },
709
+ origin: {
710
+ canonicalKey: "github.com/someone/web",
711
+ remoteUrl: "https://github.com/someone/web.git",
712
+ },
713
+ provider: SourceControlProviderKind.make("github"),
714
+ displayName: "acme/web",
715
+ owner: "acme",
716
+ name: "web",
717
+ }),
718
+ id: ProjectId.make("project-fork"),
719
+ };
720
+ const harness = yield* makeHarness({ projects: [makeProject(), other, fork] });
721
const result = yield* harness.call("write_pull_request_guides", {
722
pullRequests: [
723
{ url: "https://github.com/acme/web/pull/7" },
724
{ repository: "t3tools/t3code", number: 12 },
725
{ url: "https://github.com/someone/else/pull/1" },
726
+ { url: "https://github.com/someone/web/pull/3" },
727
],
728
});
729
expect(
@@ -711,6 +732,7 @@ describe("pull request guide tools", () => {
732
["acme/web", 7, "started"],
733
["t3tools/t3code", 12, "started"],
734
["someone/else", 1, "failed"],
735
+ ["someone/web", 3, "started"],
736
]);
737
expect(result.guides[2]?.detail).toContain("No project in this environment");
738
expect(
@@ -718,6 +740,7 @@ describe("pull request guide tools", () => {
740
).toEqual([
741
["project-web", 7],
742
[PROJECT_ID, 12],
743
+ ["project-fork", 3],
744
]);
745
}),
746
);
apps/server/src/mcp/toolkits/pullRequests/handlers.ts
+7
-16
@@ -29,7 +29,10 @@ import { resolveTarget, type ResolvedTarget } from "../../../pullRequest/pullReq
29
import * as PullRequestReviewGuideService from "../../../pullRequest/PullRequestReviewGuideService.ts";
30
import * as PullRequestService from "../../../pullRequest/PullRequestService.ts";
31
import { reviewPositionInPatch } from "../../../pullRequest/pullRequestReviewPosition.ts";
32
-import { pullRequestSyncKey } from "../../../pullRequest/pullRequestSyncKey.ts";
32
+import {
33
+ ownPullRequestRepositories,
34
+ pullRequestSyncKey,
35
+} from "../../../pullRequest/pullRequestSyncKey.ts";
36
import { sourceControlDefinitions } from "../../../sourceControl/sourceControlDefinitions.ts";
37
import {
38
normalizeThreadPullRequestKey,
@@ -233,23 +236,11 @@ function projectForPullRequest(
236
{ projectId: project.id, host: target.host, repository: target.repository, number: 1 },
237
project.repositoryIdentity ?? undefined,
238
);
236
- const projectKey =
237
- project.repositoryIdentity == null
238
- ? null
239
- : pullRequestSyncKey(
240
- {
241
- projectId: project.id,
242
- repository:
243
- sourceControlDefinitions.repositorySelector(project.repositoryIdentity) ?? "",
244
- number: 1,
245
- },
246
- project.repositoryIdentity,
247
- );
239
return (
240
own !== null &&
250
- projectKey !== null &&
251
- own.host === projectKey.host &&
252
- own.repository === projectKey.repository
241
+ ownPullRequestRepositories(project.repositoryIdentity).some(
242
+ ({ key }) => own.host === key.host && own.repository === key.repository,
243
+ )
244
);
245
};
246
if (preferred !== undefined && matches(preferred)) return preferred;
apps/server/src/pullRequest/PullRequestReviewGuideService.test.ts
+38
-2
@@ -17,6 +17,7 @@ import {
17
type PullRequestDetail,
18
type PullRequestStack,
19
type PullRequestRef,
20
+ type RepositoryOrigin,
21
} from "@t3tools/contracts";
22
23
import * as ServerConfig from "../config.ts";
@@ -159,7 +160,8 @@ const makeLayer = (input: {
160
readonly stored: Deferred.Deferred<void>;
161
readonly enabled?: boolean;
162
readonly currentHead?: Ref.Ref<string>;
162
- readonly identity?: typeof ACME_APP;
163
+ readonly identity?: typeof ACME_APP & { readonly origin?: RepositoryOrigin };
164
+ readonly locator?: typeof ACME_APP.locator;
165
}) =>
166
PullRequestReviewGuideService.layer.pipe(
167
Layer.provide(
@@ -197,7 +199,7 @@ const makeLayer = (input: {
199
workspaceRoot: input.workspace,
200
repositoryIdentity: {
201
...(input.identity ?? ACME_APP),
200
- locator: { ...ACME_APP.locator, remoteUrl: input.remote },
202
+ locator: input.locator ?? { ...ACME_APP.locator, remoteUrl: input.remote },
203
},
204
}),
205
} as unknown as ProjectService.ProjectService["Service"]),
@@ -558,6 +560,40 @@ it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideServic
560
}),
561
);
562
563
+ it.effect("writes a guide for a pull request on a fork whose canonical key is its upstream", () =>
564
+ Effect.gen(function* () {
565
+ const repositories = yield* makeRepositories;
566
+ const parent = `${repositories.root}/parent`;
567
+ yield* git(repositories.root, ["clone", "--quiet", "--bare", repositories.remote, parent]);
568
+ yield* git(repositories.workspace, ["remote", "add", "upstream", parent]);
569
+ const stored = yield* Deferred.make<void>();
570
+ const context = yield* Layer.build(
571
+ makeLayer({
572
+ ...repositories,
573
+ stored,
574
+ identity: {
575
+ ...ACME_APP,
576
+ canonicalKey: "github.com/parent/app",
577
+ displayName: "parent/app",
578
+ owner: "parent",
579
+ origin: { canonicalKey: "github.com/acme/app", remoteUrl: repositories.remote },
580
+ },
581
+ locator: { source: "git-remote", remoteName: "upstream", remoteUrl: parent },
582
+ generate: () => Effect.succeed({ stops: [] }),
583
+ }),
584
+ );
585
+ const service = Context.get(
586
+ context,
587
+ PullRequestReviewGuideService.PullRequestReviewGuideService,
588
+ );
589
+ const runs = yield* watchRuns;
590
+ yield* runs.generate(service, reference);
591
+ const result = yield* runs.settled(service, reference);
592
+ assert.notStrictEqual(result.run?._tag, "Failed", JSON.stringify(result.run));
593
+ assert.strictEqual(result.guide?.headSha, repositories.headSha);
594
+ }),
595
+ );
596
+
597
it.effect("refuses a pull request from a repository the project is not a checkout of", () =>
598
Effect.gen(function* () {
599
const repositories = yield* makeRepositories;
apps/server/src/pullRequest/PullRequestReviewGuideService.ts
+7
-20
@@ -18,7 +18,6 @@ import {
18
type PullRequestReviewGuideResult,
19
type PullRequestReviewGuideRun,
20
type PullRequestStack,
21
- ProjectId,
21
type RepositoryIdentity,
22
type ThreadPullRequestKey,
23
} from "@t3tools/contracts";
@@ -35,7 +34,7 @@ import * as TextGeneration from "../textGeneration/TextGeneration.ts";
34
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
35
import { PATCH_RENDER_PREFIX_ARGS } from "../vcs/GitVcsDriverCore.ts";
36
import * as PullRequestService from "./PullRequestService.ts";
38
-import { pullRequestSyncKey } from "./pullRequestSyncKey.ts";
37
+import { ownPullRequestRepositories, pullRequestSyncKey } from "./pullRequestSyncKey.ts";
38
import { reviewGuideInputFromPullRequest } from "./pullRequestReviewGuideInput.ts";
39
import { normalizeReviewGuide, parseReviewGuideChange } from "./pullRequestReviewGuideNormalize.ts";
40
@@ -96,30 +95,18 @@ const make = Effect.gen(function* () {
95
96
const requireOwnRepository = Effect.fn("PullRequestReviewGuideService.requireOwnRepository")(
97
function* (key: ThreadPullRequestKey, identity: RepositoryIdentity | null | undefined) {
99
- const own =
100
- identity == null
101
- ? null
102
- : pullRequestSyncKey(
103
- {
104
- projectId: ProjectId.make("own"),
105
- number: key.number,
106
- repository: sourceControlDefinitions.repositorySelector(identity) ?? "",
107
- },
108
- identity,
109
- );
110
- if (
111
- identity == null ||
112
- own === null ||
113
- own.host !== key.host ||
114
- own.repository !== key.repository
115
- ) {
98
+ const own = ownPullRequestRepositories(identity).find(
99
+ (candidate) =>
100
+ candidate.key.host === key.host && candidate.key.repository === key.repository,
101
+ );
102
+ if (own === undefined) {
103
return yield* new PullRequestOperationError({
104
operation: "generateReviewGuide",
105
detail:
106
"Guides are written from this project's checkout, and this pull request is in another repository. Write it from a project that is a checkout of that repository.",
107
});
108
}
122
- return identity.locator;
109
+ return own.locator;
110
},
111
);
112
apps/server/src/pullRequest/pullRequestSyncKey.ts
+48
-1
@@ -1,4 +1,9 @@
1
-import type { PullRequestRef, RepositoryIdentity, ThreadPullRequestKey } from "@t3tools/contracts";
1
+import {
2
+ ProjectId,
3
+ type PullRequestRef,
4
+ type RepositoryIdentity,
5
+ type ThreadPullRequestKey,
6
+} from "@t3tools/contracts";
7
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
8
import { normalizeThreadPullRequestKey } from "../sourceControl/threadPullRequests.ts";
9
@@ -38,3 +43,45 @@ export function pullRequestSyncKey(
43
number: reference.number,
44
});
45
}
46
+
47
+/**
48
+ * The repositories whose pull requests are the project's own, each with the remote to fetch it
49
+ * from: its primary one, then the fork its `origin` remote names when that is on the same host.
50
+ */
51
+export function ownPullRequestRepositories(
52
+ identity: RepositoryIdentity | null | undefined,
53
+): ReadonlyArray<{
54
+ readonly key: Pick<ThreadPullRequestKey, "host" | "repository">;
55
+ readonly locator: RepositoryIdentity["locator"];
56
+}> {
57
+ if (!identity) return [];
58
+ const candidates: Array<RepositoryIdentity> = [identity];
59
+ const origin = identity.origin;
60
+ const [host, ...path] = origin?.canonicalKey.split("/") ?? [];
61
+ if (
62
+ origin?.remoteUrl !== undefined &&
63
+ path.length > 0 &&
64
+ host === identity.canonicalKey.split("/")[0]
65
+ ) {
66
+ const { owner: _owner, name: _name, ...primary } = identity;
67
+ candidates.push({
68
+ ...primary,
69
+ canonicalKey: origin.canonicalKey,
70
+ displayName: origin.displayName ?? path.join("/"),
71
+ locator: { source: "git-remote", remoteName: "origin", remoteUrl: origin.remoteUrl },
72
+ });
73
+ }
74
+ return candidates.flatMap((candidate) => {
75
+ const key = pullRequestSyncKey(
76
+ {
77
+ projectId: ProjectId.make("own"),
78
+ repository: sourceControlDefinitions.repositorySelector(candidate) ?? "",
79
+ number: 1,
80
+ },
81
+ candidate,
82
+ );
83
+ return key === null
84
+ ? []
85
+ : [{ key: { host: key.host, repository: key.repository }, locator: candidate.locator }];
86
+ });
87
+}