@@ -48,6 +48,7 @@ import * as FileSystem from "effect/FileSystem";
4848import * as Layer from "effect/Layer";
4949import * as Option from "effect/Option";
5050import * as Path from "effect/Path";
51+ import * as PubSub from "effect/PubSub";
5152import * as Queue from "effect/Queue";
5253import * as Ref from "effect/Ref";
5354import * 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