Skip to content

Commit ce2815d

Browse files
only21milclaudejuliusmarminge
authored
fix(server): re-read stack layers for every member when the stack grows (#20415)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 5d2406b commit ce2815d

2 files changed

Lines changed: 373 additions & 4 deletions

File tree

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

Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ interface HarnessOptions {
171171
readonly stack?: (
172172
input: PullRequestRef,
173173
) => Effect.Effect<PullRequestStack | null, PullRequestOperationError>;
174+
readonly onSync?: (command: SyncCommand) => Effect.Effect<unknown>;
174175
}
175176

176177
const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options: HarnessOptions) {
@@ -205,6 +206,7 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options:
205206
const dispatch: Orchestrator.OrchestratorV2Shape["dispatch"] = (command) => {
206207
if (command.type === "thread.pull-request-link.sync") {
207208
return Ref.update(syncCommands, (recorded) => [...recorded, command]).pipe(
209+
Effect.andThen(options.onSync?.(command) ?? Effect.void),
208210
Effect.as({ sequence: 1, storedEvents: [] }),
209211
);
210212
}
@@ -383,6 +385,89 @@ function applySync(
383385
};
384386
}
385387

388+
const stackLayer = (number: number) => ({
389+
number,
390+
headBranch: `layer-${number}`,
391+
state: "open" as const,
392+
});
393+
394+
/**
395+
* Stack 80 starts as #18 and #20; `grow` pushes #83 on top. `cachedRead` behaves like
396+
* `PullRequestReadCache`: a read is kept for 60 s and invalidating the reference drops it.
397+
*/
398+
function makeGrowingStack() {
399+
let layers = [stackLayer(18), stackLayer(20)];
400+
const entries = new Map<number, { readonly layers: typeof layers; expiresAt: number }>();
401+
const invalidated: Array<number> = [];
402+
const stackOf = (stackLayers: typeof layers): PullRequestStack => ({
403+
id: "stack-80",
404+
number: 80,
405+
url: "https://github.com/owner/repository/stack/80",
406+
base: "main",
407+
layers: stackLayers,
408+
});
409+
return {
410+
invalidated,
411+
grow: () => {
412+
layers = [...layers, stackLayer(83)];
413+
},
414+
read: () => Effect.sync(() => stackOf(layers)),
415+
invalidate: (({ reference }) =>
416+
Effect.sync(() => {
417+
invalidated.push(reference?.number ?? -1);
418+
entries.delete(reference?.number ?? -1);
419+
})) satisfies PullRequestService.PullRequestService["Service"]["invalidate"],
420+
cachedRead: (input: PullRequestRef) =>
421+
Clock.currentTimeMillis.pipe(
422+
Effect.map((now) => {
423+
let entry = entries.get(input.number);
424+
if (entry === undefined || entry.expiresAt <= now) {
425+
entry = { layers, expiresAt: now + 60_000 };
426+
entries.set(input.number, entry);
427+
}
428+
return stackOf(entry.layers);
429+
}),
430+
),
431+
};
432+
}
433+
434+
const applyRecordedSyncs = (fixture: Harness) =>
435+
Ref.get(fixture.syncCommands).pipe(
436+
Effect.flatMap((commands) =>
437+
Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, commands)),
438+
),
439+
);
440+
441+
const linkPullRequest83 = (fixture: Harness) =>
442+
Ref.update(fixture.snapshots, (snapshot) => ({
443+
...snapshot,
444+
threads: snapshot.threads.map((thread) => ({
445+
...thread,
446+
pullRequests: [...thread.pullRequests, makeLink(83)],
447+
})),
448+
}));
449+
450+
const storedStackLayers = (fixture: Harness) =>
451+
Ref.get(fixture.snapshots).pipe(
452+
Effect.map((snapshot) =>
453+
snapshot.threads[0]!.pullRequests.map((link) => [
454+
link.number,
455+
link.stack?.layers.map((layer) => layer.number),
456+
]),
457+
),
458+
);
459+
460+
const requestAndApply = Effect.fn("requestPullRequestSyncHarness")(function* (
461+
fixture: Harness,
462+
reactor: PullRequestSyncReactor.PullRequestSyncReactor["Service"],
463+
number: number,
464+
) {
465+
yield* reactor.requestSync({ host: "github.com", repository: "owner/repository", number });
466+
yield* Queue.take(fixture.snapshotReads);
467+
yield* reactor.drain;
468+
yield* applyRecordedSyncs(fixture);
469+
});
470+
386471
describe("PullRequestSyncReactor", () => {
387472
it.effect("retries a failed stack read after the summary becomes terminal", () =>
388473
Effect.scoped(
@@ -1189,6 +1274,243 @@ describe("PullRequestSyncReactor", () => {
11891274
),
11901275
);
11911276

1277+
it.effect("re-reads every member's layers once a new layer joins the stack", () =>
1278+
Effect.scoped(
1279+
Effect.gen(function* () {
1280+
yield* TestClock.setTime(Date.parse(NOW));
1281+
const stack = makeGrowingStack();
1282+
const fixture = yield* makeHarness({
1283+
snapshot: makeSnapshot([
1284+
makeThread("one", { pullRequests: [makeLink(18), makeLink(20)] }),
1285+
]),
1286+
stack: stack.read,
1287+
});
1288+
const sweepAndApply = (reactor: PullRequestSyncReactor.PullRequestSyncReactor["Service"]) =>
1289+
sweepAgain(fixture, reactor).pipe(Effect.andThen(applyRecordedSyncs(fixture)));
1290+
1291+
yield* Effect.gen(function* () {
1292+
const reactor = yield* startAndSweep(fixture);
1293+
yield* applyRecordedSyncs(fixture);
1294+
assert.deepStrictEqual(yield* storedStackLayers(fixture), [
1295+
[18, [18, 20]],
1296+
[20, [18, 20]],
1297+
]);
1298+
const readsBeforeGrowth = (yield* Ref.get(fixture.stackCalls)).length;
1299+
1300+
// #83 is pushed on top and linked; only its own link starts without a snapshot.
1301+
stack.grow();
1302+
yield* linkPullRequest83(fixture);
1303+
yield* sweepAndApply(reactor);
1304+
yield* sweepAndApply(reactor);
1305+
1306+
assert.deepStrictEqual(yield* storedStackLayers(fixture), [
1307+
[18, [18, 20, 83]],
1308+
[20, [18, 20, 83]],
1309+
[83, [18, 20, 83]],
1310+
]);
1311+
const readsAfterGrowth = (yield* Ref.get(fixture.stackCalls)).slice(readsBeforeGrowth);
1312+
assert.deepStrictEqual(
1313+
readsAfterGrowth.map((ref) => ref.number).toSorted(),
1314+
[18, 20, 83],
1315+
);
1316+
1317+
// Every member agrees now, so a further sweep reads no stack.
1318+
yield* sweepAndApply(reactor);
1319+
assert.strictEqual(
1320+
(yield* Ref.get(fixture.stackCalls)).length,
1321+
readsBeforeGrowth + readsAfterGrowth.length,
1322+
);
1323+
}).pipe(Effect.provide(fixture.layer));
1324+
}),
1325+
),
1326+
);
1327+
1328+
it.effect("reads a flagged member's layers past a cache entry a requested sweep left live", () =>
1329+
Effect.scoped(
1330+
Effect.gen(function* () {
1331+
yield* TestClock.setTime(Date.parse(NOW));
1332+
const stack = makeGrowingStack();
1333+
const fixture = yield* makeHarness({
1334+
snapshot: makeSnapshot([
1335+
makeThread("one", { pullRequests: [makeLink(18), makeLink(20)] }),
1336+
]),
1337+
invalidate: stack.invalidate,
1338+
stack: stack.cachedRead,
1339+
});
1340+
1341+
yield* Effect.gen(function* () {
1342+
const reactor = yield* startAndSweep(fixture);
1343+
yield* applyRecordedSyncs(fixture);
1344+
1345+
// 30 s later a state change refreshes #18, which caches its two-layer read until 90 s.
1346+
yield* TestClock.adjust("30 seconds");
1347+
yield* requestAndApply(fixture, reactor, 18);
1348+
1349+
// At 40 s #83 is pushed on top and linked. Its requested sweep flags #18 and #20.
1350+
yield* TestClock.adjust("10 seconds");
1351+
stack.grow();
1352+
yield* linkPullRequest83(fixture);
1353+
yield* requestAndApply(fixture, reactor, 83);
1354+
1355+
// The scheduled sweep at 60 s: #20's entry has expired, #18's is still live.
1356+
const invalidatedBefore = stack.invalidated.length;
1357+
yield* sweepAgain(fixture, reactor).pipe(Effect.andThen(applyRecordedSyncs(fixture)));
1358+
1359+
assert.deepStrictEqual(yield* storedStackLayers(fixture), [
1360+
[18, [18, 20, 83]],
1361+
[20, [18, 20, 83]],
1362+
[83, [18, 20, 83]],
1363+
]);
1364+
// Only the two flagged members invalidate, once each.
1365+
assert.deepStrictEqual(stack.invalidated.slice(invalidatedBefore).toSorted(), [18, 20]);
1366+
}).pipe(Effect.provide(fixture.layer));
1367+
}),
1368+
),
1369+
);
1370+
1371+
it.effect("reads past the cache when a sibling flags a member during its summary read", () =>
1372+
Effect.scoped(
1373+
Effect.gen(function* () {
1374+
yield* TestClock.setTime(Date.parse(NOW));
1375+
const stack = makeGrowingStack();
1376+
const synced83 = yield* Deferred.make<void>();
1377+
let holdSummary = false;
1378+
const fixture = yield* makeHarness({
1379+
snapshot: makeSnapshot([makeThread("one", { pullRequests: [makeLink(18)] })]),
1380+
invalidate: stack.invalidate,
1381+
stack: stack.cachedRead,
1382+
// #18's summary stays in flight until #83 has read its stack, flagged #18 and synced.
1383+
summary: (input) =>
1384+
(holdSummary && input.number === 18 ? Deferred.await(synced83) : Effect.void).pipe(
1385+
Effect.as(makeSummary(input, { updatedAt: NOW })),
1386+
),
1387+
onSync: (command) =>
1388+
command.number === 83 ? Deferred.succeed(synced83, undefined) : Effect.void,
1389+
});
1390+
1391+
yield* Effect.gen(function* () {
1392+
const reactor = yield* startAndSweep(fixture);
1393+
yield* applyRecordedSyncs(fixture);
1394+
1395+
// 30 s later a state change refreshes #18, which caches its two-layer read until 90 s.
1396+
yield* TestClock.adjust("30 seconds");
1397+
yield* requestAndApply(fixture, reactor, 18);
1398+
1399+
// #83 is pushed on top and linked as the scheduled sweep at 60 s starts, so that sweep
1400+
// reads #83 for the first time while #18's summary is still in flight.
1401+
stack.grow();
1402+
yield* linkPullRequest83(fixture);
1403+
holdSummary = true;
1404+
const invalidatedBefore = stack.invalidated.length;
1405+
yield* TestClock.adjust("30 seconds");
1406+
yield* Queue.take(fixture.snapshotReads);
1407+
yield* reactor.drain;
1408+
yield* applyRecordedSyncs(fixture);
1409+
1410+
assert.deepStrictEqual(yield* storedStackLayers(fixture), [
1411+
[18, [18, 20, 83]],
1412+
[83, [18, 20, 83]],
1413+
]);
1414+
assert.deepStrictEqual(stack.invalidated.slice(invalidatedBefore), [18]);
1415+
}).pipe(Effect.provide(fixture.layer));
1416+
}),
1417+
),
1418+
);
1419+
1420+
it.effect("re-reads a member a sibling flags while its own stack read is in flight", () =>
1421+
Effect.scoped(
1422+
Effect.gen(function* () {
1423+
yield* TestClock.setTime(Date.parse(NOW));
1424+
const stackOf = (numbers: ReadonlyArray<number>) => ({
1425+
id: "stack-80",
1426+
number: 80,
1427+
url: "https://github.com/owner/repository/stack/80",
1428+
base: "main",
1429+
layers: numbers.map(stackLayer),
1430+
});
1431+
const updated = { updatedAt: "2026-08-28T00:00:00.000Z" };
1432+
const synced83 = yield* Deferred.make<void>();
1433+
let staleReads = 1;
1434+
const fixture = yield* makeHarness({
1435+
// #18 still holds the layers from before #83 was pushed; #83 already has all three.
1436+
snapshot: makeSnapshot([
1437+
makeThread("one", {
1438+
pullRequests: [
1439+
makeLink(18, {}, { stack: { kind: "native", ...stackOf([18, 20]) } }),
1440+
makeLink(83, {}, { stack: { kind: "native", ...stackOf([18, 20, 83]) } }),
1441+
],
1442+
}),
1443+
]),
1444+
summary: (input) => Effect.succeed(makeSummary(input, updated)),
1445+
// #18's first read answers the old two layers, and only after #83 has read its stack,
1446+
// flagged #18 and synced.
1447+
stack: (input) =>
1448+
input.number === 18 && staleReads-- > 0
1449+
? Deferred.await(synced83).pipe(Effect.as(stackOf([18, 20])))
1450+
: Effect.succeed(stackOf([18, 20, 83])),
1451+
onSync: (command) =>
1452+
command.number === 83 ? Deferred.succeed(synced83, undefined) : Effect.void,
1453+
});
1454+
1455+
yield* Effect.gen(function* () {
1456+
const reactor = yield* startAndSweep(fixture);
1457+
yield* applyRecordedSyncs(fixture);
1458+
1459+
// Neither summary changes again, so only #83's flag can make #18 read its stack.
1460+
// #18's stale answer also flags #83, whose fresh read in this sweep would flag #18
1461+
// back for a third sweep. The assertion sees the regression only because #18, the
1462+
// earlier group, finishes before #83 runs; the mocks answer synchronously, so it does.
1463+
// With the flag kept, #18 reads fresh here regardless of that order.
1464+
yield* sweepAgain(fixture, reactor);
1465+
yield* applyRecordedSyncs(fixture);
1466+
1467+
assert.deepStrictEqual(yield* storedStackLayers(fixture), [
1468+
[18, [18, 20, 83]],
1469+
[83, [18, 20, 83]],
1470+
]);
1471+
}).pipe(Effect.provide(fixture.layer));
1472+
}),
1473+
),
1474+
);
1475+
1476+
it.effect("leaves stacks in other repositories alone when their ids match", () =>
1477+
Effect.scoped(
1478+
Effect.gen(function* () {
1479+
yield* TestClock.setTime(Date.parse(NOW));
1480+
// Without a host id the GitHub decoder falls back to the stack number, so two
1481+
// repositories can both report stack "3".
1482+
const otherLink = makeLink(7, null, {
1483+
repository: "owner/other",
1484+
url: "https://github.com/owner/other/pull/7",
1485+
});
1486+
const fixture = yield* makeHarness({
1487+
snapshot: makeSnapshot([makeThread("one", { pullRequests: [makeLink(1), otherLink] })]),
1488+
stack: (input) =>
1489+
Effect.succeed({
1490+
id: "3",
1491+
number: 3,
1492+
url: `https://github.com/${input.repository}/stack/3`,
1493+
base: "main",
1494+
layers: [
1495+
{ number: input.number, headBranch: "layer-1", state: "open" },
1496+
{ number: input.number + 1, headBranch: "layer-2", state: "open" },
1497+
],
1498+
}),
1499+
});
1500+
1501+
yield* Effect.gen(function* () {
1502+
const reactor = yield* startAndSweep(fixture);
1503+
yield* applyRecordedSyncs(fixture);
1504+
assert.strictEqual((yield* Ref.get(fixture.stackCalls)).length, 2);
1505+
1506+
yield* sweepAgain(fixture, reactor);
1507+
yield* sweepAgain(fixture, reactor);
1508+
assert.strictEqual((yield* Ref.get(fixture.stackCalls)).length, 2);
1509+
}).pipe(Effect.provide(fixture.layer));
1510+
}),
1511+
),
1512+
);
1513+
11921514
it.effect("keeps existing snapshots and continues when the host fails", () =>
11931515
Effect.scoped(
11941516
Effect.gen(function* () {

0 commit comments

Comments
 (0)