Skip to content
TypeScript

fix(server): PR discovery sweeps skip hidden subagent threads (#26983)

Julius Marmingeaaba9de94f6e
diff --git a/apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts b/apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts
index 5627a6dc37a..5f380c7e0ac 100644
--- a/apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts
+++ b/apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts
@@ -179,6 +179,128 @@ describe("ThreadPullRequestServiceV2 reads", () => {
),
);
+ it.effect("leaves delegated child threads to their own refresh instead of the sweep", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const parent = {
+ ...threadShell("parent-thread"),
+ branch: "feature/parent",
+ worktreePath: "/workspace/parent",
+ };
+ const child: OrchestrationV2ThreadShell = {
+ ...threadShell("child-thread"),
+ branch: "t3/reply-ok",
+ worktreePath: "/workspace/child",
+ lineage: {
+ rootThreadId: parent.id,
+ parentThreadId: parent.id,
+ relationshipToParent: "subagent",
+ },
+ };
+ const project: OrchestrationProjectShell = {
+ id: parent.projectId,
+ title: "Project",
+ workspaceRoot: "/workspace/project",
+ defaultModelSelection: null,
+ scripts: [],
+ createdAt: "2026-08-01T00:00:00.000Z",
+ updatedAt: "2026-08-01T00:00:00.000Z",
+ };
+ const activation = yield* Deferred.make<void>();
+ const domainEvents = yield* Queue.unbounded<OrchestrationV2DomainEvent>();
+ const sweeps = yield* Queue.unbounded<void>();
+ const childLookup = yield* Deferred.make<void>();
+ const lookups: Array<{ readonly branch: string; readonly refresh: boolean }> = [];
+ const dependencies = Layer.mergeAll(
+ Layer.mock(Orchestrator.OrchestratorV2)({
+ streamDomainEvents: Stream.fromQueue(domainEvents),
+ getShellSnapshot: () =>
+ Queue.offer(sweeps, undefined).pipe(
+ Effect.as({
+ schemaVersion: 2,
+ snapshotSequence: 1,
+ threads: [parent, child],
+ archivedThreads: [],
+ }),
+ ),
+ getThreadEventSequence: () => Effect.succeed(1),
+ getThreadShell: (threadId) =>
+ Effect.succeed([parent, child].find((thread) => thread.id === threadId) ?? null),
+ }),
+ Layer.mock(ProjectStore.ProjectStoreV2)({
+ listShells: () => Effect.succeed([project]),
+ getShell: () => Effect.succeedSome(project),
+ }),
+ Layer.mock(GitManager.GitManager)({
+ branchPullRequest: (input, options) =>
+ Effect.gen(function* () {
+ const refresh = options?.refresh ?? false;
+ lookups.push({ branch: input.branch, refresh });
+ if (input.branch === child.branch && refresh) {
+ yield* Deferred.succeed(childLookup, undefined);
+ }
+ return null;
+ }),
+ }),
+ Layer.mock(PullRequestService.PullRequestService)({}),
+ Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({
+ resolve: () =>
+ Effect.succeed({
+ canonicalKey: "github.com/acme/project",
+ locator: {
+ source: "git-remote" as const,
+ remoteName: "origin",
+ remoteUrl: "[email protected]:acme/project.git",
+ },
+ provider: SourceControlProviderKind.make("github"),
+ owner: "acme",
+ name: "project",
+ }),
+ }),
+ Layer.mock(BackgroundPolicy.BackgroundPolicy)({
+ shouldRunScopeWork: () => Effect.succeed(true),
+ shouldRunOpportunisticWork: Effect.succeed(true),
+ }),
+ Layer.succeed(ServerActivation.ServerActivation, Deferred.await(activation)),
+ Layer.succeed(
+ Crypto.Crypto,
+ Crypto.make({
+ randomBytes: (size) => new Uint8Array(size).fill(1),
+ digest: (_algorithm, data) => Effect.succeed(data),
+ }),
+ ),
+ FileSystem.layerNoop({}),
+ );
+ yield* Effect.gen(function* () {
+ const service = yield* ThreadPullRequestService.make;
+ yield* service.start();
+ yield* Deferred.succeed(activation, undefined);
+
+ // Startup backfill sweep, then one periodic sweep.
+ yield* Queue.take(sweeps);
+ yield* service.drain;
+ yield* TestClock.adjust("1 minute");
+ yield* Queue.take(sweeps);
+ yield* service.drain;
+ const sweepLookups = lookups.map((lookup) => lookup.branch).toSorted();
+
+ // The child's own turn still discovers its pull request.
+ yield* Queue.offer(domainEvents, {
+ type: "checkpoint.captured",
+ threadId: child.id,
+ } as OrchestrationV2DomainEvent);
+ yield* Deferred.await(childLookup);
+ yield* service.drain;
+
+ expect(sweepLookups).toEqual([parent.branch, parent.branch]);
+ expect(lookups.slice(sweepLookups.length)).toEqual([
+ { branch: child.branch, refresh: true },
+ ]);
+ }).pipe(Effect.provide(dependencies));
+ }),
+ ),
+ );
+
it.effect.each([
{ state: "closed", sameBranch: false },
{ state: "merged", sameBranch: false },
diff --git a/apps/server/src/orchestration-v2/ThreadPullRequestService.ts b/apps/server/src/orchestration-v2/ThreadPullRequestService.ts
index 7b3c09ba15b..0db9feac858 100644
--- a/apps/server/src/orchestration-v2/ThreadPullRequestService.ts
+++ b/apps/server/src/orchestration-v2/ThreadPullRequestService.ts
@@ -139,9 +139,16 @@ export const make = Effect.gen(function* () {
readThreadSnapshot(request),
projectStore.listShells(),
]);
+ // Subagent children refresh after their own runs; sweeping them costs a lookup each per minute.
+ const snapshotThreads =
+ request.threadId === null
+ ? threadSnapshot.threads.filter(
+ (thread) => thread.lineage.relationshipToParent !== "subagent",
+ )
+ : threadSnapshot.threads;
const projects = new Map(projectShells.map((project) => [project.id, project]));
if (request.backfill) {
- for (const thread of threadSnapshot.threads) {
+ for (const thread of snapshotThreads) {
if (
(thread.settledOverride === "settled" || thread.settledAt !== null) &&
thread.branchPullRequest == null
@@ -151,13 +158,13 @@ export const make = Effect.gen(function* () {
}
}
const visibleThreadIds = new Set(
- threadSnapshot.threads.filter((thread) => thread.branch !== null).map((thread) => thread.id),
+ snapshotThreads.filter((thread) => thread.branch !== null).map((thread) => thread.id),
);
const checkedIds = request.threadId === null ? [...pendingBackfill.keys()] : [request.threadId];
for (const threadId of checkedIds) {
if (!visibleThreadIds.has(threadId)) pendingBackfill.delete(threadId);
}
- const threads = threadSnapshot.threads.filter(
+ const threads = snapshotThreads.filter(
(thread) =>
thread.archivedAt === null &&
((thread.settledOverride !== "settled" && thread.settledAt === null) ||