Skip to content

Commit 3898c32

Browse files
perf(server): source control discovery no longer stalls up to 10s on Bitbucket (pingdotgg#27102)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 624e70e commit 3898c32

4 files changed

Lines changed: 85 additions & 41 deletions

File tree

‎apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ function makeRegistry(input: {
118118
kind: provider.kind,
119119
label: provider.kind,
120120
installHint: "unused",
121-
probeAuth: Effect.die("unused"),
121+
probeAuth: Effect.succeed({
122+
status: "unknown",
123+
account: Option.none(),
124+
host: Option.none(),
125+
detail: Option.none(),
126+
}),
122127
},
123128
})),
124129
).pipe(
@@ -440,3 +445,28 @@ it.effect("asks again once Settings change", () =>
440445
assert.strictEqual((yield* resolveUnknown(registry, "c")).provider.kind, "github");
441446
}),
442447
);
448+
449+
it.effect("answers discovery without waiting on a hosting API that stalls", () =>
450+
Effect.gen(function* () {
451+
const registry = yield* makeRegistry({
452+
remotes: [],
453+
discovery: [
454+
claimingCli("gitlab"),
455+
{
456+
type: "api",
457+
kind: kind("bitbucket"),
458+
label: "Bitbucket",
459+
installHint: "unused",
460+
probeAuth: Effect.never,
461+
},
462+
],
463+
});
464+
const discovering = yield* registry.discover.pipe(Effect.forkChild);
465+
yield* TestClock.adjust("3 seconds");
466+
const items = yield* Fiber.join(discovering);
467+
const bitbucket = items.find((item) => item.kind === "bitbucket");
468+
assert.strictEqual(bitbucket?.status, "available");
469+
assert.strictEqual(bitbucket?.auth.status, "unknown");
470+
assert.strictEqual(items.find((item) => item.kind === "gitlab")?.status, "available");
471+
}),
472+
);

‎packages/source-control-bitbucket/src/server/BitbucketApi.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ it.effect("never puts a saved token that is unsafe for an HTTP header on the wir
513513
});
514514

515515
it.effect("reports saved credentials as configured when Bitbucket cannot confirm them", () => {
516-
const { layer } = makeLayer({
516+
const { execute, layer } = makeLayer({
517517
response: () => new Response(null, { status: 401 }),
518518
env: { T3CODE_BITBUCKET_API_BASE_URL: "https://api.test.local/2.0" },
519519
});
@@ -523,6 +523,7 @@ it.effect("reports saved credentials as configured when Bitbucket cannot confirm
523523
const settings = yield* TestSourceControlHost.TestSourceControlHostSettings;
524524

525525
assert.strictEqual((yield* bitbucket.probeAuth).status, "unauthenticated");
526+
assert.strictEqual(execute.mock.calls.length, 0);
526527

527528
yield* settings.update({
528529
sourceControlHosts: { bitbucket: { accessToken: "saved-access-token" } },

‎packages/source-control-bitbucket/src/server/BitbucketApi.ts‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -986,18 +986,25 @@ export const make = Effect.gen(function* () {
986986

987987
return BitbucketApi.of({
988988
request,
989-
probeAuth: executeJson(
990-
"probeAuth",
991-
HttpClientRequest.get(apiUrl("/user")),
992-
BitbucketUserSchema,
993-
).pipe(
994-
Effect.map((user) => ({
995-
status: "authenticated" as const,
996-
account: nonEmpty(user.username ?? user.display_name ?? user.account_id),
997-
host: Option.some("bitbucket.org"),
998-
detail: Option.none<string>(),
999-
})),
1000-
Effect.catch(() => currentCredential.pipe(Effect.map(authFromCredential))),
989+
// Bitbucket answers anonymous /user with 401, so without a credential there is nothing to ask.
990+
probeAuth: currentCredential.pipe(
991+
Effect.flatMap((credential) =>
992+
credential === null
993+
? Effect.succeed(authFromCredential(null))
994+
: executeJson(
995+
"probeAuth",
996+
HttpClientRequest.get(apiUrl("/user")),
997+
BitbucketUserSchema,
998+
).pipe(
999+
Effect.map((user) => ({
1000+
status: "authenticated" as const,
1001+
account: nonEmpty(user.username ?? user.display_name ?? user.account_id),
1002+
host: Option.some("bitbucket.org"),
1003+
detail: Option.none<string>(),
1004+
})),
1005+
Effect.catch(() => currentCredential.pipe(Effect.map(authFromCredential))),
1006+
),
1007+
),
10011008
),
10021009
listPullRequests: Effect.fn("BitbucketApi.listPullRequests")(function (input) {
10031010
return resolveRepository(input).pipe(

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

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ type SourceControlCliRemoteRefinementSpec = SourceControlCliDiscoverySpec & {
7575
};
7676

7777
const DEFAULT_PROBE_TIMEOUT_MS = 5_000;
78+
/** An API host that stalls must not hold the whole discovery answer behind it. */
79+
const API_PROBE_TIMEOUT = "3 seconds";
7880

7981
function probeTimeoutMs(spec: SourceControlCliDiscoverySpec): number {
8082
return spec.probeTimeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS;
@@ -236,6 +238,12 @@ export function probeSourceControlProvider(input: {
236238
if (input.spec.type === "managed-cli") return input.spec.probe(input.cwd);
237239
if (input.spec.type === "api") {
238240
return input.spec.probeAuth.pipe(
241+
Effect.timeoutOption(API_PROBE_TIMEOUT),
242+
Effect.map(
243+
Option.getOrElse(() =>
244+
unknownAuth("The hosting service did not answer in time. Refresh to try again."),
245+
),
246+
),
239247
Effect.map(
240248
(auth) =>
241249
({
@@ -253,20 +261,15 @@ export function probeSourceControlProvider(input: {
253261

254262
const spec = input.spec;
255263

256-
return probeCli({
257-
spec,
258-
process: input.process,
259-
cwd: input.cwd,
260-
}).pipe(
261-
Effect.flatMap((item) => {
262-
if (item.status !== "available") {
263-
return Effect.succeed({
264-
...item,
265-
auth: unknownAuth("Hosting integration command was not found on the server PATH."),
266-
} satisfies SourceControlProviderDiscoveryItem);
267-
}
268-
269-
return input.process
264+
// The auth check does not depend on the version output, so both spawn at once.
265+
return Effect.all(
266+
[
267+
probeCli({
268+
spec,
269+
process: input.process,
270+
cwd: input.cwd,
271+
}),
272+
input.process
270273
.run({
271274
operation: "source-control.discovery.auth",
272275
command: spec.executable,
@@ -278,21 +281,24 @@ export function probeSourceControlProvider(input: {
278281
appendTruncationMarker: true,
279282
})
280283
.pipe(
281-
Effect.map(
282-
(result) =>
283-
({
284-
...item,
285-
auth: spec.parseAuth(result),
286-
}) satisfies SourceControlProviderDiscoveryItem,
287-
),
284+
Effect.map(spec.parseAuth),
288285
Effect.catch((cause) =>
289-
Effect.succeed({
290-
...item,
291-
auth: unknownAuth(Option.getOrUndefined(detailFromCause(cause))),
292-
} satisfies SourceControlProviderDiscoveryItem),
286+
Effect.succeed(unknownAuth(Option.getOrUndefined(detailFromCause(cause)))),
293287
),
294-
);
295-
}),
288+
),
289+
],
290+
{ concurrency: "unbounded" },
291+
).pipe(
292+
Effect.map(
293+
([item, auth]) =>
294+
({
295+
...item,
296+
auth:
297+
item.status === "available"
298+
? auth
299+
: unknownAuth("Hosting integration command was not found on the server PATH."),
300+
}) satisfies SourceControlProviderDiscoveryItem,
301+
),
296302
);
297303
}
298304

0 commit comments

Comments
 (0)