@devries-cameron20 / t3code-1 / commits / ce2815d665
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): re-read stack layers for every member when the stack grows (#20415)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Victor Vogel committed Oct 20, 2026 at 01:24 UTC ce2815d665acb0dedc028c12109d3b7aebbcc134
2 files changed +373 -4
apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts
+322
@@ -171,6 +171,7 @@ interface HarnessOptions {
171 readonly stack?: (
172 input: PullRequestRef,
173 ) => Effect.Effect<PullRequestStack | null, PullRequestOperationError>;
174 + readonly onSync?: (command: SyncCommand) => Effect.Effect<unknown>;
175 }
176
177 const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options: HarnessOptions) {
@@ -205,6 +206,7 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options:
206 const dispatch: Orchestrator.OrchestratorV2Shape["dispatch"] = (command) => {
207 if (command.type === "thread.pull-request-link.sync") {
208 return Ref.update(syncCommands, (recorded) => [...recorded, command]).pipe(
209 + Effect.andThen(options.onSync?.(command) ?? Effect.void),
210 Effect.as({ sequence: 1, storedEvents: [] }),
211 );
212 }
@@ -383,6 +385,89 @@ function applySync(
385 };
386 }
387
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 +
471 describe("PullRequestSyncReactor", () => {
472 it.effect("retries a failed stack read after the summary becomes terminal", () =>
473 Effect.scoped(
@@ -1189,6 +1274,243 @@ describe("PullRequestSyncReactor", () => {
1274 ),
1275 );
1276
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 +
1514 it.effect("keeps existing snapshots and continues when the host fails", () =>
1515 Effect.scoped(
1516 Effect.gen(function* () {
apps/server/src/orchestration-v2/PullRequestSyncReactor.ts
+51 -4
@@ -128,6 +128,19 @@ function stacksEqual(
128 );
129 }
130
131 +/** Stack ids are unique only within a repository, e.g. GitHub's fallback to the stack number. */
132 +function nativeStackKeyOf(link: ThreadPullRequestLink, stack: ThreadPullRequestStack): string {
133 + const key = normalizeThreadPullRequestKey(link);
134 + return `${key.host}/${key.repository}#stack:${stack.id}`;
135 +}
136 +
137 +function sameLayerNumbers(left: ThreadPullRequestStack, right: ThreadPullRequestStack): boolean {
138 + return (
139 + left.layers.length === right.layers.length &&
140 + left.layers.every((layer, index) => layer.number === right.layers[index]!.number)
141 + );
142 +}
143 +
144 function skipReason(cause: Cause.Cause<unknown>): string {
145 const error = Cause.squash(cause);
146 return error instanceof Error ? error.message : String(error);
@@ -239,6 +252,9 @@ export const make = Effect.gen(function* () {
252 for (const key of requested.keys()) if (!groups.has(key)) requested.delete(key);
253
254 const linkedThisSweep = new Set<string>();
255 + // Fresh stack reads by group, so a member flagged by a sibling is compared against what the
256 + // host answered this sweep, not the saved layers that sibling is about to replace.
257 + const stacksReadThisSweep = new Map<string, ThreadPullRequestStack | null>();
258 const persistence = yield* Semaphore.make(1);
259
260 const syncEntry = Effect.fn("PullRequestSyncReactor.syncEntry")(function* (
@@ -316,9 +332,9 @@ export const make = Effect.gen(function* () {
332 const missingMergedAt = entries.some(
333 (entry) => entry.link.snapshot?.state === "merged" && entry.link.snapshot.mergedAt == null,
334 );
319 - if (refreshRequested || missingMergedAt) {
320 - yield* pullRequests.invalidate({ reference: ref });
321 - }
335 + // A stack retry reads past the cache, or it could get back the layer list it is retrying.
336 + const invalidated = refreshRequested || missingMergedAt || retryStacks.has(key);
337 + if (invalidated) yield* pullRequests.invalidate({ reference: ref });
338 const summary = yield* pullRequests.summary(ref, { recoverTransientFailure: false });
339 const fields = snapshotFieldsOf(summary);
340 const needsStack =
@@ -333,6 +349,14 @@ export const make = Effect.gen(function* () {
349 );
350 const knownUnstacked =
351 summary.stack === null && entries.every((entry) => entry.link.stack === null);
352 + // A sibling can flag this group while its summary read is in flight.
353 + if (!invalidated && retryStacks.has(key) && !knownUnstacked) {
354 + yield* pullRequests.invalidate({ reference: ref });
355 + }
356 + // The retry this read answers is consumed before the read, not after it: a sibling may
357 + // flag this group again while the read is in flight, and that flag must survive to the
358 + // next sweep because the answer below may predate the sibling's layers.
359 + if (needsStack) retryStacks.delete(key);
360 const fetchedStack = !needsStack
361 ? null
362 : knownUnstacked
@@ -358,7 +382,30 @@ export const make = Effect.gen(function* () {
382 retryStacks.add(key);
383 return;
384 }
361 - retryStacks.delete(key);
385 + const fetched = fetchedStack.stack;
386 + stacksReadThisSweep.set(key, fetched);
387 + // A layer pushed above an older member changes none of that member's own fields, so it
388 + // would keep its shorter layer list. Members of this stack whose layer list differs read
389 + // their stack once, in this sweep if still pending or the next; agreeing ones read none.
390 + // Only groups of the same project: another project reads with its own credential and
391 + // may see a different layer list, so flagging across projects would never settle.
392 + if (fetched !== null) {
393 + const fetchedKey = nativeStackKeyOf(first.link, fetched);
394 + for (const [siblingKey, siblingEntries] of groups) {
395 + if (siblingKey === key) continue;
396 + if (siblingEntries[0]!.thread.projectId !== first.thread.projectId) continue;
397 + const readThisSweep = stacksReadThisSweep.get(siblingKey);
398 + const stale = siblingEntries.some(({ link }) => {
399 + const stack = readThisSweep === undefined ? link.stack : readThisSweep;
400 + return (
401 + stack !== null &&
402 + nativeStackKeyOf(link, stack) === fetchedKey &&
403 + !sameLayerNumbers(stack, fetched)
404 + );
405 + });
406 + if (stale) retryStacks.add(siblingKey);
407 + }
408 + }
409 }
410 // The host answered, so the cadence clock ticks even if a dispatch below is rejected.
411 lastSyncedAt.set(key, nowMs);