Skip to content

Commit a79ca62

Browse files
only21milclaude
andcommitted
fix(server): PR sync judges a group due once it holds a shared slot
A host's own loop can have room while the shared pool is full, so the due check now runs inside the permit. A stack retry a sibling flags while another host holds the slots still counts this sweep. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 30d132a commit a79ca62

2 files changed

Lines changed: 32 additions & 26 deletions

File tree

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,11 +1157,7 @@ describe("PullRequestSyncReactor", () => {
11571157
number: 9,
11581158
url: "https://github.com/owner/repository/stack/9",
11591159
base: "main",
1160-
layers: layers.map((number) => ({
1161-
number,
1162-
headBranch: `layer-${number}`,
1163-
state: "open" as const,
1164-
})),
1160+
layers: layers.map(stackLayer),
11651161
});
11661162
// #50 is merged and not due until #1's stack read shows it a different layer list.
11671163
const merged = makeLink(
@@ -1174,9 +1170,13 @@ describe("PullRequestSyncReactor", () => {
11741170
const synced1 = yield* Deferred.make<void>();
11751171
const fixture = yield* makeHarness({
11761172
snapshot: makeSnapshot([
1177-
...Array.from({ length: 25 }, (_, index) =>
1178-
makeThread(`thread-${index + 1}`, { pullRequests: [makeLink(index + 1)] }),
1173+
// Another host takes 24 of the 25 shared slots and #1 the last one.
1174+
...Array.from({ length: 24 }, (_, index) =>
1175+
makeThread(`thread-${index + 2}`, {
1176+
pullRequests: [makeLink(index + 2, null, { host: "gitlab.example.com" })],
1177+
}),
11791178
),
1179+
makeThread("thread-1", { pullRequests: [makeLink(1)] }),
11801180
makeThread("thread-50", { pullRequests: [merged] }),
11811181
]),
11821182
batchesReads: () => false,

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -586,46 +586,52 @@ export const make = Effect.gen(function* () {
586586
// One read per pull request, often a CLI process, so these keep the narrower width.
587587
unbatched: { width: 25, reads: yield* Semaphore.make(25) },
588588
};
589+
const isPaused = (pauseKey: string, atMs: number) => (pausedUntil.get(pauseKey) ?? 0) > atMs;
590+
// Judged once a slot is free, so a stack retry a sibling flags meanwhile still counts, and
591+
// pauses set during the sweep apply at once.
589592
const readIfDue = (
590593
key: string,
591594
entries: ReadonlyArray<LinkEntry>,
595+
pauseKey: string,
592596
reads: Semaphore.Semaphore,
593-
) => {
594-
if (!isDue(key, entries, nowMs)) return Effect.void;
595-
const pauseKey = pauseKeyOf(entries[0]!);
596-
// Check pauses at each read so changes during the sweep apply immediately.
597-
return reads.withPermits(1)(
598-
Clock.currentTimeMillis.pipe(
599-
Effect.flatMap((startedAtMs) =>
600-
(pausedUntil.get(pauseKey) ?? 0) > startedAtMs
601-
? Effect.void
602-
: readGroup(key, entries, pauseKey),
603-
),
597+
) =>
598+
reads.withPermits(1)(
599+
Effect.suspend(() =>
600+
isDue(key, entries, nowMs)
601+
? Clock.currentTimeMillis.pipe(
602+
Effect.flatMap((startedAtMs) =>
603+
isPaused(pauseKey, startedAtMs) ? Effect.void : readGroup(key, entries, pauseKey),
604+
),
605+
)
606+
: Effect.void,
604607
),
605608
);
606-
};
607609
const groupsByHost = Map.groupBy(
608610
Array.from(groups).filter(([key]) => scope === "all" || requested.has(key)),
609611
([, entries]) => pauseKeyOf(entries[0]!),
610612
);
611613
yield* Effect.forEach(
612614
groupsByHost,
613615
([pauseKey, hostGroups]) => {
614-
// Stack retries only flag groups on the flagging group's own host, so a host with
615-
// nothing due now reads nothing this sweep.
616+
// A host paused or with nothing due skips the lookup. Stack retries only flag groups on
617+
// the flagging group's own host, so nothing on it becomes due this sweep.
616618
if (
617-
(pausedUntil.get(pauseKey) ?? 0) > nowMs ||
619+
isPaused(pauseKey, nowMs) ||
618620
!hostGroups.some(([key, entries]) => isDue(key, entries, nowMs))
619621
) {
620622
return Effect.void;
621623
}
622624
return pullRequests.batchesReads(referenceOf(hostGroups[0]![1][0]!)).pipe(
623625
Effect.flatMap((batches) => {
624626
const { width, reads } = batches ? pools.batched : pools.unbatched;
625-
return Effect.forEach(hostGroups, ([key, entries]) => readIfDue(key, entries, reads), {
626-
concurrency: width,
627-
discard: true,
628-
});
627+
return Effect.forEach(
628+
hostGroups,
629+
([key, entries]) => readIfDue(key, entries, pauseKey, reads),
630+
{
631+
concurrency: width,
632+
discard: true,
633+
},
634+
);
629635
}),
630636
);
631637
},

0 commit comments

Comments
 (0)