@devries-cameron20 / t3code Mirror · read-only / commits / 4e1be34436
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 23:30 UTC

feat(server): watched GitHub pull requests react to host events, not only the 2-minute sweep (#33548)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 10, 2026 at 23:12 UTC 4e1be34436df1936c0eae22e4f19979d01b0cd94
8 files changed +722 -15
apps/server/src/orchestration-v2/PullRequestWatchReactor.ts
+86 -15
@@ -20,12 +20,14 @@ import * as Context from "effect/Context";
20 import * as SecureRandom from "effect/crypto/SecureRandom";
21 import * as Effect from "effect/Effect";
22 import * as Exit from "effect/Exit";
23 +import * as FiberMap from "effect/FiberMap";
24 import * as Layer from "effect/Layer";
25 import * as Option from "effect/Option";
26 import * as Schedule from "effect/Schedule";
27 import * as Schema from "effect/Schema";
28 import * as Semaphore from "effect/Semaphore";
29 import type * as Scope from "effect/Scope";
30 +import * as Stream from "effect/Stream";
31
32 import {
33 type ProviderChangeRequestWatchFingerprint,
@@ -252,6 +254,10 @@ export const make = Effect.gen(function* () {
254 Effect.suspend(() => reportEnd(lifeKey(target), lifeOf(target), reason));
255
256 const readFailures = new Map<string, number>();
257 + /** The last sweep's groups, whose pull requests get host change feeds. */
258 + let currentGroups: ReadonlyArray<WatchGroup> = [];
259 + // A pass reads watches and records new ones, so passes must not overlap.
260 + const passes = Semaphore.makeUnsafe(1);
261 const lastReads = new Map<string, LastRead>();
262 const threadTails = new Map<
263 string,
@@ -585,19 +591,26 @@ export const make = Effect.gen(function* () {
591 });
592 });
593
588 - const sweep = Effect.gen(function* () {
594 + /** Every watch, or with `changed`, only that pull request's after its host reported a change. */
595 + // The sweep and change reads each name their own span.
596 + const pass = Effect.fnUntraced(function* (changed: string | undefined) {
597 const threads = yield* projections.getThreadsWithPullRequests();
598 const targets = threads.flatMap((thread) =>
599 visibleThreadPullRequests(thread.pullRequests ?? []).flatMap((link) =>
592 - link.watch === undefined ? [] : [{ thread, link, watch: link.watch }],
600 + link.watch === undefined ||
601 + (changed !== undefined && threadPullRequestKeyOf(link) !== changed)
602 + ? []
603 + : [{ thread, link, watch: link.watch }],
604 ),
605 );
595 - const present = new Set(targets.map(lifeKey));
596 - yield* Effect.forEach(
597 - [...lives].filter(([key]) => !present.has(key)),
598 - ([key, life]) => reportEnd(key, life, "stopped"),
599 - { discard: true },
600 - );
606 + if (changed === undefined) {
607 + const present = new Set(targets.map(lifeKey));
608 + yield* Effect.forEach(
609 + [...lives].filter(([key]) => !present.has(key)),
610 + ([key, life]) => reportEnd(key, life, "stopped"),
611 + { discard: true },
612 + );
613 + }
614 for (const target of targets) lifeOf(target);
615 const byPullRequest = new Map<string, Array<WatchTarget>>();
616 const ending: Array<readonly [WatchTarget, WatchEndReason]> = [];
@@ -616,8 +629,11 @@ export const make = Effect.gen(function* () {
629 targets: members,
630 fingerprint: [...new Set(members.map(snapshotFingerprint))].toSorted().join("\n"),
631 }));
619 - for (const cache of [readFailures, lastReads, threadTails]) {
620 - for (const key of cache.keys()) if (!byPullRequest.has(key)) cache.delete(key);
632 + if (changed === undefined) {
633 + currentGroups = groups;
634 + for (const cache of [readFailures, lastReads, threadTails]) {
635 + for (const key of cache.keys()) if (!byPullRequest.has(key)) cache.delete(key);
636 + }
637 }
638 yield* Effect.forEach(
639 ending,
@@ -639,7 +655,24 @@ export const make = Effect.gen(function* () {
655 yield* Effect.forEach(
656 groups,
657 (group) =>
642 - fingerprintOf(group).pipe(
658 + // The host just reported a change, so a cached fingerprint may predate it.
659 + (changed === undefined
660 + ? Effect.void
661 + : pullRequests.invalidate({ reference: referenceOf(group) })
662 + ).pipe(
663 + Effect.andThen(fingerprintOf(group)),
664 + Effect.tap((fingerprint) => {
665 + const last = lastReads.get(group.key);
666 + // Open pull request views refresh only when the host state moved.
667 + return changed !== undefined &&
668 + fingerprint !== null &&
669 + fingerprint !== "paused" &&
670 + (last === undefined ||
671 + last.status !== fingerprint.status ||
672 + last.remarks !== fingerprint.remarks)
673 + ? pullRequests.invalidate({ reference: referenceOf(group) }, { notifyReaders: true })
674 + : Effect.void;
675 + }),
676 Effect.flatMap((fingerprint) =>
677 fingerprint === "paused"
678 ? Effect.void
@@ -654,16 +687,54 @@ export const make = Effect.gen(function* () {
687 ),
688 { concurrency: "unbounded", discard: true },
689 );
657 - }).pipe(
690 + }, passes.withPermit);
691 +
692 + const sweep = pass(undefined).pipe(
693 Effect.catchCause(logFailure("pull request watch sweep failed", {})),
694 Effect.withSpan("PullRequestWatchReactor.sweep"),
695 );
696
662 - const start: PullRequestWatchReactor["Service"]["start"] = () =>
663 - forkParked(
664 - sweep.pipe(Effect.repeat(Schedule.spaced(`${SWEEP_MINUTES} minutes`)), Effect.asVoid),
697 + const pullRequestOf = (group: WatchGroup) => threadPullRequestKeyOf(group.targets[0]!.link);
698 +
699 + const readOnChange = (pullRequest: string) =>
700 + pass(pullRequest).pipe(
701 + Effect.catchCause(logFailure("pull request watch change read failed", { pullRequest })),
702 + Effect.withSpan("PullRequestWatchReactor.readOnChange"),
703 );
704
705 + const start: PullRequestWatchReactor["Service"]["start"] = Effect.fn(
706 + "PullRequestWatchReactor.start",
707 + )(function* () {
708 + // One feed per pull request: the host keeps one subscription per pull request and account.
709 + const feeds = yield* FiberMap.make<string>();
710 + const follow = Effect.gen(function* () {
711 + const wanted = new Map(currentGroups.map((group) => [pullRequestOf(group), group]));
712 + const stale = Array.from(feeds, ([pullRequest]) => pullRequest).filter(
713 + (pullRequest) => !wanted.has(pullRequest),
714 + );
715 + yield* Effect.forEach(stale, (pullRequest) => FiberMap.remove(feeds, pullRequest), {
716 + discard: true,
717 + });
718 + for (const [pullRequest, group] of wanted) {
719 + // An ended feed restarts here, so a host that refused it is asked again each sweep.
720 + yield* FiberMap.run(feeds, pullRequest, { onlyIfMissing: true })(
721 + pullRequests.changes(referenceOf(group)).pipe(
722 + // A review sends an event per comment; those landing during a read make one more.
723 + Stream.buffer({ capacity: 1, strategy: "sliding" }),
724 + Stream.runForEach(() => readOnChange(pullRequest)),
725 + ),
726 + );
727 + }
728 + });
729 + yield* forkParked(
730 + sweep.pipe(
731 + Effect.andThen(follow),
732 + Effect.repeat(Schedule.spaced(`${SWEEP_MINUTES} minutes`)),
733 + Effect.asVoid,
734 + ),
735 + );
736 + });
737 +
738 return { start, sweep } satisfies PullRequestWatchReactor["Service"];
739 });
740
apps/server/src/orchestration-v2/runtimeLayer.test.ts
+121
@@ -3890,6 +3890,127 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
3890 }),
3891 );
3892
3893 + it.effect(
3894 + "reads a watched pull request when the host reports a change, and wakes once per change",
3895 + () =>
3896 + Effect.scoped(
3897 + Effect.gen(function* () {
3898 + const orchestrator = yield* Orchestrator.OrchestratorV2;
3899 + const threadId = ThreadId.make("runtime-pull-request-watch-host-event");
3900 + const projectId = ProjectId.make("pr-watch-host-event-project");
3901 + yield* seedProject({
3902 + projectId,
3903 + title: "Watch host events",
3904 + workspaceRoot: "/workspace/watch-host-event",
3905 + defaultModelSelection: null,
3906 + createdAt: "2026-10-01T00:00:00.000Z",
3907 + });
3908 + yield* orchestrator.dispatch({
3909 + type: "thread.create",
3910 + createdBy: "user",
3911 + creationSource: "web",
3912 + commandId: CommandId.make("pr-watch-host-event-create"),
3913 + threadId,
3914 + projectId,
3915 + title: "Watch host events",
3916 + modelSelection,
3917 + runtimeMode: "full-access",
3918 + interactionMode: "default",
3919 + branch: null,
3920 + worktreePath: null,
3921 + });
3922 + const key = { host: "github.com", repository: "pingdotgg/t3code", number: 15 };
3923 + yield* orchestrator.dispatch({
3924 + type: "thread.pull-request.watch",
3925 + commandId: CommandId.make("pr-watch-host-event-start"),
3926 + threadId,
3927 + ...key,
3928 + watching: true,
3929 + link: { url: "https://github.com/pingdotgg/t3code/pull/15", source: "agent" },
3930 + });
3931 +
3932 + let checks: PullRequestDetail["checks"] = [
3933 + { name: "lint", status: "pending", description: null, url: null, required: true },
3934 + ];
3935 + let fingerprints = 0;
3936 + const events = yield* Queue.unbounded<void>();
3937 + const following = yield* Deferred.make<void>();
3938 + const detailReads = yield* Queue.unbounded<void>();
3939 + const host = Layer.mergeAll(
3940 + NodeServices.layer,
3941 + NodeCrypto.layer,
3942 + Layer.mock(PullRequestService.PullRequestService)({
3943 + detail: () =>
3944 + Queue.offer(detailReads, undefined).pipe(
3945 + Effect.as({
3946 + ...watchedPullRequestDetail({
3947 + projectId,
3948 + number: 15,
3949 + at: "2026-10-02T12:00:00.000Z",
3950 + }),
3951 + headSha: "aaaaaaaaaa",
3952 + checks,
3953 + }),
3954 + ),
3955 + activity: () =>
3956 + Effect.succeed({
3957 + comments: [],
3958 + commentCount: 0,
3959 + commentsTruncated: false,
3960 + reviewThreads: [],
3961 + commits: [],
3962 + }),
3963 + // A fresh fingerprint every time, so every pass reads the detail.
3964 + watchFingerprint: () =>
3965 + Effect.sync(() => ({ status: String(++fingerprints), remarks: "" })),
3966 + invalidate: () => Effect.void,
3967 + changes: () =>
3968 + Stream.fromEffectDrain(Deferred.succeed(following, undefined)).pipe(
3969 + Stream.concat(Stream.fromQueue(events)),
3970 + ),
3971 + }),
3972 + );
3973 + const wakes = Effect.map(
3974 + orchestrator.getThreadRecords(threadId, ["messages"]),
3975 + ({ messages }) =>
3976 + messages.filter(
3977 + (message) => message.notification !== undefined && message.notification !== null,
3978 + ),
3979 + );
3980 + const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
3981 + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3982 + yield* reactor.start();
3983 + // The feed starts after the first sweep has read the pull request.
3984 + yield* Deferred.await(following);
3985 + yield* Queue.take(detailReads);
3986 +
3987 + checks = [
3988 + { name: "lint", status: "failure", description: null, url: null, required: true },
3989 + ];
3990 + yield* Queue.offer(events, undefined);
3991 + // The clock never moves, so no sweep can be the one that reads the failure.
3992 + yield* Queue.take(detailReads);
3993 + // Nothing new: this read must compare against the watch the last one recorded.
3994 + yield* Queue.offer(events, undefined);
3995 + yield* Queue.take(detailReads);
3996 + // A sweep waits for the change read in flight.
3997 + yield* reactor.sweep;
3998 + yield* worker.drain();
3999 + const woken = yield* wakes;
4000 + assert.lengthOf(woken, 1);
4001 + assert.include(woken[0]!.notification?.summary ?? "", "failed");
4002 +
4003 + yield* orchestrator.dispatch({
4004 + type: "thread.pull-request.watch",
4005 + commandId: CommandId.make("pr-watch-host-event-end"),
4006 + threadId,
4007 + ...key,
4008 + watching: false,
4009 + });
4010 + }),
4011 + ),
4012 + );
4013 +
4014 it.effect("wakes for checks that complete after the no-checks notice on the same head", () =>
4015 Effect.gen(function* () {
4016 const orchestrator = yield* Orchestrator.OrchestratorV2;
apps/server/src/pullRequest/PullRequestService.ts
+24
@@ -236,6 +236,8 @@ export class PullRequestService extends Context.Service<
236 readonly activity: (
237 input: PullRequestRef,
238 ) => Effect.Effect<PullRequestActivity, PullRequestError>;
239 + /** Emits when the host reports a change; empty where the host has no event feed. */
240 + readonly changes: (input: PullRequestRef) => Stream.Stream<void>;
241 readonly threadComments: (
242 input: PullRequestThreadCommentsInput,
243 ) => Effect.Effect<PullRequestThreadCommentsResult, PullRequestError>;
@@ -570,6 +572,10 @@ function withRateLimitBackoff(
572 api.getChangeRequestWatchFingerprint,
573 ),
574 }),
575 + // A long-lived feed, not a read: the quota guard would hold its lease for hours.
576 + ...(api.changeRequestEvents === undefined
577 + ? {}
578 + : { changeRequestEvents: api.changeRequestEvents }),
579 getChangeRequestActivity: wrap("getChangeRequestActivity", api.getChangeRequestActivity),
580 ...(api.getReviewThreadComments === undefined
581 ? {}
@@ -1084,6 +1090,23 @@ export const make = Effect.gen(function* () {
1090 };
1091 });
1092
1093 + const changes: PullRequestService["Service"]["changes"] = (input) =>
1094 + Stream.unwrap(
1095 + requireProject(input).pipe(
1096 + Effect.map((project) =>
1097 + project.api.changeRequestEvents === undefined
1098 + ? Stream.empty
1099 + : project.api.changeRequestEvents({
1100 + cwd: project.project.workspaceRoot,
1101 + repository: project.repository,
1102 + host: project.host,
1103 + number: input.number,
1104 + }),
1105 + ),
1106 + Effect.orElseSucceed(() => Stream.empty),
1107 + ),
1108 + );
1109 +
1110 // Access can change after page load; recheck every write.
1111 const viewerPermissionsOf = (
1112 project: SupportedProject,
@@ -3815,6 +3838,7 @@ export const make = Effect.gen(function* () {
3838 Cache.get(watchFingerprintCache, refCacheKey(input)),
3839 ),
3840 activity: credentialCached(activity),
3841 + changes,
3842 preview: credentialCached(preview),
3843 threadComments,
3844 diff: credentialCached(diff),
packages/source-control-core/src/server/PullRequestProvider.ts
+9
@@ -1,5 +1,6 @@
1 import * as Effect from "effect/Effect";
2 import * as Schema from "effect/Schema";
3 +import type * as Stream from "effect/Stream";
4 import type {
5 PullRequestStackMembership,
6 PullRequestAction,
@@ -397,6 +398,14 @@ export interface PullRequestProviderApi {
398 input: ProviderRepositoryRef & { readonly number: number },
399 ) => Effect.Effect<ProviderChangeRequestWatchFingerprint | null, PullRequestProviderError>;
400
401 + /**
402 + * Emits whenever the host says the change request may have changed, and once on connecting.
403 + * Ends where the host or account offers no such feed; readers then rely on polling alone.
404 + */
405 + readonly changeRequestEvents?: (
406 + input: ProviderRepositoryRef & { readonly number: number },
407 + ) => Stream.Stream<void>;
408 +
409 readonly getChangeRequestStack?: (
410 input: ProviderRepositoryRef & { readonly includeDetails?: boolean; readonly number: number },
411 ) => Effect.Effect<ProviderChangeRequestStack | null, PullRequestProviderError>;
packages/source-control-github/src/server/GitHubPullRequestApi.ts
+3
@@ -47,6 +47,7 @@ import {
47
48 import * as GitHubAppAccess from "./GitHubAppAccess.ts";
49 import * as GitHubRest from "./GitHubRest.ts";
50 +import * as GitHubPullRequestEvents from "./gitHubPullRequestEvents.ts";
51 import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts";
52 import * as GitHubGraphQlClient from "./gitHubGraphQlClient.ts";
53 import { PullRequestBatchesGraphQLGroup } from "./pullRequestBatches.graphql.ts";
@@ -481,6 +482,7 @@ export class GitHubPullRequestApi extends Context.Service<
482 readonly host: string;
483 readonly number: number;
484 }) => Effect.Effect<GitHubPullRequestWatchFingerprint | null, GitHubPullRequestApiError>;
485 + readonly pullRequestEvents: Effect.Success<typeof GitHubPullRequestEvents.make>;
486
487 readonly revalidateChecks: Effect.Success<typeof makeChecksRevalidator>;
488
@@ -2019,6 +2021,7 @@ export const make = Effect.gen(function* () {
2021
2022 getPullRequestSummary,
2023 getPullRequestWatchFingerprint,
2024 + pullRequestEvents: yield* GitHubPullRequestEvents.make,
2025
2026 getPullRequestDetail,
2027 getPullRequestPreview: Effect.fn("GitHubPullRequestApi.getPullRequestPreview")(
packages/source-control-github/src/server/GitHubPullRequestProvider.ts
+2
@@ -453,6 +453,8 @@ export const make = Effect.gen(function* () {
453 .getPullRequestWatchFingerprint(input)
454 .pipe(Effect.mapError(fail("getChangeRequestWatchFingerprint"))),
455
456 + changeRequestEvents: (input) => cli.pullRequestEvents(input),
457 +
458 getChangeRequestPreview: (input) =>
459 cli.getPullRequestPreview(input).pipe(Effect.mapError(fail("getChangeRequestPreview"))),
460
packages/source-control-github/src/server/gitHubPullRequestEvents.test.ts new
+195
@@ -0,0 +1,195 @@
1 +import { assert, it } from "@effect/vitest";
2 +import * as Effect from "effect/Effect";
3 +import * as Fiber from "effect/Fiber";
4 +import * as Option from "effect/Option";
5 +import * as Redacted from "effect/Redacted";
6 +import * as Stream from "effect/Stream";
7 +import * as TestClock from "effect/testing/TestClock";
8 +import { HttpClient, HttpClientResponse } from "effect/http";
9 +
10 +import * as GitHubPullRequestEvents from "./gitHubPullRequestEvents.ts";
11 +import { GitHubApiNotFoundError, GitHubApiResponseError } from "./GitHubRequestPolicy.ts";
12 +import * as GitHubRest from "./GitHubRest.ts";
13 +
14 +const target = { host: "github.com", repository: "pingdotgg/t3code", number: 15 };
15 +
16 +const sse = (...frames: ReadonlyArray<readonly [string, string]>) =>
17 + frames.map(([event, data]) => `event: ${event}\ndata: ${data}\n\n`).join("");
18 +
19 +function fakeRest(
20 + subscribe: (
21 + count: number,
22 + ) => Effect.Effect<string, GitHubApiResponseError | GitHubApiNotFoundError>,
23 + signedIn = true,
24 +) {
25 + const posts: Array<unknown> = [];
26 + const deletes: Array<string> = [];
27 + const rest = GitHubRest.GitHubRest.of({
28 + rest: (input) =>
29 + input.method === "DELETE"
30 + ? Effect.sync(() => {
31 + deletes.push(input.path);
32 + return { status: 204, headers: {}, body: "", truncated: false, invalidUtf8: false };
33 + })
34 + : Effect.suspend(() => {
35 + posts.push(input.body);
36 + return subscribe(posts.length);
37 + }).pipe(
38 + Effect.map((body) => ({
39 + status: 201,
40 + headers: {},
41 + body,
42 + truncated: false,
43 + invalidUtf8: false,
44 + })),
45 + ),
46 + credential: (_host, options) =>
47 + Effect.succeed(
48 + options?.personal === true
49 + ? { token: Redacted.make("user-token"), fingerprint: "user", source: "gh" as const }
50 + : { token: Redacted.make("app-token"), fingerprint: "app", source: "app" as const },
51 + ),
52 + appCredential: () =>
53 + Effect.succeed(
54 + signedIn
55 + ? Option.some({
56 + token: Redacted.make("app-token"),
57 + fingerprint: "app",
58 + source: "app" as const,
59 + })
60 + : Option.none(),
61 + ),
62 + });
63 + return { rest, posts, deletes };
64 +}
65 +
66 +const eventsOf = (rest: GitHubRest.GitHubRest["Service"], httpClient: HttpClient.HttpClient) =>
67 + GitHubPullRequestEvents.make.pipe(
68 + Effect.provideService(GitHubRest.GitHubRest, rest),
69 + Effect.provideService(HttpClient.HttpClient, httpClient),
70 + );
71 +
72 +const subscription = (id: number) =>
73 + Effect.succeed(
74 + JSON.stringify({ id, events_url: `https://api.github.com/event-subscriptions/${id}/events` }),
75 + );
76 +
77 +it.effect("follows a pull request across a refused app token, reconnects, and renewals", () =>
78 + Effect.gen(function* () {
79 + const { rest, posts, deletes } = fakeRest(subscription);
80 + const connections: Array<string> = [];
81 + const answers = [
82 + // The app is not installed on this repository; the user's own token reads it.
83 + () => new Response(null, { status: 404 }),
84 + () =>
85 + new Response(
86 + sse(
87 + ["com.github.pull_request.head.changed.v1", "{}"],
88 + ["github.stream.end", '{"outcome":"reconnect","reason":"shutdown"}'],
89 + ),
90 + ),
91 + () =>
92 + new Response(
93 + sse(["github.stream.end", '{"outcome":"subscription_ended","reason":"expired"}']),
94 + ),
95 + () => new Response(": heartbeat\n\n"),
96 + ];
97 + const httpClient = HttpClient.make((request) =>
98 + Effect.sync(() => {
99 + connections.push(`${request.url} ${request.headers.authorization}`);
100 + return HttpClientResponse.fromWeb(request, answers[connections.length - 1]!());
101 + }),
102 + );
103 +
104 + const events = yield* eventsOf(rest, httpClient);
105 + const fiber = yield* events(target).pipe(Stream.take(4), Stream.runCount, Effect.forkChild);
106 + yield* TestClock.adjust("5 minutes");
107 + // An open per connection plus the head change.
108 + assert.equal(yield* Fiber.join(fiber), 4);
109 +
110 + assert.deepEqual(connections, [
111 + "https://api.github.com/event-subscriptions/1/events Bearer app-token",
112 + "https://api.github.com/event-subscriptions/1/events Bearer user-token",
113 + "https://api.github.com/event-subscriptions/1/events Bearer user-token",
114 + "https://api.github.com/event-subscriptions/2/events Bearer app-token",
115 + ]);
116 + assert.deepEqual(posts[0], {
117 + resource_url: "https://github.com/pingdotgg/t3code/pull/15",
118 + event_types: [
119 + "com.github.pull_request.review.submitted.v1",
120 + "com.github.pull_request.head.changed.v1",
121 + "com.github.pull_request.state.changed.v1",
122 + "com.github.pull_request.merge_conflict.v1",
123 + ],
124 + });
125 + assert.deepEqual(deletes, ["event-subscriptions/1", "event-subscriptions/2"]);
126 + }),
127 +);
128 +
129 +it.effect("gives up on a silent stream and deletes its subscription", () =>
130 + Effect.gen(function* () {
131 + const { rest, posts, deletes } = fakeRest(subscription);
132 + let connections = 0;
133 + // Answers, then never sends a byte, not even a keep-alive.
134 + const httpClient = HttpClient.make((request) =>
135 + Effect.sync(() => {
136 + connections += 1;
137 + return HttpClientResponse.fromWeb(request, new Response(new ReadableStream()));
138 + }),
139 + );
140 + const events = yield* eventsOf(rest, httpClient);
141 + const fiber = yield* events(target).pipe(Stream.runCount, Effect.forkChild);
142 + yield* TestClock.adjust("15 minutes");
143 +
144 + assert.equal(yield* Fiber.join(fiber), 7, "one catch-up read per connection");
145 + assert.equal(connections, 7);
146 + assert.equal(posts.length, 1);
147 + assert.deepEqual(deletes, ["event-subscriptions/1"]);
148 + }),
149 +);
150 +
151 +it.effect("leaves users without the app sign-in, other hosts, and refusals to polling", () =>
152 + Effect.gen(function* () {
153 + const httpClient = HttpClient.make(() => Effect.die("no stream without a subscription"));
154 + const signedOut = fakeRest(subscription, false);
155 + assert.equal(yield* Stream.runCount((yield* eventsOf(signedOut.rest, httpClient))(target)), 0);
156 + assert.equal(signedOut.posts.length, 0);
157 +
158 + const signedIn = fakeRest(subscription);
159 + const enterprise = { ...target, host: "ghe.example.com" };
160 + assert.equal(
161 + yield* Stream.runCount((yield* eventsOf(signedIn.rest, httpClient))(enterprise)),
162 + 0,
163 + );
164 + assert.equal(signedIn.posts.length, 0);
165 +
166 + const refused = fakeRest(() =>
167 + Effect.fail(
168 + new GitHubApiResponseError({
169 + host: "github.com",
170 + operation: "subscribePullRequestEvents",
171 + status: 403,
172 + }),
173 + ),
174 + );
175 + const events = yield* eventsOf(refused.rest, httpClient);
176 + assert.equal(yield* Stream.runCount(events(target)), 0);
177 + assert.equal(yield* Stream.runCount(events({ ...target, number: 16 })), 0);
178 + assert.equal(refused.posts.length, 1, "a refused account is not asked again for an hour");
179 + yield* TestClock.adjust("61 minutes");
180 + assert.equal(yield* Stream.runCount(events(target)), 0);
181 + assert.equal(refused.posts.length, 2);
182 +
183 + const missing = fakeRest(() =>
184 + Effect.fail(
185 + new GitHubApiNotFoundError({ host: "github.com", operation: "subscribePullRequestEvents" }),
186 + ),
187 + );
188 + const missingEvents = yield* eventsOf(missing.rest, httpClient);
189 + assert.equal(yield* Stream.runCount(missingEvents(target)), 0);
190 + assert.equal(yield* Stream.runCount(missingEvents(target)), 0);
191 + assert.equal(missing.posts.length, 1, "a pull request it cannot see is not asked again");
192 + assert.equal(yield* Stream.runCount(missingEvents({ ...target, number: 16 })), 0);
193 + assert.equal(missing.posts.length, 2, "other pull requests on the host still subscribe");
194 + }),
195 +);
packages/source-control-github/src/server/gitHubPullRequestEvents.ts new
+282
@@ -0,0 +1,282 @@
1 +import { normalizeHost } from "@t3tools/source-control-core/server/pullRequestUtils";
2 +import * as Clock from "effect/Clock";
3 +import * as Duration from "effect/Duration";
4 +import * as Effect from "effect/Effect";
5 +import * as Option from "effect/Option";
6 +import * as Random from "effect/Random";
7 +import * as Redacted from "effect/Redacted";
8 +import * as Schema from "effect/Schema";
9 +import * as Stream from "effect/Stream";
10 +import * as Sse from "effect/encoding/Sse";
11 +import { HttpClient, HttpClientRequest } from "effect/http";
12 +
13 +import * as GitHubRest from "./GitHubRest.ts";
14 +
15 +/** Events only say that something changed; readers fetch the pull request for what. */
16 +const EVENT_TYPES = [
17 + "com.github.pull_request.review.submitted.v1",
18 + "com.github.pull_request.head.changed.v1",
19 + "com.github.pull_request.state.changed.v1",
20 + "com.github.pull_request.merge_conflict.v1",
21 +];
22 +const API_ORIGIN = "https://api.github.com";
23 +/** GitHub sends a keep-alive at least every 15 seconds while a stream is idle. */
24 +const SILENCE_LIMIT = Duration.seconds(45);
25 +/** A connection open this long was healthy, however it ended. */
26 +const HEALTHY_AFTER = Duration.minutes(2);
27 +/** GitHub enables event streams per account, so a refusal holds for a while. */
28 +const REFUSED_PAUSE = Duration.hours(1);
29 +/** Another watch ending may free a slot under the per-account subscription limit. */
30 +const LIMIT_PAUSE = Duration.minutes(10);
31 +const MAX_FAILURES = 6;
32 +const MAX_BACKOFF_MS = 60_000;
33 +
34 +const Subscription = Schema.Struct({
35 + id: Schema.Union([Schema.String, Schema.Number]),
36 + events_url: Schema.String,
37 +});
38 +const decodeSubscription = Schema.decodeUnknownOption(Schema.fromJsonString(Subscription));
39 +
40 +const decodeStreamEnd = Schema.decodeUnknownOption(
41 + Schema.fromJsonString(Schema.Struct({ outcome: Schema.String })),
42 +);
43 +
44 +/**
45 + * How a connection ended. `renew` means the subscription expired or lost access, and `personal`
46 + * that the app's token was refused where the user's own may still read.
47 + */
48 +type Ending = "reconnect" | "renew" | "personal" | "stop";
49 +
50 +const endingOf = (data: string): Ending =>
51 + Option.match(decodeStreamEnd(data), {
52 + onNone: () => "reconnect",
53 + onSome: ({ outcome }) => (outcome === "subscription_ended" ? "renew" : "reconnect"),
54 + });
55 +
56 +export interface PullRequestEventsInput {
57 + readonly host: string;
58 + readonly repository: string;
59 + readonly number: number;
60 +}
61 +
62 +/**
63 + * Emits when a connection opens, so a reader catches up on what it missed, then once per change
64 + * GitHub reports. Ends quietly when GitHub refuses the subscription; the caller polls meanwhile.
65 + */
66 +export const make = Effect.gen(function* () {
67 + const rest = yield* GitHubRest.GitHubRest;
68 + const httpClient = yield* HttpClient.HttpClient;
69 + /** Keyed by host for account-wide refusals, and by pull request for one it cannot read. */
70 + const pausedUntil = new Map<string, number>();
71 +
72 + const subscribe = Effect.fn("GitHubPullRequestEvents.subscribe")(function* (
73 + input: PullRequestEventsInput,
74 + ) {
75 + const resourceUrl = `https://${input.host}/${input.repository}/pull/${input.number}`;
76 + const now = yield* Clock.currentTimeMillis;
77 + if (Math.max(pausedUntil.get(input.host) ?? 0, pausedUntil.get(resourceUrl) ?? 0) > now) {
78 + return Option.none();
79 + }
80 + const pause = (key: string, duration: Duration.Duration) =>
81 + Effect.sync(() => pausedUntil.set(key, now + Duration.toMillis(duration)));
82 + const response = yield* rest
83 + .rest({
84 + host: input.host,
85 + operation: "subscribePullRequestEvents",
86 + method: "POST",
87 + path: "event-subscriptions",
88 + body: { resource_url: resourceUrl, event_types: EVENT_TYPES },
89 + })
90 + .pipe(
91 + Effect.asSome,
92 + Effect.catchTags({
93 + GitHubApiNotFoundError: () =>
94 + pause(resourceUrl, REFUSED_PAUSE).pipe(Effect.as(Option.none())),
95 + GitHubApiResponseError: (error) =>
96 + (error.status === 403
97 + ? pause(input.host, REFUSED_PAUSE)
98 + : error.status === 422
99 + ? pause(input.host, LIMIT_PAUSE)
100 + : Effect.void
101 + ).pipe(Effect.as(Option.none())),
102 + }),
103 + Effect.catch((error) =>
104 + Effect.logDebug("pull request event subscription failed", { error }).pipe(
105 + Effect.as(Option.none()),
106 + ),
107 + ),
108 + );
109 + if (Option.isNone(response)) return Option.none();
110 + // The stream request carries the token, so its URL must stay on GitHub's API.
111 + return decodeSubscription(response.value.body).pipe(
112 + Option.filter(({ events_url }) => URL.parse(events_url)?.origin === API_ORIGIN),
113 + );
114 + });
115 +
116 + const unsubscribe = Effect.fn("GitHubPullRequestEvents.unsubscribe")(function* (
117 + host: string,
118 + id: string | number,
119 + ) {
120 + yield* rest
121 + .rest({
122 + host,
123 + operation: "unsubscribePullRequestEvents",
124 + method: "DELETE",
125 + path: `event-subscriptions/${encodeURIComponent(String(id))}`,
126 + })
127 + .pipe(Effect.ignore);
128 + });
129 +
130 + /** One SSE connection: "changed" when it opens and per event, and last, how it ended. */
131 + const connection = (
132 + host: string,
133 + eventsUrl: string,
134 + personal: boolean,
135 + ): Stream.Stream<"changed" | Ending> =>
136 + Stream.unwrap(
137 + Effect.gen(function* () {
138 + const held = yield* rest.credential(host, { personal });
139 + const response = yield* httpClient
140 + .execute(
141 + HttpClientRequest.get(eventsUrl).pipe(
142 + HttpClientRequest.setHeaders({
143 + accept: "text/event-stream",
144 + "x-github-api-version": "2022-11-28",
145 + "user-agent": "t3code",
146 + authorization: `Bearer ${Redacted.value(held.token)}`,
147 + }),
148 + ),
149 + )
150 + // Client spans would carry the subscription URL.
151 + .pipe(Effect.provideService(HttpClient.TracerDisabledWhen, () => true));
152 + if (response.status === 403 || response.status === 404) {
153 + return Stream.succeed<Ending>(held.source === "app" ? "personal" : "stop");
154 + }
155 + if (response.status !== 200) return Stream.succeed<Ending>("reconnect");
156 + return response.stream.pipe(
157 + Stream.timeoutOrElse({ duration: SILENCE_LIMIT, orElse: () => Stream.fail("silent") }),
158 + Stream.decodeText,
159 + Stream.pipeThroughChannel(Sse.decode()),
160 + Stream.map((frame): "changed" | Ending =>
161 + frame.event === "github.stream.end" ? endingOf(frame.data) : "changed",
162 + ),
163 + Stream.prepend(["changed" as const]),
164 + // A close without GitHub's end frame is reconnected like one with it.
165 + Stream.concat(Stream.succeed<Ending>("reconnect")),
166 + Stream.takeUntil((element) => element !== "changed"),
167 + );
168 + }),
169 + ).pipe(
170 + Stream.catchCause((cause) =>
171 + Stream.fromEffect(
172 + Effect.logDebug("pull request event stream failed", { cause }).pipe(
173 + Effect.as<Ending>("reconnect"),
174 + ),
175 + ),
176 + ),
177 + );
178 +
179 + const backoff = Effect.fn("GitHubPullRequestEvents.backoff")(function* (failures: number) {
180 + const jitter = yield* Random.next;
181 + yield* Effect.sleep(
182 + Duration.millis(Math.min(MAX_BACKOFF_MS, 1_000 * 2 ** failures) * (0.5 + jitter / 2)),
183 + );
184 + });
185 +
186 + return (input: PullRequestEventsInput): Stream.Stream<void> => {
187 + const target = { ...input, host: normalizeHost(input.host) };
188 + if (target.host !== "github.com") return Stream.empty;
189 +
190 + /** Follows one subscription; sets `session.renew` when it needs replacing. */
191 + const follow = (
192 + session: { renew: boolean },
193 + eventsUrl: string,
194 + personal: boolean,
195 + failuresSoFar: number,
196 + ): Stream.Stream<void> =>
197 + Stream.unwrap(
198 + Clock.currentTimeMillis.pipe(
199 + Effect.map((openedAt) => {
200 + let ending: Ending = "reconnect";
201 + let changes = 0;
202 + return connection(target.host, eventsUrl, personal).pipe(
203 + Stream.flatMap((element) => {
204 + if (element === "changed") {
205 + changes += 1;
206 + return Stream.succeed(undefined);
207 + }
208 + ending = element;
209 + return Stream.empty;
210 + }),
211 + Stream.concat(
212 + Stream.unwrap(
213 + Clock.currentTimeMillis.pipe(
214 + Effect.map((closedAt) => {
215 + // The first change is the opening; an event or a long life proves it worked.
216 + const healthy =
217 + changes > 1 || closedAt - openedAt >= Duration.toMillis(HEALTHY_AFTER);
218 + const failures = healthy ? 0 : failuresSoFar + 1;
219 + switch (ending) {
220 + case "stop":
221 + return Stream.empty;
222 + case "renew":
223 + session.renew = true;
224 + return Stream.empty;
225 + case "personal":
226 + return personal
227 + ? Stream.empty
228 + : follow(session, eventsUrl, true, failuresSoFar);
229 + case "reconnect":
230 + return failures > MAX_FAILURES
231 + ? Stream.empty
232 + : Stream.fromEffectDrain(backoff(failures)).pipe(
233 + Stream.concat(follow(session, eventsUrl, personal, failures)),
234 + );
235 + }
236 + }),
237 + ),
238 + ),
239 + ),
240 + );
241 + }),
242 + ),
243 + );
244 +
245 + /** One subscription, deleted once its stream has closed. */
246 + const subscription = (session: { renew: boolean }): Stream.Stream<void> =>
247 + Stream.unwrap(
248 + Effect.acquireRelease(subscribe(target), (held) =>
249 + Option.isSome(held) ? unsubscribe(target.host, held.value.id) : Effect.void,
250 + ).pipe(
251 + Effect.map(
252 + Option.match({
253 + onNone: () => Stream.empty,
254 + onSome: ({ events_url }) => follow(session, events_url, false, 0),
255 + }),
256 + ),
257 + ),
258 + );
259 +
260 + const subscribed = (): Stream.Stream<void> =>
261 + Stream.suspend(() => {
262 + const session = { renew: false };
263 + return subscription(session).pipe(
264 + Stream.concat(
265 + Stream.suspend(() =>
266 + session.renew
267 + ? Stream.fromEffectDrain(backoff(0)).pipe(Stream.concat(subscribed()))
268 + : Stream.empty,
269 + ),
270 + ),
271 + );
272 + });
273 +
274 + // Event streams are a GitHub preview, so only the app sign-in, itself opt-in, uses them.
275 + return Stream.unwrap(
276 + rest.appCredential(target.host).pipe(
277 + Effect.map((app) => (Option.isSome(app) ? subscribed() : Stream.empty)),
278 + Effect.orElseSucceed(() => Stream.empty),
279 + ),
280 + );
281 + };
282 +});