Skip to content

Commit 2c119bd

Browse files
fix(gitlab): share merge request reads across detail and checks (pingdotgg#24392)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent f3b0194 commit 2c119bd

5 files changed

Lines changed: 394 additions & 30 deletions

File tree

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,98 @@ it.effect("does not revive old summaries when project epochs are evicted", () =>
21892189
}),
21902190
);
21912191

2192+
it.effect("a refresh or a finished turn drops the provider's own change request reads", () =>
2193+
Effect.gen(function* () {
2194+
const forgotten: Array<{ readonly repository: string; readonly number?: number } | undefined> =
2195+
[];
2196+
const service = yield* makeService({
2197+
projects: [
2198+
project({
2199+
id: "p1",
2200+
title: "web",
2201+
workspaceRoot: "/a",
2202+
repository: "acme/web",
2203+
provider: "gitlab",
2204+
}),
2205+
],
2206+
providers: [
2207+
fakeProvider(SourceControlProviderKind.make("gitlab"), {
2208+
forgetChangeRequestReads: (scope) => Effect.sync(() => void forgotten.push(scope)),
2209+
}),
2210+
],
2211+
});
2212+
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
2213+
2214+
yield* service.invalidate({ reference });
2215+
yield* service.refreshAfterTurn(reference.projectId);
2216+
yield* service.invalidate({});
2217+
2218+
assert.deepStrictEqual(forgotten, [
2219+
{ repository: "acme/web", number: 1 },
2220+
undefined,
2221+
undefined,
2222+
]);
2223+
}),
2224+
);
2225+
2226+
it.effect("a read during a refresh cannot keep the provider's old change request", () =>
2227+
Effect.gen(function* () {
2228+
let hostTitle = "old";
2229+
let held: string | undefined;
2230+
let gate:
2231+
| { readonly entered: Deferred.Deferred<void>; readonly release: Deferred.Deferred<void> }
2232+
| undefined;
2233+
const service = yield* makeService({
2234+
projects: [
2235+
project({
2236+
id: "p1",
2237+
title: "web",
2238+
workspaceRoot: "/a",
2239+
repository: "acme/web",
2240+
provider: "gitlab",
2241+
}),
2242+
],
2243+
providers: [
2244+
fakeProvider(SourceControlProviderKind.make("gitlab"), {
2245+
getChangeRequest: () =>
2246+
Effect.sync(() => {
2247+
held ??= hostTitle;
2248+
return { ...hostedChangeRequest("body"), title: held };
2249+
}),
2250+
forgetChangeRequestReads: () =>
2251+
Effect.gen(function* () {
2252+
if (gate !== undefined) {
2253+
yield* Deferred.succeed(gate.entered, undefined);
2254+
yield* Deferred.await(gate.release);
2255+
}
2256+
held = undefined;
2257+
}),
2258+
}),
2259+
],
2260+
});
2261+
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
2262+
const refreshWithReadInBetween = (refresh: Effect.Effect<void>) =>
2263+
Effect.gen(function* () {
2264+
gate = { entered: yield* Deferred.make<void>(), release: yield* Deferred.make<void>() };
2265+
const running = yield* refresh.pipe(Effect.forkChild());
2266+
yield* Deferred.await(gate.entered);
2267+
yield* service.detail(reference);
2268+
yield* Deferred.succeed(gate.release, undefined);
2269+
yield* Fiber.join(running);
2270+
gate = undefined;
2271+
});
2272+
2273+
assert.strictEqual((yield* service.detail(reference)).title, "old");
2274+
hostTitle = "after turn";
2275+
yield* refreshWithReadInBetween(service.refreshAfterTurn(reference.projectId));
2276+
assert.strictEqual((yield* service.detail(reference)).title, "after turn");
2277+
2278+
hostTitle = "after refresh";
2279+
yield* refreshWithReadInBetween(service.invalidate({ reference }));
2280+
assert.strictEqual((yield* service.detail(reference)).title, "after refresh");
2281+
}),
2282+
);
2283+
21922284
it.effect("close and reopen notify subscribed readers after invalidating their cached state", () =>
21932285
Effect.scoped(
21942286
Effect.gen(function* () {

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

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,9 @@ function withRateLimitBackoff(
528528
listChangeRequestStats: wrap("listChangeRequestStats", api.listChangeRequestStats),
529529
}),
530530
getChangeRequest: wrap("getChangeRequest", api.getChangeRequest),
531+
...(api.forgetChangeRequestReads === undefined
532+
? {}
533+
: { forgetChangeRequestReads: api.forgetChangeRequestReads }),
531534
...(api.getChangeRequestPreview === undefined
532535
? {}
533536
: { getChangeRequestPreview: wrap("getChangeRequestPreview", api.getChangeRequestPreview) }),
@@ -3423,6 +3426,13 @@ export const make = Effect.gen(function* () {
34233426
return { stats: [...held, ...result.stats] };
34243427
});
34253428

3429+
const forgetProviderReads = (scope?: { readonly repository: string; readonly number?: number }) =>
3430+
Effect.forEach(
3431+
registry.kinds,
3432+
(kind) => registry.get(kind)?.forgetChangeRequestReads?.(scope) ?? Effect.void,
3433+
{ discard: true },
3434+
);
3435+
34263436
const invalidate: PullRequestService["Service"]["invalidate"] = Effect.fn(
34273437
"PullRequestService.invalidate",
34283438
)(function* (input, options) {
@@ -3438,13 +3448,15 @@ export const make = Effect.gen(function* () {
34383448
if (reference !== undefined) {
34393449
yield* canonicalRef(reference).pipe(
34403450
Effect.flatMap((ref) =>
3441-
readCache
3442-
.invalidate(refScope(ref))
3443-
.pipe(Effect.andThen(Effect.sync(() => bumpRefEpoch(ref)))),
3451+
forgetProviderReads({ repository: ref.repository, number: ref.number }).pipe(
3452+
Effect.andThen(readCache.invalidate(refScope(ref))),
3453+
Effect.andThen(Effect.sync(() => bumpRefEpoch(ref))),
3454+
),
34443455
),
34453456
Effect.ignore,
34463457
);
34473458
} else {
3459+
yield* forgetProviderReads();
34483460
listingsEpoch = ++epochCounter;
34493461
everyFileRevisionEpoch = ++epochCounter;
34503462
viewersByHost.clear();
@@ -3455,22 +3467,27 @@ export const make = Effect.gen(function* () {
34553467
}
34563468
});
34573469

3458-
const refreshAfterTurn: PullRequestService["Service"]["refreshAfterTurn"] = (projectId) =>
3459-
Effect.suspend(() => {
3460-
listingsEpoch = ++epochCounter;
3461-
projectEpochs.delete(projectId);
3462-
if (projectEpochs.size >= REF_EPOCH_CAPACITY) {
3463-
const oldest = projectEpochs.keys().next().value;
3464-
if (oldest !== undefined) {
3465-
projectEpochFloor = projectEpochs.get(oldest)!;
3466-
projectEpochs.delete(oldest);
3467-
}
3470+
const refreshAfterTurn: PullRequestService["Service"]["refreshAfterTurn"] = Effect.fn(
3471+
"PullRequestService.refreshAfterTurn",
3472+
)(function* (projectId) {
3473+
// Everything the provider holds, not just the project's repository: a turn can touch a
3474+
// change request reached through another repository, and no epoch may move until those
3475+
// reads are gone, or a reader in between keeps the old one under the new epoch.
3476+
yield* forgetProviderReads();
3477+
listingsEpoch = ++epochCounter;
3478+
projectEpochs.delete(projectId);
3479+
if (projectEpochs.size >= REF_EPOCH_CAPACITY) {
3480+
const oldest = projectEpochs.keys().next().value;
3481+
if (oldest !== undefined) {
3482+
projectEpochFloor = projectEpochs.get(oldest)!;
3483+
projectEpochs.delete(oldest);
34683484
}
3469-
projectEpochs.set(projectId, listingsEpoch);
3470-
return readCache
3471-
.invalidate(`project:${projectId}`)
3472-
.pipe(Effect.andThen(SubscriptionRef.set(pullRequestRefreshes, listingsEpoch)));
3473-
});
3485+
}
3486+
projectEpochs.set(projectId, listingsEpoch);
3487+
const epoch = listingsEpoch;
3488+
yield* readCache.invalidate(`project:${projectId}`);
3489+
yield* SubscriptionRef.set(pullRequestRefreshes, epoch);
3490+
});
34743491

34753492
const invalidatedByMutation =
34763493
<I extends PullRequestRef>(

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,16 @@ export interface PullRequestProviderApi {
353353
input: ProviderRepositoryRef & { readonly number: number },
354354
) => Effect.Effect<ProviderChangeRequestDetail, PullRequestProviderError>;
355355

356+
/**
357+
* For a provider that holds change request reads of its own: called when the service drops its
358+
* reads, so a refresh or a finished turn reaches the host. No scope forgets everything; a scope
359+
* without a number forgets the repository's change requests.
360+
*/
361+
readonly forgetChangeRequestReads?: (scope?: {
362+
readonly repository: string;
363+
readonly number?: number;
364+
}) => Effect.Effect<void>;
365+
356366
readonly getChangeRequestPreview?: (
357367
input: ProviderRepositoryRef & { readonly number: number },
358368
) => Effect.Effect<

‎packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { assert, describe, expect, it } from "@effect/vitest";
22
import * as Effect from "effect/Effect";
33
import * as Layer from "effect/Layer";
4+
import * as TestClock from "effect/testing/TestClock";
45

56
import * as GitLabCli from "./GitLabCli.ts";
67
import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
@@ -122,6 +123,157 @@ describe("getChangeRequest base freshness", () => {
122123
);
123124
});
124125

126+
describe("merge request reads", () => {
127+
const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 };
128+
const detail = {
129+
number: 7,
130+
title: "Merge request 7",
131+
url: "https://gitlab.com/acme/web/-/merge_requests/7",
132+
author: null,
133+
headBranch: "feat/page",
134+
baseBranch: "main",
135+
state: "open" as const,
136+
isDraft: false,
137+
mergeability: "mergeable" as const,
138+
additions: 0,
139+
deletions: 0,
140+
createdAt: "2026-07-01T00:00:00Z",
141+
updatedAt: "2026-07-02T00:00:00Z",
142+
reviewRequestLogins: [],
143+
labels: [],
144+
body: "",
145+
changedFiles: 1,
146+
mergedAt: null,
147+
closedAt: null,
148+
reviewers: [],
149+
checks: [],
150+
viewerCanMerge: true,
151+
reviewerIds: [],
152+
};
153+
154+
const countingProvider = Effect.gen(function* () {
155+
const calls = { detail: 0, mergeCapabilities: 0 };
156+
const host = { viewerCanMerge: true };
157+
const provider = yield* make.pipe(
158+
Effect.provide(
159+
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
160+
getMergeRequestDetail: (input) =>
161+
Effect.sync(() => {
162+
calls.detail += 1;
163+
return { ...detail, number: input.number, viewerCanMerge: host.viewerCanMerge };
164+
}),
165+
getProjectMergeCapabilities: () =>
166+
Effect.sync(() => {
167+
calls.mergeCapabilities += 1;
168+
return { merge: true, squash: true, rebase: true };
169+
}),
170+
runMergeRequestAction: () => Effect.void,
171+
}),
172+
),
173+
);
174+
const checks = provider.getChangeRequestChecks;
175+
const forget = provider.forgetChangeRequestReads;
176+
if (checks === undefined || forget === undefined) {
177+
return yield* Effect.die("GitLab answers checks and forgets its reads");
178+
}
179+
return { provider, checks, forget, calls, host };
180+
});
181+
182+
it.effect("asks for a project's merge methods once across its merge requests", () =>
183+
Effect.gen(function* () {
184+
const { provider, calls } = yield* countingProvider;
185+
186+
yield* provider.getChangeRequest(reference);
187+
yield* provider.getChangeRequest(reference);
188+
yield* provider.getChangeRequest({ ...reference, number: 8 });
189+
190+
expect(calls.mergeCapabilities).toBe(1);
191+
}),
192+
);
193+
194+
it.effect("answers checks from the merge request the detail just read", () =>
195+
Effect.gen(function* () {
196+
const { provider, checks, calls } = yield* countingProvider;
197+
198+
yield* provider.getChangeRequest(reference);
199+
yield* checks(reference);
200+
201+
expect(calls.detail).toBe(1);
202+
}),
203+
);
204+
205+
it.effect("reads a merge request again after a write to it", () =>
206+
Effect.gen(function* () {
207+
const { provider, checks, calls } = yield* countingProvider;
208+
209+
yield* provider.getChangeRequest(reference);
210+
yield* provider.runAction({ ...reference, action: "ready" });
211+
yield* checks(reference);
212+
213+
expect(calls.detail).toBe(2);
214+
}),
215+
);
216+
217+
it.effect("does not share reads between checkouts", () =>
218+
Effect.gen(function* () {
219+
const { provider, calls } = yield* countingProvider;
220+
221+
yield* provider.getChangeRequest(reference);
222+
yield* provider.getChangeRequest({ ...reference, cwd: "/other" });
223+
224+
expect(calls).toEqual({ detail: 2, mergeCapabilities: 2 });
225+
}),
226+
);
227+
228+
it.effect("reads a merge request again once a minute has passed", () =>
229+
Effect.gen(function* () {
230+
const { provider, calls } = yield* countingProvider;
231+
232+
yield* provider.getChangeRequest(reference);
233+
yield* TestClock.adjust("61 seconds");
234+
yield* provider.getChangeRequest(reference);
235+
236+
expect(calls.detail).toBe(2);
237+
}),
238+
);
239+
240+
it.effect("answers viewer permissions from the host, not the page's read", () =>
241+
Effect.gen(function* () {
242+
const { provider, host } = yield* countingProvider;
243+
244+
host.viewerCanMerge = false;
245+
yield* provider.getChangeRequest(reference);
246+
host.viewerCanMerge = true;
247+
const granted = yield* provider.getViewerPermissions(reference);
248+
host.viewerCanMerge = false;
249+
const revoked = yield* provider.getViewerPermissions(reference);
250+
251+
expect(granted.actions).toContain("merge");
252+
expect(revoked.actions).not.toContain("merge");
253+
}),
254+
);
255+
256+
it.effect("reads a merge request again once its reads are forgotten", () =>
257+
Effect.gen(function* () {
258+
const { provider, checks, forget, calls } = yield* countingProvider;
259+
const other = { ...reference, repository: "acme/api" };
260+
261+
yield* provider.getChangeRequest(reference);
262+
yield* provider.getChangeRequest(other);
263+
yield* forget({ repository: "ACME/web", number: 7 });
264+
yield* checks(reference);
265+
yield* checks(other);
266+
expect(calls.detail).toBe(3);
267+
268+
yield* forget({ repository: "acme/web" });
269+
yield* checks(reference);
270+
yield* forget();
271+
yield* checks(other);
272+
expect(calls.detail).toBe(5);
273+
}),
274+
);
275+
});
276+
125277
describe("optional activity reads", () => {
126278
const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 };
127279
const comment = {

0 commit comments

Comments
 (0)