Skip to content

Commit 3dad101

Browse files
committed
fix(server): keep linked PR summaries in sync
1 parent 0cc3732 commit 3dad101

2 files changed

Lines changed: 74 additions & 11 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,6 +3409,43 @@ it.effect("reuses an observed merged state for strict settlement reads", () =>
34093409
}),
34103410
);
34113411

3412+
it.effect("shares detail updates with a registered project's linked summary", () =>
3413+
Effect.gen(function* () {
3414+
let summaryCalls = 0;
3415+
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
3416+
const linkedReference = {
3417+
...reference,
3418+
url: "https://github.com/acme/web/pull/1",
3419+
};
3420+
const service = yield* makeService({
3421+
projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })],
3422+
providers: [
3423+
fakeProvider("github", {
3424+
getChangeRequest: () =>
3425+
Effect.succeed({
3426+
...hostedChangeRequest("merged body", 4),
3427+
url: linkedReference.url,
3428+
state: "merged",
3429+
updatedAt: "2026-07-03T00:00:00Z",
3430+
}),
3431+
getChangeRequestSummary: () => {
3432+
summaryCalls += 1;
3433+
return Effect.succeed({
3434+
...changeRequest(1, "2026-07-02T00:00:00Z"),
3435+
url: linkedReference.url,
3436+
});
3437+
},
3438+
}),
3439+
],
3440+
});
3441+
3442+
assert.strictEqual((yield* service.summary(linkedReference)).state, "open");
3443+
yield* service.detail(reference);
3444+
assert.strictEqual((yield* service.summary(linkedReference)).state, "merged");
3445+
assert.strictEqual(summaryCalls, 1);
3446+
}),
3447+
);
3448+
34123449
it.effect("does not let a stale detail reopen overwrite a fresher linked summary", () =>
34133450
Effect.gen(function* () {
34143451
const gate = yield* Deferred.make<void>();

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

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,27 @@ export const make = Effect.gen(function* () {
21792179
ref.url === undefined
21802180
? refCacheKey(ref)
21812181
: JSON.stringify([refEpoch(ref), ref.projectId, ref.repository, ref.number, ref.url]);
2182+
const heldSummaryCacheKey = (ref: PullRequestRef) => {
2183+
if (ref.url === undefined) return refCacheKey(ref);
2184+
const parsed = parseChangeRequestUrl(ref.url);
2185+
return parsed === null
2186+
? summaryCacheKey(ref)
2187+
: JSON.stringify([
2188+
refEpoch(ref),
2189+
ref.projectId,
2190+
ref.repository,
2191+
ref.number,
2192+
parsed.provider,
2193+
parsed.host,
2194+
]);
2195+
};
2196+
const recordHeldSummary = (key: string, next: PullRequestSummary) => {
2197+
const current = lastGoodSummary.peek(key);
2198+
if (current?.state === "merged" && next.state !== "merged") return Effect.void;
2199+
return current === undefined || next.updatedAt >= current.updatedAt
2200+
? lastGoodSummary.record(key, next)
2201+
: Effect.void;
2202+
};
21822203
const bumpRefEpoch = (ref: PullRequestRef) => {
21832204
const scope = refScope(ref);
21842205
if (!refEpochs.has(scope) && refEpochs.size >= REF_EPOCH_CAPACITY) {
@@ -2227,8 +2248,15 @@ export const make = Effect.gen(function* () {
22272248
},
22282249
);
22292250
const summary: PullRequestService["Service"]["summary"] = (input, options) => {
2230-
const key = summaryCacheKey(input);
2231-
const cached = Cache.get(summaryCache, key);
2251+
const cacheKey = summaryCacheKey(input);
2252+
const key = heldSummaryCacheKey(input);
2253+
const cached = Cache.get(summaryCache, cacheKey).pipe(
2254+
Effect.tap((value) =>
2255+
input.url === undefined
2256+
? recordHeldSummary(heldSummaryCacheKey({ ...input, url: value.url }), value)
2257+
: Effect.void,
2258+
),
2259+
);
22322260
if (options?.recoverTransientFailure !== false) {
22332261
return lastGoodSummary.serveHeld(key, cached, "reuse");
22342262
}
@@ -2337,12 +2365,6 @@ export const make = Effect.gen(function* () {
23372365
baseBranch: detail.baseBranch,
23382366
updatedAt: detail.updatedAt,
23392367
});
2340-
const shouldReplaceHeldSummary = (key: string, next: PullRequestSummary) => {
2341-
const current = lastGoodSummary.peek(key);
2342-
if (current === undefined) return true;
2343-
if (current.state === "merged" && next.state !== "merged") return false;
2344-
return next.updatedAt >= current.updatedAt;
2345-
};
23462368
const detail: PullRequestService["Service"]["detail"] = (input) => {
23472369
const key = refCacheKey(input);
23482370
// Record the summary from a host or cache read, not the stale value
@@ -2354,9 +2376,13 @@ export const make = Effect.gen(function* () {
23542376
Cache.get(detailCache, key).pipe(
23552377
Effect.tap((value) => {
23562378
const summary = summaryFromDetail(value);
2357-
return shouldReplaceHeldSummary(key, summary)
2358-
? lastGoodSummary.record(key, summary)
2359-
: Effect.void;
2379+
return Effect.all(
2380+
[
2381+
recordHeldSummary(key, summary),
2382+
recordHeldSummary(heldSummaryCacheKey({ ...input, url: summary.url }), summary),
2383+
],
2384+
{ discard: true },
2385+
);
23602386
}),
23612387
),
23622388
"revalidate",

0 commit comments

Comments
 (0)