The best way to control Claude Code, Codex and more from any computer or phone
refactor(source-control): forges handle their own listing and SSH host quirks (#24288)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 24, 2026 at 04:24 UTC
6e792117436662208bf2b6eb71ee8d0d88817e43
21 files changed
+763
-300
apps/server/package.json
+1
@@ -51,6 +51,7 @@
51
"@t3tools/source-control-azure-devops": "workspace:*",
52
"@t3tools/source-control-bitbucket": "workspace:*",
53
"@t3tools/source-control-core": "workspace:*",
54
+ "@t3tools/source-control-definitions": "workspace:*",
55
"@t3tools/source-control-forgejo": "workspace:*",
56
"@t3tools/source-control-gitcafe": "workspace:*",
57
"@t3tools/source-control-github": "workspace:*",
apps/server/src/pullRequest/PullRequestService.test.ts
+113
-43
@@ -27,6 +27,8 @@ import * as SqlitePersistence from "../persistence/Sqlite.ts";
27
import * as PullRequestFilesViewed from "../persistence/PullRequestFilesViewed.ts";
28
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
29
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
30
+import type { SourceControlProvider } from "@t3tools/source-control-core/server/SourceControlProvider";
31
+import { isSshRemoteUrl } from "@t3tools/shared/sourceControl";
32
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
33
import { fakePullRequestProvider } from "@t3tools/source-control-testing/fakeSourceControlDriver";
34
import {
@@ -170,6 +172,26 @@ const keyedByCanonicalKey = {
172
repositoryKey: ({ canonicalKey }: { readonly canonicalKey: string }) => canonicalKey,
173
};
174
175
+/** A host whose SSH remotes name an alias, so discovery says which server serves them. */
176
+const sshAliasHost = {
177
+ resolveRemoteWebHost: ({ cwd, context, resolveContext }) =>
178
+ isSshRemoteUrl(context.remoteUrl)
179
+ ? resolveContext({
180
+ cwd,
181
+ context: {
182
+ ...context,
183
+ provider: { kind: SourceControlProviderKind.make("unknown"), name: "", baseUrl: "" },
184
+ },
185
+ }).pipe(
186
+ Effect.map((resolved) =>
187
+ resolved === null || resolved.provider.kind === "unknown"
188
+ ? null
189
+ : new URL(resolved.provider.baseUrl).host,
190
+ ),
191
+ )
192
+ : Effect.succeed(null),
193
+} as SourceControlProvider["Service"];
194
+
195
function makeService(input: {
196
readonly settings?: ServerSettings;
197
readonly projects: ReadonlyArray<OrchestrationProjectShell>;
@@ -193,6 +215,10 @@ function makeService(input: {
215
resolveLink: () => undefined,
216
resolveHandle:
217
input.resolveHandle ?? (() => Effect.die("Unexpected provider refinement")),
218
+ get: (kind) =>
219
+ Effect.succeed(
220
+ kind === "forgejo" ? sshAliasHost : ({} as SourceControlProvider["Service"]),
221
+ ),
222
}),
223
Layer.mock(ProjectService.ProjectService)({
224
listShells: (options) =>
@@ -1526,6 +1552,52 @@ it.effect("routes explicit Forgejo HTTP authorities through SSH checkouts after
1552
}),
1553
);
1554
1555
+it.effect("names an SSH remote's web host from a later checkout when the first cannot", () =>
1556
+ Effect.gen(function* () {
1557
+ const hosts: Array<string> = [];
1558
+ const service = yield* makeService({
1559
+ projects: ["/first", "/second"].map((workspaceRoot, index) =>
1560
+ project({
1561
+ id: `p${index}`,
1562
+ title: `checkout ${index}`,
1563
+ workspaceRoot,
1564
+ repository: "team/repo",
1565
+ provider: SourceControlProviderKind.make("forgejo"),
1566
+ host: "ssh.code.example",
1567
+ remoteUrl: "git@ssh.code.example:team/repo.git",
1568
+ }),
1569
+ ),
1570
+ providers: [
1571
+ fakePullRequestProvider(SourceControlProviderKind.make("forgejo"), {
1572
+ listChangeRequests: (input) => {
1573
+ hosts.push(input.host);
1574
+ return Effect.succeed({ items: [], truncated: false, continues: true });
1575
+ },
1576
+ }),
1577
+ ],
1578
+ resolveHandle: ({ cwd, context }) =>
1579
+ Effect.succeed({
1580
+ context:
1581
+ cwd === "/first"
1582
+ ? context!
1583
+ : {
1584
+ ...context!,
1585
+ provider: {
1586
+ kind: SourceControlProviderKind.make("forgejo"),
1587
+ name: "Forgejo",
1588
+ baseUrl: "http://code.example:3000",
1589
+ },
1590
+ },
1591
+ provider: undefined as never,
1592
+ }),
1593
+ });
1594
+
1595
+ yield* service.list({ state: "open" });
1596
+
1597
+ assert.deepStrictEqual(hosts, ["code.example:3000"]);
1598
+ }),
1599
+);
1600
+
1601
it.effect("rejects a different Forgejo HTTP port for an HTTP checkout", () =>
1602
Effect.gen(function* () {
1603
const service = yield* makeService({
@@ -2191,7 +2263,6 @@ it.effect("carries every repository of a slice on from the oldest row in it", ()
2263
],
2264
providers: [
2265
fakePullRequestProvider(SourceControlProviderKind.make("github"), {
2194
- listsAcrossMayOmitRepositories: true,
2266
listChangeRequestsAcross: () =>
2267
Effect.succeed({
2268
items: [
@@ -2200,6 +2271,7 @@ it.effect("carries every repository of a slice on from the oldest row in it", ()
2271
batchedChangeRequest(3, "acme/web", "2026-07-02T00:00:00Z"),
2272
],
2273
truncated: true,
2274
+ unresolved: ["acme/docs"],
2275
}),
2276
}),
2277
],
@@ -2283,51 +2355,49 @@ it.effect("reads a workspace larger than one search in chunks, and merges them",
2355
assert.strictEqual(result.entries.length, 101);
2356
}),
2357
);
2286
-it.effect(
2287
- "asks on its own for a repository a search answered nothing for, where searches can omit one",
2288
- () =>
2289
- Effect.gen(function* () {
2290
- const separately: string[] = [];
2291
- const service = yield* makeService({
2292
- projects: [
2293
- project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" }),
2294
- project({ id: "p2", title: "docs", workspaceRoot: "/b", repository: "acme/docs" }),
2295
- ],
2296
- providers: [
2297
- fakePullRequestProvider(SourceControlProviderKind.make("github"), {
2298
- listsAcrossMayOmitRepositories: true,
2299
- listChangeRequests: ({ repository }) => {
2300
- separately.push(repository);
2301
- return repository === "acme/docs"
2302
- ? Effect.fail(requestFailed)
2303
- : Effect.succeed({ items: [], truncated: false, continues: true });
2304
- },
2305
- listChangeRequestsAcross: () =>
2306
- Effect.succeed({
2307
- items: [batchedChangeRequest(1, "acme/web", "2026-07-03T00:00:00Z")],
2308
- truncated: false,
2309
- }),
2310
- }),
2311
- ],
2312
- });
2358
+it.effect("asks on its own for a repository a search could not vouch for", () =>
2359
+ Effect.gen(function* () {
2360
+ const separately: string[] = [];
2361
+ const service = yield* makeService({
2362
+ projects: [
2363
+ project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" }),
2364
+ project({ id: "p2", title: "docs", workspaceRoot: "/b", repository: "acme/docs" }),
2365
+ ],
2366
+ providers: [
2367
+ fakePullRequestProvider(SourceControlProviderKind.make("github"), {
2368
+ listChangeRequests: ({ repository }) => {
2369
+ separately.push(repository);
2370
+ return repository === "acme/docs"
2371
+ ? Effect.fail(requestFailed)
2372
+ : Effect.succeed({ items: [], truncated: false, continues: true });
2373
+ },
2374
+ listChangeRequestsAcross: () =>
2375
+ Effect.succeed({
2376
+ items: [batchedChangeRequest(1, "acme/web", "2026-07-03T00:00:00Z")],
2377
+ truncated: false,
2378
+ unresolved: ["acme/docs"],
2379
+ }),
2380
+ }),
2381
+ ],
2382
+ });
2383
2314
- const result = yield* service.list({ state: "open" });
2384
+ const result = yield* service.list({ state: "open" });
2385
2316
- assert.deepStrictEqual(separately, ["acme/docs"]);
2317
- assert.deepStrictEqual(result.errors, [
2318
- {
2319
- projectId: "p2" as ProjectId,
2320
- projectTitle: "docs",
2321
- message: "acme/docs could not be read.",
2322
- },
2323
- ]);
2324
- assert.deepStrictEqual(
2325
- result.entries.map((entry) => entry.number),
2326
- [1],
2327
- );
2328
- }),
2386
+ assert.deepStrictEqual(separately, ["acme/docs"]);
2387
+ assert.deepStrictEqual(result.errors, [
2388
+ {
2389
+ projectId: "p2" as ProjectId,
2390
+ projectTitle: "docs",
2391
+ message: "acme/docs could not be read.",
2392
+ },
2393
+ ]);
2394
+ assert.deepStrictEqual(
2395
+ result.entries.map((entry) => entry.number),
2396
+ [1],
2397
+ );
2398
+ }),
2399
);
2330
-it.effect("believes a search's silence about a repository where searches cover every one", () =>
2400
+it.effect("believes a search's silence about a repository it vouches for", () =>
2401
Effect.gen(function* () {
2402
const separately: string[] = [];
2403
const service = yield* makeService({
apps/server/src/pullRequest/PullRequestService.ts
+158
-151
@@ -82,6 +82,7 @@ import * as ServerSettings from "../serverSettings.ts";
82
import * as PullRequestFilesViewed from "../persistence/PullRequestFilesViewed.ts";
83
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
84
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
85
+import type { SourceControlProviderContext } from "@t3tools/source-control-core/server/SourceControlProvider";
86
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
87
import {
88
type ProviderChangeRequest,
@@ -142,7 +143,6 @@ const FILES_VIEWED_CACHE_TTL = Duration.seconds(15);
143
const DIFF_STALE_WINDOW = Duration.minutes(10);
144
const DIFF_SWEEP_INTERVAL = Duration.minutes(1);
145
const VIEWER_CACHE_TTL = Duration.minutes(10);
145
-const SEARCH_VISIBILITY_TTL = Duration.minutes(10);
146
const STALE_DETAIL_WINDOW = Duration.minutes(10);
147
const isPullRequestProviderError = Schema.is(PullRequestProviderError);
148
const LIST_CACHE_CAPACITY = 64;
@@ -506,9 +506,6 @@ function withRateLimitBackoff(
506
...(api.unavailableRequirement === undefined
507
? {}
508
: { unavailableRequirement: api.unavailableRequirement }),
509
- ...(api.listsAcrossMayOmitRepositories === undefined
510
- ? {}
511
- : { listsAcrossMayOmitRepositories: api.listsAcrossMayOmitRepositories }),
509
...(api.repositoryKey === undefined ? {} : { repositoryKey: api.repositoryKey }),
510
...(api.readsPublicHostWithoutCheckout === undefined
511
? {}
@@ -661,16 +658,8 @@ function rankFilterOptions<A>(
658
};
659
}
660
664
-/** Whether the project's remote is SSH on a host whose SSH remotes do not name its web host. */
665
-function sshRemoteHidesWebHost(
666
- identity: NonNullable<OrchestrationProjectShell["repositoryIdentity"]>,
667
-) {
668
- return (
669
- identity.provider !== undefined &&
670
- sourceControlDefinitions.find(identity.provider)?.sshRemoteHidesWebHost === true &&
671
- isSshRemoteUrl(identity.locator.remoteUrl)
672
- );
673
-}
661
+const remoteHostKey = (recognized: SourceControlProviderInfo, remoteUrl: string) =>
662
+ `${recognized.baseUrl}\n${isSshRemoteUrl(remoteUrl) ? "ssh" : "web"}`;
663
664
/** An HTTP remote names its server's web host, port included. */
665
function httpRemoteHost(remoteUrl: string): string | null {
@@ -706,25 +695,36 @@ export const make = Effect.gen(function* () {
695
const filesViewedStore = yield* PullRequestFilesViewed.PullRequestFilesViewedRepository;
696
const readCache = yield* PullRequestReadCache.PullRequestReadCache;
697
709
- const refineUnknownProjectKinds = (
698
+ /**
699
+ * Where each project's remote is served from, by the remote's recognised base URL: a remote no
700
+ * definition placed is identified by the hosting CLIs, and a host whose remotes may not name
701
+ * its web server says which one does. One project per base URL and transport answers for all
702
+ * of them.
703
+ */
704
+ const resolveRemoteHosts = Effect.fn("PullRequestService.resolveRemoteHosts")(function* (
705
projects: ReadonlyArray<OrchestrationProjectShell>,
706
filter: Pick<PullRequestListInput, "projectId" | "host">,
712
- ) => {
713
- type RefinementCandidate = {
707
+ ) {
708
+ type Candidate = {
709
readonly project: OrchestrationProjectShell;
715
- readonly provider: SourceControlProviderInfo;
716
- readonly remoteName: string;
717
- readonly remoteUrl: string;
710
+ readonly context: SourceControlProviderContext;
711
};
719
- const refinements = new Map<string, RefinementCandidate[]>();
712
+ const candidatesByKey = new Map<string, Candidate[]>();
713
for (const project of projects) {
714
if (filter.projectId !== undefined && project.id !== filter.projectId) continue;
715
const identity = project.repositoryIdentity;
716
if (
724
- (identity?.provider !== "unknown" && !(identity && sshRemoteHidesWebHost(identity))) ||
725
- sourceControlDefinitions.repositorySelector(project.repositoryIdentity) === null
717
+ !identity ||
718
+ identity.provider === undefined ||
719
+ sourceControlDefinitions.repositorySelector(identity) === null
720
)
721
continue;
722
+ if (identity.provider !== "unknown") {
723
+ const provider = yield* sourceControlProviders
724
+ .get(identity.provider)
725
+ .pipe(Effect.orElseSucceed(() => null));
726
+ if (provider?.resolveRemoteWebHost === undefined) continue;
727
+ }
728
const host = sourceControlDefinitions.changeRequestHost(identity, "unknown");
729
if (
730
filter.host !== undefined &&
@@ -736,50 +736,90 @@ export const make = Effect.gen(function* () {
736
continue;
737
}
738
const { remoteName, remoteUrl } = identity.locator;
739
- const provider = sourceControlDefinitions.recognizeRemote(remoteUrl);
740
- if (provider !== null) {
741
- const candidates = refinements.get(provider.baseUrl);
742
- const candidate = { project, provider, remoteName, remoteUrl };
743
- if (candidates === undefined) refinements.set(provider.baseUrl, [candidate]);
744
- else candidates.push(candidate);
745
- }
739
+ const recognized = sourceControlDefinitions.recognizeRemote(remoteUrl);
740
+ if (recognized === null) continue;
741
+ const candidate = {
742
+ project,
743
+ context: {
744
+ provider:
745
+ identity.provider === "unknown"
746
+ ? recognized
747
+ : { ...recognized, kind: identity.provider },
748
+ remoteName,
749
+ remoteUrl,
750
+ ...(filter.host !== undefined && isSshRemoteUrl(remoteUrl)
751
+ ? { requestedHost: filter.host }
752
+ : {}),
753
+ },
754
+ };
755
+ const key = remoteHostKey(recognized, remoteUrl);
756
+ const candidates = candidatesByKey.get(key);
757
+ if (candidates === undefined) candidatesByKey.set(key, [candidate]);
758
+ else candidates.push(candidate);
759
}
760
748
- return Effect.forEach(
749
- refinements,
750
- ([baseUrl, candidates]) =>
751
- Effect.firstSuccessOf(
752
- candidates.map(({ project, provider, remoteName, remoteUrl }) =>
753
- Effect.suspend(() =>
754
- sourceControlProviders.resolveHandle({
755
- cwd: project.workspaceRoot,
756
- context: {
757
- provider: sourceControlDefinitions.find(provider.kind)?.sshRemoteHidesWebHost
758
- ? { ...provider, kind: SourceControlProviderKind.make("unknown") }
759
- : provider,
760
- remoteName,
761
- remoteUrl,
762
- ...(filter.host !== undefined && isSshRemoteUrl(remoteUrl)
763
- ? { requestedHost: filter.host }
764
- : {}),
765
- },
766
- }),
767
- ).pipe(
768
- Effect.flatMap((handle) => {
769
- const refined = handle.context?.provider;
770
- return refined === undefined || refined.kind === "unknown"
771
- ? Effect.fail(undefined)
772
- : Effect.succeed(refined);
773
- }),
761
+ const resolveContext = (input: {
762
+ readonly cwd: string;
763
+ readonly context: SourceControlProviderContext;
764
+ }) => sourceControlProviders.resolveHandle(input).pipe(Effect.map((handle) => handle.context));
765
+ type RemoteHost = { readonly kind: SourceControlProviderKind; readonly host: string | null };
766
+ const resolveCandidate = ({ project, context }: Candidate) =>
767
+ (context.provider.kind === "unknown"
768
+ ? resolveContext({ cwd: project.workspaceRoot, context }).pipe(
769
+ Effect.map((resolved) =>
770
+ resolved === null || resolved.provider.kind === "unknown"
771
+ ? null
772
+ : { context: resolved, discovered: true },
773
),
775
- ),
776
- ).pipe(
777
- Effect.map((provider) => [baseUrl, provider] as const),
778
- Effect.orElseSucceed(() => [baseUrl, null] as const),
774
+ )
775
+ : Effect.succeed({ context, discovered: false })
776
+ ).pipe(
777
+ Effect.flatMap((found) =>
778
+ found === null
779
+ ? Effect.succeed(null)
780
+ : sourceControlProviders.get(found.context.provider.kind).pipe(
781
+ Effect.flatMap(
782
+ (provider) =>
783
+ provider.resolveRemoteWebHost?.({
784
+ cwd: project.workspaceRoot,
785
+ context: found.context,
786
+ // A remote discovery just placed is not looked up a second time.
787
+ resolveContext: found.discovered
788
+ ? () => Effect.succeed(found.context)
789
+ : resolveContext,
790
+ }) ?? Effect.succeed(null),
791
+ ),
792
+ Effect.orElseSucceed(() => null),
793
+ Effect.map((host): RemoteHost => ({ kind: found.context.provider.kind, host })),
794
+ ),
795
),
796
+ Effect.orElseSucceed(() => null),
797
+ );
798
+
799
+ // An SSH remote whose web host one checkout could not name may be named by the next; a
800
+ // kind found without a host still stands if none can.
801
+ const resolveKey = Effect.fn("PullRequestService.resolveRemoteHost")(function* (
802
+ candidates: ReadonlyArray<Candidate>,
803
+ ) {
804
+ let fallback: RemoteHost | null = null;
805
+ for (const candidate of candidates) {
806
+ const resolved = yield* resolveCandidate(candidate);
807
+ if (resolved === null) continue;
808
+ if (resolved.host !== null || !isSshRemoteUrl(candidate.context.remoteUrl)) {
809
+ return resolved;
810
+ }
811
+ fallback ??= resolved;
812
+ }
813
+ return fallback;
814
+ });
815
+
816
+ return yield* Effect.forEach(
817
+ candidatesByKey,
818
+ ([key, candidates]) =>
819
+ resolveKey(candidates).pipe(Effect.map((resolved) => [key, resolved] as const)),
820
{ concurrency: REPOSITORY_CONCURRENCY },
821
).pipe(Effect.map((resolved) => new Map(resolved)));
782
- };
822
+ });
823
824
const listWorkspaceProjects = (
825
filter: Pick<PullRequestListInput, "projectId" | "projectIds" | "host">,
@@ -811,11 +851,11 @@ export const make = Effect.gen(function* () {
851
),
852
),
853
Effect.flatMap((projects) =>
814
- refineUnknownProjectKinds(projects, filter).pipe(
815
- Effect.map((refinedProviders) => ({ refinedProviders, projects })),
854
+ resolveRemoteHosts(projects, filter).pipe(
855
+ Effect.map((remoteHosts) => ({ remoteHosts, projects })),
856
),
857
),
818
- Effect.map(({ refinedProviders, projects }) => {
858
+ Effect.map(({ remoteHosts, projects }) => {
859
const supported: SupportedProject[] = [];
860
const unimplemented = new Map<
861
string,
@@ -832,17 +872,13 @@ export const make = Effect.gen(function* () {
872
project.repositoryIdentity,
873
);
874
if (!identity || kind === undefined || repository === null) continue;
835
- let refinedProvider: SourceControlProviderInfo | null | undefined;
836
- if (kind === "unknown" || sshRemoteHidesWebHost(identity)) {
837
- const provider = sourceControlDefinitions.recognizeRemote(identity.locator.remoteUrl);
838
- refinedProvider = provider === null ? null : refinedProviders.get(provider.baseUrl);
839
- kind = refinedProvider?.kind ?? kind;
840
- }
841
- const host =
842
- refinedProvider &&
843
- sourceControlDefinitions.find(refinedProvider.kind)?.sshRemoteHidesWebHost
844
- ? new URL(refinedProvider.baseUrl).host.toLowerCase()
845
- : sourceControlDefinitions.changeRequestHost(identity, kind);
875
+ const recognized = sourceControlDefinitions.recognizeRemote(identity.locator.remoteUrl);
876
+ const remote =
877
+ recognized === null
878
+ ? undefined
879
+ : remoteHosts.get(remoteHostKey(recognized, identity.locator.remoteUrl));
880
+ kind = remote?.kind ?? kind;
881
+ const host = remote?.host ?? sourceControlDefinitions.changeRequestHost(identity, kind);
882
if (filter.host !== undefined && host !== filter.host.toLowerCase()) {
883
continue;
884
}
@@ -1193,13 +1229,6 @@ export const make = Effect.gen(function* () {
1229
};
1230
};
1231
1196
- // On a host whose read across repositories may omit some, a repository that has appeared in
1197
- // one is known to be covered. Empty reads for that same repository are therefore real empty
1198
- // answers, not a reason to read it on its own again.
1199
- const searchVisibleAt = new Map<string, number>();
1200
- const searchVisibilityKey = (host: string, repository: string) =>
1201
- `${host}\n${repository.trim().toLowerCase()}`;
1202
-
1232
const listUncached: PullRequestService["Service"]["list"] = Effect.fn(
1233
"PullRequestService.listUncached",
1234
)(function* (input) {
@@ -1409,75 +1438,53 @@ export const make = Effect.gen(function* () {
1438
: { cursor: { updatedBefore: cursor.updatedBefore, delivered: cursor.delivered } }),
1439
}).pipe(
1440
observeRead,
1412
- Effect.flatMap(({ value: page, observedAt }) =>
1413
- Effect.flatMap(Clock.currentTimeMillis, (now) => {
1414
- const rows = new Map<string, Array<ProviderChangeRequest>>();
1415
- for (const [key, visibleAt] of searchVisibleAt) {
1416
- if (now - visibleAt > Duration.toMillis(SEARCH_VISIBILITY_TTL)) {
1417
- searchVisibleAt.delete(key);
1418
- }
1419
- }
1420
- for (const item of page.items) {
1421
- const key = item.repository.trim().toLowerCase();
1422
- const held = rows.get(key);
1423
- if (held === undefined) rows.set(key, [item]);
1424
- else held.push(item);
1425
- searchVisibleAt.set(searchVisibilityKey(first.host, item.repository), now);
1426
- }
1427
- const boundary = page.items.reduce<string | null>(
1428
- (oldest, item) =>
1429
- oldest === null || item.updatedAt < oldest ? item.updatedAt : oldest,
1430
- null,
1431
- );
1432
- return Effect.forEach(
1433
- chunk,
1434
- (project): Effect.Effect<RepositoryBatch> => {
1435
- const fetched = rows.get(project.repository.trim().toLowerCase()) ?? [];
1436
- // A repository the read said nothing at all about, on a host whose read can omit
1437
- // one, is read on its own, once, before it is believed. Only on its first slice:
1438
- // after that it has a boundary to carry on from, and silence past one means the
1439
- // rows are older rather than absent.
1440
- const lastVisible = searchVisibleAt.get(
1441
- searchVisibilityKey(project.host, project.repository),
1442
- );
1443
- const searchIsKnownVisible =
1444
- first.api.listsAcrossMayOmitRepositories !== true ||
1445
- (!page.truncated &&
1446
- lastVisible !== undefined &&
1447
- now - lastVisible <= Duration.toMillis(SEARCH_VISIBILITY_TTL));
1448
- if (
1449
- fetched.length === 0 &&
1450
- cursorOf(project) === undefined &&
1451
- !searchIsKnownVisible
1452
- ) {
1453
- return readRepository(project);
1454
- }
1455
- const cursorHere = cursorOf(project);
1456
- const items =
1457
- cursorHere === undefined
1458
- ? fetched
1459
- : fetched.filter(
1460
- (item) =>
1461
- item.updatedAt !== cursorHere.updatedBefore ||
1462
- !cursorHere.seenAt.includes(item.number),
1463
- );
1464
- return Effect.succeed({
1465
- key: project.cursorKey,
1466
- entries: items
1467
- .filter((item) => matchesRowFilters(item, input.filters, viewer))
1468
- .map((item) => toEntry({ project, item, viewer, observedAt })),
1469
- errors: [],
1470
- truncated: page.truncated,
1471
- nextCursor:
1472
- page.truncated && boundary !== null
1473
- ? listCursorAt(cursorHere, boundary, fetched, items.length)
1474
- : null,
1475
- });
1476
- },
1477
- { concurrency: REPOSITORY_CONCURRENCY },
1478
- );
1479
- }),
1480
- ),
1441
+ Effect.flatMap(({ value: page, observedAt }) => {
1442
+ const rows = new Map<string, Array<ProviderChangeRequest>>();
1443
+ for (const item of page.items) {
1444
+ const key = item.repository.trim().toLowerCase();
1445
+ const held = rows.get(key);
1446
+ if (held === undefined) rows.set(key, [item]);
1447
+ else held.push(item);
1448
+ }
1449
+ const unresolved = new Set(
1450
+ (page.unresolved ?? []).map((repository) => repository.trim().toLowerCase()),
1451
+ );
1452
+ const boundary = page.items.reduce<string | null>(
1453
+ (oldest, item) =>
1454
+ oldest === null || item.updatedAt < oldest ? item.updatedAt : oldest,
1455
+ null,
1456
+ );
1457
+ return Effect.forEach(
1458
+ chunk,
1459
+ (project): Effect.Effect<RepositoryBatch> => {
1460
+ const key = project.repository.trim().toLowerCase();
1461
+ const fetched = rows.get(key) ?? [];
1462
+ if (fetched.length === 0 && unresolved.has(key)) return readRepository(project);
1463
+ const cursorHere = cursorOf(project);
1464
+ const items =
1465
+ cursorHere === undefined
1466
+ ? fetched
1467
+ : fetched.filter(
1468
+ (item) =>
1469
+ item.updatedAt !== cursorHere.updatedBefore ||
1470
+ !cursorHere.seenAt.includes(item.number),
1471
+ );
1472
+ return Effect.succeed({
1473
+ key: project.cursorKey,
1474
+ entries: items
1475
+ .filter((item) => matchesRowFilters(item, input.filters, viewer))
1476
+ .map((item) => toEntry({ project, item, viewer, observedAt })),
1477
+ errors: [],
1478
+ truncated: page.truncated,
1479
+ nextCursor:
1480
+ page.truncated && boundary !== null
1481
+ ? listCursorAt(cursorHere, boundary, fetched, items.length)
1482
+ : null,
1483
+ });
1484
+ },
1485
+ { concurrency: REPOSITORY_CONCURRENCY },
1486
+ );
1487
+ }),
1488
Effect.catch(separately),
1489
);
1490
};
apps/server/src/sourceControl/sourceControlDefinitions.test.ts
new
+12
@@ -0,0 +1,12 @@
1
+import { expect, it } from "@effect/vitest";
2
+
3
+import {
4
+ BUILT_IN_SOURCE_CONTROL_DRIVER_LIST,
5
+ sourceControlDefinitions,
6
+} from "./sourceControlDefinitions.ts";
7
+
8
+it("runs a driver for every host the server and clients read, in their order", () => {
9
+ expect(BUILT_IN_SOURCE_CONTROL_DRIVER_LIST.map((driver) => driver.definition)).toEqual(
10
+ sourceControlDefinitions.definitions,
11
+ );
12
+});
apps/server/src/sourceControl/sourceControlDefinitions.ts
+3
-22
@@ -1,18 +1,12 @@
1
import * as AzureDevOpsDriver from "@t3tools/source-control-azure-devops/server/driver";
2
import * as BitbucketDriver from "@t3tools/source-control-bitbucket/server/driver";
3
-import { makeSourceControlDefinitionRegistry } from "@t3tools/source-control-core/shared/definition";
3
+import { builtInSourceControlDefinitions } from "@t3tools/source-control-definitions/registry";
4
import * as ForgejoDriver from "@t3tools/source-control-forgejo/server/driver";
5
import * as GitCafeDriver from "@t3tools/source-control-gitcafe/server/driver";
6
import * as GitHubDriver from "@t3tools/source-control-github/server/driver";
7
import * as GitLabDriver from "@t3tools/source-control-gitlab/server/driver";
8
9
-/**
10
- * The same order as the clients' registry in `@t3tools/client-runtime/source-control-clients`.
11
- * A change request URL goes to the first host whose shape it has, so GitCafe, which claims
12
- * `/pull/` and `/pulls/` only on its own hostnames, claims before GitHub and Forgejo, which
13
- * claim them everywhere. A remote goes to the first host that recognises it, and a hostname can
14
- * carry more than one host's label (`gitea.github.example`), so recognition has its own order.
15
- */
9
+/** In the shared registry's order, which is the order hosts are listed and probed in. */
10
export const BUILT_IN_SOURCE_CONTROL_DRIVER_LIST = [
11
GitHubDriver.driver,
12
GitLabDriver.driver,
@@ -22,17 +16,4 @@ export const BUILT_IN_SOURCE_CONTROL_DRIVER_LIST = [
16
AzureDevOpsDriver.driver,
17
] as const;
18
25
-export const sourceControlDefinitions = makeSourceControlDefinitionRegistry(
26
- BUILT_IN_SOURCE_CONTROL_DRIVER_LIST.map((driver) => driver.definition),
27
- {
28
- claimChangeRequestUrlsFirst: [GitCafeDriver.driver.kind],
29
- recognizeRemotesIn: [
30
- GitCafeDriver.driver.kind,
31
- ForgejoDriver.driver.kind,
32
- GitHubDriver.driver.kind,
33
- GitLabDriver.driver.kind,
34
- AzureDevOpsDriver.driver.kind,
35
- BitbucketDriver.driver.kind,
36
- ],
37
- },
38
-);
19
+export const sourceControlDefinitions = builtInSourceControlDefinitions;
package.json
+1
-1
@@ -29,7 +29,7 @@
29
"check:changed": "node scripts/check-changed.ts",
30
"lint": "vp lint --report-unused-disable-directives",
31
"knip": "knip --preprocessor ./scripts/knip-schemas.ts",
32
- "knip:check": "knip --include files,dependencies --no-config-hints && knip --workspace apps/server --workspace apps/desktop --workspace apps/web --workspace packages/client-runtime --workspace packages/contracts --workspace packages/effect-acp --workspace packages/effect-codex-app-server --workspace packages/shared --workspace packages/ssh --workspace packages/tailscale --workspace packages/provider-core --workspace packages/provider-acp --workspace packages/provider-acp-registry --workspace packages/provider-testing --workspace packages/provider-pi --workspace packages/provider-muse --workspace packages/provider-opencode --workspace packages/provider-cursor --workspace packages/provider-grok --workspace packages/source-control-core --workspace packages/source-control-azure-devops --workspace packages/source-control-bitbucket --workspace packages/source-control-forgejo --workspace packages/source-control-gitcafe --workspace packages/source-control-github --workspace packages/source-control-gitlab --workspace packages/source-control-testing --exports --preprocessor ./scripts/knip-schemas.ts --no-config-hints",
32
+ "knip:check": "knip --include files,dependencies --no-config-hints && knip --workspace apps/server --workspace apps/desktop --workspace apps/web --workspace packages/client-runtime --workspace packages/contracts --workspace packages/effect-acp --workspace packages/effect-codex-app-server --workspace packages/shared --workspace packages/ssh --workspace packages/tailscale --workspace packages/provider-core --workspace packages/provider-acp --workspace packages/provider-acp-registry --workspace packages/provider-testing --workspace packages/provider-pi --workspace packages/provider-muse --workspace packages/provider-opencode --workspace packages/provider-cursor --workspace packages/provider-grok --workspace packages/source-control-core --workspace packages/source-control-definitions --workspace packages/source-control-azure-devops --workspace packages/source-control-bitbucket --workspace packages/source-control-forgejo --workspace packages/source-control-gitcafe --workspace packages/source-control-github --workspace packages/source-control-gitlab --workspace packages/source-control-testing --exports --preprocessor ./scripts/knip-schemas.ts --no-config-hints",
33
"knip:production": "knip --production --preprocessor ./scripts/knip-schemas.ts",
34
"lint:mobile": "node scripts/mobile-native-static-check.ts",
35
"test": "vp run -r test",
packages/client-runtime/package.json
+1
-6
@@ -464,13 +464,8 @@
464
"@pierre/diffs": "catalog:",
465
"@t3tools/contracts": "workspace:*",
466
"@t3tools/shared": "workspace:*",
467
- "@t3tools/source-control-azure-devops": "workspace:*",
468
- "@t3tools/source-control-bitbucket": "workspace:*",
467
"@t3tools/source-control-core": "workspace:*",
470
- "@t3tools/source-control-forgejo": "workspace:*",
471
- "@t3tools/source-control-gitcafe": "workspace:*",
472
- "@t3tools/source-control-github": "workspace:*",
473
- "@t3tools/source-control-gitlab": "workspace:*",
468
+ "@t3tools/source-control-definitions": "workspace:*",
469
"effect": "catalog:",
470
"three": "0.180.0"
471
},
packages/client-runtime/src/sourceControlClients.ts
+2
-38
@@ -1,8 +1,6 @@
1
-import * as AzureDevOps from "@t3tools/source-control-azure-devops/shared/definition";
2
-import * as Bitbucket from "@t3tools/source-control-bitbucket/shared/definition";
1
+import { builtInSourceControlDefinitions } from "@t3tools/source-control-definitions/registry";
2
import {
3
HostCredentialSettings,
5
- makeSourceControlDefinitionRegistry,
4
readSourceControlHostSettings,
5
} from "@t3tools/source-control-core/shared/definition";
6
import type { ServerSettings } from "@t3tools/contracts";
@@ -20,42 +18,8 @@ export {
18
type SourceControlHostCredentials,
19
type SourceControlIcon,
20
} from "@t3tools/source-control-core/shared/definition";
23
-import * as Forgejo from "@t3tools/source-control-forgejo/shared/definition";
24
-import * as GitCafe from "@t3tools/source-control-gitcafe/shared/definition";
25
-import * as GitHub from "@t3tools/source-control-github/shared/definition";
26
-import * as GitLab from "@t3tools/source-control-gitlab/shared/definition";
21
28
-/**
29
- * GitHub comes first because a repository that has not reported its host yet reads as GitHub,
30
- * which is what clients have always shown there. A change request URL goes to the first host
31
- * whose shape it has, so GitCafe, which claims `/pull/` and `/pulls/` only on its own hostnames,
32
- * claims before GitHub and Forgejo, which claim them everywhere. A hostname can carry more than
33
- * one host's label (`gitea.github.example`), so remote recognition has its own order. The server's
34
- * `sourceControl/sourceControlClients.ts` keeps the same order.
35
- */
36
-const BUILT_IN_SOURCE_CONTROL_CLIENTS = [
37
- GitHub.definition,
38
- GitLab.definition,
39
- GitCafe.definition,
40
- Forgejo.definition,
41
- Bitbucket.definition,
42
- AzureDevOps.definition,
43
-];
44
-
45
-export const sourceControlClients = makeSourceControlDefinitionRegistry(
46
- BUILT_IN_SOURCE_CONTROL_CLIENTS,
47
- {
48
- claimChangeRequestUrlsFirst: [GitCafe.definition.kind],
49
- recognizeRemotesIn: [
50
- GitCafe.definition.kind,
51
- Forgejo.definition.kind,
52
- GitHub.definition.kind,
53
- GitLab.definition.kind,
54
- AzureDevOps.definition.kind,
55
- Bitbucket.definition.kind,
56
- ],
57
- },
58
-);
22
+export const sourceControlClients = builtInSourceControlDefinitions;
23
24
/** The saved per-hostname accounts and tokens of a host whose definition has `credentials`. */
25
export function readHostCredentialSettings(
packages/source-control-core/src/server/PullRequestProvider.ts
+5
-6
@@ -176,6 +176,11 @@ export interface ProviderBatchedChangeRequest extends ProviderChangeRequest {
176
export interface ProviderBatchedChangeRequestPage {
177
readonly items: ReadonlyArray<ProviderBatchedChangeRequest>;
178
readonly truncated: boolean;
179
+ /**
180
+ * Repositories asked about that the page cannot vouch for: its silence about them may be an
181
+ * omission rather than an empty answer, so the caller reads each on its own.
182
+ */
183
+ readonly unresolved?: ReadonlyArray<string>;
184
}
185
186
export interface ProviderChangeRequestStat {
@@ -343,12 +348,6 @@ export interface PullRequestProviderApi {
348
* rows a page ordered by update shows. `cursor` is one boundary for all of them, so a caller
349
* with repositories standing at different boundaries asks in groups rather than in one call.
350
*/
346
- /**
347
- * Set when `listChangeRequestsAcross` can answer with silence for a repository it does not
348
- * cover rather than with an error, so a repository the read said nothing about is read on its
349
- * own before it is believed empty.
350
- */
351
- readonly listsAcrossMayOmitRepositories?: boolean;
351
readonly listChangeRequestsAcross?: (input: {
352
readonly cwd: string;
353
readonly host: string;
packages/source-control-core/src/server/SourceControlProvider.ts
+13
@@ -141,6 +141,19 @@ export class SourceControlProvider extends Context.Service<
141
readonly context: SourceControlProviderContext;
142
}) => Effect.Effect<SourceControlProviderContext | null, SourceControlProviderError>;
143
}) => Effect.Effect<RepositoryIdentity, SourceControlProviderError>;
144
+ /**
145
+ * The web host serving a remote, for a host whose remote URL may not name it (an SSH alias
146
+ * for one of several servers). It answers for the context's `requestedHost` when the caller
147
+ * narrows to one. Absent, or null, means the remote's own host is the web host.
148
+ */
149
+ readonly resolveRemoteWebHost?: (input: {
150
+ readonly cwd: string;
151
+ readonly context: SourceControlProviderContext;
152
+ readonly resolveContext: (input: {
153
+ readonly cwd: string;
154
+ readonly context: SourceControlProviderContext;
155
+ }) => Effect.Effect<SourceControlProviderContext | null, SourceControlProviderError>;
156
+ }) => Effect.Effect<string | null, SourceControlProviderError>;
157
readonly resolveLink?: ResolveSourceControlLink;
158
/**
159
* Whether `createChangeRequest` from `cwd` targets `repository`, the `owner/name` of the
packages/source-control-core/src/shared/definition.ts
-5
@@ -295,11 +295,6 @@ export interface SourceControlDefinition {
295
* equal under. Null when the key is not this host's.
296
*/
297
readonly canonicalRepositoryKey?: (key: string) => string | null;
298
- /**
299
- * An SSH remote names the SSH server only, so the web host, and its port, come from resolving
300
- * the provider rather than from the remote.
301
- */
302
- readonly sshRemoteHidesWebHost?: boolean;
298
/**
299
* Null when `url` is not media this host keeps behind a credential. A client cannot load such
300
* media itself, so it asks the server, whose driver for this host fetches it.
packages/source-control-definitions/package.json
new
+23
@@ -0,0 +1,23 @@
1
+{
2
+ "name": "@t3tools/source-control-definitions",
3
+ "private": true,
4
+ "type": "module",
5
+ "exports": {
6
+ "./registry": {
7
+ "types": "./src/registry.ts",
8
+ "import": "./src/registry.ts"
9
+ }
10
+ },
11
+ "scripts": {
12
+ "typecheck": "tsc --noEmit"
13
+ },
14
+ "dependencies": {
15
+ "@t3tools/source-control-azure-devops": "workspace:*",
16
+ "@t3tools/source-control-bitbucket": "workspace:*",
17
+ "@t3tools/source-control-core": "workspace:*",
18
+ "@t3tools/source-control-forgejo": "workspace:*",
19
+ "@t3tools/source-control-gitcafe": "workspace:*",
20
+ "@t3tools/source-control-github": "workspace:*",
21
+ "@t3tools/source-control-gitlab": "workspace:*"
22
+ }
23
+}
packages/source-control-definitions/src/registry.ts
new
+38
@@ -0,0 +1,38 @@
1
+import * as AzureDevOps from "@t3tools/source-control-azure-devops/shared/definition";
2
+import * as Bitbucket from "@t3tools/source-control-bitbucket/shared/definition";
3
+import { makeSourceControlDefinitionRegistry } from "@t3tools/source-control-core/shared/definition";
4
+import * as Forgejo from "@t3tools/source-control-forgejo/shared/definition";
5
+import * as GitCafe from "@t3tools/source-control-gitcafe/shared/definition";
6
+import * as GitHub from "@t3tools/source-control-github/shared/definition";
7
+import * as GitLab from "@t3tools/source-control-gitlab/shared/definition";
8
+
9
+/**
10
+ * The server and every client read hosts through this one registry, so they agree on which host
11
+ * owns a URL or remote. GitHub comes first because a repository that has not reported its host
12
+ * yet reads as GitHub, which is what clients have always shown there. A change request URL goes
13
+ * to the first host whose shape it has, so GitCafe, which claims `/pull/` and `/pulls/` only on
14
+ * its own hostnames, claims before GitHub and Forgejo, which claim them everywhere. A hostname can
15
+ * carry more than one host's label (`gitea.github.example`), so remote recognition has its own
16
+ * order.
17
+ */
18
+export const builtInSourceControlDefinitions = makeSourceControlDefinitionRegistry(
19
+ [
20
+ GitHub.definition,
21
+ GitLab.definition,
22
+ GitCafe.definition,
23
+ Forgejo.definition,
24
+ Bitbucket.definition,
25
+ AzureDevOps.definition,
26
+ ],
27
+ {
28
+ claimChangeRequestUrlsFirst: [GitCafe.definition.kind],
29
+ recognizeRemotesIn: [
30
+ GitCafe.definition.kind,
31
+ Forgejo.definition.kind,
32
+ GitHub.definition.kind,
33
+ GitLab.definition.kind,
34
+ AzureDevOps.definition.kind,
35
+ Bitbucket.definition.kind,
36
+ ],
37
+ },
38
+);
packages/source-control-definitions/tsconfig.json
new
+4
@@ -0,0 +1,4 @@
1
+{
2
+ "extends": "../../tsconfig.base.json",
3
+ "include": ["src"]
4
+}
packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts
+68
@@ -649,6 +649,74 @@ it.effect("gives Forgejo identities the browser URL of the login that serves the
649
),
650
);
651
652
+it.effect("names the web server behind an SSH alias from the login that serves it", () =>
653
+ Effect.gen(function* () {
654
+ const provider = yield* ForgejoSourceControlProvider.make;
655
+ const resolveWebHost = provider.resolveRemoteWebHost!;
656
+ const asked: Array<string | undefined> = [];
657
+ const context = (remoteUrl: string) => ({
658
+ provider: {
659
+ kind: SourceControlProviderKind.make("forgejo"),
660
+ name: "Forgejo",
661
+ baseUrl: "https://ssh.forge.test",
662
+ },
663
+ remoteName: "origin",
664
+ remoteUrl,
665
+ requestedHost: "forge.test:3000",
666
+ });
667
+ const served = (input: {
668
+ readonly context: { readonly requestedHost?: string; readonly provider: { kind: string } };
669
+ }) =>
670
+ Effect.sync(() => {
671
+ asked.push(input.context.requestedHost);
672
+ assert.strictEqual(input.context.provider.kind, "unknown");
673
+ return {
674
+ provider: {
675
+ kind: SourceControlProviderKind.make("forgejo"),
676
+ name: "Forgejo / Gitea",
677
+ baseUrl: "http://forge.test:3000/git",
678
+ },
679
+ remoteName: "origin",
680
+ remoteUrl: "",
681
+ };
682
+ });
683
+
684
+ assert.strictEqual(
685
+ yield* resolveWebHost({
686
+ cwd: "/repo",
687
+ context: context("git@ssh.forge.test:team/repo.git"),
688
+ resolveContext: served,
689
+ }),
690
+ "forge.test:3000",
691
+ );
692
+ assert.deepStrictEqual(asked, ["forge.test:3000"]);
693
+ assert.strictEqual(
694
+ yield* resolveWebHost({
695
+ cwd: "/repo",
696
+ context: context("https://forge.test:3000/team/repo.git"),
697
+ resolveContext: () => Effect.die("an HTTP remote names its own web host"),
698
+ }),
699
+ null,
700
+ );
701
+ assert.strictEqual(
702
+ yield* resolveWebHost({
703
+ cwd: "/repo",
704
+ context: context("git@ssh.forge.test:team/repo.git"),
705
+ resolveContext: () => Effect.succeed(null),
706
+ }),
707
+ null,
708
+ );
709
+ }).pipe(
710
+ Effect.provide(
711
+ Layer.mergeAll(
712
+ Layer.succeed(FileSystem.FileSystem, FileSystem.makeNoop({})),
713
+ TestSourceControlHost.layer(),
714
+ Layer.mock(ForgejoCli.ForgejoCli)({}),
715
+ ),
716
+ ),
717
+ ),
718
+);
719
+
720
it.effect("loads Forgejo pull request references from files and commits views", () =>
721
Effect.gen(function* () {
722
const provider = yield* ForgejoSourceControlProvider.make;
packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.ts
+22
@@ -234,6 +234,27 @@ const refineRepositoryIdentity: NonNullable<
234
};
235
});
236
237
+// An SSH remote names an alias rather than the web server, so the login that serves it says which.
238
+const resolveRemoteWebHost: NonNullable<
239
+ SourceControlProvider.SourceControlProvider["Service"]["resolveRemoteWebHost"]
240
+> = Effect.fn("ForgejoSourceControlProvider.resolveRemoteWebHost")(function* ({
241
+ cwd,
242
+ context,
243
+ resolveContext,
244
+}) {
245
+ if (!ForgejoCli.parseForgejoRemote(context.remoteUrl)?.ssh) return null;
246
+ const resolved = yield* resolveContext({
247
+ cwd,
248
+ context: {
249
+ ...context,
250
+ provider: { kind: SourceControlProviderKind.make("unknown"), name: "Unknown", baseUrl: "" },
251
+ },
252
+ });
253
+ return resolved?.provider.kind === "forgejo"
254
+ ? new URL(resolved.provider.baseUrl).host.toLowerCase()
255
+ : null;
256
+});
257
+
258
const RepositorySchema = Schema.Struct({
259
full_name: Schema.String,
260
clone_url: Schema.String,
@@ -306,6 +327,7 @@ export const make = Effect.gen(function* () {
327
kind: SourceControlProviderKind.make("forgejo"),
328
repositoryNameFromRemoteUrl,
329
refineRepositoryIdentity,
330
+ resolveRemoteWebHost,
331
listChangeRequests: Effect.fn("ForgejoSourceControlProvider.listChangeRequests")(
332
function* (input) {
333
const repo = yield* cli.resolveRepository(input);
packages/source-control-forgejo/src/shared/definition.ts
-1
@@ -107,7 +107,6 @@ export const definition = defineSourceControl({
107
}
108
: null,
109
changeRequestHost,
110
- sshRemoteHidesWebHost: true,
110
isChangeRequestInRepository: (identity, link) => {
111
if (!isChangeRequestAuthorityOfProject(identity, link)) return false;
112
const web = resolvedWebUrl(identity);
packages/source-control-github/src/server/GitHubPullRequestProvider.test.ts
+169
-1
@@ -2,12 +2,16 @@ import { describe, expect, it } from "@effect/vitest";
2
import * as Effect from "effect/Effect";
3
import * as Layer from "effect/Layer";
4
import * as Redacted from "effect/Redacted";
5
+import * as TestClock from "effect/testing/TestClock";
6
import type { PullRequestReaction } from "@t3tools/contracts";
7
8
import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts";
9
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
10
import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts";
10
-import type { GitHubPullRequestCore } from "./gitHubPullRequestJson.ts";
11
+import type {
12
+ GitHubPullRequestCore,
13
+ GitHubPullRequestSearchItem,
14
+} from "./gitHubPullRequestJson.ts";
15
import { gitHubViewerPermissions, loginAvatarUrl, make } from "./GitHubPullRequestProvider.ts";
16
import type { GitHubReviewThreadComments } from "./gitHubPullRequestJson.ts";
17
@@ -770,3 +774,167 @@ describe("loginAvatarUrl", () => {
774
}
775
});
776
});
777
+
778
+describe("listChangeRequestsAcross", () => {
779
+ const searched = (repository: string): GitHubPullRequestSearchItem => ({
780
+ repository,
781
+ number: 1,
782
+ title: "Change",
783
+ url: `https://github.com/${repository}/pull/1`,
784
+ author: null,
785
+ authorId: null,
786
+ headBranch: "feature",
787
+ baseBranch: "main",
788
+ state: "open" as const,
789
+ isDraft: false,
790
+ mergeability: "mergeable" as const,
791
+ additions: 0,
792
+ deletions: 0,
793
+ createdAt: "2026-07-01T00:00:00Z",
794
+ updatedAt: "2026-07-02T00:00:00Z",
795
+ reviewRequestLogins: [],
796
+ hasTeamReviewRequest: false,
797
+ labels: [],
798
+ reviewDecision: null,
799
+ checksState: null,
800
+ });
801
+ const across = {
802
+ cwd: "/w",
803
+ host: "github.com",
804
+ repositories: ["acme/web", "acme/docs"],
805
+ state: "open" as const,
806
+ involvement: "all" as const,
807
+ viewer: "viewer",
808
+ limit: 50,
809
+ };
810
+ type Search = Parameters<
811
+ GitHubPullRequestApi.GitHubPullRequestApi["Service"]["searchPullRequests"]
812
+ >[0];
813
+ // Pages answer in turn; a one-repository check for any pull request answers from `covered`.
814
+ const providerAnswering = (input: {
815
+ readonly pages: Array<GitHubPullRequestApi.GitHubPullRequestSearchBatch>;
816
+ readonly covered?: (repository: string) => boolean;
817
+ readonly checks?: Array<string>;
818
+ }) =>
819
+ make.pipe(
820
+ Effect.provide(
821
+ Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
822
+ searchPullRequests: (search: Search) => {
823
+ if (search.state !== "all") return Effect.succeed(input.pages.shift()!);
824
+ const repository = search.repositories[0]!;
825
+ input.checks?.push(repository);
826
+ return input.covered === undefined
827
+ ? Effect.fail(
828
+ new GitHubPullRequestApi.GitHubViewerLoginUnavailableError({ cwd: search.cwd }),
829
+ )
830
+ : Effect.succeed({
831
+ items: input.covered(repository) ? [searched(repository)] : [],
832
+ truncated: false,
833
+ });
834
+ },
835
+ }),
836
+ ),
837
+ Effect.map((provider) => provider.listChangeRequestsAcross!),
838
+ );
839
+
840
+ it.effect("believes silence about a repository search covers, and checks it once", () =>
841
+ Effect.gen(function* () {
842
+ const checks: Array<string> = [];
843
+ const read = yield* providerAnswering({
844
+ pages: [
845
+ { items: [searched("acme/web")], truncated: false },
846
+ { items: [searched("acme/web")], truncated: false },
847
+ ],
848
+ covered: () => true,
849
+ checks,
850
+ });
851
+ expect((yield* read(across)).unresolved).toEqual([]);
852
+ expect((yield* read(across)).unresolved).toEqual([]);
853
+ expect(checks).toEqual(["acme/docs"]);
854
+ }),
855
+ );
856
+
857
+ it.effect("leaves a repository search does not cover to be read on its own", () =>
858
+ Effect.gen(function* () {
859
+ const read = yield* providerAnswering({
860
+ pages: [{ items: [searched("acme/web")], truncated: false }],
861
+ covered: (repository) => repository !== "acme/docs",
862
+ });
863
+ expect((yield* read(across)).unresolved).toEqual(["acme/docs"]);
864
+ }),
865
+ );
866
+
867
+ it.effect("does not check a repository a page had rows for", () =>
868
+ Effect.gen(function* () {
869
+ const checks: Array<string> = [];
870
+ const read = yield* providerAnswering({
871
+ pages: [
872
+ { items: [searched("acme/web"), searched("acme/docs")], truncated: false },
873
+ { items: [searched("acme/web")], truncated: false },
874
+ ],
875
+ covered: () => true,
876
+ checks,
877
+ });
878
+ yield* read(across);
879
+ expect((yield* read(across)).unresolved).toEqual([]);
880
+ expect(checks).toEqual([]);
881
+ }),
882
+ );
883
+
884
+ it.effect("vouches for no silent repository in a truncated first slice", () =>
885
+ Effect.gen(function* () {
886
+ const read = yield* providerAnswering({
887
+ pages: [{ items: [searched("acme/web")], truncated: true }],
888
+ covered: () => true,
889
+ });
890
+ expect((yield* read(across)).unresolved).toEqual(["acme/docs"]);
891
+ }),
892
+ );
893
+
894
+ it.effect("past the first slice, believes a covered repository and not an uncovered one", () =>
895
+ Effect.gen(function* () {
896
+ const read = yield* providerAnswering({
897
+ pages: [
898
+ { items: [searched("acme/web")], truncated: true },
899
+ { items: [searched("acme/web")], truncated: true },
900
+ ],
901
+ covered: (repository) => repository !== "acme/renamed",
902
+ });
903
+ const later = { ...across, cursor: { updatedBefore: "2026-07-02T00:00:00Z", delivered: 1 } };
904
+ expect((yield* read(later)).unresolved).toEqual([]);
905
+ expect(
906
+ (yield* read({ ...later, repositories: ["acme/web", "acme/renamed"] })).unresolved,
907
+ ).toEqual(["acme/renamed"]);
908
+ }),
909
+ );
910
+
911
+ it.effect("asks again after a failed check, and after coverage has had time to change", () =>
912
+ Effect.gen(function* () {
913
+ const checks: Array<string> = [];
914
+ const failing = yield* providerAnswering({
915
+ pages: [
916
+ { items: [searched("acme/web")], truncated: false },
917
+ { items: [searched("acme/web")], truncated: false },
918
+ ],
919
+ checks,
920
+ });
921
+ expect((yield* failing(across)).unresolved).toEqual(["acme/docs"]);
922
+ expect((yield* failing(across)).unresolved).toEqual(["acme/docs"]);
923
+ expect(checks).toEqual(["acme/docs", "acme/docs"]);
924
+
925
+ checks.length = 0;
926
+ const read = yield* providerAnswering({
927
+ pages: [
928
+ { items: [searched("acme/web")], truncated: false },
929
+ { items: [searched("acme/web")], truncated: false },
930
+ ],
931
+ covered: () => true,
932
+ checks,
933
+ });
934
+ yield* read(across);
935
+ yield* TestClock.adjust("61 minutes");
936
+ yield* read(across);
937
+ expect(checks).toEqual(["acme/docs", "acme/docs"]);
938
+ }),
939
+ );
940
+});
packages/source-control-github/src/server/GitHubPullRequestProvider.ts
+100
-9
@@ -1,5 +1,10 @@
1
import { SourceControlProviderKind } from "@t3tools/contracts";
2
import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage";
3
+import * as Cache from "effect/Cache";
4
+import * as Duration from "effect/Duration";
5
+import * as Equal from "effect/Equal";
6
+import * as Exit from "effect/Exit";
7
+import * as Hash from "effect/Hash";
8
import * as Effect from "effect/Effect";
9
import type {
10
PullRequestActor,
@@ -185,12 +190,98 @@ export function loginAvatarUrl(login: string, host: string): string | null {
190
return /^[a-z0-9][a-z0-9-]{0,38}$/iu.test(login) ? `https://${host}/${login}.png?size=80` : null;
191
}
192
193
+// Whether search covers a repository changes only when the repository is renamed or moved.
194
+const SEARCH_COVERAGE_TTL = Duration.hours(1);
195
+const SEARCH_COVERAGE_CAPACITY = 4096;
196
+const SEARCH_COVERAGE_CONCURRENCY = 4;
197
+
198
+/**
199
+ * Whether GitHub search covers a repository. `cwd` only says where to ask, so equality ignores
200
+ * it and `lookup`, and every checkout of a repository shares one answer.
201
+ */
202
+class SearchCoverage {
203
+ readonly key: string;
204
+ readonly lookup: Effect.Effect<boolean, GitHubPullRequestApi.GitHubPullRequestApiError>;
205
+
206
+ constructor(
207
+ host: string,
208
+ repository: string,
209
+ lookup: Effect.Effect<boolean, GitHubPullRequestApi.GitHubPullRequestApiError>,
210
+ ) {
211
+ this.key = `${host.toLowerCase()}\n${repository.trim().toLowerCase()}`;
212
+ this.lookup = lookup;
213
+ }
214
+
215
+ [Equal.symbol](that: unknown): boolean {
216
+ return that instanceof SearchCoverage && that.key === this.key;
217
+ }
218
+ [Hash.symbol](): number {
219
+ return Hash.string(this.key);
220
+ }
221
+}
222
+
223
const rendersEmpty = (body: string): boolean =>
224
body.replace(/<!--[\s\S]*?-->/g, "").trim().length === 0;
225
226
export const make = Effect.gen(function* () {
227
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
228
229
+ // GitHub does not index every repository for search: a renamed one answers for its old name
230
+ // with silence rather than with an error. Where search has any pull request for a repository it
231
+ // covers it, so its silence on a page is a real empty answer; any other the caller reads on its
232
+ // own. A failed check is not kept.
233
+ const searchCoverage = yield* Cache.makeWith((request: SearchCoverage) => request.lookup, {
234
+ capacity: SEARCH_COVERAGE_CAPACITY,
235
+ timeToLive: (exit) => (Exit.isSuccess(exit) ? SEARCH_COVERAGE_TTL : Duration.zero),
236
+ });
237
+ const coverageOf = (
238
+ input: Parameters<NonNullable<PullRequestProviderApi["listChangeRequestsAcross"]>>[0],
239
+ repository: string,
240
+ ) =>
241
+ new SearchCoverage(
242
+ input.host,
243
+ repository,
244
+ Effect.suspend(() =>
245
+ cli.searchPullRequests({
246
+ cwd: input.cwd,
247
+ host: input.host,
248
+ repositories: [repository],
249
+ state: "all",
250
+ involvement: "all",
251
+ viewer: input.viewer,
252
+ limit: 1,
253
+ }),
254
+ ).pipe(Effect.map((batch) => batch.items.length > 0)),
255
+ );
256
+ const unresolvedBySearch = Effect.fn("GitHubPullRequestProvider.unresolvedBySearch")(function* (
257
+ input: Parameters<NonNullable<PullRequestProviderApi["listChangeRequestsAcross"]>>[0],
258
+ batch: GitHubPullRequestApi.GitHubPullRequestSearchBatch,
259
+ ) {
260
+ // A repository a page has rows for is covered without asking.
261
+ yield* Effect.forEach(
262
+ batch.items,
263
+ (item) => Cache.set(searchCoverage, coverageOf(input, item.repository), true),
264
+ { discard: true },
265
+ );
266
+ const answered = new Set(batch.items.map((item) => item.repository.trim().toLowerCase()));
267
+ const silent = input.repositories.filter(
268
+ (repository) => !answered.has(repository.trim().toLowerCase()),
269
+ );
270
+ // A truncated first slice can be silent about a covered repository whose rows are just
271
+ // older than the cut, so it vouches for none. Past the first slice silence from a covered
272
+ // repository means its rows are older rather than absent.
273
+ if (input.cursor === undefined && batch.truncated) return silent;
274
+ return yield* Effect.filter(
275
+ silent,
276
+ (repository) =>
277
+ Cache.get(searchCoverage, coverageOf(input, repository)).pipe(
278
+ Effect.map((covered) => !covered),
279
+ Effect.orElseSucceed(() => true),
280
+ ),
281
+ { concurrency: SEARCH_COVERAGE_CONCURRENCY },
282
+ );
283
+ });
284
+
285
const fail = (operation: string) => (error: GitHubPullRequestApi.GitHubPullRequestApiError) =>
286
new PullRequestProviderError({
287
provider: SourceControlProviderKind.make("github"),
@@ -306,9 +397,6 @@ export const make = Effect.gen(function* () {
397
),
398
),
399
309
- // GitHub does not index every repository for search: a renamed one answers for its old name
310
- // with silence rather than with an error.
311
- listsAcrossMayOmitRepositories: true,
400
listChangeRequestsAcross: (input) =>
401
cli
402
.searchPullRequests({
@@ -325,13 +413,16 @@ export const make = Effect.gen(function* () {
413
})
414
.pipe(
415
Effect.mapError(fail("listChangeRequestsAcross")),
328
- Effect.map((batch) => ({
329
- truncated: batch.truncated,
330
- items: batch.items.map((item) => ({
331
- ...item,
332
- author: withAvatar(item.author, new Map<string, string>(), input.host),
416
+ Effect.flatMap((batch) =>
417
+ Effect.map(unresolvedBySearch(input, batch), (unresolved) => ({
418
+ truncated: batch.truncated,
419
+ items: batch.items.map((item) => ({
420
+ ...item,
421
+ author: withAvatar(item.author, new Map<string, string>(), input.host),
422
+ })),
423
+ unresolved,
424
})),
334
- })),
425
+ ),
426
),
427
428
listChangeRequestStats: (input) =>
pnpm-lock.yaml
+29
-17
@@ -891,6 +891,9 @@ importers:
891
'@t3tools/source-control-core':
892
specifier: workspace:*
893
version: link:../../packages/source-control-core
894
+ '@t3tools/source-control-definitions':
895
+ specifier: workspace:*
896
+ version: link:../../packages/source-control-definitions
897
'@t3tools/source-control-forgejo':
898
specifier: workspace:*
899
version: link:../../packages/source-control-forgejo
@@ -1339,27 +1342,12 @@ importers:
1342
'@t3tools/shared':
1343
specifier: workspace:*
1344
version: link:../shared
1342
- '@t3tools/source-control-azure-devops':
1343
- specifier: workspace:*
1344
- version: link:../source-control-azure-devops
1345
- '@t3tools/source-control-bitbucket':
1346
- specifier: workspace:*
1347
- version: link:../source-control-bitbucket
1345
'@t3tools/source-control-core':
1346
specifier: workspace:*
1347
version: link:../source-control-core
1351
- '@t3tools/source-control-forgejo':
1352
- specifier: workspace:*
1353
- version: link:../source-control-forgejo
1354
- '@t3tools/source-control-gitcafe':
1348
+ '@t3tools/source-control-definitions':
1349
specifier: workspace:*
1356
- version: link:../source-control-gitcafe
1357
- '@t3tools/source-control-github':
1358
- specifier: workspace:*
1359
- version: link:../source-control-github
1360
- '@t3tools/source-control-gitlab':
1361
- specifier: workspace:*
1362
- version: link:../source-control-gitlab
1350
+ version: link:../source-control-definitions
1351
effect:
1352
specifier: 4.0.5
1353
version: 4.0.5
@@ -1906,6 +1894,30 @@ importers:
1894
specifier: 1.3.0
1895
version: 1.3.0(@astrojs/compiler-rs@0.5.1)(@tsc-rs/typescript@0.8.0)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(@tsc-rs/typescript@0.8.0))(bufferutil@4.1.0)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.60.0)(terser@5.48.0)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0)
1896
1897
+ packages/source-control-definitions:
1898
+ dependencies:
1899
+ '@t3tools/source-control-azure-devops':
1900
+ specifier: workspace:*
1901
+ version: link:../source-control-azure-devops
1902
+ '@t3tools/source-control-bitbucket':
1903
+ specifier: workspace:*
1904
+ version: link:../source-control-bitbucket
1905
+ '@t3tools/source-control-core':
1906
+ specifier: workspace:*
1907
+ version: link:../source-control-core
1908
+ '@t3tools/source-control-forgejo':
1909
+ specifier: workspace:*
1910
+ version: link:../source-control-forgejo
1911
+ '@t3tools/source-control-gitcafe':
1912
+ specifier: workspace:*
1913
+ version: link:../source-control-gitcafe
1914
+ '@t3tools/source-control-github':
1915
+ specifier: workspace:*
1916
+ version: link:../source-control-github
1917
+ '@t3tools/source-control-gitlab':
1918
+ specifier: workspace:*
1919
+ version: link:../source-control-gitlab
1920
+
1921
packages/source-control-forgejo:
1922
dependencies:
1923
'@t3tools/contracts':
scripts/release-smoke.ts
+1
@@ -40,6 +40,7 @@ const workspaceFiles = [
40
"packages/source-control-azure-devops/package.json",
41
"packages/source-control-bitbucket/package.json",
42
"packages/source-control-core/package.json",
43
+ "packages/source-control-definitions/package.json",
44
"packages/source-control-forgejo/package.json",
45
"packages/source-control-gitcafe/package.json",
46
"packages/source-control-github/package.json",