Skip to content

Commit aaba9de

Browse files
fix(server): PR discovery sweeps skip hidden subagent threads (pingdotgg#26983)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent c6f2b7d commit aaba9de

2 files changed

Lines changed: 132 additions & 3 deletions

File tree

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

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,128 @@ describe("ThreadPullRequestServiceV2 reads", () => {
179179
),
180180
);
181181

182+
it.effect("leaves delegated child threads to their own refresh instead of the sweep", () =>
183+
Effect.scoped(
184+
Effect.gen(function* () {
185+
const parent = {
186+
...threadShell("parent-thread"),
187+
branch: "feature/parent",
188+
worktreePath: "/workspace/parent",
189+
};
190+
const child: OrchestrationV2ThreadShell = {
191+
...threadShell("child-thread"),
192+
branch: "t3/reply-ok",
193+
worktreePath: "/workspace/child",
194+
lineage: {
195+
rootThreadId: parent.id,
196+
parentThreadId: parent.id,
197+
relationshipToParent: "subagent",
198+
},
199+
};
200+
const project: OrchestrationProjectShell = {
201+
id: parent.projectId,
202+
title: "Project",
203+
workspaceRoot: "/workspace/project",
204+
defaultModelSelection: null,
205+
scripts: [],
206+
createdAt: "2026-08-01T00:00:00.000Z",
207+
updatedAt: "2026-08-01T00:00:00.000Z",
208+
};
209+
const activation = yield* Deferred.make<void>();
210+
const domainEvents = yield* Queue.unbounded<OrchestrationV2DomainEvent>();
211+
const sweeps = yield* Queue.unbounded<void>();
212+
const childLookup = yield* Deferred.make<void>();
213+
const lookups: Array<{ readonly branch: string; readonly refresh: boolean }> = [];
214+
const dependencies = Layer.mergeAll(
215+
Layer.mock(Orchestrator.OrchestratorV2)({
216+
streamDomainEvents: Stream.fromQueue(domainEvents),
217+
getShellSnapshot: () =>
218+
Queue.offer(sweeps, undefined).pipe(
219+
Effect.as({
220+
schemaVersion: 2,
221+
snapshotSequence: 1,
222+
threads: [parent, child],
223+
archivedThreads: [],
224+
}),
225+
),
226+
getThreadEventSequence: () => Effect.succeed(1),
227+
getThreadShell: (threadId) =>
228+
Effect.succeed([parent, child].find((thread) => thread.id === threadId) ?? null),
229+
}),
230+
Layer.mock(ProjectStore.ProjectStoreV2)({
231+
listShells: () => Effect.succeed([project]),
232+
getShell: () => Effect.succeedSome(project),
233+
}),
234+
Layer.mock(GitManager.GitManager)({
235+
branchPullRequest: (input, options) =>
236+
Effect.gen(function* () {
237+
const refresh = options?.refresh ?? false;
238+
lookups.push({ branch: input.branch, refresh });
239+
if (input.branch === child.branch && refresh) {
240+
yield* Deferred.succeed(childLookup, undefined);
241+
}
242+
return null;
243+
}),
244+
}),
245+
Layer.mock(PullRequestService.PullRequestService)({}),
246+
Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({
247+
resolve: () =>
248+
Effect.succeed({
249+
canonicalKey: "github.com/acme/project",
250+
locator: {
251+
source: "git-remote" as const,
252+
remoteName: "origin",
253+
remoteUrl: "[email protected]:acme/project.git",
254+
},
255+
provider: SourceControlProviderKind.make("github"),
256+
owner: "acme",
257+
name: "project",
258+
}),
259+
}),
260+
Layer.mock(BackgroundPolicy.BackgroundPolicy)({
261+
shouldRunScopeWork: () => Effect.succeed(true),
262+
shouldRunOpportunisticWork: Effect.succeed(true),
263+
}),
264+
Layer.succeed(ServerActivation.ServerActivation, Deferred.await(activation)),
265+
Layer.succeed(
266+
Crypto.Crypto,
267+
Crypto.make({
268+
randomBytes: (size) => new Uint8Array(size).fill(1),
269+
digest: (_algorithm, data) => Effect.succeed(data),
270+
}),
271+
),
272+
FileSystem.layerNoop({}),
273+
);
274+
yield* Effect.gen(function* () {
275+
const service = yield* ThreadPullRequestService.make;
276+
yield* service.start();
277+
yield* Deferred.succeed(activation, undefined);
278+
279+
// Startup backfill sweep, then one periodic sweep.
280+
yield* Queue.take(sweeps);
281+
yield* service.drain;
282+
yield* TestClock.adjust("1 minute");
283+
yield* Queue.take(sweeps);
284+
yield* service.drain;
285+
const sweepLookups = lookups.map((lookup) => lookup.branch).toSorted();
286+
287+
// The child's own turn still discovers its pull request.
288+
yield* Queue.offer(domainEvents, {
289+
type: "checkpoint.captured",
290+
threadId: child.id,
291+
} as OrchestrationV2DomainEvent);
292+
yield* Deferred.await(childLookup);
293+
yield* service.drain;
294+
295+
expect(sweepLookups).toEqual([parent.branch, parent.branch]);
296+
expect(lookups.slice(sweepLookups.length)).toEqual([
297+
{ branch: child.branch, refresh: true },
298+
]);
299+
}).pipe(Effect.provide(dependencies));
300+
}),
301+
),
302+
);
303+
182304
it.effect.each([
183305
{ state: "closed", sameBranch: false },
184306
{ state: "merged", sameBranch: false },

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,16 @@ export const make = Effect.gen(function* () {
139139
readThreadSnapshot(request),
140140
projectStore.listShells(),
141141
]);
142+
// Subagent children refresh after their own runs; sweeping them costs a lookup each per minute.
143+
const snapshotThreads =
144+
request.threadId === null
145+
? threadSnapshot.threads.filter(
146+
(thread) => thread.lineage.relationshipToParent !== "subagent",
147+
)
148+
: threadSnapshot.threads;
142149
const projects = new Map(projectShells.map((project) => [project.id, project]));
143150
if (request.backfill) {
144-
for (const thread of threadSnapshot.threads) {
151+
for (const thread of snapshotThreads) {
145152
if (
146153
(thread.settledOverride === "settled" || thread.settledAt !== null) &&
147154
thread.branchPullRequest == null
@@ -151,13 +158,13 @@ export const make = Effect.gen(function* () {
151158
}
152159
}
153160
const visibleThreadIds = new Set(
154-
threadSnapshot.threads.filter((thread) => thread.branch !== null).map((thread) => thread.id),
161+
snapshotThreads.filter((thread) => thread.branch !== null).map((thread) => thread.id),
155162
);
156163
const checkedIds = request.threadId === null ? [...pendingBackfill.keys()] : [request.threadId];
157164
for (const threadId of checkedIds) {
158165
if (!visibleThreadIds.has(threadId)) pendingBackfill.delete(threadId);
159166
}
160-
const threads = threadSnapshot.threads.filter(
167+
const threads = snapshotThreads.filter(
161168
(thread) =>
162169
thread.archivedAt === null &&
163170
((thread.settledOverride !== "settled" && thread.settledAt === null) ||

0 commit comments

Comments
 (0)