Skip to content

Commit 0045ad9

Browse files
committed
refactor(pr): simplify saved summary sync
Key saved thread snapshots by the existing summary key and order them with newestPullRequestSummary. Detail reads announce on state, host update time, or mergeability, and the snapshot contract no longer carries observedAt.
1 parent 30f831b commit 0045ad9

8 files changed

Lines changed: 50 additions & 118 deletions

File tree

‎apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,7 @@ describe("PullRequestSyncReactor", () => {
457457
const fixture = yield* makeHarness({
458458
snapshot: makeSnapshot([makeThread("one", { pullRequests: [makeLink(42)] })]),
459459
summary: (input) =>
460-
Effect.succeed(
461-
makeSummary(input, { title: "Ship it", isDraft: true, observedAt: 123 }),
462-
),
460+
Effect.succeed(makeSummary(input, { title: "Ship it", isDraft: true })),
463461
});
464462

465463
yield* Effect.gen(function* () {
@@ -489,7 +487,6 @@ describe("PullRequestSyncReactor", () => {
489487
baseBranch: "main",
490488
isDraft: true,
491489
updatedAt: "2026-08-27T00:00:00.000Z",
492-
observedAt: 123,
493490
syncedAt: NOW,
494491
closedAt: null,
495492
mergedAt: null,
@@ -569,17 +566,12 @@ describe("PullRequestSyncReactor", () => {
569566
yield* TestClock.setTime(Date.parse(NOW));
570567
const fixture = yield* makeHarness({
571568
snapshot: makeSnapshot([makeThread("one", { pullRequests: [makeLink(42)] })]),
572-
summary: (input) =>
573-
Clock.currentTimeMillis.pipe(
574-
Effect.map((observedAt) => makeSummary(input, { observedAt })),
575-
),
576569
});
577570

578571
yield* Effect.gen(function* () {
579572
const reactor = yield* startAndSweep(fixture);
580573
const firstSweep = yield* Ref.get(fixture.syncCommands);
581574
assert.strictEqual(firstSweep.length, 1);
582-
assert.strictEqual(firstSweep[0]?.snapshot.observedAt, Date.parse(NOW));
583575
yield* Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, firstSweep));
584576

585577
yield* sweepAgain(fixture, reactor);

‎apps/server/src/orchestration-v2/PullRequestSyncReactor.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ function snapshotFieldsOf(summary: PullRequestSummary): SnapshotFields {
5757
baseBranch: summary.baseBranch,
5858
isDraft: summary.isDraft ?? false,
5959
updatedAt: summary.updatedAt,
60-
...(summary.observedAt === undefined ? {} : { observedAt: summary.observedAt }),
6160
closedAt: summary.closedAt ?? null,
6261
mergedAt: summary.mergedAt ?? null,
6362
...(summary.author === undefined ? {} : { author: summary.author }),

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5128,7 +5128,7 @@ it.effect("reuses an observed merged state for strict settlement reads", () =>
51285128
}),
51295129
);
51305130

5131-
it.effect("announces changed overview fields a detail read sees", () =>
5131+
it.effect("announces state, edits, and conflicts a detail read sees", () =>
51325132
Effect.gen(function* () {
51335133
let detail = {
51345134
...hostedChangeRequest("body"),
@@ -5164,15 +5164,12 @@ it.effect("announces changed overview fields a detail read sees", () =>
51645164
yield* readDetail;
51655165
assert.deepStrictEqual(announced, ["github.com/acme/web#1"]);
51665166

5167-
// Draft and title changes announce even when lifecycle and host update time stay equal.
5168-
detail = { ...detail, isDraft: true };
5167+
// An edit bumps the host update time; a new conflict does not, and announces anyway.
5168+
detail = { ...detail, title: "Updated title", updatedAt: "2026-07-02T12:00:00Z" };
51695169
yield* readDetail;
51705170
assert.strictEqual(announced.length, 2);
5171+
detail = { ...detail, mergeability: "conflicting" };
51715172
yield* readDetail;
5172-
assert.strictEqual(announced.length, 2);
5173-
detail = { ...detail, title: "Updated title" };
5174-
yield* readDetail;
5175-
assert.strictEqual(announced.length, 3);
51765173
yield* readDetail;
51775174
assert.strictEqual(announced.length, 3);
51785175

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ export interface PullRequestMergeEvent extends PullRequestRef {
9999
readonly mergedAt: string;
100100
}
101101

102-
type PullRequestReading = Pick<PullRequestSummary, "state" | "updatedAt" | "observedAt">;
102+
type PullRequestReading = Pick<
103+
PullRequestSummary,
104+
"state" | "updatedAt" | "observedAt" | "mergeability"
105+
>;
103106

104107
/** Whether `next` is a newer reading of a pull request than `current`. Merged is final. */
105108
function supersedesReading(current: PullRequestReading | undefined, next: PullRequestReading) {
@@ -239,8 +242,8 @@ export class PullRequestService extends Context.Service<
239242
Scope.Scope
240243
>;
241244
/**
242-
* Pull requests a detail read saw for the first time or with changed overview fields,
243-
* so thread links can catch up before the next sync sweep. Best effort.
245+
* Pull requests a detail read saw for the first time or changed (state, host update time,
246+
* mergeability), so thread links can catch up before the next sync sweep. Best effort.
244247
*/
245248
readonly subscribeStateChanges: Effect.Effect<
246249
Stream.Stream<ThreadPullRequestKey>,
@@ -3132,40 +3135,36 @@ export const make = Effect.gen(function* () {
31323135
});
31333136
const shouldReplaceHeldSummary = (key: string, next: PullRequestSummary) =>
31343137
supersedesReading(lastGoodSummary.peek(key), next);
3135-
// The newest overview a detail read found per pull request, whatever cache epoch or credential
3138+
// The newest state a detail read found per pull request, whatever cache epoch or credential
31363139
// it read under. Summary reads stay out: they would hide a change from the detail read after.
3137-
const detailReadings = new Map<string, PullRequestSummary>();
3140+
const detailStates = new Map<string, PullRequestReading>();
31383141
/**
3139-
* Announces changed link fields a detail read already knows, so thread badges and actions
3140-
* follow the open detail without waiting for the next sync sweep. Read timestamps alone
3141-
* do not announce unchanged host data.
3142+
* Announces a pull request a detail read sees first or changed, so a client showing a fresh
3143+
* merge, edit, or conflict brings the thread's link and its settlement along, rather than
3144+
* leaving them to the next sync sweep.
31423145
*/
31433146
const noteDetailReading = (ref: PullRequestRef, next: PullRequestSummary) =>
31443147
Effect.suspend(() => {
31453148
const scope = refScope(ref);
3146-
const current = detailReadings.get(scope);
3149+
const current = detailStates.get(scope);
31473150
if (!supersedesReading(current, next)) return Effect.void;
3148-
detailReadings.delete(scope);
3149-
if (detailReadings.size >= REF_EPOCH_CAPACITY) {
3150-
const oldest = detailReadings.keys().next().value;
3151-
if (oldest !== undefined) detailReadings.delete(oldest);
3151+
detailStates.delete(scope);
3152+
if (detailStates.size >= REF_EPOCH_CAPACITY) {
3153+
const oldest = detailStates.keys().next().value;
3154+
if (oldest !== undefined) detailStates.delete(oldest);
31523155
}
3153-
detailReadings.set(scope, next);
3156+
detailStates.set(scope, {
3157+
state: next.state,
3158+
updatedAt: next.updatedAt,
3159+
observedAt: next.observedAt,
3160+
mergeability: next.mergeability,
3161+
});
3162+
// Hosts bump `updatedAt` for title, branch, draft, and diff changes; mergeability moves
3163+
// with the base branch instead.
31543164
const changed =
31553165
current === undefined ||
31563166
current.state !== next.state ||
3157-
current.title !== next.title ||
3158-
current.headBranch !== next.headBranch ||
3159-
current.baseBranch !== next.baseBranch ||
3160-
current.isDraft !== next.isDraft ||
31613167
current.updatedAt !== next.updatedAt ||
3162-
(current.closedAt ?? null) !== (next.closedAt ?? null) ||
3163-
(current.mergedAt ?? null) !== (next.mergedAt ?? null) ||
3164-
(current.author?.login ?? null) !== (next.author?.login ?? null) ||
3165-
(current.author?.avatarUrl ?? null) !== (next.author?.avatarUrl ?? null) ||
3166-
current.additions !== next.additions ||
3167-
current.deletions !== next.deletions ||
3168-
current.changedFiles !== next.changedFiles ||
31693168
current.mergeability !== next.mergeability;
31703169
return changed && ref.host !== undefined
31713170
? PubSub.publish(stateChanges, {
@@ -3175,7 +3174,7 @@ export const make = Effect.gen(function* () {
31753174
})
31763175
: Effect.void;
31773176
});
3178-
// The last state each routed read reported, kept apart from `detailReadings`: an unverified report
3177+
// The last state each routed read reported, kept apart from `detailStates`: an unverified report
31793178
// must never outrank a reading this environment made itself.
31803179
const reportedStates = new Map<
31813180
string,

‎apps/web/src/components/RightPanelTabs.tsx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,11 +790,10 @@ function PullRequestSurfaceIcon({
790790
const reference = useMemo(
791791
() => ({
792792
projectId: surface.projectId as ProjectId,
793-
...(host === null ? {} : { host }),
794793
repository: surface.repository,
795794
number: surface.number,
796795
}),
797-
[surface.projectId, host, surface.repository, surface.number],
796+
[surface.projectId, surface.repository, surface.number],
798797
);
799798
const sharedSummary = useSharedPullRequestSummary(resolvedEnvironmentId, reference, null);
800799
// The compact tab intentionally shows lifecycle and draft state only. Conflict warnings have

‎apps/web/src/state/pullRequests.test.ts‎

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -110,35 +110,4 @@ describe("pull request summary cache", () => {
110110
expect(newestPullRequestSummary(fresh, saved)).toBe(fresh);
111111
expect(threadPullRequestLinkToSummary({ ...link, snapshot: null }, stale)).toBeNull();
112112
});
113-
114-
it("uses the saved provider read time for same-dated overview changes", () => {
115-
const stale = summary({ observedAt: 100 });
116-
const link = {
117-
host: "github.com",
118-
repository: stale.repository,
119-
number: stale.number,
120-
url: stale.url,
121-
source: "manual" as const,
122-
linkedAt: stale.updatedAt,
123-
stack: null,
124-
snapshot: {
125-
...stale,
126-
title: "Updated widget",
127-
isDraft: true,
128-
mergeability: "conflicting" as const,
129-
observedAt: 200,
130-
syncedAt: "2026-09-12T00:00:00Z",
131-
},
132-
};
133-
const saved = threadPullRequestLinkToSummary(link, stale);
134-
expect(newestPullRequestSummary(stale, saved)).toMatchObject({
135-
title: "Updated widget",
136-
isDraft: true,
137-
mergeability: "conflicting",
138-
observedAt: 200,
139-
});
140-
// A later sync of an older cached provider read must not replace newer detail.
141-
const fresh = summary({ observedAt: 300 });
142-
expect(newestPullRequestSummary(fresh, saved)).toBe(fresh);
143-
});
144113
});

‎apps/web/src/state/pullRequests.ts‎

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import type {
1414
PullRequestSummary,
1515
ThreadPullRequestLink,
1616
} from "@t3tools/contracts";
17-
import {
18-
threadPullRequestKeyOf,
19-
visibleThreadPullRequests,
20-
} from "@t3tools/shared/threadPullRequests";
17+
import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
2118
import * as Option from "effect/Option";
2219
import { AsyncResult, Atom } from "effect/reactivity";
2320
import { useCallback, useLayoutEffect, useMemo } from "react";
@@ -174,41 +171,29 @@ export function threadPullRequestLinkToSummary(
174171
};
175172
}
176173

177-
// Index once per shell update, rather than scanning every thread for each PR control.
178-
const linkedPullRequestSnapshotsAtom = Atom.make((get) => {
179-
const links = new Map<string, ThreadPullRequestLink>();
174+
// Saved thread snapshots by summary key, indexed once per shell update rather than per control.
175+
const savedPullRequestSummariesAtom = Atom.make((get) => {
176+
const summaries = new Map<string, PullRequestSummary>();
180177
for (const thread of get(environmentThreadShells.threadShellsAtom)) {
181178
for (const link of visibleThreadPullRequests(thread.pullRequests)) {
182-
if (link.snapshot === null) continue;
183-
const key = `${thread.environmentId}:${threadPullRequestKeyOf(link)}`;
184-
const previous = links.get(key);
185-
if (previous === undefined || link.snapshot.syncedAt > (previous.snapshot?.syncedAt ?? "")) {
186-
links.set(key, link);
187-
}
179+
const reference = {
180+
projectId: thread.projectId,
181+
repository: link.repository,
182+
number: link.number,
183+
};
184+
const summary = threadPullRequestLinkToSummary(link, reference);
185+
if (summary === null) continue;
186+
const key = pullRequestSummaryKey(thread.environmentId, reference);
187+
summaries.set(key, newestPullRequestSummary(summaries.get(key) ?? null, summary) ?? summary);
188188
}
189189
}
190-
return links;
190+
return summaries;
191191
});
192192

193-
const linkedPullRequestSummaryAtom = Atom.family((key: string) =>
194-
Atom.make((get) => {
195-
const target = JSON.parse(key) as {
196-
environmentId: EnvironmentId;
197-
reference: PullRequestRef;
198-
} | null;
199-
if (target === null) return null;
200-
const { environmentId, reference } = target;
201-
const host = reference.host;
202-
if (host === undefined) return null;
203-
const link = get(linkedPullRequestSnapshotsAtom).get(
204-
`${environmentId}:${threadPullRequestKeyOf({ ...reference, host })}`,
205-
);
206-
return link === undefined ? null : threadPullRequestLinkToSummary(link, reference);
207-
}).pipe(
193+
const savedPullRequestSummaryAtom = Atom.family((key: string) =>
194+
Atom.make((get) => get(savedPullRequestSummariesAtom).get(key) ?? null).pipe(
208195
// Shell updates also resend unchanged links; keep their subscribers quiet.
209-
Atom.withEquality((previous, next) =>
210-
previous === next || JSON.stringify(previous) === JSON.stringify(next),
211-
),
196+
Atom.withEquality((previous, next) => JSON.stringify(previous) === JSON.stringify(next)),
212197
Atom.setIdleTTL(5 * 60_000),
213198
),
214199
);
@@ -225,13 +210,7 @@ export function useSharedPullRequestSummary(
225210
: pullRequestSummaryKey(environmentId, reference);
226211
const atom = observedPullRequestSummaryAtom(key);
227212
const observed = useAtomValue(atom);
228-
const linked = useAtomValue(
229-
linkedPullRequestSummaryAtom(
230-
JSON.stringify(
231-
environmentId === null || reference === null ? null : { environmentId, reference },
232-
),
233-
),
234-
);
213+
const saved = useAtomValue(savedPullRequestSummaryAtom(key));
235214
useLayoutEffect(() => {
236215
if (environmentId === null || reference === null || current === null || observedAt === null)
237216
return;
@@ -242,7 +221,7 @@ export function useSharedPullRequestSummary(
242221
observed,
243222
current === null || observedAt === null ? null : { summary: current, observedAt },
244223
)?.summary ?? current;
245-
return newestPullRequestSummary(summary, linked);
224+
return newestPullRequestSummary(summary, saved);
246225
}
247226
export const pullRequestStackAtom = createPullRequestStackAtomFamily(
248227
connectionAtomRuntime,

‎packages/contracts/src/threadPullRequest.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ export const ThreadPullRequestSnapshot = Schema.Struct({
5454
isDraft: Schema.Boolean,
5555
updatedAt: Schema.NullOr(IsoDateTime),
5656
syncedAt: IsoDateTime,
57-
/** Provider read-start time, preserved when a cached summary is synced later. */
58-
observedAt: Schema.optional(Schema.Finite),
5957
closedAt: Schema.optional(Schema.NullOr(Schema.String)),
6058
mergedAt: Schema.optional(Schema.NullOr(Schema.String)),
6159
author: Schema.optional(Schema.NullOr(PullRequestActor)),

0 commit comments

Comments
 (0)