@devries-cameron20 / t3code-1 / commits / c3bfef820a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): clear automatic PRs from other branches (#20223)

Rodrigo Brechard committed Oct 18, 2026 at 21:20 UTC c3bfef820a0756f1156ce2ac92f077fa1731e8b8
2 files changed +124 -1
apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts
+120
@@ -2,10 +2,12 @@ import {
2 EventId,
3 ProjectId,
4 ProviderInstanceId,
5 + SourceControlProviderKind,
6 ThreadId,
7 type OrchestrationProjectShell,
8 type OrchestrationV2DomainEvent,
9 type OrchestrationV2ThreadShell,
10 + type OrchestrationV2ServerCommand,
11 } from "@t3tools/contracts";
12 import { describe, expect, it } from "@effect/vitest";
13 import * as Crypto from "effect/Crypto";
@@ -169,6 +171,124 @@ describe("ThreadPullRequestServiceV2 reads", () => {
171 };
172 };
173
174 + it.effect.each([
175 + { state: "closed", sameBranch: false },
176 + { state: "merged", sameBranch: false },
177 + { state: "closed", sameBranch: true },
178 + { state: "merged", sameBranch: true },
179 + ] as const)(
180 + "restores a $state automatic PR only for its own branch (sameBranch=$sameBranch)",
181 + ({ state, sameBranch }) =>
182 + Effect.scoped(
183 + Effect.gen(function* () {
184 + const savedReference = {
185 + projectId: ProjectId.make("project-1"),
186 + repository: "acme/project",
187 + number: 42,
188 + url: "https://github.com/acme/project/pull/42",
189 + };
190 + const thread = {
191 + ...threadShell("branch-pr-thread"),
192 + branch: "main",
193 + branchPullRequest: savedReference,
194 + linkedPullRequest: savedReference,
195 + };
196 + const project: OrchestrationProjectShell = {
197 + id: thread.projectId,
198 + title: "Project",
199 + workspaceRoot: "/workspace/project",
200 + defaultModelSelection: null,
201 + scripts: [],
202 + createdAt: "2026-08-01T00:00:00.000Z",
203 + updatedAt: "2026-08-01T00:00:00.000Z",
204 + };
205 + const activation = yield* Deferred.make<void>();
206 + const sweepStarted = yield* Deferred.make<void>();
207 + const commands: Array<OrchestrationV2ServerCommand> = [];
208 + const dependencies = Layer.mergeAll(
209 + Layer.mock(Orchestrator.OrchestratorV2)({
210 + streamDomainEvents: Stream.never,
211 + getShellSnapshot: () =>
212 + Deferred.succeed(sweepStarted, undefined).pipe(
213 + Effect.as({
214 + schemaVersion: 2,
215 + snapshotSequence: 1,
216 + threads: [thread],
217 + archivedThreads: [],
218 + }),
219 + ),
220 + dispatch: (command) =>
221 + Effect.sync(() => {
222 + commands.push(command);
223 + return { sequence: 2, storedEvents: [] };
224 + }),
225 + }),
226 + Layer.mock(ProjectStore.ProjectStoreV2)({
227 + listShells: () => Effect.succeed([project]),
228 + getShell: () => Effect.succeed(Option.some(project)),
229 + }),
230 + Layer.mock(GitManager.GitManager)({
231 + branchPullRequest: () => Effect.succeed(null),
232 + }),
233 + Layer.mock(PullRequestService.PullRequestService)({
234 + summary: () =>
235 + Effect.succeed({
236 + ...savedReference,
237 + provider: SourceControlProviderKind.make("github"),
238 + title: "Previous branch work",
239 + state,
240 + headBranch: sameBranch ? thread.branch : "feature/previous-work",
241 + baseBranch: "release",
242 + updatedAt: "2026-09-19T00:00:00.000Z",
243 + }),
244 + }),
245 + Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({
246 + resolve: () =>
247 + Effect.succeed({
248 + canonicalKey: "github.com/acme/project",
249 + locator: {
250 + source: "git-remote" as const,
251 + remoteName: "origin",
252 + remoteUrl: "git@github.com:acme/project.git",
253 + },
254 + provider: "github" as const,
255 + owner: "acme",
256 + name: "project",
257 + }),
258 + }),
259 + Layer.succeed(ServerActivation.ServerActivation, Deferred.await(activation)),
260 + Layer.succeed(
261 + Crypto.Crypto,
262 + Crypto.make({
263 + randomBytes: (size) => new Uint8Array(size).fill(1),
264 + digest: (_algorithm, data) => Effect.succeed(data),
265 + }),
266 + ),
267 + FileSystem.layerNoop({}),
268 + );
269 + yield* Effect.gen(function* () {
270 + const service = yield* ThreadPullRequestService.make;
271 + yield* service.start();
272 + yield* Deferred.succeed(activation, undefined);
273 + yield* Deferred.await(sweepStarted);
274 + yield* service.drain;
275 + }).pipe(Effect.provide(dependencies));
276 +
277 + if (sameBranch) {
278 + expect(commands).toEqual([]);
279 + } else {
280 + expect(commands).toHaveLength(1);
281 + expect(commands[0]).toMatchObject({
282 + type: "thread.pull-request.sync",
283 + threadId: thread.id,
284 + branchPullRequest: null,
285 + });
286 + expect(commands[0]).not.toHaveProperty("linkedPullRequest");
287 + }
288 + }),
289 + ),
290 + );
291 +
292 it.effect("an event for one thread reads that thread's shell, not every thread's", () =>
293 Effect.scoped(
294 Effect.gen(function* () {
apps/server/src/orchestration-v2/ThreadPullRequestService.ts
+4 -1
@@ -223,7 +223,10 @@ export const make = Effect.gen(function* () {
223 const previous = yield* pullRequests.summary(thread.branchPullRequest, {
224 recoverTransientFailure: false,
225 });
226 - if (previous.state === "merged" || previous.state === "closed") {
226 + if (
227 + previous.headBranch === thread.branch &&
228 + (previous.state === "merged" || previous.state === "closed")
229 + ) {
230 branchPullRequest = thread.branchPullRequest;
231 }
232 }