Skip to content

Commit 87fafdc

Browse files
committed
fix(source-control): use shared transport and fresh authenticated reads
1 parent ea1b4fd commit 87fafdc

8 files changed

Lines changed: 425 additions & 476 deletions

File tree

‎apps/server/src/sourceControl/builtInDrivers.ts‎

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ import * as GitLabCli from "@t3tools/source-control-gitlab/server/GitLabCli";
2525
import * as GitLabPullRequestCli from "@t3tools/source-control-gitlab/server/GitLabPullRequestCli";
2626
import * as GitLabDriver from "@t3tools/source-control-gitlab/server/driver";
2727
import type { SourceControlDriver } from "@t3tools/source-control-core/server/driver";
28-
import * as Undici from "@effect/platform-node/Undici";
29-
import * as Effect from "effect/Effect";
3028
import * as Layer from "effect/Layer";
31-
import * as FetchHttpClient from "effect/http/FetchHttpClient";
3229

3330
import * as ServerSourceControlHost from "./ServerSourceControlHost.ts";
3431

@@ -50,56 +47,6 @@ export const BUILT_IN_SOURCE_CONTROL_DRIVERS: ReadonlyArray<
5047
SourceControlDriver<BuiltInSourceControlDriversEnv>
5148
> = drivers;
5249

53-
/**
54-
* The client the HTTP drivers read through: fetch, so redirects and headers behave as before, over
55-
* one pool of its own. The server's fetch drops a connection after four idle seconds and opens one
56-
* per concurrent request, so nearly every pull request opened after a pause paid a new TLS
57-
* handshake per read. This pool keeps connections for minutes, or for as long as the host does,
58-
* and carries a panel's parallel reads over one HTTP/2 connection where the host offers it.
59-
* Fresh, since the server builds the same `FetchHttpClient.layer` for itself and a memoized one
60-
* would ignore the `Fetch` given here.
61-
*/
62-
const httpDriverClient = Layer.fresh(FetchHttpClient.layer).pipe(
63-
Layer.provide(
64-
Layer.effect(FetchHttpClient.Fetch)(
65-
Effect.acquireRelease(
66-
Effect.sync(() => {
67-
const options = {
68-
allowH2: true,
69-
keepAliveTimeout: 300_000,
70-
keepAliveMaxTimeout: 300_000,
71-
};
72-
// Match Node's proxy opt-in, including flags supplied through NODE_OPTIONS.
73-
const proxyFlags = [
74-
...(process.env.NODE_OPTIONS?.match(/--(?:no-)?use-env-proxy\b/g) ?? []),
75-
...process.execArgv,
76-
];
77-
const proxyFlag = proxyFlags.findLast(
78-
(flag) => flag === "--use-env-proxy" || flag === "--no-use-env-proxy",
79-
);
80-
const useProxy =
81-
proxyFlag === undefined
82-
? process.env.NODE_USE_ENV_PROXY === "1"
83-
: proxyFlag === "--use-env-proxy";
84-
return useProxy ? new Undici.EnvHttpProxyAgent(options) : new Undici.Agent(options);
85-
}),
86-
(dispatcher) => Effect.promise(() => dispatcher.destroy()),
87-
).pipe(
88-
// Node's own fetch types and this undici's describe the same init and response, but name
89-
// a different `Dispatcher`, so they meet through `unknown`.
90-
Effect.map(
91-
(dispatcher): typeof globalThis.fetch =>
92-
(input, init) =>
93-
Undici.fetch(input as Undici.RequestInfo, {
94-
...(init as unknown as Undici.RequestInit),
95-
dispatcher,
96-
}) as unknown as Promise<Response>,
97-
),
98-
),
99-
),
100-
),
101-
);
102-
10350
/** The services the built-in drivers' packages own, plus the host port they all run against. */
10451
export const layer = Layer.mergeAll(
10552
// `GitHubApi.layerWithDependencies` carries the quota reserve and rate-limit pause every GitHub
@@ -110,4 +57,4 @@ export const layer = Layer.mergeAll(
11057
ForgejoCli.layer,
11158
GitCafeApi.layer.pipe(Layer.provideMerge(GitCafeCredentials.layer)),
11259
GitLabPullRequestCli.layer.pipe(Layer.provideMerge(GitLabCli.layer)),
113-
).pipe(Layer.provide(httpDriverClient), Layer.provideMerge(ServerSourceControlHost.layer));
60+
).pipe(Layer.provideMerge(ServerSourceControlHost.layer));

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestProvider.test.ts‎

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { describe, expect, it } from "@effect/vitest";
2+
import * as Deferred from "effect/Deferred";
23
import * as Effect from "effect/Effect";
4+
import * as Fiber from "effect/Fiber";
35
import * as Layer from "effect/Layer";
46

7+
import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
58
import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
69
import {
710
LOCATION_CACHE_CAPACITY,
@@ -368,15 +371,119 @@ describe("what one diff slice spends", () => {
368371
it.effect("carries on from where the last slice stopped", () =>
369372
Effect.gen(function* () {
370373
const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
371-
const read = yield* readSlice({
372-
paths,
373-
lines: 100,
374-
width: 900,
375-
cursor: `${ITERATION.id}:2`,
376-
});
377-
378-
expect(patchedPaths(read.slice.patch)).toEqual(["c.ts", "d.ts"]);
379-
expect(read.slice.nextCursor).toBe(`${ITERATION.id}:4`);
374+
let signedOut = false;
375+
let pushed = false;
376+
let holdIterations = false;
377+
let iterationReads = 0;
378+
const started = yield* Deferred.make<void>();
379+
const previewStarted = yield* Deferred.make<void>();
380+
const release = yield* Deferred.make<void>();
381+
const contentReads: string[] = [];
382+
const provider = yield* make.pipe(
383+
Effect.provide(
384+
Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
385+
getPullRequest: () =>
386+
Effect.gen(function* () {
387+
if (signedOut) {
388+
return yield* new AzureDevOpsCli.AzureDevOpsCliAuthenticationError({
389+
operation: "execute",
390+
command: "az",
391+
cwd: "/w",
392+
argumentCount: 1,
393+
cause: "signed out",
394+
});
395+
}
396+
if (holdIterations) {
397+
yield* Deferred.succeed(previewStarted, undefined);
398+
yield* Deferred.await(release);
399+
}
400+
return PULL_REQUEST;
401+
}),
402+
listIterations: () =>
403+
Effect.gen(function* () {
404+
iterationReads += 1;
405+
if (signedOut) {
406+
return yield* new AzureDevOpsCli.AzureDevOpsCliAuthenticationError({
407+
operation: "execute",
408+
command: "az",
409+
cwd: "/w",
410+
argumentCount: 1,
411+
cause: "signed out",
412+
});
413+
}
414+
if (holdIterations) {
415+
yield* Deferred.succeed(started, undefined);
416+
yield* Deferred.await(release);
417+
}
418+
return pushed
419+
? [ITERATION, { ...ITERATION, id: ITERATION.id + 1, headCommit: "new-head" }]
420+
: [ITERATION];
421+
}),
422+
listIterationChanges: () =>
423+
Effect.succeed({ changes: paths.map((path) => change(path)), truncated: false }),
424+
readItemContent: (item) =>
425+
Effect.sync(() => {
426+
contentReads.push(item.commit);
427+
return {
428+
contents: side(item.commit === ITERATION.headCommit ? "new" : "old", 100, 900),
429+
isBinary: false,
430+
};
431+
}),
432+
}),
433+
),
434+
);
435+
const input = { cwd: "/w", repository: "acme/web", host: "dev.azure.com", number: 7 };
436+
const first = yield* provider.getDiff(input);
437+
expect(first.nextCursor).toBe(`${ITERATION.id}:2`);
438+
const continuation = { ...input, cursor: first.nextCursor ?? undefined };
439+
const slice = yield* provider.getDiff(continuation);
440+
441+
expect(patchedPaths(slice.patch)).toEqual(["c.ts", "d.ts"]);
442+
expect(slice.nextCursor).toBe(`${ITERATION.id}:4`);
443+
444+
pushed = true;
445+
const warmedReads = contentReads.length;
446+
const again = yield* provider.getDiff(continuation);
447+
expect(again).toEqual(slice);
448+
expect(contentReads).toHaveLength(warmedReads);
449+
450+
const fileInput = {
451+
...input,
452+
oldPath: "c.ts",
453+
newPath: "c.ts",
454+
changeType: "change" as const,
455+
};
456+
const revisionsInput = { ...input, paths: ["c.ts"] };
457+
const contents = yield* provider.getDiffFileContents!(fileInput);
458+
yield* provider.getFileRevisions!(revisionsInput);
459+
const warmedContentsReads = contentReads.length;
460+
461+
holdIterations = true;
462+
const pending = yield* Effect.forkChild(provider.getDiffFileContents!(fileInput));
463+
yield* Deferred.await(started);
464+
const pendingPreview = yield* Effect.forkChild(provider.getChangeRequestPreview!(input));
465+
yield* Deferred.await(previewStarted);
466+
signedOut = true;
467+
const signedOutReads = yield* Effect.all(
468+
[
469+
provider.getDiff(continuation).pipe(Effect.asVoid, Effect.result),
470+
provider.getDiffFileContents!(fileInput).pipe(Effect.asVoid, Effect.result),
471+
provider.getFileRevisions!(revisionsInput).pipe(Effect.asVoid, Effect.result),
472+
provider.getChangeRequestSummary!(input).pipe(Effect.asVoid, Effect.result),
473+
provider.getChangeRequestPreview!(input).pipe(Effect.asVoid, Effect.result),
474+
provider.getChangeRequest(input).pipe(Effect.asVoid, Effect.result),
475+
].map((read) => Effect.forkChild(read, { startImmediately: true })),
476+
);
477+
yield* Deferred.succeed(release, undefined);
478+
expect(yield* Fiber.join(pending)).toEqual(contents);
479+
expect((yield* Fiber.join(pendingPreview)).title).toBe(PULL_REQUEST.title);
480+
for (const result of yield* Effect.all(signedOutReads.map(Fiber.join))) {
481+
expect(result._tag).toBe("Failure");
482+
const error = yield* Effect.flip(Effect.fromResult(result));
483+
expect(error.reason).toBe("unauthenticated");
484+
}
485+
expect(iterationReads).toBe(9);
486+
expect(contentReads).toHaveLength(warmedContentsReads);
380487
}),
381488
);
382489
it.effect("keeps the pull request being read, not the one looked up first", () =>

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestProvider.ts‎

Lines changed: 17 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -257,31 +257,15 @@ export const make = Effect.gen(function* () {
257257
return held;
258258
};
259259

260-
/**
261-
* `az repos pr show`, shared by the reads of one pull request that overlap: a review opening asks
262-
* for its detail, conversation and diff at once, and each of them can start here. Nothing is kept
263-
* once it answers, so a read that starts afterwards asks Azure again.
264-
*/
265-
const pullRequestReads = yield* Cache.makeWith(
266-
(key: string) => {
267-
const [cwd = "", number = ""] = key.split("\n").map(decodeURIComponent);
268-
return cli
269-
.getPullRequest({ cwd, number: Number(number) })
270-
.pipe(
271-
Effect.tap((pullRequest) =>
272-
Effect.sync(() => rememberLocation(cwd, pullRequest, { evict: true })),
273-
),
274-
);
275-
},
276-
{ capacity: LOCATION_CACHE_CAPACITY, timeToLive: () => Duration.zero },
277-
);
278-
260+
// Each read uses the current CLI sign-in, including reads overlapping an account switch.
279261
const readPullRequest = (input: { readonly cwd: string; readonly number: number }) =>
280-
Cache.get(pullRequestReads, cacheKey(input.cwd, input.number));
281-
282-
/** Run after a write, so a read asked for once it lands cannot join one that started before. */
283-
const forgetRead = (input: { readonly cwd: string; readonly number: number }) =>
284-
Cache.invalidate(pullRequestReads, cacheKey(input.cwd, input.number));
262+
cli
263+
.getPullRequest(input)
264+
.pipe(
265+
Effect.tap((pullRequest) =>
266+
Effect.sync(() => rememberLocation(input.cwd, pullRequest, { evict: true })),
267+
),
268+
);
285269

286270
const locationOf = (input: { readonly cwd: string; readonly number: number }) => {
287271
const held = knownLocation(input);
@@ -318,35 +302,6 @@ export const make = Effect.gen(function* () {
318302
encodeURIComponent(iteration.mergeBaseCommit),
319303
].join("\n");
320304

321-
/**
322-
* The pushes a pull request has had, shared by overlapping reads the way a pull request read is.
323-
* Never kept: the newest push is what the list is read for, and a push adds one.
324-
*/
325-
const iterationReads = yield* Cache.makeWith(
326-
(key: string) => cli.listIterations(fromIterationKey(key)),
327-
{ capacity: LOCATION_CACHE_CAPACITY, timeToLive: () => Duration.zero },
328-
);
329-
330-
/**
331-
* One push by its id, which is all a diff cursor names. A push never changes once it lands, so
332-
* the latest one is kept from every listing and a slice carrying on from it lists nothing.
333-
*/
334-
const iterationsById = yield* Cache.makeWith(
335-
(key: string) => {
336-
const scope = fromIterationKey(key);
337-
return Cache.get(iterationReads, iterationKey(scope)).pipe(
338-
Effect.map((iterations) =>
339-
iterations.find((candidate) => candidate.id === scope.iterationId),
340-
),
341-
);
342-
},
343-
{
344-
capacity: LOCATION_CACHE_CAPACITY,
345-
timeToLive: (exit) =>
346-
Exit.isSuccess(exit) && exit.value !== undefined ? Duration.infinity : Duration.zero,
347-
},
348-
);
349-
350305
/**
351306
* What one push changed, against the merge base. Never changes once the push lands, so the
352307
* detail's file count, every slice of the diff and the viewed marks share one complete read.
@@ -361,16 +316,6 @@ export const make = Effect.gen(function* () {
361316
},
362317
);
363318

364-
const listIterations = (scope: IterationScope) =>
365-
Cache.get(iterationReads, iterationKey(scope)).pipe(
366-
Effect.tap((iterations) => {
367-
const latest = iterations.at(-1);
368-
return latest === undefined
369-
? Effect.void
370-
: Cache.set(iterationsById, iterationKey(scope, latest.id), latest);
371-
}),
372-
);
373-
374319
/**
375320
* Everything a diff read needs before it can ask for a file: where the repository lives, and
376321
* which pushes the pull request has had. A client names neither, and the iterations are read
@@ -380,7 +325,7 @@ export const make = Effect.gen(function* () {
380325
Effect.gen(function* () {
381326
const location = yield* locationOf(input);
382327
if (location === null) return null;
383-
const iterations = yield* listIterations({
328+
const iterations = yield* cli.listIterations({
384329
cwd: input.cwd,
385330
location,
386331
number: input.number,
@@ -534,7 +479,7 @@ export const make = Effect.gen(function* () {
534479
// riding on them, so a failure leaves it unknown rather than losing the whole detail.
535480
const countFiles = (location: AzureDevOpsRepositoryLocation) => {
536481
const scope = { cwd: input.cwd, location, number: input.number };
537-
return listIterations(scope).pipe(
482+
return cli.listIterations(scope).pipe(
538483
Effect.flatMap((iterations) => listLatestChanges({ ...scope, iterations })),
539484
Effect.map((listed) => listed.changes.length),
540485
Effect.orElseSucceed(() => 0),
@@ -611,12 +556,14 @@ export const make = Effect.gen(function* () {
611556
if (location === null) return EMPTY_DIFF_SLICE;
612557
const scope = { cwd: input.cwd, location, number: input.number };
613558
const cursor = parseAzureDevOpsDiffCursor(input.cursor);
559+
// Even a fully cached continuation must check the current sign-in can read this review.
560+
const iterations = yield* cli.listIterations(scope);
614561
// Reading on stays with the push the first slice was taken against. A push landing
615562
// mid-read would otherwise renumber the files and hand the reader one twice, or none.
616563
const iteration =
617564
cursor === null
618-
? (yield* listIterations(scope)).at(-1)
619-
: yield* Cache.get(iterationsById, iterationKey(scope, cursor.iterationId));
565+
? iterations.at(-1)
566+
: iterations.find((candidate) => candidate.id === cursor.iterationId);
620567
if (iteration === undefined) return EMPTY_DIFF_SLICE;
621568
const listed = yield* Cache.get(iterationChanges, changesKey(scope, iteration));
622569
const changes = listed.changes;
@@ -772,7 +719,7 @@ export const make = Effect.gen(function* () {
772719
action: input.action,
773720
...(input.mergeMethod === undefined ? {} : { mergeMethod: input.mergeMethod }),
774721
})
775-
.pipe(Effect.ensuring(forgetRead(input)), Effect.mapError(fail("runAction"))),
722+
.pipe(Effect.mapError(fail("runAction"))),
776723

777724
updateChangeRequest: (input) =>
778725
cli
@@ -782,7 +729,7 @@ export const make = Effect.gen(function* () {
782729
title: input.title,
783730
body: input.body,
784731
})
785-
.pipe(Effect.ensuring(forgetRead(input)), Effect.mapError(fail("updateChangeRequest"))),
732+
.pipe(Effect.mapError(fail("updateChangeRequest"))),
786733

787734
// Never called: `capabilities.reviewers.listCandidates` is false, and the service refuses the
788735
// list without it.
@@ -806,7 +753,7 @@ export const make = Effect.gen(function* () {
806753
reviewers: input.reviewers.map((reviewer) => reviewer.id),
807754
requested: input.requested,
808755
})
809-
.pipe(Effect.ensuring(forgetRead(input)), Effect.mapError(fail("setReviewerRequest"))),
756+
.pipe(Effect.mapError(fail("setReviewerRequest"))),
810757

811758
// Never called: `capabilities.comment` is false, and the service refuses a comment without it.
812759
comment: () => unsupported("comment"),

0 commit comments

Comments
 (0)