Skip to content

Commit 435b998

Browse files
refactor(source-control): PullRequestService reads GitHub capabilities, not its kind
PullRequestService checked `kind === "github"` for routing identity, verified credentials, and merge-message cleanup, and imported GitHub's AllowGitHubReserve to mark interactive requests. - AllowGitHubReserve becomes core's SourceControlRateLimit.Interactive: the same reference (default false), named for what callers mean. GitHub's API reads it to decide whether a request may spend the GraphQL reserve, so the server no longer imports from a host package. - Routing identity and verified credentials go to whichever provider implements getRoutingIdentity / withVerifiedCredential. Only GitHub does, so behavior is unchanged. - removesAgentCreditsOnMerge is a PullRequestProviderApi capability GitHub sets; the rate-limit wrapper passes it through. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 604a15a commit 435b998

11 files changed

Lines changed: 46 additions & 31 deletions

File tree

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ function fakeProvider(
463463
setReaction: () => Effect.void,
464464
listReviewerCandidates: () => Effect.succeed({ candidates: [], truncated: false }),
465465
setReviewerRequest: () => Effect.void,
466+
// GitHub's own capability, which the service reads instead of the kind.
467+
...(kind === "github" ? { removesAgentCreditsOnMerge: true } : {}),
466468
...overrides,
467469
};
468470
}

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ import {
7777
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
7878
import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
7979

80-
import { AllowGitHubReserve } from "@t3tools/source-control-github/server/GitHubApi";
8180
import * as ProjectService from "../project/ProjectService.ts";
8281
import * as ServerSettings from "../serverSettings.ts";
8382
import * as PullRequestFilesViewed from "../persistence/PullRequestFilesViewed.ts";
@@ -557,7 +556,7 @@ function withRateLimitBackoff(
557556
),
558557
Effect.flatMap((lease) =>
559558
effect.pipe(
560-
Effect.provideService(AllowGitHubReserve, allowPaused),
559+
Effect.provideService(SourceControlRateLimit.Interactive, allowPaused),
561560
Effect.tap(() => limits.recordSuccess({ ...key, lease })),
562561
Effect.tapError((error) =>
563562
error.reason === "rate-limited"
@@ -587,6 +586,9 @@ function withRateLimitBackoff(
587586
const wrapped = {
588587
kind: api.kind,
589588
capabilities: api.capabilities,
589+
...(api.removesAgentCreditsOnMerge === undefined
590+
? {}
591+
: { removesAgentCreditsOnMerge: api.removesAgentCreditsOnMerge }),
590592
// Refused during a pause like any other read, except for the caller that asks for the
591593
// bypass: a lookup that failed is not held, so letting every background read through would
592594
// spawn this host's CLI on each of them and re-extend the pause it was already in.
@@ -1558,8 +1560,10 @@ export const make = Effect.gen(function* () {
15581560
)(function* (input) {
15591561
const host = input.host.toLowerCase();
15601562
const { supported } = yield* listWorkspaceProjects({ host });
1561-
const project = supported.find((candidate) => candidate.api.kind === "github");
1562-
const api = registry.get("github");
1563+
const project = supported.find(
1564+
(candidate) => registry.get(candidate.api.kind)?.getRoutingIdentity !== undefined,
1565+
);
1566+
const api = project === undefined ? null : registry.get(project.api.kind);
15631567
if (project === undefined || api?.getRoutingIdentity === undefined) {
15641568
return yield* new PullRequestUnavailableError({ reason: "provider-unsupported" });
15651569
}
@@ -1569,6 +1573,7 @@ export const make = Effect.gen(function* () {
15691573
host,
15701574
})
15711575
.pipe(Effect.mapError(toPullRequestError("routeIdentity")));
1576+
// Only GitHub reports a routing identity, and the contract names it.
15721577
return { ...identity, host, provider: "github" as const };
15731578
});
15741579

@@ -1584,7 +1589,7 @@ export const make = Effect.gen(function* () {
15841589
detail: "The GitHub account could not be verified before starting the operation.",
15851590
});
15861591
const project = yield* requireProject(input).pipe(Effect.mapError(rejected));
1587-
const api = project.api.kind === "github" ? registry.get("github") : null;
1592+
const api = registry.get(project.api.kind);
15881593
if (
15891594
api?.withVerifiedCredential === undefined ||
15901595
input.host?.toLowerCase() !== project.host.toLowerCase()
@@ -1605,7 +1610,7 @@ export const make = Effect.gen(function* () {
16051610

16061611
const routing = Effect.fn("PullRequestService.routing")(function* (input: PullRequestRef) {
16071612
const project = yield* requireProject(input);
1608-
const api = project.api.kind === "github" ? registry.get("github") : null;
1613+
const api = registry.get(project.api.kind);
16091614
if (api?.getRoutingIdentity === undefined) {
16101615
return yield* new PullRequestUnavailableError({ reason: "provider-unsupported" });
16111616
}
@@ -2047,7 +2052,7 @@ export const make = Effect.gen(function* () {
20472052
);
20482053
}
20492054
const mergeSettings =
2050-
project.api.kind === "github" &&
2055+
project.api.removesAgentCreditsOnMerge === true &&
20512056
input.stackNumber === undefined &&
20522057
(input.action === "merge" || input.action === "enable-auto-merge")
20532058
? serverSettings.getSettings.pipe(

‎packages/source-control-core/src/server/PullRequestProvider.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ export interface PullRequestProviderApi {
345345
>;
346346
readonly kind: SourceControlProviderKind;
347347
readonly capabilities: PullRequestCapabilities;
348+
/**
349+
* Whether a merge may strip agent credits from the merge message when the project asks for
350+
* it. Server-side only: clients see the effect, not the switch.
351+
*/
352+
readonly removesAgentCreditsOnMerge?: boolean;
348353

349354
/** The signed-in account, which is what involvement filtering compares against. */
350355
readonly getViewer: (input: {
@@ -561,7 +566,7 @@ export interface PullRequestProviderApi {
561566
readonly action: PullRequestAction;
562567
readonly stackNumber?: number;
563568
readonly expectedStackHeads?: ReadonlyArray<PullRequestStackHead>;
564-
/** GitHub merge message cleanup; ignored by hosts without support. */
569+
/** Merge message cleanup; only sent to a host with `removesAgentCreditsOnMerge`. */
565570
readonly removeAgentCreditsOnMerge?: boolean;
566571
/** Meaningful for `merge` and `enable-auto-merge`; absent takes the host's own default. */
567572
readonly mergeMethod?: PullRequestMergeMethod;

‎packages/source-control-core/src/server/SourceControlRateLimit.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ export const CredentialScope = Context.Reference<string>(
2020
},
2121
);
2222

23+
/**
24+
* Set by interactive callers (a user's read or write, not a background sweep). A host may let
25+
* requests made under it spend a reserved quota and go through a rate-limit pause: a user acting
26+
* on a change request should not be refused because a background read exhausted the quota.
27+
*/
28+
export const Interactive = Context.Reference<boolean>(
29+
"@t3tools/source-control-core/server/SourceControlRateLimit/Interactive",
30+
{ defaultValue: () => false },
31+
);
32+
2333
interface RateLimitKey {
2434
readonly provider: SourceControlProviderKind;
2535
readonly host: string;

‎packages/source-control-github/src/server/GitHubApi.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ describe("GitHubApi", () => {
275275
retryAt: reset * 1000,
276276
});
277277
// A user's own request may spend the reserve, and GraphQL has a quota of its own.
278-
yield* api.rest(read).pipe(Effect.provideService(GitHubApi.AllowGitHubReserve, true));
278+
yield* api.rest(read).pipe(Effect.provideService(SourceControlRateLimit.Interactive, true));
279279
yield* api.graphql({ host: "github.com", operation: "x", query: "query { viewer { id } }" });
280280
expect(requests).toHaveLength(3);
281281
// The reset gives the background its quota back.
@@ -344,7 +344,7 @@ describe("GitHubApi", () => {
344344
method: "PUT",
345345
path: "repos/acme/web/pulls/7/merge",
346346
})
347-
.pipe(Effect.provideService(GitHubApi.AllowGitHubReserve, true));
347+
.pipe(Effect.provideService(SourceControlRateLimit.Interactive, true));
348348
expect(merged.status).toBe(200);
349349
expect(requests).toHaveLength(2);
350350
}).pipe(Effect.provide(layer));

‎packages/source-control-github/src/server/GitHubApi.ts‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ export const PinnedGitHubCredential = Context.Reference<{
3434
defaultValue: () => null,
3535
});
3636

37-
/**
38-
* Set by interactive callers (a user's read or write, not a background sweep). Requests made
39-
* under it may spend the GraphQL reserve and go through a rate-limit pause: a user acting on a
40-
* pull request should not be refused because a background read exhausted the quota.
41-
*/
42-
export const AllowGitHubReserve = Context.Reference<boolean>(
43-
"@t3tools/source-control-github/server/GitHubApi/AllowGitHubReserve",
44-
{ defaultValue: () => false },
45-
);
46-
4737
export class GitHubApiRequestError extends Schema.TaggedError<GitHubApiRequestError>()(
4838
"GitHubApiRequestError",
4939
{ host: Schema.String, operation: Schema.String, cause: Schema.Defect() },
@@ -137,7 +127,7 @@ export interface GitHubRestInput {
137127
readonly maxResponseBytes?: number;
138128
/** Defaults to 30 seconds; a whole pull request's patch may need longer. */
139129
readonly timeout?: Duration.Input;
140-
/** Overrides `AllowGitHubReserve` for this one request. */
130+
/** Overrides `SourceControlRateLimit.Interactive` for this one request. */
141131
readonly allowReserve?: boolean;
142132
}
143133

@@ -525,7 +515,7 @@ export const make = Effect.gen(function* () {
525515
input.body === undefined
526516
? withEtag
527517
: withEtag.pipe(HttpClientRequest.bodyJsonUnsafe(input.body));
528-
return AllowGitHubReserve.pipe(
518+
return SourceControlRateLimit.Interactive.pipe(
529519
Effect.flatMap((interactive) =>
530520
send({
531521
host: input.host,
@@ -545,7 +535,7 @@ export const make = Effect.gen(function* () {
545535
const host = normalizeHost(input.host);
546536
const { fingerprint } = yield* credential(host);
547537
const scope = (yield* SourceControlRateLimit.CredentialScope) || fingerprint;
548-
const allowReserve = input.allowReserve ?? (yield* AllowGitHubReserve);
538+
const allowReserve = input.allowReserve ?? (yield* SourceControlRateLimit.Interactive);
549539
// The document, never its variables: user text (bodies, search terms) travels as variables.
550540
yield* Effect.annotateCurrentSpan({
551541
"github.operation": input.operation,

‎packages/source-control-github/src/server/GitHubPullRequestApi.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import * as Redacted from "effect/Redacted";
1212

1313
import { HttpClient, HttpClientResponse } from "effect/http";
1414

15-
import { AllowGitHubReserve } from "./GitHubApi.ts";
1615
import * as GitHubApi from "./GitHubApi.ts";
1716
import * as GitHubCredentials from "./GitHubCredentials.ts";
1817
import * as GitHubQuota from "./GitHubQuota.ts";
@@ -78,7 +77,7 @@ const mockApi = Layer.effect(
7877
const quota = yield* GitHubQuota.GitHubQuota;
7978
return GitHubApi.GitHubApi.of({
8079
graphql: (input) =>
81-
GitHubApi.AllowGitHubReserve.pipe(
80+
SourceControlRateLimit.Interactive.pipe(
8281
Effect.flatMap((interactive) =>
8382
quota.admit(input.host, "graphql", {
8483
allowReserve: input.allowReserve ?? interactive,
@@ -3864,7 +3863,9 @@ layer("GitHubPullRequestApi.layer", (it) => {
38643863
const error = yield* Effect.flip(cli.getPullRequestDetail(input));
38653864
expect(error._tag).toBe("SourceControlRateLimitPausedError");
38663865
expect(mockedExecute).toHaveBeenCalledOnce();
3867-
yield* cli.getPullRequestDetail(input).pipe(Effect.provideService(AllowGitHubReserve, true));
3866+
yield* cli
3867+
.getPullRequestDetail(input)
3868+
.pipe(Effect.provideService(SourceControlRateLimit.Interactive, true));
38683869
expect(mockedExecute).toHaveBeenCalledTimes(2);
38693870
}),
38703871
);

‎packages/source-control-github/src/server/GitHubPullRequestApi.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ export const make = Effect.gen(function* () {
15261526

15271527
const getPullRequestDetail: GitHubPullRequestApi["Service"]["getPullRequestDetail"] = (input) => {
15281528
const { owner, name } = parseRepositorySelector(input.repository);
1529-
return GitHubApi.AllowGitHubReserve.pipe(
1529+
return SourceControlRateLimit.Interactive.pipe(
15301530
Effect.flatMap((allowReserve) =>
15311531
graphqlRead({
15321532
allowReserve,

‎packages/source-control-github/src/server/GitHubPullRequestProvider.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { PullRequestReaction } from "@t3tools/contracts";
77

88
import { decodePullRequestDetailJson } from "./gitHubPullRequestJson.ts";
99
import * as GitHubApi from "./GitHubApi.ts";
10+
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
1011
import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts";
1112
import type { GitHubPullRequestCore } from "./gitHubPullRequestJson.ts";
1213
import { gitHubViewerPermissions, loginAvatarUrl, make } from "./GitHubPullRequestProvider.ts";
@@ -725,7 +726,7 @@ describe("getViewerPermissions", () => {
725726
Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
726727
revalidateChecks: (_input, read) => read,
727728
getPullRequestDetail: () =>
728-
GitHubApi.AllowGitHubReserve.pipe(
729+
SourceControlRateLimit.Interactive.pipe(
729730
Effect.tap((allowReserve) => Effect.sync(() => onDetail(allowReserve))),
730731
Effect.flatMap(() => detail),
731732
),

‎packages/source-control-github/src/server/GitHubPullRequestProvider.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
PullRequestViewerPermissions,
88
} from "@t3tools/contracts";
99

10-
import * as GitHubApi from "./GitHubApi.ts";
10+
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
1111
import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts";
1212
import {
1313
PullRequestProviderError,
@@ -262,6 +262,7 @@ export const make = Effect.gen(function* () {
262262
const provider: PullRequestProviderApi = {
263263
kind: "github",
264264
capabilities: CAPABILITIES,
265+
removesAgentCreditsOnMerge: true,
265266
getRoutingIdentity: (input) =>
266267
cli.getRoutingIdentity(input).pipe(Effect.mapError(fail("routeIdentity"))),
267268
withVerifiedCredential: (input, use) =>
@@ -526,7 +527,7 @@ export const make = Effect.gen(function* () {
526527
// comparison, so one read usually answers what used to take three. When that heavier read
527528
// fails, the light access read still answers, withholding only update-branch.
528529
return cli.getPullRequestDetail(input).pipe(
529-
Effect.provideService(GitHubApi.AllowGitHubReserve, true),
530+
Effect.provideService(SourceControlRateLimit.Interactive, true),
530531
Effect.map((pullRequest) =>
531532
gitHubViewerPermissions({
532533
...pullRequest.viewerAccess,

0 commit comments

Comments
 (0)