Skip to content

Commit 5cc369b

Browse files
authored
fix(pull-requests): refresh data after thread turns (pingdotgg#9496)
Co-authored-by: maria-rcks <[email protected]>
1 parent f559fe0 commit 5cc369b

15 files changed

Lines changed: 229 additions & 21 deletions

File tree

‎apps/server/integration/OrchestrationEngineHarness.integration.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import { VcsStatusBroadcaster } from "../src/vcs/VcsStatusBroadcaster.ts";
8686
import { GitWorkflowService } from "../src/git/GitWorkflowService.ts";
8787
import * as VcsProcess from "../src/vcs/VcsProcess.ts";
8888
import * as AgentAwarenessRelay from "../src/relay/AgentAwarenessRelay.ts";
89+
import * as PullRequestService from "../src/pullRequest/PullRequestService.ts";
8990

9091
const decodeCodexSettings = Schema.decodeEffect(CodexSettings);
9192

@@ -350,6 +351,11 @@ export const makeOrchestrationIntegrationHarness = (
350351
);
351352
const checkpointReactorLayer = CheckpointReactorLive.pipe(
352353
Layer.provideMerge(runtimeServicesLayer),
354+
Layer.provideMerge(
355+
Layer.mock(PullRequestService.PullRequestService)({
356+
refreshAfterTurn: Effect.void,
357+
}),
358+
),
353359
Layer.provideMerge(
354360
Layer.succeed(VcsStatusBroadcaster, {
355361
getStatus: () => Effect.die("getStatus should not be called in this test"),

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export const RPC_REQUIRED_SCOPES = {
8282
// Read scope like the reads it un-caches: refreshing is part of reading, and a read-only
8383
// client pressing refresh must not be told it may not look again.
8484
[WS_METHODS.pullRequestsInvalidate]: AuthOrchestrationReadScope,
85+
[WS_METHODS.pullRequestsSubscribeRefreshes]: AuthOrchestrationReadScope,
8586
// The candidate list is a read like the detail beside it; asking somebody for a review is a
8687
// write like every other one.
8788
[WS_METHODS.pullRequestsReviewerCandidates]: AuthOrchestrationReadScope,

‎apps/server/src/orchestration/Layers/CheckpointReactor.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import { ProviderValidationError } from "../../provider/Errors.ts";
6262
import { ServerConfig } from "../../config.ts";
6363
import * as WorkspaceEntries from "../../workspace/WorkspaceEntries.ts";
6464
import * as WorkspacePaths from "../../workspace/WorkspacePaths.ts";
65+
import { PullRequestService } from "../../pullRequest/PullRequestService.ts";
6566

6667
const asProjectId = (value: string): ProjectId => ProjectId.make(value);
6768
const asTurnId = (value: string): TurnId => TurnId.make(value);
@@ -325,6 +326,8 @@ describe("CheckpointReactor", () => {
325326
const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
326327
prefix: "t3-checkpoint-reactor-test-",
327328
});
329+
const pullRequestRefreshes: number[] = [];
330+
const refreshAfterTurn = Effect.sync(() => void pullRequestRefreshes.push(1));
328331
const vcsStatusBroadcasterLayer = Layer.succeed(VcsStatusBroadcaster, {
329332
getStatus: () => Effect.die("getStatus should not be called in this test"),
330333
refreshLocalStatus: (cwd: string) =>
@@ -355,6 +358,7 @@ describe("CheckpointReactor", () => {
355358
Layer.provideMerge(projectionSnapshotLayer),
356359
Layer.provideMerge(RuntimeReceiptBusLive),
357360
Layer.provideMerge(Layer.succeed(ProviderService, provider.service)),
361+
Layer.provideMerge(Layer.mock(PullRequestService)({ refreshAfterTurn })),
358362
Layer.provideMerge(vcsStatusBroadcasterLayer),
359363
Layer.provideMerge(CheckpointStore.layer.pipe(Layer.provide(VcsDriverRegistry.layer))),
360364
Layer.provideMerge(
@@ -466,6 +470,7 @@ describe("CheckpointReactor", () => {
466470
provider,
467471
cwd,
468472
drain,
473+
pullRequestRefreshes,
469474
};
470475
}
471476

@@ -766,6 +771,14 @@ describe("CheckpointReactor", () => {
766771

767772
NodeFS.writeFileSync(NodePath.join(harness.cwd, "README.md"), "v2\n", "utf8");
768773

774+
harness.provider.emit({
775+
type: "turn.started",
776+
eventId: EventId.make("evt-turn-started-aux"),
777+
provider: ProviderDriverKind.make("codex"),
778+
createdAt,
779+
threadId: ThreadId.make("thread-1"),
780+
turnId: asTurnId("turn-aux"),
781+
});
769782
harness.provider.emit({
770783
type: "turn.completed",
771784
eventId: EventId.make("evt-turn-completed-aux"),
@@ -782,6 +795,7 @@ describe("CheckpointReactor", () => {
782795
const midThread = midReadModel.threads.find((entry) => entry.id === ThreadId.make("thread-1"));
783796
expect(midThread?.checkpoints).toHaveLength(0);
784797
expect(pullRequestRefreshCalls).toEqual([]);
798+
expect(harness.pullRequestRefreshes).toEqual([]);
785799

786800
harness.provider.emit({
787801
type: "turn.completed",
@@ -801,6 +815,7 @@ describe("CheckpointReactor", () => {
801815
expect(thread.checkpoints[0]?.checkpointTurnCount).toBe(1);
802816
await harness.drain();
803817
expect(pullRequestRefreshCalls).toEqual([harness.cwd]);
818+
expect(harness.pullRequestRefreshes).toEqual([1]);
804819
});
805820

806821
it("captures pre-turn and completion checkpoints for claude runtime events", async () => {

‎apps/server/src/orchestration/Layers/CheckpointReactor.ts‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import type { OrchestrationDispatchError } from "../Errors.ts";
3838
import { isGitRepository } from "../../git/Utils.ts";
3939
import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts";
4040
import * as WorkspaceEntries from "../../workspace/WorkspaceEntries.ts";
41+
import * as PullRequestService from "../../pullRequest/PullRequestService.ts";
4142

4243
const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
4344

@@ -88,6 +89,9 @@ const make = Effect.gen(function* () {
8889
const receiptBus = yield* RuntimeReceiptBus;
8990
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
9091
const vcsStatusBroadcaster = yield* VcsStatusBroadcaster;
92+
const pullRequests = yield* PullRequestService.PullRequestService;
93+
const startedTurns = new Map<ThreadId, TurnId>();
94+
const pending = new Set<ThreadId>();
9195

9296
const appendRevertFailureActivity = (input: {
9397
readonly threadId: ThreadId;
@@ -854,6 +858,7 @@ const make = Effect.gen(function* () {
854858

855859
const processDomainEvent = Effect.fn("processDomainEvent")(function* (event: OrchestrationEvent) {
856860
if (event.type === "thread.turn-start-requested" || event.type === "thread.message-sent") {
861+
if (event.type === "thread.turn-start-requested") pending.add(event.payload.threadId);
857862
yield* ensurePreTurnBaselineFromDomainTurnStart(event);
858863
return;
859864
}
@@ -897,14 +902,46 @@ const make = Effect.gen(function* () {
897902
const processRuntimeEvent = Effect.fn("processRuntimeEvent")(function* (
898903
event: ProviderRuntimeEvent,
899904
) {
905+
if (event.type === "session.exited") {
906+
startedTurns.delete(event.threadId);
907+
pending.delete(event.threadId);
908+
return;
909+
}
910+
900911
if (event.type === "turn.started") {
912+
const turnId = toTurnId(event.turnId);
913+
const activeTurnId = (yield* providerService.listSessions()).find((session) =>
914+
sameId(session.threadId, event.threadId),
915+
)?.activeTurnId;
916+
const mayReplace = pending.has(event.threadId) && sameId(activeTurnId, turnId);
917+
if (turnId !== null && (!startedTurns.has(event.threadId) || mayReplace)) {
918+
startedTurns.set(event.threadId, turnId);
919+
pending.delete(event.threadId);
920+
}
901921
yield* ensurePreTurnBaselineFromTurnStart(event);
902922
return;
903923
}
904924

905-
if (event.type === "turn.completed") {
925+
if (event.type === "turn.completed" || event.type === "turn.aborted") {
906926
const turnId = toTurnId(event.turnId);
907-
yield* refreshLocalGitStatusFromTurnCompletion(event);
927+
const thread = yield* resolveThreadDetail(event.threadId);
928+
const startedTurnId = startedTurns.get(event.threadId);
929+
const isTrackedTurn = sameId(startedTurnId, turnId);
930+
if (isTrackedTurn) startedTurns.delete(event.threadId);
931+
if (event.type === "turn.completed") {
932+
yield* refreshLocalGitStatusFromTurnCompletion(event);
933+
}
934+
if (
935+
turnId !== null &&
936+
thread !== undefined &&
937+
(isTrackedTurn ||
938+
sameId(thread.session?.activeTurnId, turnId) ||
939+
(startedTurnId === undefined && !thread.session?.activeTurnId))
940+
) {
941+
pending.delete(event.threadId);
942+
yield* pullRequests.refreshAfterTurn;
943+
}
944+
if (event.type === "turn.aborted") return;
908945
yield* captureCheckpointFromTurnCompletion(event).pipe(
909946
Effect.catch((error) =>
910947
Effect.flatMap(nowIso, (createdAt) =>
@@ -963,7 +1000,12 @@ const make = Effect.gen(function* () {
9631000

9641001
yield* forkParked(
9651002
Stream.runForEach(providerService.streamEvents, (event) => {
966-
if (event.type !== "turn.started" && event.type !== "turn.completed") {
1003+
if (
1004+
event.type !== "turn.started" &&
1005+
event.type !== "turn.completed" &&
1006+
event.type !== "turn.aborted" &&
1007+
event.type !== "session.exited"
1008+
) {
9671009
return Effect.void;
9681010
}
9691011
return worker.enqueue({ source: "runtime", event });

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,10 +2646,11 @@ it.effect("a listing narrowed to some projects is its own cache entry", () =>
26462646
}),
26472647
);
26482648

2649-
it.effect("an explicit invalidation makes the next listing ask the host again", () =>
2649+
it.effect("explicit and turn invalidations make the next listing ask the host again", () =>
26502650
Effect.gen(function* () {
26512651
let hostCalls = 0;
26522652
let viewerCalls = 0;
2653+
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
26532654
const service = yield* makeService({
26542655
projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })],
26552656
providers: [
@@ -2673,11 +2674,14 @@ it.effect("an explicit invalidation makes the next listing ask the host again",
26732674
assert.strictEqual(viewerCalls, 2);
26742675

26752676
// Forgetting one change request leaves the listings shared.
2676-
yield* service.invalidate({
2677-
reference: { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 },
2678-
});
2677+
yield* service.invalidate({ reference });
26792678
yield* service.list({ state: "open" });
26802679
assert.strictEqual(hostCalls, 2);
2680+
yield* service.refreshAfterTurn;
2681+
const refresh = Option.getOrThrow(yield* Stream.runHead(service.subscribeRefreshes));
2682+
yield* service.list({ state: "open" });
2683+
assert.isAbove(refresh, 0);
2684+
assert.strictEqual(hostCalls, 3);
26812685
}),
26822686
);
26832687

@@ -3900,7 +3904,7 @@ it.effect("refuses a remark rewritten into nothing but whitespace", () =>
39003904
}),
39013905
);
39023906

3903-
it.effect("forgets the cached detail after a rewrite, like the other mutations", () =>
3907+
it.effect("forgets the cached detail after a rewrite or terminal turn", () =>
39043908
Effect.gen(function* () {
39053909
let coreCalls = 0;
39063910
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
@@ -3935,8 +3939,11 @@ it.effect("forgets the cached detail after a rewrite, like the other mutations",
39353939
yield* service.detail(reference);
39363940
yield* service.update({ ...reference, title: "Renamed" });
39373941
yield* service.detail(reference);
3938-
39393942
assert.strictEqual(coreCalls, 2);
3943+
3944+
yield* service.refreshAfterTurn;
3945+
yield* service.detail(reference);
3946+
assert.strictEqual(coreCalls, 3);
39403947
}),
39413948
);
39423949

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as PubSub from "effect/PubSub";
1010
import * as Schema from "effect/Schema";
1111
import type * as Scope from "effect/Scope";
1212
import * as Stream from "effect/Stream";
13+
import * as SubscriptionRef from "effect/SubscriptionRef";
1314
import {
1415
PullRequestOperationError,
1516
PullRequestUnavailableError,
@@ -148,6 +149,8 @@ export class PullRequestService extends Context.Service<
148149
never,
149150
Scope.Scope
150151
>;
152+
readonly subscribeRefreshes: Stream.Stream<number>;
153+
readonly refreshAfterTurn: Effect.Effect<void>;
151154
readonly detail: (input: PullRequestRef) => Effect.Effect<PullRequestDetail, PullRequestError>;
152155
readonly activity: (
153156
input: PullRequestRef,
@@ -531,6 +534,7 @@ export function repositoryIdentityOf(project: OrchestrationProjectShell): string
531534

532535
export const make = Effect.gen(function* () {
533536
const mergedPullRequests = yield* PubSub.sliding<PullRequestMergeEvent>(64);
537+
const pullRequestRefreshes = yield* SubscriptionRef.make(0);
534538
const registry = yield* PullRequestProviderRegistry;
535539
const projections = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
536540
const sourceControlProviders = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
@@ -2123,10 +2127,12 @@ export const make = Effect.gen(function* () {
21232127
// scope re-entering `refEpochs` after eviction can never mint a key an old entry still has.
21242128
let epochCounter = 0;
21252129
let listingsEpoch = 0;
2130+
let turnRefreshEpoch = 0;
21262131
const refEpochs = new Map<string, number>();
21272132
const REF_EPOCH_CAPACITY = 2_048;
21282133
const refScope = (ref: PullRequestRef) => `${ref.projectId} ${ref.repository} ${ref.number}`;
2129-
const refEpoch = (ref: PullRequestRef) => refEpochs.get(refScope(ref)) ?? 0;
2134+
const refEpoch = (ref: PullRequestRef) =>
2135+
Math.max(turnRefreshEpoch, refEpochs.get(refScope(ref)) ?? 0);
21302136
const refCacheKey = (ref: PullRequestRef) =>
21312137
JSON.stringify([refEpoch(ref), ref.projectId, ref.repository, ref.number]);
21322138
const bumpRefEpoch = (ref: PullRequestRef) => {
@@ -2396,6 +2402,11 @@ export const make = Effect.gen(function* () {
23962402
}).pipe(Effect.andThen(Cache.invalidateAll(viewerFlights)));
23972403
};
23982404

2405+
const refreshAfterTurn: PullRequestService["Service"]["refreshAfterTurn"] = Effect.suspend(() => {
2406+
turnRefreshEpoch = listingsEpoch = ++epochCounter;
2407+
return SubscriptionRef.set(pullRequestRefreshes, turnRefreshEpoch);
2408+
});
2409+
23992410
// A mutation's own client re-reads right after it, and every other client's next read must
24002411
// see the action too — so a write forgets the change request it touched and the listings its
24012412
// state change reorders, for everyone, without any client asking.
@@ -2435,6 +2446,10 @@ export const make = Effect.gen(function* () {
24352446
subscribeMerges: PubSub.subscribe(mergedPullRequests).pipe(
24362447
Effect.map((subscription) => Stream.fromSubscription(subscription)),
24372448
),
2449+
subscribeRefreshes: SubscriptionRef.changes(pullRequestRefreshes).pipe(
2450+
Stream.filter((revision) => revision > 0),
2451+
),
2452+
refreshAfterTurn,
24382453
detail,
24392454
activity,
24402455
threadComments,

‎apps/server/src/ws.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,12 @@ const makeWsRpcLayer = (
20962096
observeRpcEffect(WS_METHODS.pullRequestsInvalidate, pullRequests.invalidate(input), {
20972097
"rpc.aggregate": "pull-requests",
20982098
}),
2099+
[WS_METHODS.pullRequestsSubscribeRefreshes]: () =>
2100+
observeRpcStream(
2101+
WS_METHODS.pullRequestsSubscribeRefreshes,
2102+
pullRequests.subscribeRefreshes,
2103+
{ "rpc.aggregate": "pull-requests" },
2104+
),
20992105
[WS_METHODS.pullRequestsReviewerCandidates]: (input) =>
21002106
observeRpcEffect(
21012107
WS_METHODS.pullRequestsReviewerCandidates,

‎apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ import { useProjects } from "~/state/entities";
6464
import { useEnvironments } from "~/state/environments";
6565
import { useEnvironmentQuery } from "~/state/query";
6666
import { useLiveRefresh } from "~/hooks/useLiveRefresh";
67-
import { pullRequestEnvironment, useSharedPullRequestSummary } from "~/state/pullRequests";
67+
import {
68+
pullRequestEnvironment,
69+
usePullRequestTurnRefresh,
70+
useSharedPullRequestSummary,
71+
} from "~/state/pullRequests";
6872
import { useAtomCommand } from "~/state/use-atom-command";
6973
import { vcsEnvironment } from "~/state/vcs";
7074
import { formatRelativeTimeLabel } from "~/timestampFormat";
@@ -577,6 +581,7 @@ export function PullRequestDetailPanel({
577581
const activityQuery = useEnvironmentQuery(
578582
pullRequestEnvironment.activity({ environmentId, input: reference }),
579583
);
584+
const turnRefresh = usePullRequestTurnRefresh(environmentId);
580585
const [cachedDetail, setCachedDetail] = useState(() =>
581586
readPullRequestDetailSnapshot(
582587
typeof window === "undefined" ? undefined : window.localStorage,
@@ -675,6 +680,8 @@ export function PullRequestDetailPanel({
675680
detailQuery.refresh();
676681
activityQuery.refresh();
677682
}, [activityQuery.refresh, detailQuery.refresh]);
683+
const [refreshToken, setRefreshToken] = useState(0);
684+
const codeRefreshToken = refreshToken + (turnRefresh ?? 0);
678685
const activityRevision = useRef<{ readonly key: string; readonly updatedAt: string } | null>(
679686
null,
680687
);
@@ -698,15 +705,18 @@ export function PullRequestDetailPanel({
698705
// revision effect above reads it only after this same pull request reports a change. Keyed by
699706
// the pull request rather than by the panel, because this one panel shows a different pull
700707
// request every time it is opened.
701-
useLiveRefresh(detailQuery.refresh, {
702-
key: `pull-request:${reference.projectId}:${reference.repository}#${reference.number}`,
703-
});
708+
useLiveRefresh(
709+
() => {
710+
detailQuery.refresh();
711+
setRefreshToken((token) => token + 1);
712+
},
713+
{ key: `pull-request:${reference.projectId}:${reference.repository}#${reference.number}` },
714+
);
704715
// The button, on the other hand, goes around the server's cache rather than through it: it is
705716
// the answer for a reader who can see that what they are looking at is behind. The
706717
// invalidation goes first so the re-reads miss that cache; if it fails, the reads still run
707718
// and at worst answer from it.
708719
const invalidate = useAtomCommand(pullRequestEnvironment.invalidate, { reportFailure: false });
709-
const [refreshToken, setRefreshToken] = useState(0);
710720
const refreshFromHost = useCallback(async () => {
711721
await invalidate({ environmentId, input: { reference } });
712722
refreshDetail();
@@ -2352,7 +2362,7 @@ export function PullRequestDetailPanel({
23522362
fixFindingLabel={handoffLabels.fixFinding}
23532363
onFixFinding={startFixFinding}
23542364
onRefresh={refreshDetail}
2355-
refreshToken={refreshToken}
2365+
refreshToken={codeRefreshToken}
23562366
/>
23572367
</Suspense>
23582368
</div>

0 commit comments

Comments
 (0)