Skip to content

Commit 173374c

Browse files
fix(server): opening a pull request loads while its host is rate-limited (pingdotgg#27595)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 81e9368 commit 173374c

3 files changed

Lines changed: 102 additions & 29 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,71 @@ it.effect("stops new reads after a rate limit while leaving manual actions avail
18911891
}),
18921892
);
18931893

1894+
it.effect(
1895+
"opening a pull request during a rate-limit pause still reads its detail and activity",
1896+
() =>
1897+
Effect.gen(function* () {
1898+
let detailReads = 0;
1899+
let activityReads = 0;
1900+
const service = yield* makeService({
1901+
projects: [
1902+
project({ id: "p1", title: "cloud", workspaceRoot: "/cloud", repository: "acme/web" }),
1903+
],
1904+
providers: [
1905+
fakePullRequestProvider(SourceControlProviderKind.make("github"), {
1906+
getChangeRequest: ({ number }) =>
1907+
Effect.suspend(() => {
1908+
detailReads += 1;
1909+
return number === 1
1910+
? Effect.fail(
1911+
new PullRequestProviderError({
1912+
provider: SourceControlProviderKind.make("github"),
1913+
operation: "getChangeRequest",
1914+
reason: "rate-limited",
1915+
detail: "GitHub API rate limit exceeded.",
1916+
}),
1917+
)
1918+
: Effect.succeed({ ...hostedChangeRequest("Body"), number });
1919+
}),
1920+
getChangeRequestActivity: () =>
1921+
Effect.sync(() => {
1922+
activityReads += 1;
1923+
return {
1924+
comments: [],
1925+
commentCount: 0,
1926+
commentsTruncated: false,
1927+
reviewThreads: [],
1928+
commits: [],
1929+
};
1930+
}),
1931+
}),
1932+
],
1933+
});
1934+
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 2 };
1935+
1936+
yield* Effect.result(service.detail({ ...reference, number: 1 }));
1937+
const background = yield* Effect.result(service.detail(reference));
1938+
const opened = yield* Effect.result(
1939+
service
1940+
.detail(reference)
1941+
.pipe(Effect.provideService(SourceControlRateLimit.Interactive, true)),
1942+
);
1943+
const timeline = yield* Effect.result(
1944+
service
1945+
.activity(reference)
1946+
.pipe(Effect.provideService(SourceControlRateLimit.Interactive, true)),
1947+
);
1948+
1949+
expect(background).toMatchObject({ _tag: "Failure" });
1950+
expect({ opened, timeline }).toMatchObject({
1951+
opened: { _tag: "Success", success: { number: 2 } },
1952+
timeline: { _tag: "Success", success: { commentCount: 0 } },
1953+
});
1954+
assert.strictEqual(detailReads, 2);
1955+
assert.strictEqual(activityReads, 1);
1956+
}),
1957+
);
1958+
18941959
it.effect("refuses a repository that does not belong to the requested project", () =>
18951960
Effect.gen(function* () {
18961961
const service = yield* makeService({

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

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -471,32 +471,37 @@ function withRateLimitBackoff(
471471
const protect = <A>(
472472
operation: string,
473473
effect: Effect.Effect<A, PullRequestProviderError>,
474-
allowPaused: boolean,
474+
alwaysInteractive: boolean,
475475
) =>
476-
limits.check(key, allowPaused ? { allowPaused: true } : undefined).pipe(
477-
Effect.mapError(
478-
(error) =>
479-
new PullRequestProviderError({
480-
provider: api.kind,
481-
operation,
482-
reason: "rate-limited",
483-
detail: error.detail,
484-
retryAt: error.retryAt,
485-
cause: error,
486-
}),
487-
),
488-
Effect.flatMap((lease) =>
489-
effect.pipe(
490-
Effect.provideService(SourceControlRateLimit.Interactive, allowPaused),
491-
Effect.tap(() => limits.recordSuccess({ ...key, lease })),
492-
Effect.tapError((error) =>
493-
error.reason === "rate-limited"
494-
? limits.recordRateLimit({
495-
...key,
496-
lease,
497-
...(error.retryAt === undefined ? {} : { retryAt: error.retryAt }),
498-
})
499-
: Effect.void,
476+
SourceControlRateLimit.Interactive.pipe(
477+
Effect.map((callerInteractive) => alwaysInteractive || callerInteractive),
478+
Effect.flatMap((allowPaused) =>
479+
limits.check(key, allowPaused ? { allowPaused: true } : undefined).pipe(
480+
Effect.mapError(
481+
(error) =>
482+
new PullRequestProviderError({
483+
provider: api.kind,
484+
operation,
485+
reason: "rate-limited",
486+
detail: error.detail,
487+
retryAt: error.retryAt,
488+
cause: error,
489+
}),
490+
),
491+
Effect.flatMap((lease) =>
492+
effect.pipe(
493+
Effect.provideService(SourceControlRateLimit.Interactive, allowPaused),
494+
Effect.tap(() => limits.recordSuccess({ ...key, lease })),
495+
Effect.tapError((error) =>
496+
error.reason === "rate-limited"
497+
? limits.recordRateLimit({
498+
...key,
499+
lease,
500+
...(error.retryAt === undefined ? {} : { retryAt: error.retryAt }),
501+
})
502+
: Effect.void,
503+
),
504+
),
500505
),
501506
),
502507
),
@@ -505,10 +510,10 @@ function withRateLimitBackoff(
505510
<Args extends ReadonlyArray<unknown>, A>(
506511
operation: string,
507512
call: (...args: Args) => Effect.Effect<A, PullRequestProviderError>,
508-
allowPaused = false,
513+
alwaysInteractive = false,
509514
) =>
510515
(...args: Args) =>
511-
protect(operation, call(...args), allowPaused);
516+
protect(operation, call(...args), alwaysInteractive);
512517
const interactive = <Args extends ReadonlyArray<unknown>, A>(
513518
operation: string,
514519
call: (...args: Args) => Effect.Effect<A, PullRequestProviderError>,

‎apps/server/src/ws.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ import * as VcsProjectConfig from "./vcs/VcsProjectConfig.ts";
258258
import * as PairingGrantStore from "./auth/PairingGrantStore.ts";
259259
import * as SessionStore from "./auth/SessionStore.ts";
260260
import { failEnvironmentAuthInvalid, failEnvironmentInternal } from "./auth/http.ts";
261+
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
261262
import { isNetworkPath } from "@t3tools/shared/path";
262263
import * as RelayClient from "@t3tools/shared/relayClient";
263264
import {
@@ -1540,6 +1541,8 @@ export const layerWsRpc = (
15401541
yield* SourceControlRepositoryService.SourceControlRepositoryService;
15411542
const sourceControlSignIn = yield* SourceControlSignIn.SourceControlSignIn;
15421543
const withPullRequestViewer = pullRequests.withRoutingCredential;
1544+
// The panel's reads for a pull request the user opened go out during a rate-limit pause.
1545+
const openedPullRequestRead = Effect.provideService(SourceControlRateLimit.Interactive, true);
15431546
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
15441547
const sessions = yield* SessionStore.SessionStore;
15451548
const processDiagnostics = yield* ProcessDiagnostics.ProcessDiagnostics;
@@ -2666,13 +2669,13 @@ export const layerWsRpc = (
26662669
),
26672670
),
26682671
[WS_METHODS.pullRequestsDetail]: (input) =>
2669-
withPullRequestViewer(input, pullRequests.detail(input)),
2672+
withPullRequestViewer(input, openedPullRequestRead(pullRequests.detail(input))),
26702673
[WS_METHODS.pullRequestsPreview]: (input) =>
26712674
withPullRequestViewer(input, pullRequests.preview(input)),
26722675
[WS_METHODS.pullRequestsChecks]: (input) =>
26732676
withPullRequestViewer(input, pullRequests.checks(input)),
26742677
[WS_METHODS.pullRequestsActivity]: (input) =>
2675-
withPullRequestViewer(input, pullRequests.activity(input)),
2678+
withPullRequestViewer(input, openedPullRequestRead(pullRequests.activity(input))),
26762679
[WS_METHODS.pullRequestsReviewGuide]: (input) =>
26772680
withPullRequestViewer(input, reviewGuides.get(input)),
26782681
[WS_METHODS.pullRequestsGenerateReviewGuide]: (input) =>

0 commit comments

Comments
 (0)