fix(server): threads settle as soon as a client sees their PR merge (#16761)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
c93080b0a3
commit
cd41c4ada0
4 files changed
+174
-10
No files matched your search
@@ -72,6 +72,7 @@ import {
|
||||
type PullRequestUpdateInput,
|
||||
type SourceControlProviderInfo,
|
||||
type SourceControlProviderKind,
|
||||
type ThreadPullRequestKey,
|
||||
} from "@t3tools/contracts";
|
||||
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
|
||||
import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
|
||||
@@ -98,6 +99,16 @@ export interface PullRequestMergeEvent extends PullRequestRef {
|
||||
readonly mergedAt: string;
|
||||
}
|
||||
|
||||
type PullRequestReading = Pick<PullRequestSummary, "state" | "updatedAt" | "observedAt">;
|
||||
|
||||
/** Whether `next` is a newer reading of a pull request than `current`. Merged is final. */
|
||||
function supersedesReading(current: PullRequestReading | undefined, next: PullRequestReading) {
|
||||
if (current === undefined) return true;
|
||||
if (current.state === "merged" && next.state !== "merged") return false;
|
||||
if (next.updatedAt !== current.updatedAt) return next.updatedAt > current.updatedAt;
|
||||
return (next.observedAt ?? -Infinity) >= (current.observedAt ?? -Infinity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rows per repository when the client does not ask for a page size, and rows per slice when a
|
||||
* listing is carried on from a cursor.
|
||||
@@ -225,6 +236,15 @@ export class PullRequestService extends Context.Service<
|
||||
never,
|
||||
Scope.Scope
|
||||
>;
|
||||
/**
|
||||
* Pull requests a detail read saw for the first time or in a new state (opened, closed,
|
||||
* merged), so thread links can catch up before the next sync sweep. Best effort.
|
||||
*/
|
||||
readonly subscribeStateChanges: Effect.Effect<
|
||||
Stream.Stream<ThreadPullRequestKey>,
|
||||
never,
|
||||
Scope.Scope
|
||||
>;
|
||||
readonly subscribeRefreshes: Stream.Stream<number>;
|
||||
readonly refreshAfterTurn: (projectId: ProjectId) => Effect.Effect<void>;
|
||||
readonly detail: (input: PullRequestRef) => Effect.Effect<PullRequestDetail, PullRequestError>;
|
||||
@@ -666,6 +686,7 @@ const observeRead = Effect.fnUntraced(function* <A, E, R>(read: Effect.Effect<A,
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const mergedPullRequests = yield* PubSub.sliding<PullRequestMergeEvent>(64);
|
||||
const stateChanges = yield* PubSub.sliding<ThreadPullRequestKey>(64);
|
||||
const pullRequestRefreshes = yield* SubscriptionRef.make(0);
|
||||
const registry = yield* PullRequestProviderRegistry.PullRequestProviderRegistry;
|
||||
const serverSettings = yield* ServerSettings.ServerSettingsService;
|
||||
@@ -3057,13 +3078,39 @@ export const make = Effect.gen(function* () {
|
||||
updatedAt: detail.updatedAt,
|
||||
observedAt: detail.observedAt,
|
||||
});
|
||||
const shouldReplaceHeldSummary = (key: string, next: PullRequestSummary) => {
|
||||
const current = lastGoodSummary.peek(key);
|
||||
if (current === undefined) return true;
|
||||
if (current.state === "merged" && next.state !== "merged") return false;
|
||||
if (next.updatedAt !== current.updatedAt) return next.updatedAt > current.updatedAt;
|
||||
return (next.observedAt ?? -Infinity) >= (current.observedAt ?? -Infinity);
|
||||
};
|
||||
const shouldReplaceHeldSummary = (key: string, next: PullRequestSummary) =>
|
||||
supersedesReading(lastGoodSummary.peek(key), next);
|
||||
// The newest state a detail read found per pull request, whatever cache epoch or credential
|
||||
// it read under. Summary reads stay out: they would hide a change from the detail read after.
|
||||
const detailStates = new Map<string, PullRequestReading>();
|
||||
/**
|
||||
* Announces a pull request a detail read sees first or in a new state, so a client showing a
|
||||
* fresh merge brings the thread's link and its settlement along, rather than leaving them to
|
||||
* the next sync sweep.
|
||||
*/
|
||||
const noteDetailReading = (ref: PullRequestRef, next: PullRequestSummary) =>
|
||||
Effect.suspend(() => {
|
||||
const scope = refScope(ref);
|
||||
const current = detailStates.get(scope);
|
||||
if (!supersedesReading(current, next)) return Effect.void;
|
||||
detailStates.delete(scope);
|
||||
if (detailStates.size >= REF_EPOCH_CAPACITY) {
|
||||
const oldest = detailStates.keys().next().value;
|
||||
if (oldest !== undefined) detailStates.delete(oldest);
|
||||
}
|
||||
detailStates.set(scope, {
|
||||
state: next.state,
|
||||
updatedAt: next.updatedAt,
|
||||
observedAt: next.observedAt,
|
||||
});
|
||||
return current?.state !== next.state && ref.host !== undefined
|
||||
? PubSub.publish(stateChanges, {
|
||||
host: ref.host,
|
||||
repository: ref.repository,
|
||||
number: ref.number,
|
||||
})
|
||||
: Effect.void;
|
||||
});
|
||||
const detail: PullRequestService["Service"]["detail"] = (input) => {
|
||||
const key = refCacheKey(input);
|
||||
// Record the summary from a host or cache read, not the stale value
|
||||
@@ -3073,9 +3120,13 @@ export const make = Effect.gen(function* () {
|
||||
const read = Cache.get(detailCache, key).pipe(
|
||||
Effect.tap((value) => {
|
||||
const summary = summaryFromDetail(value, lastGoodSummary.peek(key));
|
||||
return shouldReplaceHeldSummary(key, summary)
|
||||
? lastGoodSummary.record(key, summary)
|
||||
: Effect.void;
|
||||
return noteDetailReading(input, summary).pipe(
|
||||
Effect.andThen(
|
||||
shouldReplaceHeldSummary(key, summary)
|
||||
? lastGoodSummary.record(key, summary)
|
||||
: Effect.void,
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
return input.allowStale === false
|
||||
@@ -3374,6 +3425,9 @@ export const make = Effect.gen(function* () {
|
||||
subscribeMerges: PubSub.subscribe(mergedPullRequests).pipe(
|
||||
Effect.map((subscription) => Stream.fromSubscription(subscription)),
|
||||
),
|
||||
subscribeStateChanges: PubSub.subscribe(stateChanges).pipe(
|
||||
Effect.map((subscription) => Stream.fromSubscription(subscription)),
|
||||
),
|
||||
subscribeRefreshes: SubscriptionRef.changes(pullRequestRefreshes).pipe(
|
||||
Stream.filter((revision) => revision > 0),
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user