Skip to content

Commit ec7c2b3

Browse files
committed
fix(server): serialize shared stack link persistence
1 parent a0c373d commit ec7c2b3

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎apps/server/src/orchestration/PullRequestSyncReactor.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,46 @@ describe("PullRequestSyncReactor", () => {
403403
),
404404
);
405405

406+
it.effect("concurrent stack reads persist a shared sibling once before syncing both roots", () =>
407+
Effect.scoped(
408+
Effect.gen(function* () {
409+
const readsReady = yield* Deferred.make<void>();
410+
let reads = 0;
411+
let linked = false;
412+
const fixture = yield* makeHarness({
413+
snapshot: makeSnapshot([makeThread("one", { pullRequests: [makeLink(7), makeLink(8)] })]),
414+
stack: () =>
415+
Effect.gen(function* () {
416+
if (++reads === 2) yield* Deferred.succeed(readsReady, undefined);
417+
yield* Deferred.await(readsReady);
418+
return {
419+
id: "stack",
420+
number: 7,
421+
url: "https://github.com/owner/repository/stacks/7",
422+
base: "main",
423+
layers: [{ number: 9, headBranch: "sibling", state: "open" as const }],
424+
};
425+
}),
426+
onDispatch: (command) =>
427+
Effect.gen(function* () {
428+
if (command.type === "thread.pull-request.link") {
429+
yield* Effect.yieldNow;
430+
assert.strictEqual(linked, false);
431+
linked = true;
432+
} else {
433+
assert.strictEqual(linked, true);
434+
}
435+
}),
436+
});
437+
yield* Effect.gen(function* () {
438+
yield* startAndSweep(fixture);
439+
assert.strictEqual((yield* Ref.get(fixture.linkCommands)).length, 1);
440+
assert.strictEqual((yield* Ref.get(fixture.syncCommands)).length, 2);
441+
}).pipe(Effect.provide(fixture.layer));
442+
}),
443+
),
444+
);
445+
406446
it.effect("explicit refresh reads a changed stack even when its PR summary is unchanged", () =>
407447
Effect.scoped(
408448
Effect.gen(function* () {

‎apps/server/src/orchestration/PullRequestSyncReactor.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as Effect from "effect/Effect";
2323
import * as Layer from "effect/Layer";
2424
import * as Schedule from "effect/Schedule";
2525
import type * as Scope from "effect/Scope";
26+
import * as Semaphore from "effect/Semaphore";
2627
import * as Stream from "effect/Stream";
2728

2829
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
@@ -175,6 +176,7 @@ export const make = Effect.gen(function* () {
175176
// Layers auto-linked this sweep, so two links of one thread that share a
176177
// stack do not both try to add the same sibling.
177178
const linkedThisSweep = new Set<string>();
179+
const persistence = yield* Semaphore.make(1);
178180

179181
const syncEntry = Effect.fn("PullRequestSyncReactor.syncEntry")(function* (
180182
entry: LinkEntry,
@@ -281,6 +283,7 @@ export const make = Effect.gen(function* () {
281283
entries,
282284
(entry) =>
283285
syncEntry(entry, fields, fetchedStack).pipe(
286+
persistence.withPermits(1),
284287
Effect.catchCause((cause) => {
285288
if (!Cause.hasInterruptsOnly(cause)) retryStacks.add(key);
286289
return logSkipped("pull request sync skipped", { threadId: entry.thread.id, key })(

0 commit comments

Comments
 (0)