Skip to content

Commit dd2be5c

Browse files
fix(server): a merged PR whose read fails still wakes and ends its watch
The watch now holds settling, so a merged PR must not wait out a host that keeps failing. When the detail read fails and the stored snapshot says merged, the wake comes from the snapshot, without the merger or merge commit. The reactor's tests now give each event subscriber every event, as each event sink stream does: the merge commands and main's watch-start reads subscribe separately, and a shared queue split events between them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8c33ab1 commit dd2be5c

2 files changed

Lines changed: 133 additions & 26 deletions

File tree

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -545,18 +545,26 @@ export const make = Effect.gen(function* () {
545545
);
546546
});
547547

548-
const finished = (groupKey: string, target: WatchTarget, detail: PullRequestDetail) =>
549-
hold({
550-
groupKey,
551-
target,
552-
next: null,
553-
wake: pullRequestWatchEndMessage({
554-
number: target.link.number,
555-
url: target.link.url,
556-
detail,
557-
}),
558-
reason: detail.state === "merged" ? "merged" : "closed",
559-
});
548+
const finished = (
549+
groupKey: string,
550+
target: WatchTarget,
551+
detail: Pick<PullRequestDetail, "state" | "baseBranch" | "mergeCommitSha" | "mergedBy">,
552+
) => {
553+
const reason = detail.state === "merged" ? "merged" : "closed";
554+
return endedByAgent(target)
555+
? record(target, null).pipe(Effect.tap(() => ended(target, reason)))
556+
: hold({
557+
groupKey,
558+
target,
559+
next: null,
560+
wake: pullRequestWatchEndMessage({
561+
number: target.link.number,
562+
url: target.link.url,
563+
detail,
564+
}),
565+
reason,
566+
});
567+
};
560568

561569
// A merged snapshot still gets one read, so the wake can name the merge commit.
562570
const endsWithoutRead = ({ thread }: WatchTarget): WatchEndReason | undefined =>
@@ -637,6 +645,13 @@ export const make = Effect.gen(function* () {
637645
if (Cause.hasInterruptsOnly(detailRead.cause))
638646
return yield* Effect.failCause(detailRead.cause);
639647
lastReads.delete(group.key);
648+
// A merged PR's watch holds its thread from settling, so it must not wait out the host.
649+
if (group.targets.every((target) => target.link.snapshot?.state === "merged")) {
650+
readFailures.delete(group.key);
651+
return yield* eachTarget(group, (target) =>
652+
target.link.snapshot ? finished(group.key, target, target.link.snapshot) : Effect.void,
653+
);
654+
}
640655
const reason = providerFailureReason(detailRead.cause);
641656
if (reason === "rate-limited") return;
642657
// Transport and host failures can outlast the failure limit; keep the watch for recovery.
@@ -668,12 +683,7 @@ export const make = Effect.gen(function* () {
668683
}
669684
if (detail.state !== "open") {
670685
lastReads.delete(group.key);
671-
const reason = detail.state === "merged" ? "merged" : "closed";
672-
return yield* eachTarget(group, (target) =>
673-
endedByAgent(target)
674-
? record(target, null).pipe(Effect.tap(() => ended(target, reason)))
675-
: finished(group.key, target, detail),
676-
);
686+
return yield* eachTarget(group, (target) => finished(group.key, target, detail));
677687
}
678688

679689
// A failed comments read leaves remarks incomplete, so the next pass reads them again;

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

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import * as FileSystem from "effect/FileSystem";
4848
import * as Layer from "effect/Layer";
4949
import * as Option from "effect/Option";
5050
import * as Path from "effect/Path";
51+
import * as PubSub from "effect/PubSub";
5152
import * as Queue from "effect/Queue";
5253
import * as Ref from "effect/Ref";
5354
import * as Stream from "effect/Stream";
@@ -4744,7 +4745,8 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
47444745
]);
47454746
const detailReads = yield* Queue.unbounded<number>();
47464747
const agentRead = yield* Deferred.make<void>();
4747-
const agentEvents = yield* Queue.unbounded<OrchestrationV2DomainEvent>();
4748+
// Each of the reactor's subscribers reads every event, as each event sink stream does.
4749+
const agentEvents = yield* PubSub.unbounded<OrchestrationV2DomainEvent>({ replay: 16 });
47484750
const host = Layer.mergeAll(
47494751
NodeServices.layer,
47504752
NodeCrypto.layer,
@@ -4778,7 +4780,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
47784780
}),
47794781
Layer.succeed(Orchestrator.OrchestratorV2, {
47804782
...orchestrator,
4781-
streamDomainEvents: Stream.fromQueue(agentEvents),
4783+
streamDomainEvents: Stream.fromPubSub(agentEvents),
47824784
}),
47834785
// Only the read at a command's end asks for one thread's watches.
47844786
Layer.effect(
@@ -4832,11 +4834,11 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
48324834
yield* Queue.take(detailReads);
48334835

48344836
// While the agent merges #19, someone else closes #20.
4835-
yield* Queue.offer(agentEvents, yield* command("running"));
4837+
yield* PubSub.publish(agentEvents, yield* command("running"));
48364838
states.set(19, "merged");
48374839
states.set(20, "closed");
48384840
// The command's end reads the thread's watches in full, after that sweep.
4839-
yield* Queue.offer(agentEvents, yield* command(ended));
4841+
yield* PubSub.publish(agentEvents, yield* command(ended));
48404842
yield* Deferred.await(agentRead);
48414843
yield* TestClock.adjust("1 second");
48424844
yield* Queue.take(detailReads);
@@ -4966,6 +4968,100 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
49664968
}),
49674969
);
49684970

4971+
it.effect("wakes from a merged snapshot when the host cannot be read", () =>
4972+
Effect.gen(function* () {
4973+
const orchestrator = yield* Orchestrator.OrchestratorV2;
4974+
const threadId = ThreadId.make("runtime-pull-request-watch-merged-unreadable");
4975+
const projectId = ProjectId.make("pr-watch-merged-unreadable-project");
4976+
yield* seedProject({
4977+
projectId,
4978+
title: "Watch merged unreadable",
4979+
workspaceRoot: "/workspace/watch-merged-unreadable",
4980+
defaultModelSelection: null,
4981+
createdAt: "2026-10-01T00:00:00.000Z",
4982+
});
4983+
yield* orchestrator.dispatch({
4984+
type: "thread.create",
4985+
createdBy: "user",
4986+
creationSource: "web",
4987+
commandId: CommandId.make("pr-watch-merged-unreadable-create"),
4988+
threadId,
4989+
projectId,
4990+
title: "Watch merged unreadable",
4991+
modelSelection,
4992+
runtimeMode: "full-access",
4993+
interactionMode: "default",
4994+
branch: null,
4995+
worktreePath: null,
4996+
});
4997+
const key = { host: "github.com", repository: "pingdotgg/t3code", number: 8 };
4998+
yield* orchestrator.dispatch({
4999+
type: "thread.pull-request.watch",
5000+
commandId: CommandId.make("pr-watch-merged-unreadable-start"),
5001+
threadId,
5002+
...key,
5003+
watching: true,
5004+
link: { url: "https://github.com/pingdotgg/t3code/pull/8", source: "agent" },
5005+
});
5006+
yield* orchestrator.dispatch({
5007+
type: "thread.pull-request-link.sync",
5008+
commandId: CommandId.make("pr-watch-merged-unreadable-synced"),
5009+
threadId,
5010+
...key,
5011+
snapshot: {
5012+
state: "merged",
5013+
title: "Watched pull request",
5014+
headBranch: "feature",
5015+
baseBranch: "main",
5016+
isDraft: false,
5017+
updatedAt: null,
5018+
syncedAt: "2026-10-02T12:30:00.000Z",
5019+
mergedAt: "2026-10-02T12:30:00.000Z",
5020+
},
5021+
stack: null,
5022+
});
5023+
// A transport failure, which an open pull request's watch outlasts.
5024+
const unreachable = new PullRequestOperationError({
5025+
operation: "detail",
5026+
detail: "host unreachable",
5027+
cause: new PullRequestProviderError({
5028+
provider: SourceControlProviderKind.make("github"),
5029+
operation: "detail",
5030+
reason: "failed",
5031+
detail: "host unreachable",
5032+
}),
5033+
});
5034+
const reactor = yield* PullRequestWatchReactor.make.pipe(
5035+
Effect.provide(
5036+
Layer.mergeAll(
5037+
NodeServices.layer,
5038+
NodeCrypto.layer,
5039+
Layer.mock(PullRequestService.PullRequestService)({
5040+
watchFingerprint: () => Effect.succeed(null),
5041+
invalidate: () => Effect.void,
5042+
detail: () => Effect.fail(unreachable),
5043+
activity: () => Effect.fail(unreachable),
5044+
}),
5045+
),
5046+
),
5047+
);
5048+
yield* reactor.sweep;
5049+
yield* TestClock.adjust("10 minutes");
5050+
yield* reactor.sweep;
5051+
5052+
const thread = yield* orchestrator.getThreadShell(threadId);
5053+
assert.isUndefined(thread?.pullRequests?.[0]?.watch);
5054+
const wakes = (yield* orchestrator.getThreadRecords(threadId, ["messages"])).messages.filter(
5055+
(message) => message.notification != null,
5056+
);
5057+
assert.deepEqual(
5058+
wakes.map((message) => message.notification?.summary),
5059+
["#8: merged, stopped watching"],
5060+
);
5061+
assert.include(wakes[0]!.text, "merged into main, so");
5062+
}),
5063+
);
5064+
49695065
it.effect("ends a watch on a closed pull request whose activity cannot be read", () =>
49705066
Effect.gen(function* () {
49715067
const orchestrator = yield* Orchestrator.OrchestratorV2;
@@ -5943,7 +6039,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
59436039
];
59446040
const detailReads = yield* Queue.unbounded<void>();
59456041
const watchListings = yield* Queue.unbounded<void>();
5946-
const domainEvents = yield* Queue.unbounded<OrchestrationV2DomainEvent>();
6042+
const domainEvents = yield* PubSub.unbounded<OrchestrationV2DomainEvent>({ replay: 16 });
59476043
const host = Layer.mergeAll(
59486044
NodeServices.layer,
59496045
NodeCrypto.layer,
@@ -5975,10 +6071,11 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
59756071
invalidate: () => Effect.void,
59766072
changes: () => Stream.never,
59776073
}),
5978-
// Events come from a queue that exists before the reactor subscribes, so none is missed.
6074+
// Each subscriber reads every event, as each event sink stream does, and replay means one
6075+
// that subscribes late misses none.
59796076
Layer.mock(Orchestrator.OrchestratorV2)({
59806077
dispatch: orchestrator.dispatch,
5981-
streamDomainEvents: Stream.fromQueue(domainEvents),
6078+
streamDomainEvents: Stream.fromPubSub(domainEvents),
59826079
}),
59836080
Layer.mock(ProjectionStore.ProjectionStoreV2)({
59846081
getThreadsWithPullRequests: (id) =>
@@ -6009,7 +6106,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
60096106
watching: true,
60106107
link: { url: "https://github.com/pingdotgg/t3code/pull/16", source: "agent" },
60116108
});
6012-
yield* Queue.offerAll(
6109+
yield* PubSub.publishAll(
60136110
domainEvents,
60146111
started.storedEvents.map((stored) => stored.event),
60156112
);

0 commit comments

Comments
 (0)