Skip to content

Commit 93cd39a

Browse files
committed
fix(server): clear automatic PRs from other branches
Discovery could restore a closed or merged PR after a thread switched to a branch without one. Only restore the saved automatic association when its source branch still matches the thread. Implemented and verified with gpt-6.1-sol through the Codex harness in T3 Code.
1 parent cfafb4b commit 93cd39a

2 files changed

Lines changed: 124 additions & 1 deletion

File tree

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

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import {
22
EventId,
33
ProjectId,
44
ProviderInstanceId,
5+
SourceControlProviderKind,
56
ThreadId,
67
type OrchestrationProjectShell,
78
type OrchestrationV2DomainEvent,
89
type OrchestrationV2ThreadShell,
10+
type OrchestrationV2ServerCommand,
911
} from "@t3tools/contracts";
1012
import { describe, expect, it } from "@effect/vitest";
1113
import * as Crypto from "effect/Crypto";
@@ -169,6 +171,124 @@ describe("ThreadPullRequestServiceV2 reads", () => {
169171
};
170172
};
171173

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: "[email protected]: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+
172292
it.effect("an event for one thread reads that thread's shell, not every thread's", () =>
173293
Effect.scoped(
174294
Effect.gen(function* () {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ export const make = Effect.gen(function* () {
223223
const previous = yield* pullRequests.summary(thread.branchPullRequest, {
224224
recoverTransientFailure: false,
225225
});
226-
if (previous.state === "merged" || previous.state === "closed") {
226+
if (
227+
previous.headBranch === thread.branch &&
228+
(previous.state === "merged" || previous.state === "closed")
229+
) {
227230
branchPullRequest = thread.branchPullRequest;
228231
}
229232
}

0 commit comments

Comments
 (0)