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 22, 18:15 UTC
fix(bitbucket): stop the startup sweep from flooding Bitbucket with 429s (#23889)
Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
shivam committed
Oct 23, 2026 at 17:38 UTC
c96a4bb481042eb175f1b1c099079f6907200e08
7 files changed
+226
-63
apps/server/src/git/GitManager.ts
+7
-2
@@ -95,6 +95,7 @@ import * as ServerSettings from "../serverSettings.ts";
95
import type { GitManagerServiceError } from "@t3tools/contracts";
96
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
97
import * as SourceControlProvider from "@t3tools/source-control-core/server/SourceControlProvider";
98
+import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
99
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
100
import type { ChangeRequest } from "@t3tools/contracts";
101
@@ -2715,6 +2716,9 @@ export const make = Effect.gen(function* () {
2716
},
2717
);
2718
2719
+ // A user is waiting on these, so a host's background rate-limit pause must not refuse them.
2720
+ const asUserAction = Effect.provideService(SourceControlRateLimit.Interactive, true);
2721
+
2722
const resolvePullRequest: GitManager["Service"]["resolvePullRequest"] = Effect.fn(
2723
"resolvePullRequest",
2724
)(function* (input) {
@@ -2726,7 +2730,7 @@ export const make = Effect.gen(function* () {
2730
.pipe(Effect.map((resolved) => toResolvedPullRequest(resolved)));
2731
2732
return { pullRequest };
2729
- });
2733
+ }, asUserAction);
2734
2735
const ensureNoTrackedChanges = Effect.fn("ensureNoTrackedChanges")(function* (
2736
cwd: string,
@@ -3036,7 +3040,7 @@ export const make = Effect.gen(function* () {
3040
isOnPullRequestHead: true,
3041
};
3042
}).pipe(Effect.ensuring(invalidateStatus(input.cwd)));
3039
- });
3043
+ }, asUserAction);
3044
3045
const runFeatureBranchStep = Effect.fn("runFeatureBranchStep")(function* (
3046
settings: SourceControlTextGenerationSettings,
@@ -3281,6 +3285,7 @@ export const make = Effect.gen(function* () {
3285
),
3286
);
3287
},
3288
+ asUserAction,
3289
);
3290
3291
return GitManager.of({
apps/server/src/sourceControl/BitbucketApi.test.ts
+92
-1
@@ -3,14 +3,19 @@ import { assert, it, vi } from "@effect/vitest";
3
import * as NodeServices from "@effect/platform-node/NodeServices";
4
import * as ConfigProvider from "effect/ConfigProvider";
5
import * as DateTime from "effect/DateTime";
6
+import * as Deferred from "effect/Deferred";
7
import * as Effect from "effect/Effect";
8
+import * as Fiber from "effect/Fiber";
9
import * as FileSystem from "effect/FileSystem";
10
import * as Layer from "effect/Layer";
11
import * as Option from "effect/Option";
12
+import * as Queue from "effect/Queue";
13
import * as Schema from "effect/Schema";
14
+import * as TestClock from "effect/testing/TestClock";
15
import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http";
16
17
import * as BitbucketApi from "@t3tools/source-control-bitbucket/server/BitbucketApi";
18
+import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
19
import * as ServerSettings from "../serverSettings.ts";
20
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
21
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
@@ -60,6 +65,8 @@ const repositoryJson = {
65
66
function makeLayer(input: {
67
readonly response: (request: HttpClientRequest.HttpClientRequest) => Response;
68
+ /** Holds the answer until it resolves, in place of answering at once. */
69
+ readonly answer?: (response: Response) => Effect.Effect<Response>;
70
readonly requestFailure?: (
71
request: HttpClientRequest.HttpClientRequest,
72
) => HttpClientError.HttpClientError;
@@ -72,7 +79,9 @@ function makeLayer(input: {
79
? Effect.never
80
: input.requestFailure
81
? Effect.fail(input.requestFailure(request))
75
- : Effect.succeed(HttpClientResponse.fromWeb(request, input.response(request))),
82
+ : (input.answer ?? Effect.succeed)(input.response(request)).pipe(
83
+ Effect.map((response) => HttpClientResponse.fromWeb(request, response)),
84
+ ),
85
);
86
const gitMock = {
87
readConfigValue: vi.fn<GitVcsDriver.GitVcsDriver["Service"]["readConfigValue"]>(() =>
@@ -144,6 +153,7 @@ function makeLayer(input: {
153
154
const layer = BitbucketApi.layer.pipe(
155
Layer.provide(ServerSourceControlHost.layer),
156
+ Layer.provide(SourceControlRateLimit.layer),
157
Layer.provide(Layer.mock(VcsProcess.VcsProcess)({})),
158
Layer.provide(
159
Layer.succeed(
@@ -736,3 +746,84 @@ it.effect("cuts a response short rather than reading an unbounded diff into memo
746
),
747
),
748
);
749
+
750
+it.effect("pauses background reads after a 429 until Bitbucket's retry-after passes", () => {
751
+ let answered = 0;
752
+ const { execute, layer } = makeLayer({
753
+ response: () => {
754
+ answered += 1;
755
+ return answered === 1
756
+ ? new Response("Rate limit exceeded", { status: 429, headers: { "retry-after": "60" } })
757
+ : Response.json({ values: [] });
758
+ },
759
+ });
760
+
761
+ return Effect.gen(function* () {
762
+ const bitbucket = yield* BitbucketApi.BitbucketApi;
763
+ const lookup = bitbucket.listPullRequests({
764
+ cwd: "/repo",
765
+ headSelector: "feature/a",
766
+ state: "all",
767
+ });
768
+
769
+ yield* Effect.flip(lookup);
770
+ const paused = yield* Effect.flip(lookup);
771
+ // Keyed by the repository host, where PullRequestService records its own backoff.
772
+ assert.deepInclude(paused, {
773
+ _tag: "SourceControlRateLimitPausedError",
774
+ host: "bitbucket.org",
775
+ retryAt: 60_000,
776
+ });
777
+ assert.strictEqual(execute.mock.calls.length, 1);
778
+
779
+ // A user waiting on an answer is not held back by the background pause, and their success
780
+ // does not end it early.
781
+ yield* lookup.pipe(Effect.provideService(SourceControlRateLimit.Interactive, true));
782
+ yield* bitbucket.request({
783
+ method: "POST",
784
+ url: "/repositories/acme/web/pullrequests/1/approve",
785
+ });
786
+
787
+ yield* TestClock.adjust("59 seconds");
788
+ assert.strictEqual((yield* Effect.flip(lookup))._tag, "SourceControlRateLimitPausedError");
789
+
790
+ yield* TestClock.adjust("1 second");
791
+ yield* lookup;
792
+ }).pipe(Effect.provide(layer));
793
+});
794
+
795
+it.effect("keeps a sweep over many branches to a few Bitbucket requests at a time", () =>
796
+ Effect.gen(function* () {
797
+ const started = yield* Queue.unbounded<void>();
798
+ const release = yield* Deferred.make<void>();
799
+ let inFlight = 0;
800
+ let peak = 0;
801
+ const { layer } = makeLayer({
802
+ response: () => Response.json({ values: [] }),
803
+ answer: (response) =>
804
+ Effect.gen(function* () {
805
+ inFlight += 1;
806
+ peak = Math.max(peak, inFlight);
807
+ yield* Queue.offer(started, undefined);
808
+ yield* Deferred.await(release);
809
+ inFlight -= 1;
810
+ return response;
811
+ }),
812
+ });
813
+
814
+ yield* Effect.gen(function* () {
815
+ const bitbucket = yield* BitbucketApi.BitbucketApi;
816
+ const sweep = yield* Effect.forEach(
817
+ Array.from({ length: 12 }, (_, index) => `feature/${index}`),
818
+ (branch) =>
819
+ bitbucket.listPullRequests({ cwd: "/repo", headSelector: branch, state: "all" }),
820
+ { concurrency: "unbounded", discard: true },
821
+ ).pipe(Effect.forkChild);
822
+ for (let index = 0; index < 4; index += 1) yield* Queue.take(started);
823
+ yield* Deferred.succeed(release, undefined);
824
+ yield* Fiber.join(sweep);
825
+ }).pipe(Effect.provide(layer));
826
+
827
+ assert.strictEqual(peak, 4);
828
+ }),
829
+);
apps/server/src/sourceControl/SourceControlRepositoryService.ts
+18
-14
@@ -24,6 +24,7 @@ import {
24
25
import * as ServerConfig from "../config.ts";
26
import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
27
+import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
28
import {
29
parseGitCloneProgressLine,
30
type GitCloneProgressLine,
@@ -231,20 +232,23 @@ export const make = Effect.gen(function* () {
232
);
233
};
234
234
- const lookupRepository = Effect.fn("SourceControlRepositoryService.lookupRepository")(function* (
235
- input: SourceControlRepositoryLookupInput,
236
- ) {
237
- const providerKind = yield* ensureConcreteProvider({
238
- operation: "lookupRepository",
239
- provider: input.provider,
240
- });
241
- const provider = yield* providers.get(providerKind);
242
- const urls = yield* provider.getRepositoryCloneUrls({
243
- cwd: input.cwd ?? config.cwd,
244
- repository: input.repository.trim(),
245
- });
246
- return toRepositoryInfo(providerKind, urls);
247
- });
235
+ const lookupRepository = Effect.fn("SourceControlRepositoryService.lookupRepository")(
236
+ function* (input: SourceControlRepositoryLookupInput) {
237
+ const providerKind = yield* ensureConcreteProvider({
238
+ operation: "lookupRepository",
239
+ provider: input.provider,
240
+ });
241
+ const provider = yield* providers.get(providerKind);
242
+ const urls = yield* provider.getRepositoryCloneUrls({
243
+ cwd: input.cwd ?? config.cwd,
244
+ repository: input.repository.trim(),
245
+ });
246
+ return toRepositoryInfo(providerKind, urls);
247
+ },
248
+ // Someone adding or cloning a repository is waiting, so a background rate-limit pause must not
249
+ // refuse the lookup.
250
+ Effect.provideService(SourceControlRateLimit.Interactive, true),
251
+ );
252
253
const normalizeDestinationPath = Effect.fn("SourceControlRepositoryService.normalizeDestination")(
254
function* (destinationPath: string) {
apps/server/src/sourceControl/builtInDrivers.ts
+4
-1
@@ -16,6 +16,7 @@ import * as GitLabCli from "@t3tools/source-control-gitlab/server/GitLabCli";
16
import * as GitLabPullRequestCli from "@t3tools/source-control-gitlab/server/GitLabPullRequestCli";
17
import * as GitLabDriver from "@t3tools/source-control-gitlab/server/driver";
18
import type { SourceControlDriver } from "@t3tools/source-control-core/server/driver";
19
+import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
20
import * as Layer from "effect/Layer";
21
22
import * as ServerSourceControlHost from "./ServerSourceControlHost.ts";
@@ -41,7 +42,9 @@ export const layer = Layer.mergeAll(
42
// reader shares, so the server builds it once, here.
43
GitHubPullRequestApi.layer.pipe(Layer.provideMerge(GitHubRest.layerWithDependencies)),
44
AzureDevOpsPullRequestCli.layer.pipe(Layer.provideMerge(AzureDevOpsCli.layer)),
44
- BitbucketPullRequestApi.layer.pipe(Layer.provideMerge(BitbucketApi.layer)),
45
+ BitbucketPullRequestApi.layer.pipe(
46
+ Layer.provideMerge(BitbucketApi.layer.pipe(Layer.provide(SourceControlRateLimit.layer))),
47
+ ),
48
ForgejoCli.layer,
49
GitCafeApi.layer.pipe(Layer.provideMerge(GitCafeCredentials.layer)),
50
GitLabPullRequestCli.layer.pipe(Layer.provideMerge(GitLabCli.layer)),
packages/source-control-bitbucket/src/server/BitbucketApi.ts
+90
-45
@@ -6,8 +6,10 @@ import * as FileSystem from "effect/FileSystem";
6
import * as Layer from "effect/Layer";
7
import * as Option from "effect/Option";
8
import * as Schema from "effect/Schema";
9
+import * as Semaphore from "effect/Semaphore";
10
import {
11
NonNegativeInt,
12
+ SourceControlProviderKind,
13
TrimmedNonEmptyString,
14
type SourceControlProviderAuth,
15
type SourceControlRepositoryCloneUrls,
@@ -32,12 +34,14 @@ import * as SourceControlHost from "@t3tools/source-control-core/server/SourceCo
34
import { readSourceControlHostSettings } from "@t3tools/source-control-core/client/definition";
35
36
import * as BitbucketClient from "../client/definition.ts";
35
-import { retryAtFromHeader } from "@t3tools/source-control-core/server/SourceControlRateLimit";
37
+import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
38
39
const DEFAULT_API_BASE_URL = "https://api.bitbucket.org/2.0";
40
const DEFAULT_MAX_RESPONSE_BYTES = 8 * 1024 * 1024;
41
const MAX_REDIRECTS = 3;
42
const BITBUCKET_API_REQUEST_TIMEOUT = "30 seconds";
43
+/** A sweep over many branches would otherwise reach Bitbucket as one burst of lookups. */
44
+const CONCURRENCY = 4;
45
46
const BitbucketApiEnvConfig = Config.all({
47
baseUrl: Config.String("T3CODE_BITBUCKET_API_BASE_URL").pipe(
@@ -262,6 +266,7 @@ export const BitbucketApiError = Schema.Union([
266
BitbucketRepositoryRemoteNotFoundError,
267
BitbucketPullRequestBodyReadError,
268
BitbucketCheckoutError,
269
+ SourceControlRateLimit.SourceControlRateLimitPausedError,
270
]);
271
export type BitbucketApiError = typeof BitbucketApiError.Type;
272
const isBitbucketApiError = Schema.is(BitbucketApiError);
@@ -607,7 +612,7 @@ const responseError = Effect.fn("BitbucketApi.responseError")(function* (
612
response: HttpClientResponse.HttpClientResponse,
613
): Effect.fn.Return<never, BitbucketApiError> {
614
const now = yield* Clock.currentTimeMillis;
610
- const retryAt = retryAtFromHeader(response.headers["retry-after"], now);
615
+ const retryAt = SourceControlRateLimit.retryAtFromHeader(response.headers["retry-after"], now);
616
const collected = yield* collectUint8StreamText({
617
stream: response.stream,
618
maxBytes: DEFAULT_MAX_RESPONSE_BYTES,
@@ -637,6 +642,13 @@ export const make = Effect.gen(function* () {
642
const git = host.git;
643
const httpClient = yield* HttpClient.HttpClient;
644
const fileSystem = yield* FileSystem.FileSystem;
645
+ const limits = yield* SourceControlRateLimit.SourceControlRateLimit;
646
+ const slots = yield* Semaphore.make(CONCURRENCY);
647
+ // The repository host PullRequestService keys its own backoff under, so both share one pause.
648
+ const rateLimitKey = {
649
+ provider: SourceControlProviderKind.make("bitbucket"),
650
+ host: "bitbucket.org",
651
+ };
652
653
const apiUrl = (path: string) => `${config.baseUrl.replace(/\/+$/u, "")}${path}`;
654
@@ -673,6 +685,33 @@ export const make = Effect.gen(function* () {
685
),
686
);
687
688
+ /**
689
+ * Every request waits for one of the shared slots, then honors the pause a 429 starts for the
690
+ * whole host. Each queued lookup would otherwise spend its own 429 and keep the quota exhausted.
691
+ * Background reads fail at once while paused; a write or an interactive read has a user waiting
692
+ * on it, so it still goes out.
693
+ */
694
+ const throttled = Effect.fn("BitbucketApi.throttled")(
695
+ function* <A, R>(method: string, effect: Effect.Effect<A, BitbucketApiError, R>) {
696
+ const interactive = yield* SourceControlRateLimit.Interactive;
697
+ const lease = yield* limits.check(
698
+ rateLimitKey,
699
+ interactive || method !== "GET" ? { allowPaused: true } : undefined,
700
+ );
701
+ return yield* effect.pipe(
702
+ Effect.tap(() => limits.recordSuccess({ ...rateLimitKey, lease })),
703
+ Effect.tapError((error) =>
704
+ (error._tag === "BitbucketResponseError" ||
705
+ error._tag === "BitbucketResponseBodyReadError") &&
706
+ error.status === 429
707
+ ? limits.recordRateLimit({ ...rateLimitKey, lease, retryAt: error.retryAt })
708
+ : Effect.void,
709
+ ),
710
+ );
711
+ },
712
+ (effect) => slots.withPermit(effect),
713
+ );
714
+
715
const decodeResponse = <S extends Schema.Top>(
716
operation: BitbucketApiOperation,
717
schema: S,
@@ -698,20 +737,23 @@ export const make = Effect.gen(function* () {
737
request: HttpClientRequest.HttpClientRequest,
738
schema: S,
739
): Effect.Effect<S["Type"], BitbucketApiError, S["DecodingServices"]> =>
701
- withAuth(request.pipe(HttpClientRequest.acceptJson)).pipe(
702
- Effect.flatMap(httpClient.execute),
703
- Effect.mapError(
704
- (cause) =>
705
- new BitbucketRequestError({
706
- operation,
707
- cause,
708
- }),
740
+ throttled(
741
+ request.method,
742
+ withAuth(request.pipe(HttpClientRequest.acceptJson)).pipe(
743
+ Effect.flatMap(httpClient.execute),
744
+ Effect.mapError(
745
+ (cause) =>
746
+ new BitbucketRequestError({
747
+ operation,
748
+ cause,
749
+ }),
750
+ ),
751
+ Effect.flatMap((response) => decodeResponse(operation, schema, response)),
752
+ Effect.timeout(BITBUCKET_API_REQUEST_TIMEOUT),
753
+ Effect.catchTags({
754
+ TimeoutError: (cause) => Effect.fail(new BitbucketRequestError({ operation, cause })),
755
+ }),
756
),
710
- Effect.flatMap((response) => decodeResponse(operation, schema, response)),
711
- Effect.timeout(BITBUCKET_API_REQUEST_TIMEOUT),
712
- Effect.catchTags({
713
- TimeoutError: (cause) => Effect.fail(new BitbucketRequestError({ operation, cause })),
714
- }),
757
);
758
759
const resolveRepository = Effect.fn("BitbucketApi.resolveRepository")(function* (input: {
@@ -913,38 +955,41 @@ export const make = Effect.gen(function* () {
955
};
956
957
const request: BitbucketApi["Service"]["request"] = (input) =>
916
- send({ ...input, redirects: 0 }).pipe(
917
- Effect.flatMap((response) =>
918
- HttpClientResponse.matchStatus({
919
- // Read through the body stream rather than `text`, so an oversized diff is stopped as
920
- // it arrives instead of being materialized whole and then cut.
921
- "2xx": (success) =>
922
- collectUint8StreamText({
923
- stream: success.stream,
924
- maxBytes: input.maxBytes ?? DEFAULT_MAX_RESPONSE_BYTES,
925
- }).pipe(
926
- Effect.mapError(
927
- (cause) =>
928
- new BitbucketResponseBodyReadError({
929
- operation: "request",
930
- status: success.status,
931
- cause,
932
- }),
958
+ throttled(
959
+ input.method,
960
+ send({ ...input, redirects: 0 }).pipe(
961
+ Effect.flatMap((response) =>
962
+ HttpClientResponse.matchStatus({
963
+ // Read through the body stream rather than `text`, so an oversized diff is stopped as
964
+ // it arrives instead of being materialized whole and then cut.
965
+ "2xx": (success) =>
966
+ collectUint8StreamText({
967
+ stream: success.stream,
968
+ maxBytes: input.maxBytes ?? DEFAULT_MAX_RESPONSE_BYTES,
969
+ }).pipe(
970
+ Effect.mapError(
971
+ (cause) =>
972
+ new BitbucketResponseBodyReadError({
973
+ operation: "request",
974
+ status: success.status,
975
+ cause,
976
+ }),
977
+ ),
978
+ Effect.map((collected) => ({
979
+ body: collected.text,
980
+ truncated: collected.truncated,
981
+ })),
982
),
934
- Effect.map((collected) => ({
935
- body: collected.text,
936
- truncated: collected.truncated,
937
- })),
938
- ),
939
- orElse: (failed) => responseError("request", failed),
940
- })(response),
983
+ orElse: (failed) => responseError("request", failed),
984
+ })(response),
985
+ ),
986
+ // This also caps redirect hops: no request can finish headers then stall its body forever.
987
+ Effect.timeout(BITBUCKET_API_REQUEST_TIMEOUT),
988
+ Effect.catchTags({
989
+ TimeoutError: (cause) =>
990
+ Effect.fail(new BitbucketRequestError({ operation: "request", cause })),
991
+ }),
992
),
942
- // This also caps redirect hops: no request can finish headers then stall its body forever.
943
- Effect.timeout(BITBUCKET_API_REQUEST_TIMEOUT),
944
- Effect.catchTags({
945
- TimeoutError: (cause) =>
946
- Effect.fail(new BitbucketRequestError({ operation: "request", cause })),
947
- }),
993
);
994
995
return BitbucketApi.of({
packages/source-control-bitbucket/src/server/BitbucketPullRequestProvider.test.ts
+12
@@ -1,7 +1,9 @@
1
+import { SourceControlProviderKind } from "@t3tools/contracts";
2
import { describe, expect, it } from "@effect/vitest";
3
import * as Effect from "effect/Effect";
4
import * as Layer from "effect/Layer";
5
import * as Result from "effect/Result";
6
+import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
7
8
import * as BitbucketApi from "./BitbucketApi.ts";
9
import * as BitbucketPullRequestApi from "./BitbucketPullRequestApi.ts";
@@ -188,6 +190,16 @@ describe("bitbucketProviderFailure", () => {
190
expect(bitbucketProviderFailure(responseError(401)).reason).toBe("unauthenticated");
191
expect(bitbucketProviderFailure(responseError(403)).reason).toBe("failed");
192
});
193
+
194
+ it("reports a request held back by the host-wide pause as rate limited until it ends", () => {
195
+ const paused = new SourceControlRateLimit.SourceControlRateLimitPausedError({
196
+ provider: SourceControlProviderKind.make("bitbucket"),
197
+ host: "bitbucket.org",
198
+ retryAt: 120_000,
199
+ });
200
+
201
+ expect(bitbucketProviderFailure(paused)).toEqual({ reason: "rate-limited", retryAt: 120_000 });
202
+ });
203
});
204
205
describe("bitbucketViewerPermissions", () => {
packages/source-control-bitbucket/src/server/BitbucketPullRequestProvider.ts
+3
@@ -61,6 +61,9 @@ export function bitbucketProviderFailure(
61
if (error._tag === "BitbucketResponseError" && error.status === 401) {
62
return { reason: "unauthenticated" };
63
}
64
+ if (error._tag === "SourceControlRateLimitPausedError") {
65
+ return { reason: "rate-limited", retryAt: error.retryAt };
66
+ }
67
if (
68
(error._tag === "BitbucketResponseError" || error._tag === "BitbucketResponseBodyReadError") &&
69
error.status === 429