Skip to content

Commit 5bb54a0

Browse files
fix(server): pause periodic PR discovery without foreground clients (#23852)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 171f8d8 commit 5bb54a0

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
SourceControlProviderKind,
55
ThreadId,
66
type OrchestrationProjectShell,
7+
type OrchestrationV2DomainEvent,
78
type OrchestrationV2ThreadShell,
89
type OrchestrationV2ServerCommand,
910
} from "@t3tools/contracts";
@@ -15,8 +16,11 @@ import * as Effect from "effect/Effect";
1516
import * as FileSystem from "effect/FileSystem";
1617
import * as Layer from "effect/Layer";
1718
import * as Option from "effect/Option";
19+
import * as Queue from "effect/Queue";
1820
import * as Stream from "effect/Stream";
21+
import * as TestClock from "effect/testing/TestClock";
1922

23+
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
2024
import * as GitManager from "../git/GitManager.ts";
2125
import * as ProjectStore from "./ProjectStore.ts";
2226
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
@@ -78,6 +82,105 @@ describe("ThreadPullRequestServiceV2 reads", () => {
7882
};
7983
};
8084

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: "[email protected]: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+
81184
it.effect.each([
82185
{ state: "closed", sameBranch: false },
83186
{ state: "merged", sameBranch: false },
@@ -163,6 +266,7 @@ describe("ThreadPullRequestServiceV2 reads", () => {
163266
name: "project",
164267
}),
165268
}),
269+
Layer.mock(BackgroundPolicy.BackgroundPolicy)({}),
166270
Layer.succeed(ServerActivation.ServerActivation, Deferred.await(activation)),
167271
Layer.succeed(
168272
Crypto.Crypto,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as Schedule from "effect/Schedule";
2323
import type * as Scope from "effect/Scope";
2424
import * as Stream from "effect/Stream";
2525

26+
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
2627
import * as GitManager from "../git/GitManager.ts";
2728
import * as ProjectStore from "./ProjectStore.ts";
2829
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
@@ -106,6 +107,7 @@ export const make = Effect.gen(function* () {
106107
const repositoryIdentities = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
107108
const crypto = yield* Crypto.Crypto;
108109
const fileSystem = yield* FileSystem.FileSystem;
110+
const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
109111
const pendingBackfill = new Map<ThreadId, number>();
110112

111113
const finishBackfill = (threads: ReadonlyArray<Pick<OrchestrationV2ThreadShell, "id">>) => {
@@ -395,6 +397,9 @@ export const make = Effect.gen(function* () {
395397
yield* worker.enqueue({ threadId: null, refresh: false, backfill: true });
396398
yield* worker.drain;
397399
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;
398403
yield* worker.enqueue({ threadId: null, refresh: false });
399404
yield* worker.drain;
400405
}).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.delay("1 minute"));

0 commit comments

Comments
 (0)