@devries-cameron20 / t3code Mirror · read-only / commits / eadeaf2282
Fork 0

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 24, 00:15 UTC

fix(bitbucket): preserve rate limits from optional PR reads (#12486)

Julius Marminge committed Sep 18, 2026 at 10:54 UTC eadeaf228267be215a2e9486f16b876d703dcccc
4 files changed +142 -10
apps/server/src/pullRequest/BitbucketPullRequestProvider.test.ts
+80 -1
@@ -1,11 +1,90 @@
1 -import { describe, expect, it } from "vite-plus/test";
1 +import { describe, expect, it } from "@effect/vitest";
2 +import * as Effect from "effect/Effect";
3 +import * as Layer from "effect/Layer";
4 +import * as Result from "effect/Result";
5
6 import * as BitbucketApi from "../sourceControl/BitbucketApi.ts";
7 +import * as BitbucketPullRequestApi from "./BitbucketPullRequestApi.ts";
8 +import { decodePullRequestJson } from "./bitbucketPullRequestJson.ts";
9 import {
10 bitbucketProviderFailure,
11 bitbucketViewerPermissions,
12 + make,
13 } from "./BitbucketPullRequestProvider.ts";
14
15 +for (const operation of [
16 + "getMergeability",
17 + "listChecks",
18 + "getRepositoryPermission",
19 + "listComments",
20 + "listCommits",
21 +] as const) {
22 + it.effect.each(["response", "body read"])(
23 + `preserves rate limits from ${operation} on %s errors while recovering other optional-read failures`,
24 + (variant) =>
25 + Effect.gen(function* () {
26 + const pullRequest = Result.getOrThrow(
27 + decodePullRequestJson(`{
28 + "id": 1, "title": "Check polling", "state": "OPEN",
29 + "source": { "branch": { "name": "feature" } },
30 + "destination": { "branch": { "name": "main" } },
31 + "created_on": "2026-09-16T00:00:00Z",
32 + "updated_on": "2026-09-16T00:00:00Z",
33 + "links": { "html": { "href": "https://bitbucket.org/acme/web/pull-requests/1" } }
34 + }`),
35 + );
36 + for (const status of [429, 403]) {
37 + const provider = yield* make.pipe(
38 + Effect.provide(
39 + Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
40 + getPullRequest: () => Effect.succeed(pullRequest),
41 + getDiffStat: () => Effect.succeed({ additions: 0, deletions: 0, changedFiles: 0 }),
42 + getMergeability: () => Effect.succeed("unknown" as const),
43 + listChecks: () => Effect.succeed([]),
44 + getRepositoryPermission: () => Effect.succeed(true),
45 + listComments: () => Effect.succeed({ comments: [], threads: [], truncated: false }),
46 + listCommits: () => Effect.succeed([]),
47 + [operation]: () =>
48 + Effect.fail(
49 + variant === "response"
50 + ? new BitbucketApi.BitbucketResponseError({
51 + operation: "request",
52 + status,
53 + responseBodyLength: 0,
54 + retryAt: 120_000,
55 + })
56 + : new BitbucketApi.BitbucketResponseBodyReadError({
57 + operation: "request",
58 + status,
59 + cause: new Error("response stream failed"),
60 + retryAt: 120_000,
61 + }),
62 + ),
63 + }),
64 + ),
65 + );
66 + const reference = {
67 + cwd: "/repo",
68 + repository: "acme/web",
69 + number: 1,
70 + host: "bitbucket.org",
71 + };
72 + const result = yield* operation === "listComments" || operation === "listCommits"
73 + ? Effect.result(provider.getChangeRequestActivity(reference))
74 + : Effect.result(provider.getChangeRequest(reference));
75 + if (status === 429) {
76 + expect(result).toMatchObject({
77 + _tag: "Failure",
78 + failure: { reason: "rate-limited", retryAt: 120_000 },
79 + });
80 + } else {
81 + expect(result._tag).toBe("Success");
82 + }
83 + }
84 + }),
85 + );
86 +}
87 +
88 describe("bitbucketProviderFailure", () => {
89 it("treats only an HTTP 401 as unusable credentials", () => {
90 const responseError = (status: number) =>
apps/server/src/pullRequest/BitbucketPullRequestProvider.ts
+34 -8
@@ -73,7 +73,10 @@ export function bitbucketProviderFailure(
73 if (error._tag === "BitbucketResponseError" && error.status === 401) {
74 return { reason: "unauthenticated" };
75 }
76 - if (error._tag === "BitbucketResponseError" && error.status === 429) {
76 + if (
77 + (error._tag === "BitbucketResponseError" || error._tag === "BitbucketResponseBodyReadError") &&
78 + error.status === 429
79 + ) {
80 return {
81 reason: "rate-limited",
82 ...(error.retryAt === undefined ? {} : { retryAt: error.retryAt }),
@@ -122,6 +125,31 @@ export const make = Effect.gen(function* () {
125 cause: error,
126 });
127
128 + const recoverRead = <A>(
129 + read: Effect.Effect<A, BitbucketPullRequestApi.BitbucketPullRequestApiError>,
130 + fallback: A,
131 + ) => {
132 + const recover = () => Effect.succeed(fallback);
133 + return Effect.catchTags(read, {
134 + BitbucketResponseError: (error) => (error.status === 429 ? Effect.fail(error) : recover()),
135 + BitbucketUntrustedUrlError: recover,
136 + BitbucketRepositoryLocatorError: recover,
137 + BitbucketRequestError: recover,
138 + BitbucketResponseBodyReadError: (error) =>
139 + error.status === 429 ? Effect.fail(error) : recover(),
140 + BitbucketResponseDecodeError: recover,
141 + BitbucketRepositoryVcsResolveError: recover,
142 + BitbucketRepositoryRemotesListError: recover,
143 + BitbucketRepositoryRemoteNotFoundError: recover,
144 + BitbucketPullRequestBodyReadError: recover,
145 + BitbucketCheckoutError: recover,
146 + BitbucketPullRequestReadError: recover,
147 + BitbucketViewerUnavailableError: recover,
148 + BitbucketRepositoryUnsupportedError: recover,
149 + BitbucketDiffCommitError: recover,
150 + });
151 + };
152 +
153 const provider: PullRequestProviderApi = {
154 kind: "bitbucket",
155 capabilities: CAPABILITIES,
@@ -156,12 +184,12 @@ export const make = Effect.gen(function* () {
184 [
185 api.getPullRequest(target),
186 api.getDiffStat(target),
159 - api.getMergeability(target).pipe(Effect.orElseSucceed(() => "unknown" as const)),
160 - api.listChecks(target).pipe(Effect.orElseSucceed(() => [])),
187 + recoverRead(api.getMergeability(target), "unknown" as const),
188 + recoverRead(api.listChecks(target), []),
189 // A permission that could not be read is an unknown one, which is granted: a hidden
190 // Merge leaves someone entitled to it with no way through, and one Bitbucket refuses
191 // at least says why.
164 - api.getRepositoryPermission(target).pipe(Effect.orElseSucceed(() => true)),
192 + recoverRead(api.getRepositoryPermission(target), true),
193 ],
194 { concurrency: 5 },
195 ).pipe(
@@ -200,10 +228,8 @@ export const make = Effect.gen(function* () {
228 // Reviews ride on the pull request itself, so this inexpensive core read is repeated
229 // here rather than making the core response wait for the conversation endpoints.
230 api.getPullRequest(target),
203 - api
204 - .listComments(target)
205 - .pipe(Effect.orElseSucceed(() => ({ comments: [], threads: [], truncated: true }))),
206 - api.listCommits(target).pipe(Effect.orElseSucceed(() => [])),
231 + recoverRead(api.listComments(target), { comments: [], threads: [], truncated: true }),
232 + recoverRead(api.listCommits(target), []),
233 ],
234 { concurrency: 3 },
235 ).pipe(
apps/server/src/sourceControl/BitbucketApi.test.ts
+24
@@ -618,6 +618,30 @@ it.effect("preserves Bitbucket response body read failures as their immediate ca
618 }).pipe(Effect.provide(layer));
619 });
620
621 +it.effect("keeps the 429 retry time when the response body cannot be read", () => {
622 + const { layer } = makeLayer({
623 + response: () =>
624 + new Response(
625 + new ReadableStream<Uint8Array>({
626 + start: (controller) => controller.error(new Error("response stream failed")),
627 + }),
628 + { status: 429, headers: { "Retry-After": "120" } },
629 + ),
630 + });
631 +
632 + return Effect.gen(function* () {
633 + yield* TestClock.setTime(1_000);
634 + const bitbucket = yield* BitbucketApi.BitbucketApi;
635 + const error = yield* bitbucket
636 + .request({ method: "GET", url: "/repositories/acme/web" })
637 + .pipe(Effect.flip);
638 +
639 + assert.instanceOf(error, BitbucketApi.BitbucketResponseBodyReadError);
640 + assert.strictEqual(error.status, 429);
641 + assert.strictEqual(error.retryAt, 121_000);
642 + }).pipe(Effect.provide(layer));
643 +});
644 +
645 it.effect("checks out same-repository pull requests with the existing Bitbucket remote", () => {
646 const { git, layer } = makeLayer({
647 response: () =>
apps/server/src/sourceControl/BitbucketApi.ts
+4 -1
@@ -117,6 +117,7 @@ export class BitbucketResponseBodyReadError extends Schema.TaggedError<Bitbucket
117 {
118 operation: BitbucketApiOperation,
119 status: Schema.Int,
120 + retryAt: Schema.optional(Schema.Number),
121 cause: Schema.Defect(),
122 },
123 ) {
@@ -584,6 +585,7 @@ function responseError(
585 // only its length is reported anyway.
586 return Effect.gen(function* () {
587 const now = yield* Clock.currentTimeMillis;
588 + const retryAt = retryAtFromHeader(response.headers["retry-after"], now);
589 const collected = yield* collectUint8StreamText({
590 stream: response.stream,
591 maxBytes: DEFAULT_MAX_RESPONSE_BYTES,
@@ -593,6 +595,7 @@ function responseError(
595 new BitbucketResponseBodyReadError({
596 operation,
597 status: response.status,
598 + retryAt,
599 cause,
600 }),
601 ),
@@ -601,7 +604,7 @@ function responseError(
604 operation,
605 status: response.status,
606 responseBodyLength: collected.text.length,
604 - retryAt: retryAtFromHeader(response.headers["retry-after"], now),
607 + retryAt,
608 });
609 });
610 }