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

This commit is contained in:
t3-code[bot] authored and GitHub committed 2026-09-10 02:54:25 -03:00
1 parent bb5e824c9f
commit d29c56a5c4
2 files changed
+30 -1

No files matched your search

@@ -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();
}
},
@@ -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 =