fix(server): thread PR badges catch up when another environment reads the PR (#17729)

Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
oliverandClaude Opus 5.5 authored and GitHub committed 2026-10-10 02:24:11 -05:00
1 parent 42f71dfe07
commit 6d5ea190a4
9 files changed
+139

No files matched your search

+1
View File
@@ -135,6 +135,7 @@ export const RPC_REQUIRED_SCOPES = {
// Read scope like the reads it un-caches: refreshing is part of reading, and a read-only
// client pressing refresh must not be told it may not look again.
[WS_METHODS.pullRequestsInvalidate]: AuthOrchestrationReadScope,
[WS_METHODS.pullRequestsReportState]: AuthOrchestrationReadScope,
[WS_METHODS.pullRequestsSubscribeRefreshes]: AuthOrchestrationReadScope,
// The candidate list is a read like the detail beside it; asking somebody for a review is a
// write like every other one.
@@ -118,6 +118,7 @@ const RPC_AGGREGATES = {
[WS_METHODS.pullRequestsSetReaction]: "pull-requests",
[WS_METHODS.pullRequestsSetFilesViewed]: "pull-requests",
[WS_METHODS.pullRequestsInvalidate]: "pull-requests",
[WS_METHODS.pullRequestsReportState]: "pull-requests",
[WS_METHODS.pullRequestsSubscribeRefreshes]: "pull-requests",
[WS_METHODS.pullRequestsReviewerCandidates]: "pull-requests",
[WS_METHODS.pullRequestsRequestReviewers]: "pull-requests",
@@ -4972,6 +4972,48 @@ it.effect("announces state a detail read sees first or newly", () =>
}),
);
it.effect("announces a reported state without trusting it", () =>
Effect.gen(function* () {
let detail = { state: "open" as "open" | "closed", updatedAt: "2026-07-02T00:00:00Z" };
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
const service = yield* makeService({
projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })],
providers: [
fakeProvider("github", {
getChangeRequest: () => Effect.succeed({ ...hostedChangeRequest("body"), ...detail }),
}),
],
});
const announced: Array<string> = [];
yield* Stream.runForEach(yield* service.subscribeStateChanges, (key) =>
Effect.sync(() => announced.push(`${key.host}/${key.repository}#${key.number}`)),
).pipe(Effect.forkChild({ startImmediately: true }));
const note = (state: "open" | "closed" | "merged") =>
service.reportState({ reference, state }).pipe(Effect.andThen(Effect.yieldNow));
const readDetail = Effect.gen(function* () {
yield* service.invalidate({ reference });
yield* service.detail(reference);
yield* Effect.yieldNow;
});
yield* readDetail;
yield* note("closed");
yield* note("closed");
assert.strictEqual(announced.length, 2);
yield* TestClock.adjust("5 minutes");
yield* note("closed");
assert.strictEqual(announced.length, 3);
yield* note("open");
assert.strictEqual(announced.length, 4);
yield* note("merged");
detail = { state: "closed", updatedAt: "2026-07-03T00:00:00Z" };
yield* readDetail;
assert.strictEqual(announced.length, 6);
}),
);
it.effect("does not let a stale detail reopen overwrite a fresher linked summary", () =>
Effect.gen(function* () {
const gate = yield* Deferred.make<void>();
@@ -42,6 +42,7 @@ import {
type PullRequestFilesViewedResult,
type PullRequestDiffResult,
type PullRequestInvalidateInput,
type PullRequestReportStateInput,
type PullRequestListEntry,
type PullRequestListFilters,
type PullRequestListInput,
@@ -160,6 +161,8 @@ const detailTimeToLive = (state: PullRequestState | undefined) =>
* page and a watched pull request's change reaches its watch.
*/
const CHECKS_CACHE_TTL = Duration.seconds(15);
/** How long a repeated reported state is skipped, so a wrong one cannot hide a real change for long. */
const REPORTED_STATE_DEDUPE_MS = 5 * 60 * 1_000;
const DIFF_CACHE_TTL = Duration.seconds(60);
/** A commit is content-addressed, so its own diff cannot change under its key. */
const COMMIT_DIFF_CACHE_TTL = Duration.minutes(10);
@@ -244,6 +247,13 @@ export class PullRequestService extends Context.Service<
never,
Scope.Scope
>;
/**
* The state a read routed to another environment saw. A new one reaches
* `subscribeStateChanges`, so the host is asked again; the report itself is never trusted.
*/
readonly reportState: (
input: PullRequestReportStateInput,
) => Effect.Effect<void, PullRequestError>;
readonly subscribeRefreshes: Stream.Stream<number>;
readonly refreshAfterTurn: (projectId: ProjectId) => Effect.Effect<void>;
readonly detail: (input: PullRequestRef) => Effect.Effect<PullRequestDetail, PullRequestError>;
@@ -3115,6 +3125,36 @@ export const make = Effect.gen(function* () {
})
: Effect.void;
});
// The last state each routed read reported, kept apart from `detailStates`: an unverified report
// must never outrank a reading this environment made itself.
const reportedStates = new Map<
string,
{ readonly state: PullRequestState; readonly atMs: number }
>();
const reportState: PullRequestService["Service"]["reportState"] = ({ reference, state }) =>
Effect.all([canonicalRef(reference), Clock.currentTimeMillis]).pipe(
Effect.flatMap(([ref, nowMs]) =>
Effect.suspend(() => {
const scope = refScope(ref);
const reported = reportedStates.get(scope);
if (reported?.state === state && nowMs - reported.atMs < REPORTED_STATE_DEDUPE_MS)
return Effect.void;
reportedStates.delete(scope);
if (reportedStates.size >= REF_EPOCH_CAPACITY) {
const oldest = reportedStates.keys().next().value;
if (oldest !== undefined) reportedStates.delete(oldest);
}
reportedStates.set(scope, { state, atMs: nowMs });
return ref.host === undefined
? Effect.void
: PubSub.publish(stateChanges, {
host: ref.host,
repository: ref.repository,
number: ref.number,
});
}),
),
);
const detail: PullRequestService["Service"]["detail"] = (input) => {
const key = refCacheKey(input);
// Record the summary from a host or cache read, not the stale value
@@ -3432,6 +3472,7 @@ export const make = Effect.gen(function* () {
subscribeStateChanges: PubSub.subscribe(stateChanges).pipe(
Effect.map((subscription) => Stream.fromSubscription(subscription)),
),
reportState,
subscribeRefreshes: SubscriptionRef.changes(pullRequestRefreshes).pipe(
Stream.filter((revision) => revision > 0),
),
+1
View File
@@ -2518,6 +2518,7 @@ const layerWsRpc = (
withPullRequestViewer(input, pullRequests.setThreadResolution(input)),
[WS_METHODS.pullRequestsSetReaction]: (input) =>
withPullRequestViewer(input, pullRequests.setReaction(input)),
[WS_METHODS.pullRequestsReportState]: (input) => pullRequests.reportState(input),
[WS_METHODS.pullRequestsInvalidate]: (input) =>
pullRequests.invalidate(input, { notifyReaders: true }).pipe(
// A reader asking for fresh host state also wants the thread badges it feeds to
@@ -2,6 +2,7 @@ import {
WS_METHODS,
PullRequestRef,
PullRequestInvalidateInput,
PullRequestState,
type EnvironmentId,
type PullRequestRoutingResult,
type PullRequestRoutingIdentityResult,
@@ -57,6 +58,7 @@ const writes = new Set<string>([
WS_METHODS.pullRequestsSetLabels,
]);
const isRef = Schema.is(PullRequestRef);
const hasState = Schema.is(Schema.Struct({ state: PullRequestState }));
const isInvalidation = Schema.is(PullRequestInvalidateInput);
const readTimeout = (environmentId: EnvironmentId) =>
Effect.timeoutOrElse({
@@ -208,6 +210,21 @@ const invalidateTarget = Effect.fn("PullRequestRouting.invalidateTarget")(functi
);
});
/**
* The origin owns the threads linked to this pull request, but a routed read never reaches it.
* Tell it which state the read saw so it checks again. Best effort, and never holds up the read.
*/
const reportState = (
{ allowStale: _allowStale, expectedAccountId: _expectedAccountId, ...reference }: PullRequestRef,
state: PullRequestState,
) =>
request(WS_METHODS.pullRequestsReportState, { reference, state }).pipe(
Effect.timeoutOption("5 seconds"),
Effect.ignore,
Effect.forkDetach,
Effect.asVoid,
);
/** Credentials stay on their environments. Only a verified host and account cross the wire. */
export function createPullRequestRouter() {
const routedRequest = Effect.fn("PullRequestRouting.request")(function* <
@@ -356,6 +373,11 @@ export function createPullRequestRouter() {
? guardedSource
: registry.run(id, request(tag, routedInput))
).pipe(
Effect.tap((result) =>
id !== origin.target.environmentId && hasState(result)
? reportState(ref, result.state)
: Effect.void,
),
Effect.tap(() =>
Effect.sync(() => {
const entry = used.get(refKey) ?? {
@@ -995,6 +995,7 @@ it.effect.each(
it.live("keeps source workspace metadata when an alternate answers a detail read", () =>
Effect.scoped(
Effect.gen(function* () {
const noted = yield* Deferred.make<unknown>();
const clientFor = (local: boolean) =>
({
[local ? WS_METHODS.pullRequestsRoutingIdentity : WS_METHODS.pullRequestsRouting]: () =>
@@ -1013,8 +1014,12 @@ it.live("keeps source workspace metadata when an alternate answers a detail read
projectTitle: "local project",
workspaceRoot: "/Users/local/repo",
title: "github title",
state: "closed",
updatedAt: "2026-10-10T01:52:36Z",
})
: Effect.never,
[WS_METHODS.pullRequestsReportState]: (input: unknown) =>
Deferred.succeed(noted, input).pipe(Effect.asVoid),
}) as unknown as WsRpcProtocolClient;
const { environmentRegistry, supervisor } = yield* makeTestRuntime(
clientFor(false),
@@ -1035,6 +1040,12 @@ it.live("keeps source workspace metadata when an alternate answers a detail read
projectTitle: "source project",
workspaceRoot: "/srv/source/repo",
title: "github title",
state: "closed",
updatedAt: "2026-10-10T01:52:36Z",
});
expect(yield* Deferred.await(noted)).toEqual({
reference: { projectId: "project-1", repository: "acme/web", number: 7 },
state: "closed",
});
}),
),
+11
View File
@@ -849,6 +849,17 @@ export const PullRequestInvalidateInput = Schema.Struct({
});
export type PullRequestInvalidateInput = typeof PullRequestInvalidateInput.Type;
/**
* The state a read routed to another environment saw, reported to the environment the read was
* for so its thread links can catch up. A hint only: that environment confirms with the host
* before writing anything.
*/
export const PullRequestReportStateInput = Schema.Struct({
reference: PullRequestRef,
state: PullRequestState,
});
export type PullRequestReportStateInput = typeof PullRequestReportStateInput.Type;
export const PullRequestDetail = Schema.Struct({
provider: SourceControlProviderKind,
capabilities: PullRequestCapabilities,
+9
View File
@@ -171,6 +171,7 @@ import {
PullRequestDiffFileContentsResult,
PullRequestFilesViewedResult,
PullRequestInvalidateInput,
PullRequestReportStateInput,
PullRequestListInput,
PullRequestListResult,
PullRequestListStatsInput,
@@ -543,6 +544,7 @@ export const WS_METHODS = {
pullRequestsSetThreadResolution: "pullRequests.setThreadResolution",
pullRequestsSetReaction: "pullRequests.setReaction",
pullRequestsInvalidate: "pullRequests.invalidate",
pullRequestsReportState: "pullRequests.reportState",
pullRequestsSubscribeRefreshes: "pullRequests.subscribeRefreshes",
pullRequestsReviewerCandidates: "pullRequests.reviewerCandidates",
pullRequestsRequestReviewers: "pullRequests.requestReviewers",
@@ -1087,6 +1089,12 @@ const WsPullRequestsInvalidateRpc = Rpc.make(WS_METHODS.pullRequestsInvalidate,
error: PullRequestRpcError,
});
const WsPullRequestsReportStateRpc = Rpc.make(WS_METHODS.pullRequestsReportState, {
payload: PullRequestReportStateInput,
success: Schema.Void,
error: PullRequestRpcError,
});
const WsPullRequestsSubscribeRefreshesRpc = Rpc.make(WS_METHODS.pullRequestsSubscribeRefreshes, {
payload: Schema.Struct({}),
success: NonNegativeInt,
@@ -1916,6 +1924,7 @@ export const WsRpcGroup = RpcGroup.make(
WsPullRequestsSetThreadResolutionRpc,
WsPullRequestsSetReactionRpc,
WsPullRequestsInvalidateRpc,
WsPullRequestsReportStateRpc,
WsPullRequestsSubscribeRefreshesRpc,
WsPullRequestsReviewerCandidatesRpc,
WsPullRequestsRequestReviewersRpc,