Skip to content
TypeScript

fix(server): resolve project identity before legacy pr relinks (#11045)

t3-code[bot]d29c56a5c404
diff --git a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts
index 3929136f90f..6d062ebcab4 100644
--- a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts
+++ b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts
@@ -27,7 +27,7 @@ import * as Option from "effect/Option";
import * as Queue from "effect/Queue";
import * as Stream from "effect/Stream";
import { TestClock } from "effect/testing";
-import { describe, expect, it } from "vite-plus/test";
+import { describe, expect, it, vi } from "vite-plus/test";
import { PersistenceSqlError } from "../../persistence/Errors.ts";
import { OrchestrationCommandReceiptRepositoryLive } from "../../persistence/Layers/OrchestrationCommandReceipts.ts";
@@ -1039,6 +1039,8 @@ describe("OrchestrationEngine", () => {
},
}),
});
+ // Same-tick links must replace the old PR, not rely on timestamp ordering.
+ const clock = vi.spyOn(Date, "now").mockReturnValue(Date.parse(now()));
try {
const projectId = ProjectId.make("pr-race-project");
const threadId = ThreadId.make("pr-race-thread");
@@ -1141,6 +1143,9 @@ describe("OrchestrationEngine", () => {
if (change === "delete") return;
const current = (await system.readModel()).threads[0];
expect(current?.branchPullRequest ?? null).toBeNull();
+ expect(current?.pullRequests.map((link) => link.number)).toEqual(
+ change === "unlink" ? [] : change === "relink" ? [3] : [1],
+ );
expect(current?.linkedPullRequest ?? null).toEqual(
change === "unlink"
? null
@@ -1149,6 +1154,7 @@ describe("OrchestrationEngine", () => {
: previous,
);
} finally {
+ clock.mockRestore();
await system.dispose();
}
},
diff --git a/apps/server/src/orchestration/Layers/OrchestrationEngine.ts b/apps/server/src/orchestration/Layers/OrchestrationEngine.ts
index 6557888c38a..fb2fadde5e6 100644
--- a/apps/server/src/orchestration/Layers/OrchestrationEngine.ts
+++ b/apps/server/src/orchestration/Layers/OrchestrationEngine.ts
@@ -212,6 +212,29 @@ const makeOrchestrationEngine = Effect.gen(function* () {
});
}
+ // New and moved projects do not carry a resolved identity in the event-derived
+ // command model. Legacy PR edits need it to identify the link they replace.
+ if (
+ envelope.command.type === "thread.meta.update" &&
+ envelope.command.linkedPullRequest !== undefined
+ ) {
+ const threadId = envelope.command.threadId;
+ const thread = commandReadModel.threads.find((thread) => thread.id === threadId);
+ if (thread !== undefined) {
+ const project = yield* projectionSnapshotQuery.getProjectShellById(thread.projectId);
+ if (Option.isSome(project)) {
+ commandReadModel = {
+ ...commandReadModel,
+ projects: commandReadModel.projects.map((entry) =>
+ entry.id === thread.projectId
+ ? { ...entry, repositoryIdentity: project.value.repositoryIdentity }
+ : entry,
+ ),
+ };
+ }
+ }
+ }
+
// Command snapshots omit activities at startup and cap them while running.
// Read this request's durable state before deciding how to send the answer.
const userInputActivity =