Skip to content

Commit 86c1fbe

Browse files
committed
fix(server): refresh routed pr readers and preserve open stack work
1 parent 78dd4d7 commit 86c1fbe

5 files changed

Lines changed: 111 additions & 32 deletions

File tree

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
} from "./Services/OrchestrationEngine.ts";
3535
import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts";
3636
import * as PullRequestSyncReactor from "./PullRequestSyncReactor.ts";
37+
import { resolveAutoSettlementAt } from "./ThreadSettlementPolicy.ts";
3738

3839
const NOW = "2026-08-28T12:00:00.000Z";
3940
const PROJECT_ID = ProjectId.make("sync-project");
@@ -155,6 +156,7 @@ function makeSummary(
155156
}
156157

157158
interface HarnessOptions {
159+
readonly onDispatch?: (command: SyncCommand | LinkCommand) => Effect.Effect<void>;
158160
readonly invalidate?: PullRequestService["Service"]["invalidate"];
159161
readonly snapshot: OrchestrationShellSnapshot;
160162
readonly summary?: (
@@ -192,11 +194,13 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options:
192194
const dispatch: OrchestrationEngineShape["dispatch"] = (command) => {
193195
if (command.type === "thread.pull-request-link.sync") {
194196
return Ref.update(syncCommands, (recorded) => [...recorded, command]).pipe(
197+
Effect.andThen(options.onDispatch?.(command) ?? Effect.void),
195198
Effect.as({ sequence: 1 }),
196199
);
197200
}
198201
if (command.type === "thread.pull-request.link") {
199202
return Ref.update(linkCommands, (recorded) => [...recorded, command]).pipe(
203+
Effect.andThen(options.onDispatch?.(command) ?? Effect.void),
200204
Effect.as({ sequence: 1 }),
201205
);
202206
}
@@ -700,20 +704,38 @@ describe("PullRequestSyncReactor", () => {
700704
base: "main",
701705
layers: [
702706
{ number: 41, headBranch: "layer-1", state: "merged" },
703-
{ number: 42, headBranch: "layer-2", state: "open" },
707+
{ number: 42, headBranch: "layer-2", state: "merged" },
704708
{ number: 43, headBranch: "layer-3", state: "open" },
705709
],
706710
};
711+
let thread = makeThread("one", {
712+
pullRequests: [
713+
makeLink(42, { state: "open" }),
714+
makeLink(41, { state: "merged" }, { source: "stack-dismissed" }),
715+
],
716+
});
707717
const fixture = yield* makeHarness({
708-
snapshot: makeSnapshot([
709-
makeThread("one", {
710-
pullRequests: [
711-
makeLink(42),
712-
makeLink(41, { state: "merged" }, { source: "stack-dismissed" }),
713-
],
714-
}),
715-
]),
718+
snapshot: makeSnapshot([thread]),
719+
summary: (input) =>
720+
Effect.succeed(makeSummary(input, { state: "merged", mergedAt: NOW })),
716721
stack: () => Effect.succeed(stack),
722+
onDispatch: (command) =>
723+
Effect.sync(() => {
724+
thread =
725+
command.type === "thread.pull-request.link"
726+
? { ...thread, pullRequests: [...thread.pullRequests, makeLink(command.number)] }
727+
: applySync(makeSnapshot([thread]), [command]).threads[0]!;
728+
// Every projected event may wake settlement, including the terminal root update.
729+
assert.isNull(
730+
resolveAutoSettlementAt({
731+
thread,
732+
pullRequest: null,
733+
now: NOW,
734+
autoSettleAfterDays: null,
735+
autoSettleOnMerge: true,
736+
}),
737+
);
738+
}),
717739
});
718740

719741
yield* Effect.gen(function* () {

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,8 @@ export const make = Effect.gen(function* () {
187187
link.snapshot === null ||
188188
!snapshotFieldsEqual(link.snapshot, fields) ||
189189
!stacksEqual(link.stack, nextStack);
190-
if (changed) {
191-
const uuid = yield* crypto.randomUUIDv4;
192-
yield* engine.dispatch({
193-
type: "thread.pull-request-link.sync",
194-
commandId: CommandId.make(`server:pr-sync:${thread.id}:${uuid}`),
195-
threadId: thread.id,
196-
host: normalizeThreadPullRequestKey(link).host,
197-
repository: link.repository,
198-
number: link.number,
199-
snapshot: { ...fields, syncedAt: nowIso },
200-
stack: nextStack,
201-
});
202-
}
203-
if (fetchedStack === null || fetchedStack.stack === null) return;
204-
for (const layer of fetchedStack.stack.layers) {
190+
// Persist discovered siblings before a terminal snapshot can trigger settlement.
191+
for (const layer of fetchedStack?.stack?.layers ?? []) {
205192
const layerKey = {
206193
host: normalizeThreadPullRequestKey(link).host,
207194
repository: link.repository,
@@ -237,6 +224,19 @@ export const make = Effect.gen(function* () {
237224
),
238225
);
239226
}
227+
if (changed) {
228+
const uuid = yield* crypto.randomUUIDv4;
229+
yield* engine.dispatch({
230+
type: "thread.pull-request-link.sync",
231+
commandId: CommandId.make(`server:pr-sync:${thread.id}:${uuid}`),
232+
threadId: thread.id,
233+
host: normalizeThreadPullRequestKey(link).host,
234+
repository: link.repository,
235+
number: link.number,
236+
snapshot: { ...fields, syncedAt: nowIso },
237+
stack: nextStack,
238+
});
239+
}
240240
});
241241

242242
const syncGroup = Effect.fn("PullRequestSyncReactor.syncGroup")(function* (

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,6 +3319,55 @@ it.effect("close and reopen notify subscribed readers after invalidating their c
33193319
),
33203320
);
33213321

3322+
it.effect("explicit invalidation refreshes origin readers after a routed host mutation", () =>
3323+
Effect.scoped(
3324+
Effect.gen(function* () {
3325+
let state: "open" | "closed" = "open";
3326+
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
3327+
const service = yield* makeService({
3328+
projects: [
3329+
project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" }),
3330+
],
3331+
providers: [
3332+
fakeProvider("github", {
3333+
getChangeRequestSummary: () =>
3334+
Effect.succeed({ ...changeRequest(1, "2026-09-16T00:00:00.000Z"), state }),
3335+
}),
3336+
],
3337+
});
3338+
yield* service.refreshAfterTurn(reference.projectId);
3339+
let revision = Option.getOrThrow(yield* Stream.runHead(service.subscribeRefreshes));
3340+
// The sync reactor invalidates before reading; it must not notify itself again.
3341+
yield* service.invalidate({ reference });
3342+
assert.strictEqual(
3343+
Option.getOrThrow(yield* Stream.runHead(service.subscribeRefreshes)),
3344+
revision,
3345+
);
3346+
assert.strictEqual((yield* service.summary(reference)).state, "open");
3347+
3348+
for (const nextState of ["closed", "open"] as const) {
3349+
const refreshed = yield* service.subscribeRefreshes.pipe(
3350+
Stream.drop(1),
3351+
Stream.take(1),
3352+
Stream.mapEffect((nextRevision) =>
3353+
service
3354+
.summary(reference)
3355+
.pipe(Effect.map((summary) => ({ revision: nextRevision, state: summary.state }))),
3356+
),
3357+
Stream.runHead,
3358+
Effect.forkChild({ startImmediately: true }),
3359+
);
3360+
state = nextState;
3361+
yield* service.invalidate({ reference }, { notifyReaders: true });
3362+
const result = Option.getOrThrow(yield* Fiber.join(refreshed));
3363+
assert.strictEqual(result.state, nextState);
3364+
assert.isAbove(result.revision, revision);
3365+
revision = result.revision;
3366+
}
3367+
}),
3368+
),
3369+
);
3370+
33223371
it.effect("does not cache a failed listing", () =>
33233372
Effect.gen(function* () {
33243373
let hostCalls = 0;

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ export class PullRequestService extends Context.Service<
232232
readonly setLabels: (
233233
input: PullRequestLabelChangeInput,
234234
) => Effect.Effect<void, PullRequestError>;
235-
readonly invalidate: (input: PullRequestInvalidateInput) => Effect.Effect<void>;
235+
readonly invalidate: (
236+
input: PullRequestInvalidateInput,
237+
options?: { readonly notifyReaders?: boolean },
238+
) => Effect.Effect<void>;
236239
}
237240
>()("t3/pullRequest/PullRequestService") {}
238241

@@ -2839,23 +2842,28 @@ export const make = Effect.gen(function* () {
28392842
return { stats: [...held, ...result.stats] };
28402843
});
28412844

2842-
const invalidate: PullRequestService["Service"]["invalidate"] = (input) => {
2845+
const invalidate: PullRequestService["Service"]["invalidate"] = Effect.fn(
2846+
"PullRequestService.invalidate",
2847+
)(function* (input, options) {
28432848
const reference = input.reference;
28442849
if (reference !== undefined) {
2845-
return canonicalRef(reference).pipe(
2850+
yield* canonicalRef(reference).pipe(
28462851
Effect.flatMap((ref) =>
28472852
readCache
28482853
.invalidate(refScope(ref))
28492854
.pipe(Effect.andThen(Effect.sync(() => bumpRefEpoch(ref)))),
28502855
),
28512856
Effect.ignore,
28522857
);
2853-
}
2854-
return Effect.sync(() => {
2858+
} else {
28552859
listingsEpoch = ++epochCounter;
28562860
viewersByHost.clear();
2857-
}).pipe(Effect.andThen(Cache.invalidateAll(viewerFlights)));
2858-
};
2861+
yield* Cache.invalidateAll(viewerFlights);
2862+
}
2863+
if (options?.notifyReaders) {
2864+
yield* SubscriptionRef.set(pullRequestRefreshes, ++epochCounter);
2865+
}
2866+
});
28592867

28602868
const refreshAfterTurn: PullRequestService["Service"]["refreshAfterTurn"] = (projectId) =>
28612869
Effect.suspend(() => {

‎apps/server/src/ws.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2808,7 +2808,7 @@ const makeWsRpcLayer = (
28082808
[WS_METHODS.pullRequestsInvalidate]: (input) =>
28092809
observeRpcEffect(
28102810
WS_METHODS.pullRequestsInvalidate,
2811-
pullRequests.invalidate(input).pipe(
2811+
pullRequests.invalidate(input, { notifyReaders: true }).pipe(
28122812
// A reader asking for fresh host state also wants the thread badges it feeds to
28132813
// catch up, including a merged link the sweep would otherwise never revisit.
28142814
Effect.andThen(

0 commit comments

Comments
 (0)