Skip to content

Commit 7e50071

Browse files
fix(server): event reads see the latest watch, and refused or flaky streams back off
- A host event runs a sweep pass over that pull request, serialized with the sweep, so it compares against the watch the last read recorded and does not wake the agent twice for one change. - Clients refresh on an event only when the host fingerprint moved. - A refused subscription waits before asking again (403 and the limit per host, 404 per pull request). - Reconnects back off unless the connection carried an event or stayed up; stream failures are logged; the subscription is created and deleted under acquireRelease. - The events module yields its services. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c67a005 commit 7e50071

5 files changed

Lines changed: 276 additions & 155 deletions

File tree

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

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,10 @@ export const make = Effect.gen(function* () {
252252
Effect.suspend(() => reportEnd(lifeKey(target), lifeOf(target), reason));
253253

254254
const readFailures = new Map<string, number>();
255-
/** The last sweep's groups, which host change events read from between sweeps. */
255+
/** The last sweep's groups, whose pull requests get host change feeds. */
256256
let currentGroups: ReadonlyArray<WatchGroup> = [];
257-
// A change event can land while the sweep reads the same group.
258-
const groupLocks = new Map<string, Semaphore.Semaphore>();
259-
const locked = <A, E>(key: string, effect: Effect.Effect<A, E>) =>
260-
Effect.suspend(() => {
261-
let lock = groupLocks.get(key);
262-
if (lock === undefined) {
263-
lock = Semaphore.makeUnsafe(1);
264-
groupLocks.set(key, lock);
265-
}
266-
return lock.withPermit(effect);
267-
});
257+
// A pass reads watches and records new ones, so passes must not overlap.
258+
const passes = Semaphore.makeUnsafe(1);
268259
const lastReads = new Map<string, LastRead>();
269260
const threadTails = new Map<
270261
string,
@@ -596,19 +587,26 @@ export const make = Effect.gen(function* () {
596587
});
597588
});
598589

599-
const sweep = Effect.gen(function* () {
590+
/** Every watch, or with `changed`, only that pull request's after its host reported a change. */
591+
// The sweep and change reads each name their own span.
592+
const pass = Effect.fnUntraced(function* (changed: string | undefined) {
600593
const threads = yield* projections.getThreadsWithPullRequests();
601594
const targets = threads.flatMap((thread) =>
602595
visibleThreadPullRequests(thread.pullRequests ?? []).flatMap((link) =>
603-
link.watch === undefined ? [] : [{ thread, link, watch: link.watch }],
596+
link.watch === undefined ||
597+
(changed !== undefined && threadPullRequestKeyOf(link) !== changed)
598+
? []
599+
: [{ thread, link, watch: link.watch }],
604600
),
605601
);
606-
const present = new Set(targets.map(lifeKey));
607-
yield* Effect.forEach(
608-
[...lives].filter(([key]) => !present.has(key)),
609-
([key, life]) => reportEnd(key, life, "stopped"),
610-
{ discard: true },
611-
);
602+
if (changed === undefined) {
603+
const present = new Set(targets.map(lifeKey));
604+
yield* Effect.forEach(
605+
[...lives].filter(([key]) => !present.has(key)),
606+
([key, life]) => reportEnd(key, life, "stopped"),
607+
{ discard: true },
608+
);
609+
}
612610
for (const target of targets) lifeOf(target);
613611
const byPullRequest = new Map<string, Array<WatchTarget>>();
614612
const ending: Array<readonly [WatchTarget, WatchEndReason]> = [];
@@ -627,9 +625,11 @@ export const make = Effect.gen(function* () {
627625
targets: members,
628626
fingerprint: [...new Set(members.map(snapshotFingerprint))].toSorted().join("\n"),
629627
}));
630-
currentGroups = groups;
631-
for (const cache of [readFailures, lastReads, threadTails, groupLocks]) {
632-
for (const key of cache.keys()) if (!byPullRequest.has(key)) cache.delete(key);
628+
if (changed === undefined) {
629+
currentGroups = groups;
630+
for (const cache of [readFailures, lastReads, threadTails]) {
631+
for (const key of cache.keys()) if (!byPullRequest.has(key)) cache.delete(key);
632+
}
633633
}
634634
yield* Effect.forEach(
635635
ending,
@@ -651,12 +651,29 @@ export const make = Effect.gen(function* () {
651651
yield* Effect.forEach(
652652
groups,
653653
(group) =>
654-
fingerprintOf(group).pipe(
654+
// The host just reported a change, so a cached fingerprint may predate it.
655+
(changed === undefined
656+
? Effect.void
657+
: pullRequests.invalidate({ reference: referenceOf(group) })
658+
).pipe(
659+
Effect.andThen(fingerprintOf(group)),
660+
Effect.tap((fingerprint) => {
661+
const last = lastReads.get(group.key);
662+
// Open pull request views refresh only when the host state moved.
663+
return changed !== undefined &&
664+
fingerprint !== null &&
665+
fingerprint !== "paused" &&
666+
(last === undefined ||
667+
last.status !== fingerprint.status ||
668+
last.remarks !== fingerprint.remarks)
669+
? pullRequests.invalidate({ reference: referenceOf(group) }, { notifyReaders: true })
670+
: Effect.void;
671+
}),
655672
Effect.flatMap((fingerprint) =>
656673
fingerprint === "paused"
657674
? Effect.void
658675
: reads.withPermit(
659-
locked(group.key, readGroup(group, fingerprint)).pipe(
676+
readGroup(group, fingerprint).pipe(
660677
Effect.catchCause(
661678
logFailure("pull request watch check failed", { pullRequest: group.key }),
662679
),
@@ -666,35 +683,20 @@ export const make = Effect.gen(function* () {
666683
),
667684
{ concurrency: "unbounded", discard: true },
668685
);
669-
}).pipe(
686+
}, passes.withPermit);
687+
688+
const sweep = pass(undefined).pipe(
670689
Effect.catchCause(logFailure("pull request watch sweep failed", {})),
671690
Effect.withSpan("PullRequestWatchReactor.sweep"),
672691
);
673692

674693
const pullRequestOf = (group: WatchGroup) => threadPullRequestKeyOf(group.targets[0]!.link);
675694

676-
/** A host event can come from any change, so reread now rather than at the next sweep. */
677-
const readOnChange = Effect.fn("PullRequestWatchReactor.readOnChange")(function* (
678-
pullRequest: string,
679-
) {
680-
const groups = currentGroups.filter((group) => pullRequestOf(group) === pullRequest);
681-
yield* Effect.forEach(
682-
groups,
683-
(group) =>
684-
pullRequests.invalidate({ reference: referenceOf(group) }, { notifyReaders: true }).pipe(
685-
Effect.andThen(fingerprintOf(group)),
686-
Effect.flatMap((fingerprint) =>
687-
fingerprint === "paused"
688-
? Effect.void
689-
: locked(group.key, readGroup(group, fingerprint)).pipe(Effect.asVoid),
690-
),
691-
Effect.catchCause(
692-
logFailure("pull request watch change read failed", { pullRequest: group.key }),
693-
),
694-
),
695-
{ discard: true },
695+
const readOnChange = (pullRequest: string) =>
696+
pass(pullRequest).pipe(
697+
Effect.catchCause(logFailure("pull request watch change read failed", { pullRequest })),
698+
Effect.withSpan("PullRequestWatchReactor.readOnChange"),
696699
);
697-
});
698700

699701
const start: PullRequestWatchReactor["Service"]["start"] = Effect.fn(
700702
"PullRequestWatchReactor.start",

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

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3855,7 +3855,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
38553855
);
38563856

38573857
it.effect(
3858-
"reads a watched pull request when the host reports a change, not at the next sweep",
3858+
"reads a watched pull request when the host reports a change, and wakes once per change",
38593859
() =>
38603860
Effect.scoped(
38613861
Effect.gen(function* () {
@@ -3893,26 +3893,29 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
38933893
link: { url: "https://github.com/pingdotgg/t3code/pull/15", source: "agent" },
38943894
});
38953895

3896-
let headSha = "aaaaaaaaaa";
3896+
let checks: PullRequestDetail["checks"] = [
3897+
{ name: "lint", status: "pending", description: null, url: null, required: true },
3898+
];
3899+
let fingerprints = 0;
38973900
const events = yield* Queue.unbounded<void>();
38983901
const following = yield* Deferred.make<void>();
3899-
const readPush = yield* Deferred.make<void>();
3902+
const detailReads = yield* Queue.unbounded<void>();
39003903
const host = Layer.mergeAll(
39013904
NodeServices.layer,
39023905
NodeCrypto.layer,
39033906
Layer.mock(PullRequestService.PullRequestService)({
39043907
detail: () =>
3905-
Effect.sync(() => {
3906-
if (headSha !== "aaaaaaaaaa") Deferred.doneUnsafe(readPush, Effect.void);
3907-
return {
3908+
Queue.offer(detailReads, undefined).pipe(
3909+
Effect.as({
39083910
...watchedPullRequestDetail({
39093911
projectId,
39103912
number: 15,
39113913
at: "2026-10-02T12:00:00.000Z",
39123914
}),
3913-
headSha,
3914-
};
3915-
}),
3915+
headSha: "aaaaaaaaaa",
3916+
checks,
3917+
}),
3918+
),
39163919
activity: () =>
39173920
Effect.succeed({
39183921
comments: [],
@@ -3921,23 +3924,45 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
39213924
reviewThreads: [],
39223925
commits: [],
39233926
}),
3924-
watchFingerprint: () => Effect.sync(() => ({ status: headSha, remarks: "" })),
3927+
// A fresh fingerprint every time, so every pass reads the detail.
3928+
watchFingerprint: () =>
3929+
Effect.sync(() => ({ status: String(++fingerprints), remarks: "" })),
39253930
invalidate: () => Effect.void,
39263931
changes: () =>
39273932
Stream.fromEffectDrain(Deferred.succeed(following, undefined)).pipe(
39283933
Stream.concat(Stream.fromQueue(events)),
39293934
),
39303935
}),
39313936
);
3937+
const wakes = Effect.map(
3938+
orchestrator.getThreadRecords(threadId, ["messages"]),
3939+
({ messages }) =>
3940+
messages.filter(
3941+
(message) => message.notification !== undefined && message.notification !== null,
3942+
),
3943+
);
39323944
const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
3945+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
39333946
yield* reactor.start();
39343947
// The feed starts after the first sweep has read the pull request.
39353948
yield* Deferred.await(following);
3949+
yield* Queue.take(detailReads);
39363950

3937-
headSha = "bbbbbbbbbb";
3951+
checks = [
3952+
{ name: "lint", status: "failure", description: null, url: null, required: true },
3953+
];
39383954
yield* Queue.offer(events, undefined);
3939-
// The clock never moves, so no sweep can be the one that reads the push.
3940-
yield* Deferred.await(readPush);
3955+
// The clock never moves, so no sweep can be the one that reads the failure.
3956+
yield* Queue.take(detailReads);
3957+
// Nothing new: this read must compare against the watch the last one recorded.
3958+
yield* Queue.offer(events, undefined);
3959+
yield* Queue.take(detailReads);
3960+
// A sweep waits for the change read in flight.
3961+
yield* reactor.sweep;
3962+
yield* worker.drain();
3963+
const woken = yield* wakes;
3964+
assert.lengthOf(woken, 1);
3965+
assert.include(woken[0]!.notification?.summary ?? "", "failed");
39413966

39423967
yield* orchestrator.dispatch({
39433968
type: "thread.pull-request.watch",

‎packages/source-control-github/src/server/GitHubPullRequestApi.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ export class GitHubPullRequestApi extends Context.Service<
482482
readonly host: string;
483483
readonly number: number;
484484
}) => Effect.Effect<GitHubPullRequestWatchFingerprint | null, GitHubPullRequestApiError>;
485-
readonly pullRequestEvents: ReturnType<typeof GitHubPullRequestEvents.make>;
485+
readonly pullRequestEvents: Effect.Success<typeof GitHubPullRequestEvents.make>;
486486

487487
readonly revalidateChecks: Effect.Success<typeof makeChecksRevalidator>;
488488

@@ -2021,7 +2021,7 @@ export const make = Effect.gen(function* () {
20212021

20222022
getPullRequestSummary,
20232023
getPullRequestWatchFingerprint,
2024-
pullRequestEvents: GitHubPullRequestEvents.make(api, httpClient),
2024+
pullRequestEvents: yield* GitHubPullRequestEvents.make,
20252025

20262026
getPullRequestDetail,
20272027
getPullRequestPreview: Effect.fn("GitHubPullRequestApi.getPullRequestPreview")(

‎packages/source-control-github/src/server/gitHubPullRequestEvents.test.ts‎

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ import * as TestClock from "effect/testing/TestClock";
88
import { HttpClient, HttpClientResponse } from "effect/http";
99

1010
import * as GitHubPullRequestEvents from "./gitHubPullRequestEvents.ts";
11+
import { GitHubApiResponseError } from "./GitHubRequestPolicy.ts";
1112
import * as GitHubRest from "./GitHubRest.ts";
1213

1314
const target = { host: "github.com", repository: "pingdotgg/t3code", number: 15 };
1415

1516
const sse = (...frames: ReadonlyArray<readonly [string, string]>) =>
1617
frames.map(([event, data]) => `event: ${event}\ndata: ${data}\n\n`).join("");
1718

18-
function fakeRest(subscribe: (count: number) => Effect.Effect<string>, signedIn = true) {
19+
function fakeRest(
20+
subscribe: (count: number) => Effect.Effect<string, GitHubApiResponseError>,
21+
signedIn = true,
22+
) {
1923
const posts: Array<unknown> = [];
2024
const deletes: Array<string> = [];
2125
const rest = GitHubRest.GitHubRest.of({
@@ -57,6 +61,12 @@ function fakeRest(subscribe: (count: number) => Effect.Effect<string>, signedIn
5761
return { rest, posts, deletes };
5862
}
5963

64+
const eventsOf = (rest: GitHubRest.GitHubRest["Service"], httpClient: HttpClient.HttpClient) =>
65+
GitHubPullRequestEvents.make.pipe(
66+
Effect.provideService(GitHubRest.GitHubRest, rest),
67+
Effect.provideService(HttpClient.HttpClient, httpClient),
68+
);
69+
6070
const subscription = (id: number) =>
6171
Effect.succeed(
6272
JSON.stringify({ id, events_url: `https://api.github.com/event-subscriptions/${id}/events` }),
@@ -89,7 +99,7 @@ it.effect("follows a pull request across a refused app token, reconnects, and re
8999
}),
90100
);
91101

92-
const events = GitHubPullRequestEvents.make(rest, httpClient);
102+
const events = yield* eventsOf(rest, httpClient);
93103
const fiber = yield* events(target).pipe(Stream.take(4), Stream.runCount, Effect.forkChild);
94104
yield* TestClock.adjust("5 minutes");
95105
// An open per connection plus the head change.
@@ -114,21 +124,58 @@ it.effect("follows a pull request across a refused app token, reconnects, and re
114124
}),
115125
);
116126

117-
it.effect("leaves users without the app sign-in, and other hosts, to polling", () =>
127+
it.effect("gives up on a silent stream and deletes its subscription", () =>
128+
Effect.gen(function* () {
129+
const { rest, posts, deletes } = fakeRest(subscription);
130+
let connections = 0;
131+
// Answers, then never sends a byte, not even a keep-alive.
132+
const httpClient = HttpClient.make((request) =>
133+
Effect.sync(() => {
134+
connections += 1;
135+
return HttpClientResponse.fromWeb(request, new Response(new ReadableStream()));
136+
}),
137+
);
138+
const events = yield* eventsOf(rest, httpClient);
139+
const fiber = yield* events(target).pipe(Stream.runCount, Effect.forkChild);
140+
yield* TestClock.adjust("15 minutes");
141+
142+
assert.equal(yield* Fiber.join(fiber), 7, "one catch-up read per connection");
143+
assert.equal(connections, 7);
144+
assert.equal(posts.length, 1);
145+
assert.deepEqual(deletes, ["event-subscriptions/1"]);
146+
}),
147+
);
148+
149+
it.effect("leaves users without the app sign-in, other hosts, and refusals to polling", () =>
118150
Effect.gen(function* () {
119151
const httpClient = HttpClient.make(() => Effect.die("no stream without a subscription"));
120152
const signedOut = fakeRest(subscription, false);
121-
assert.equal(
122-
yield* Stream.runCount(GitHubPullRequestEvents.make(signedOut.rest, httpClient)(target)),
123-
0,
124-
);
153+
assert.equal(yield* Stream.runCount((yield* eventsOf(signedOut.rest, httpClient))(target)), 0);
154+
assert.equal(signedOut.posts.length, 0);
125155

126156
const signedIn = fakeRest(subscription);
127157
const enterprise = { ...target, host: "ghe.example.com" };
128158
assert.equal(
129-
yield* Stream.runCount(GitHubPullRequestEvents.make(signedIn.rest, httpClient)(enterprise)),
159+
yield* Stream.runCount((yield* eventsOf(signedIn.rest, httpClient))(enterprise)),
130160
0,
131161
);
132-
assert.equal(signedOut.posts.length + signedIn.posts.length, 0);
162+
assert.equal(signedIn.posts.length, 0);
163+
164+
const refused = fakeRest(() =>
165+
Effect.fail(
166+
new GitHubApiResponseError({
167+
host: "github.com",
168+
operation: "subscribePullRequestEvents",
169+
status: 403,
170+
}),
171+
),
172+
);
173+
const events = yield* eventsOf(refused.rest, httpClient);
174+
assert.equal(yield* Stream.runCount(events(target)), 0);
175+
assert.equal(yield* Stream.runCount(events({ ...target, number: 16 })), 0);
176+
assert.equal(refused.posts.length, 1, "a refused account is not asked again for an hour");
177+
yield* TestClock.adjust("61 minutes");
178+
assert.equal(yield* Stream.runCount(events(target)), 0);
179+
assert.equal(refused.posts.length, 2);
133180
}),
134181
);

0 commit comments

Comments
 (0)