The best way to control Claude Code, Codex and more from any computer or phone
fix(server): pause periodic PR discovery without foreground clients (#23852)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 23, 2026 at 11:40 UTC
5bb54a0f77c6655fdb2ab551612ca1ca1fcd3667
2 files changed
+109
apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts
+104
@@ -4,6 +4,7 @@ import {
4
SourceControlProviderKind,
5
ThreadId,
6
type OrchestrationProjectShell,
7
+ type OrchestrationV2DomainEvent,
8
type OrchestrationV2ThreadShell,
9
type OrchestrationV2ServerCommand,
10
} from "@t3tools/contracts";
@@ -15,8 +16,11 @@ import * as Effect from "effect/Effect";
16
import * as FileSystem from "effect/FileSystem";
17
import * as Layer from "effect/Layer";
18
import * as Option from "effect/Option";
19
+import * as Queue from "effect/Queue";
20
import * as Stream from "effect/Stream";
21
+import * as TestClock from "effect/testing/TestClock";
22
23
+import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
24
import * as GitManager from "../git/GitManager.ts";
25
import * as ProjectStore from "./ProjectStore.ts";
26
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
@@ -78,6 +82,105 @@ describe("ThreadPullRequestServiceV2 reads", () => {
82
};
83
};
84
85
+ it.effect("does not sweep branch pull requests while no client is connected", () =>
86
+ Effect.scoped(
87
+ Effect.gen(function* () {
88
+ // Reproduces #23702: an idle server with one active branch thread kept
89
+ // calling GitHub for that branch every minute with no client connected.
90
+ const thread = { ...threadShell("idle-branch-thread"), branch: "feature/idle" };
91
+ const project: OrchestrationProjectShell = {
92
+ id: thread.projectId,
93
+ title: "Project",
94
+ workspaceRoot: "/workspace/project",
95
+ defaultModelSelection: null,
96
+ scripts: [],
97
+ createdAt: "2026-08-01T00:00:00.000Z",
98
+ updatedAt: "2026-08-01T00:00:00.000Z",
99
+ };
100
+ const activation = yield* Deferred.make<void>();
101
+ const domainEvents = yield* Queue.unbounded<OrchestrationV2DomainEvent>();
102
+ const turnLookup = yield* Deferred.make<void>();
103
+ const lookups: Array<{ readonly refresh: boolean }> = [];
104
+ const dependencies = Layer.mergeAll(
105
+ Layer.mock(Orchestrator.OrchestratorV2)({
106
+ streamDomainEvents: Stream.fromQueue(domainEvents),
107
+ getShellSnapshot: () =>
108
+ Effect.succeed({
109
+ schemaVersion: 2,
110
+ snapshotSequence: 1,
111
+ threads: [thread],
112
+ archivedThreads: [],
113
+ }),
114
+ getThreadEventSequence: () => Effect.succeed(1),
115
+ getThreadShell: () => Effect.succeed(thread),
116
+ }),
117
+ Layer.mock(ProjectStore.ProjectStoreV2)({
118
+ listShells: () => Effect.succeed([project]),
119
+ getShell: () => Effect.succeed(Option.some(project)),
120
+ }),
121
+ Layer.mock(GitManager.GitManager)({
122
+ branchPullRequest: (_input, options) =>
123
+ Effect.gen(function* () {
124
+ const refresh = options?.refresh ?? false;
125
+ lookups.push({ refresh });
126
+ if (refresh) yield* Deferred.succeed(turnLookup, undefined);
127
+ return null;
128
+ }),
129
+ }),
130
+ Layer.mock(PullRequestService.PullRequestService)({}),
131
+ Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({
132
+ resolve: () =>
133
+ Effect.succeed({
134
+ canonicalKey: "github.com/acme/project",
135
+ locator: {
136
+ source: "git-remote" as const,
137
+ remoteName: "origin",
138
+ remoteUrl: "git@github.com:acme/project.git",
139
+ },
140
+ provider: "github" as const,
141
+ owner: "acme",
142
+ name: "project",
143
+ }),
144
+ }),
145
+ // No client holds an activity lease.
146
+ Layer.mock(BackgroundPolicy.BackgroundPolicy)({
147
+ hasDemand: () => Effect.succeed(false),
148
+ shouldRunScopeWork: () => Effect.succeed(false),
149
+ shouldRunOpportunisticWork: Effect.succeed(false),
150
+ }),
151
+ Layer.succeed(ServerActivation.ServerActivation, Deferred.await(activation)),
152
+ Layer.succeed(
153
+ Crypto.Crypto,
154
+ Crypto.make({
155
+ randomBytes: (size) => new Uint8Array(size).fill(1),
156
+ digest: (_algorithm, data) => Effect.succeed(data),
157
+ }),
158
+ ),
159
+ FileSystem.layerNoop({}),
160
+ );
161
+ yield* Effect.gen(function* () {
162
+ const service = yield* ThreadPullRequestService.make;
163
+ yield* service.start();
164
+ yield* Deferred.succeed(activation, undefined);
165
+
166
+ // A finished turn still discovers the pull request for its branch.
167
+ yield* Queue.offer(domainEvents, {
168
+ type: "checkpoint.captured",
169
+ threadId: thread.id,
170
+ } as OrchestrationV2DomainEvent);
171
+ yield* Deferred.await(turnLookup);
172
+ yield* service.drain;
173
+ const lookupsBeforeIdle = lookups.length;
174
+
175
+ yield* TestClock.adjust("10 minutes");
176
+ yield* service.drain;
177
+
178
+ expect(lookups.slice(lookupsBeforeIdle)).toEqual([]);
179
+ }).pipe(Effect.provide(dependencies));
180
+ }),
181
+ ),
182
+ );
183
+
184
it.effect.each([
185
{ state: "closed", sameBranch: false },
186
{ state: "merged", sameBranch: false },
@@ -163,6 +266,7 @@ describe("ThreadPullRequestServiceV2 reads", () => {
266
name: "project",
267
}),
268
}),
269
+ Layer.mock(BackgroundPolicy.BackgroundPolicy)({}),
270
Layer.succeed(ServerActivation.ServerActivation, Deferred.await(activation)),
271
Layer.succeed(
272
Crypto.Crypto,
apps/server/src/orchestration-v2/ThreadPullRequestService.ts
+5
@@ -23,6 +23,7 @@ import * as Schedule from "effect/Schedule";
23
import type * as Scope from "effect/Scope";
24
import * as Stream from "effect/Stream";
25
26
+import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
27
import * as GitManager from "../git/GitManager.ts";
28
import * as ProjectStore from "./ProjectStore.ts";
29
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
@@ -106,6 +107,7 @@ export const make = Effect.gen(function* () {
107
const repositoryIdentities = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
108
const crypto = yield* Crypto.Crypto;
109
const fileSystem = yield* FileSystem.FileSystem;
110
+ const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
111
const pendingBackfill = new Map<ThreadId, number>();
112
113
const finishBackfill = (threads: ReadonlyArray<Pick<OrchestrationV2ThreadShell, "id">>) => {
@@ -395,6 +397,9 @@ export const make = Effect.gen(function* () {
397
yield* worker.enqueue({ threadId: null, refresh: false, backfill: true });
398
yield* worker.drain;
399
yield* Effect.gen(function* () {
400
+ // The sweep only catches pull requests opened outside a turn, which
401
+ // nobody sees without a client. Thread events still refresh headless work.
402
+ if (!(yield* backgroundPolicy.shouldRunOpportunisticWork)) return;
403
yield* worker.enqueue({ threadId: null, refresh: false });
404
yield* worker.drain;
405
}).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.delay("1 minute"));