fix(pull-requests): shared state + not settling? (#9332)

Co-authored-by: maria-rcks <[email protected]>
This commit is contained in:
mariaandmaria-rcks authored and GitHub committed 2026-09-03 01:47:41 -04:00
1 parent 48ba76bc26
commit 854541a04e
9 files changed
+440 -166

No files matched your search

@@ -1,7 +1,10 @@
import { assert, it } from "@effect/vitest";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
import type {
OrchestrationProjectShell,
@@ -1002,6 +1005,41 @@ it.effect("refuses an action the host never claimed it could run", () =>
}),
);
it.effect("publishes a successful merge for immediate settlement", () =>
Effect.scoped(
Effect.gen(function* () {
const mergedAt = "2026-09-03T02:00:00.000Z";
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", {
runAction: () => TestClock.setTime(Date.parse(mergedAt)),
}),
],
});
const merges = yield* service.subscribeMerges;
const observedMerge = yield* Stream.runHead(merges).pipe(
Effect.forkChild({ startImmediately: true }),
);
yield* service.runAction({
...reference,
repository: " ACME/WEB ",
action: "merge",
mergeMethod: "merge",
});
assert.deepStrictEqual(Option.getOrThrow(yield* Fiber.join(observedMerge)), {
...reference,
mergedAt,
});
}),
),
);
it.effect("refuses an action this viewer may not take, and says what access it takes", () =>
Effect.gen(function* () {
let ran: string | null = null;
@@ -3192,6 +3230,32 @@ it.effect("does not ask the host again for a linked summary it already holds", (
}),
);
it.effect("reuses an observed merged state for strict settlement reads", () =>
Effect.gen(function* () {
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("merged body", 4),
state: "merged",
updatedAt: "2026-07-03T00:00:00Z",
}),
getChangeRequestSummary: () => Effect.die("strict merged state must not refresh"),
}),
],
});
yield* service.detail(reference);
const summary = yield* service.summary(reference, { recoverTransientFailure: false });
assert.strictEqual(summary.state, "merged");
assert.strictEqual(summary.updatedAt, "2026-07-03T00:00:00Z");
}),
);
it.effect("does not let a stale detail reopen overwrite a fresher linked summary", () =>
Effect.gen(function* () {
const gate = yield* Deferred.make<void>();
@@ -1,11 +1,15 @@
import * as Cache from "effect/Cache";
import * as Clock from "effect/Clock";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Layer from "effect/Layer";
import * as PubSub from "effect/PubSub";
import * as Schema from "effect/Schema";
import type * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import {
PullRequestOperationError,
PullRequestUnavailableError,
@@ -63,6 +67,10 @@ import {
} from "./PullRequestProvider.ts";
import { PullRequestProviderRegistry } from "./PullRequestProviderRegistry.ts";
export interface PullRequestMergeEvent extends PullRequestRef {
readonly mergedAt: string;
}
/**
* 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.
@@ -135,6 +143,11 @@ export class PullRequestService extends Context.Service<
input: PullRequestRef,
options?: { readonly recoverTransientFailure?: boolean },
) => Effect.Effect<PullRequestSummary, PullRequestError>;
readonly subscribeMerges: Effect.Effect<
Stream.Stream<PullRequestMergeEvent>,
never,
Scope.Scope
>;
readonly detail: (input: PullRequestRef) => Effect.Effect<PullRequestDetail, PullRequestError>;
readonly activity: (
input: PullRequestRef,
@@ -517,6 +530,7 @@ export function repositoryIdentityOf(project: OrchestrationProjectShell): string
}
export const make = Effect.gen(function* () {
const mergedPullRequests = yield* PubSub.sliding<PullRequestMergeEvent>(64);
const registry = yield* PullRequestProviderRegistry;
const projections = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
const sourceControlProviders = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
@@ -1410,9 +1424,9 @@ export const make = Effect.gen(function* () {
}),
);
const runAction: PullRequestService["Service"]["runAction"] = (input) =>
const runAction = (input: PullRequestActionInput): Effect.Effect<string, PullRequestError> =>
requireProject(input).pipe(
Effect.flatMap((project): Effect.Effect<void, PullRequestError> => {
Effect.flatMap((project): Effect.Effect<string, PullRequestError> => {
// The surface hides what a host cannot do, and this refuses it as well: a request that
// reached here anyway must not be handed to a provider that never claimed the action.
if (!project.api.capabilities.actions.includes(input.action)) {
@@ -1454,7 +1468,7 @@ export const make = Effect.gen(function* () {
// have to say yes. The second is asked last, because it costs a request and the checks
// above do not.
return viewerPermissionsOf(project, input, "runAction").pipe(
Effect.flatMap((viewer): Effect.Effect<void, PullRequestError> => {
Effect.flatMap((viewer): Effect.Effect<string, PullRequestError> => {
if (!viewer.actions.includes(input.action)) {
return Effect.fail(
new PullRequestOperationError({
@@ -1484,7 +1498,10 @@ export const make = Effect.gen(function* () {
...(input.mergeMethod === undefined ? {} : { mergeMethod: input.mergeMethod }),
...(input.updateMethod === undefined ? {} : { updateMethod: input.updateMethod }),
})
.pipe(Effect.mapError(toPullRequestError("runAction")));
.pipe(
Effect.mapError(toPullRequestError("runAction")),
Effect.as(project.repository),
);
}),
);
}),
@@ -2151,9 +2168,13 @@ export const make = Effect.gen(function* () {
const summary: PullRequestService["Service"]["summary"] = (input, options) => {
const key = refCacheKey(input);
const cached = Cache.get(summaryCache, key);
return options?.recoverTransientFailure === false
? cached.pipe(Effect.tap((value) => lastGoodSummary.record(key, value)))
: lastGoodSummary.serveHeld(key, cached, "reuse");
if (options?.recoverTransientFailure !== false) {
return lastGoodSummary.serveHeld(key, cached, "reuse");
}
const held = lastGoodSummary.peek(key);
return held?.state === "merged"
? Effect.succeed(held)
: cached.pipe(Effect.tap((value) => lastGoodSummary.record(key, value)));
};
// Keys serialize positionally and parse back in the lookup, so the cache is the only holder
@@ -2391,17 +2412,35 @@ export const make = Effect.gen(function* () {
}),
),
);
const runActionAndInvalidate: PullRequestService["Service"]["runAction"] = Effect.fn(
"PullRequestService.runActionAndInvalidate",
)(function* (input) {
const repository = yield* runAction(input);
bumpRefEpoch({ ...input, repository });
listingsEpoch = ++epochCounter;
if (input.action === "merge") {
yield* PubSub.publish(mergedPullRequests, {
projectId: input.projectId,
repository,
number: input.number,
mergedAt: DateTime.formatIso(yield* DateTime.now),
});
}
});
return PullRequestService.of({
list,
listStats,
summary,
subscribeMerges: PubSub.subscribe(mergedPullRequests).pipe(
Effect.map((subscription) => Stream.fromSubscription(subscription)),
),
detail,
activity,
threadComments,
diff,
diffFileContents,
runAction: invalidatedByMutation(runAction),
runAction: runActionAndInvalidate,
update: invalidatedByMutation(update),
comment: invalidatedByMutation(comment),
updateComment: invalidatedByMutation(updateComment),