Skip to content
TypeScript

fix(server): reuse pr state when settling threads (#9459)

mariac78f05a45e1c
diff --git a/apps/server/src/orchestration/ThreadSettlementReactor.test.ts b/apps/server/src/orchestration/ThreadSettlementReactor.test.ts
index 382d5812c1a..f6264200d2f 100644
--- a/apps/server/src/orchestration/ThreadSettlementReactor.test.ts
+++ b/apps/server/src/orchestration/ThreadSettlementReactor.test.ts
@@ -17,6 +17,7 @@ import { assert, describe, it } from "@effect/vitest";
import * as Crypto from "effect/Crypto";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
+import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as PubSub from "effect/PubSub";
import * as Queue from "effect/Queue";
@@ -134,6 +135,7 @@ interface HarnessOptions {
readonly settings?: ServerSettings;
readonly branchPullRequest?: GitManager["Service"]["branchPullRequest"];
readonly pullRequestSummary?: PullRequestService["Service"]["summary"];
+ readonly existingWorktreePaths?: ReadonlyArray<string>;
readonly onDispatch?: (
command: AutoSettleCommand,
) => Effect.Effect<void, OrchestrationCommandInvariantError>;
@@ -235,6 +237,9 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
Layer.succeed(ServerSettingsService, serverSettings),
Layer.succeed(ServerActivation, Deferred.await(activation)),
Layer.succeed(Crypto.Crypto, testCrypto),
+ FileSystem.layerNoop({
+ exists: (path) => Effect.succeed(options.existingWorktreePaths?.includes(path) ?? false),
+ }),
);
return {
@@ -647,6 +652,43 @@ describe("ThreadSettlementReactor", () => {
),
);
+ it.effect("looks up the branch pull request from a thread's live worktree", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ yield* TestClock.setTime(Date.parse(NOW));
+ const fixture = yield* makeHarness({
+ snapshot: makeSnapshot(
+ [
+ makeThread("live-worktree", {
+ branch: "feature/live",
+ worktreePath: "/workspace/project-root/.worktrees/live",
+ }),
+ makeThread("deleted-worktree", {
+ branch: "feature/deleted",
+ worktreePath: "/workspace/project-root/.worktrees/deleted",
+ }),
+ ],
+ [makeProject(PROJECT_ID, "/workspace/project-root")],
+ ),
+ existingWorktreePaths: ["/workspace/project-root/.worktrees/live"],
+ });
+
+ yield* Effect.gen(function* () {
+ const reactor = yield* ThreadSettlementReactor.ThreadSettlementReactor;
+ yield* startHarness(reactor, fixture.activation, fixture.snapshotReads);
+
+ assert.deepStrictEqual(
+ new Set(yield* Ref.get(fixture.branchCalls)),
+ new Set([
+ { cwd: "/workspace/project-root/.worktrees/live", branch: "feature/live" },
+ { cwd: "/workspace/project-root", branch: "feature/deleted" },
+ ]),
+ );
+ }).pipe(Effect.provide(fixture.layer));
+ }),
+ ),
+ );
+
it.effect("carries the snapshot guard and survives a stale dispatch rejection", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/apps/server/src/orchestration/ThreadSettlementReactor.ts b/apps/server/src/orchestration/ThreadSettlementReactor.ts
index 9dd7cd5e76f..9867a855a85 100644
--- a/apps/server/src/orchestration/ThreadSettlementReactor.ts
+++ b/apps/server/src/orchestration/ThreadSettlementReactor.ts
@@ -5,6 +5,7 @@ import * as Context from "effect/Context";
import * as Crypto from "effect/Crypto";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
+import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Schedule from "effect/Schedule";
import type * as Scope from "effect/Scope";
@@ -37,6 +38,7 @@ export const make = Effect.gen(function* () {
const git = yield* GitManager.GitManager;
const pullRequests = yield* PullRequestService.PullRequestService;
const crypto = yield* Crypto.Crypto;
+ const fileSystem = yield* FileSystem.FileSystem;
const sweep = Effect.fn("ThreadSettlementReactor.sweep")(function* (
mergedPullRequest: PullRequestService.PullRequestMergeEvent | null,
@@ -54,6 +56,26 @@ export const make = Effect.gen(function* () {
mergedPullRequest.repository.toLowerCase() &&
thread.linkedPullRequest.number === mergedPullRequest.number)),
);
+ // Use the same cwd as the sidebar so both paths share GitManager's PR cache.
+ const lookupCwdByThreadId = new Map<string, string>();
+ yield* Effect.forEach(
+ candidates,
+ (thread) =>
+ Effect.gen(function* () {
+ const project = projects.get(thread.projectId);
+ if (project === undefined || thread.linkedPullRequest != null) return;
+ const worktreeExists =
+ thread.worktreePath !== null &&
+ (yield* fileSystem.exists(thread.worktreePath).pipe(Effect.orElseSucceed(() => false)));
+ lookupCwdByThreadId.set(
+ thread.id,
+ worktreeExists && thread.worktreePath !== null
+ ? thread.worktreePath
+ : project.workspaceRoot,
+ );
+ }),
+ { concurrency: 8, discard: true },
+ );
const lookupKey = (thread: (typeof candidates)[number]) => {
if (thread.linkedPullRequest != null) {
return JSON.stringify([
@@ -64,11 +86,9 @@ export const make = Effect.gen(function* () {
]);
}
if (thread.branch === null) return JSON.stringify(["none", thread.id]);
- const project = projects.get(thread.projectId);
+ const cwd = lookupCwdByThreadId.get(thread.id);
return JSON.stringify(
- project === undefined
- ? ["missing-project", thread.id]
- : ["branch", project.workspaceRoot, thread.branch],
+ cwd === undefined ? ["missing-project", thread.id] : ["branch", cwd, thread.branch],
);
};
const groups = Map.groupBy(candidates, lookupKey);
@@ -100,11 +120,11 @@ export const make = Effect.gen(function* () {
} satisfies SettlementPullRequest;
}
if (thread.branch === null) return null;
- const project = projects.get(thread.projectId);
- if (project === undefined) {
+ const cwd = lookupCwdByThreadId.get(thread.id);
+ if (cwd === undefined) {
return yield* Effect.die(new Error("thread project not found"));
}
- return yield* git.branchPullRequest({ cwd: project.workspaceRoot, branch: thread.branch });
+ return yield* git.branchPullRequest({ cwd, branch: thread.branch });
});
yield* Effect.forEach(