Skip to content

Commit f3ef0f9

Browse files
StiensWoutclaude
andcommitted
fix(pull-requests): PR detail reads no longer drain the GitHub quota
Every turn told each PR query a client had touched in the last five to ten minutes to read again, whether or not it was still on screen. The server held detail and activity for 15 seconds, so each of those reads, and each client's own focus refresh, went back to GitHub. Merged PRs included. A query now refreshes on the signal only while something reads it. An idle one is marked stale and refreshes once when it is next read. The server shares detail, activity, and preview for 60 seconds, the clients' own stale time, and for 10 minutes once a PR is merged or closed. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 0fcd5f9 commit f3ef0f9

4 files changed

Lines changed: 205 additions & 10 deletions

File tree

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

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,63 @@ it.effect("reuses only unexpired detail for previews", () =>
229229
yield* service.detail(ref);
230230
assert.strictEqual((yield* service.preview(ref)).title, "Change request 1");
231231
assert.strictEqual(previewReads, 0);
232-
yield* TestClock.adjust("16 seconds");
232+
yield* TestClock.adjust("61 seconds");
233233
const preview = yield* service.preview(ref);
234234
assert.strictEqual(preview.title, "Updated title");
235235
assert.strictEqual(preview.state, "closed");
236236
assert.strictEqual(previewReads, 1);
237237
}),
238238
);
239239

240+
it.effect("shares detail and activity for a minute, and for ten once merged", () =>
241+
Effect.gen(function* () {
242+
const reads: Array<string> = [];
243+
const service = yield* makeService({
244+
projects: [project({ id: "p1", title: "web", workspaceRoot: "/w", repository: "acme/web" })],
245+
providers: [
246+
fakeProvider("github", {
247+
getChangeRequest: ({ number }) =>
248+
Effect.sync(() => {
249+
reads.push(`detail #${number}`);
250+
return {
251+
...hostedChangeRequest("Description"),
252+
number,
253+
state: number === 1 ? ("open" as const) : ("merged" as const),
254+
};
255+
}),
256+
getChangeRequestActivity: ({ number }) =>
257+
Effect.sync(() => {
258+
reads.push(`activity #${number}`);
259+
return {
260+
comments: [],
261+
commentCount: 0,
262+
commentsTruncated: false,
263+
reviewThreads: [],
264+
commits: [],
265+
};
266+
}),
267+
}),
268+
],
269+
});
270+
// What every client does on focus or a refresh signal for each panel it shows.
271+
const reread = Effect.forEach([1, 2], (number) => {
272+
const ref = { projectId: "p1" as ProjectId, repository: "acme/web", number };
273+
return Effect.andThen(service.detail(ref), service.activity(ref));
274+
}).pipe(
275+
Effect.andThen(Effect.yieldNow),
276+
Effect.andThen(Effect.sync(() => reads.splice(0).toSorted())),
277+
);
278+
279+
assert.deepStrictEqual(yield* reread, ["activity #1", "activity #2", "detail #1", "detail #2"]);
280+
yield* TestClock.adjust("59 seconds");
281+
assert.deepStrictEqual(yield* reread, []);
282+
yield* TestClock.adjust("2 seconds");
283+
assert.deepStrictEqual(yield* reread, ["activity #1", "detail #1"]);
284+
yield* TestClock.adjust("9 minutes");
285+
assert.deepStrictEqual(yield* reread, ["activity #1", "activity #2", "detail #1", "detail #2"]);
286+
}),
287+
);
288+
240289
it.effect("does not wait for an in-flight detail read to display a preview", () =>
241290
Effect.gen(function* () {
242291
const detailStarted = yield* Deferred.make<void>();
@@ -4121,7 +4170,7 @@ it.effect("reads the fresh diff when detail or summary discovers a changed revis
41214170
assert.strictEqual((yield* service.diff(reference)).patch, "old patch");
41224171
revision = "2026-07-02T00:01:00Z";
41234172
patch = "new patch";
4124-
yield* TestClock.adjust("16 seconds");
4173+
yield* TestClock.adjust("61 seconds");
41254174
yield* service.detail(reference);
41264175
yield* Effect.yieldNow;
41274176
assert.strictEqual((yield* service.detail(reference)).updatedAt, revision);
@@ -4491,7 +4540,7 @@ it.effect("answers a known pull request immediately while the host refreshes", (
44914540
assert.strictEqual(first.body, "cached body");
44924541
assert.strictEqual(first.additions, 4);
44934542

4494-
yield* TestClock.adjust("16 seconds");
4543+
yield* TestClock.adjust("61 seconds");
44954544
const second = yield* service.detail(reference);
44964545
assert.strictEqual(second.body, "cached body");
44974546
assert.strictEqual(second.additions, 4);
@@ -4728,7 +4777,7 @@ it.effect("keeps recent detail on a transient refresh failure but not after inva
47284777
});
47294778

47304779
yield* service.detail(reference);
4731-
yield* TestClock.adjust("16 seconds");
4780+
yield* TestClock.adjust("61 seconds");
47324781
failing = true;
47334782
const strict = yield* Effect.flip(service.detail({ ...reference, allowStale: false }));
47344783
assert.strictEqual(strict._tag, "PullRequestOperationError");

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import {
5959
type PullRequestLabelCandidateList,
6060
type PullRequestLabelChangeInput,
6161
type PullRequestSetFilesViewedInput,
62+
type PullRequestState,
6263
type PullRequestSubmitReviewInput,
6364
PullRequestStack,
6465
PullRequestSummary,
@@ -128,7 +129,15 @@ const REPOSITORY_SEARCH_CHUNK = 100;
128129
* `invalidate` rather than a flag on the read, so an ordinary read can never opt out.
129130
*/
130131
const LIST_CACHE_TTL = Duration.seconds(30);
131-
const DETAIL_CACHE_TTL = Duration.seconds(15);
132+
/**
133+
* Each connected client re-reads the pull request it shows on focus, on a timer, and after every
134+
* turn, so detail, activity, and preview answers are shared for the clients' own minute of
135+
* staleness. A merged or closed change request stops moving on its own and is held for ten.
136+
*/
137+
const DETAIL_CACHE_TTL = Duration.seconds(60);
138+
const SETTLED_DETAIL_CACHE_TTL = Duration.minutes(10);
139+
const detailTimeToLive = (state: PullRequestState | undefined) =>
140+
state === "merged" || state === "closed" ? SETTLED_DETAIL_CACHE_TTL : DETAIL_CACHE_TTL;
132141
const DIFF_CACHE_TTL = Duration.seconds(60);
133142
/** A commit is content-addressed, so its own diff cannot change under its key. */
134143
const COMMIT_DIFF_CACHE_TTL = Duration.minutes(10);
@@ -2880,7 +2889,8 @@ export const make = Effect.gen(function* () {
28802889
},
28812890
{
28822891
capacity: DETAIL_CACHE_CAPACITY,
2883-
timeToLive: (exit) => (Exit.isSuccess(exit) ? DETAIL_CACHE_TTL : Duration.zero),
2892+
timeToLive: (exit) =>
2893+
Exit.isSuccess(exit) ? detailTimeToLive(exit.value.state) : Duration.zero,
28842894
},
28852895
);
28862896
const summaryFromDetail = (
@@ -2941,7 +2951,9 @@ export const make = Effect.gen(function* () {
29412951
},
29422952
{
29432953
capacity: DETAIL_CACHE_CAPACITY,
2944-
timeToLive: (exit) => (Exit.isSuccess(exit) ? DETAIL_CACHE_TTL : Duration.zero),
2954+
// Activity carries no state of its own; the detail or summary read under the same key does.
2955+
timeToLive: (exit, key) =>
2956+
Exit.isSuccess(exit) ? detailTimeToLive(lastGoodSummary.peek(key)?.state) : Duration.zero,
29452957
},
29462958
);
29472959

@@ -2951,7 +2963,8 @@ export const make = Effect.gen(function* () {
29512963
},
29522964
{
29532965
capacity: DETAIL_CACHE_CAPACITY,
2954-
timeToLive: (exit) => (Exit.isSuccess(exit) ? DETAIL_CACHE_TTL : Duration.zero),
2966+
timeToLive: (exit) =>
2967+
Exit.isSuccess(exit) ? detailTimeToLive(exit.value.state) : Duration.zero,
29552968
},
29562969
);
29572970
const preview: PullRequestService["Service"]["preview"] = (input) => {

‎packages/client-runtime/src/state/runtime.test.ts‎

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from "@effect/vitest";
1+
import { describe, expect, it, vi } from "@effect/vitest";
22
import { EnvironmentId } from "@t3tools/contracts";
33
import * as Cause from "effect/Cause";
44
import * as Deferred from "effect/Deferred";
@@ -81,6 +81,10 @@ function queryConnectionState(
8181

8282
const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness")(function* <A, E>(
8383
execute: Effect.Effect<A, E>,
84+
{
85+
refreshTrigger,
86+
idleTtlMs,
87+
}: { readonly refreshTrigger?: Atom.Atom<unknown>; readonly idleTtlMs?: number } = {},
8488
) {
8589
const supervisorState = yield* SubscriptionRef.make(queryConnectionState());
8690
const supervisorSession = yield* SubscriptionRef.make(Option.some(QUERY_RPC_SESSION));
@@ -111,6 +115,8 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
111115
label: "test.environment-query",
112116
staleTimeMs: 60_000,
113117
execute: () => execute,
118+
...(refreshTrigger === undefined ? {} : { refreshTrigger: () => refreshTrigger }),
119+
...(idleTtlMs === undefined ? {} : { idleTtlMs }),
114120
});
115121

116122
return {
@@ -120,6 +126,18 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
120126
};
121127
});
122128

129+
/**
130+
* Reads a query's settled value without leaving it mounted. `getResult` resumes from inside the
131+
* query's listener, so it only unsubscribes once the reader yields.
132+
*/
133+
const readEnvironmentQuery = <A, E>(
134+
registry: AtomRegistry.AtomRegistry,
135+
atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>,
136+
) =>
137+
AtomRegistry.getResult(registry, atom, { suspendOnWaiting: true }).pipe(
138+
Effect.tap(() => Effect.yieldNow),
139+
);
140+
123141
const mountEnvironmentQuery = Effect.fn("TestEnvironmentQuery.mount")(function* <A, E>(
124142
atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>,
125143
) {
@@ -548,6 +566,92 @@ describe("environment query lifecycle", () => {
548566
}),
549567
),
550568
);
569+
570+
it.effect("refreshes on its trigger while read, and once on its next read when idle", () =>
571+
Effect.scoped(
572+
Effect.gen(function* () {
573+
let executions = 0;
574+
const trigger = Atom.make(0).pipe(Atom.keepAlive);
575+
const harness = yield* makeEnvironmentQueryHarness(
576+
Effect.sync(() => (executions += 1)),
577+
{ refreshTrigger: trigger },
578+
);
579+
const registry = AtomRegistry.make();
580+
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
581+
const read = readEnvironmentQuery(registry, harness.atom);
582+
583+
const unmount = registry.mount(harness.atom);
584+
expect(yield* read).toBe(1);
585+
registry.set(trigger, 1);
586+
expect(yield* read).toBe(2);
587+
588+
// Unmounted, the query stays alive on its idle TTL, but nothing reads it.
589+
unmount();
590+
registry.set(trigger, 2);
591+
registry.set(trigger, 3);
592+
expect(executions).toBe(2);
593+
594+
expect(yield* read).toBe(3);
595+
}),
596+
),
597+
);
598+
599+
it.effect("keeps an idle query when its trigger recomputes to the same value", () =>
600+
Effect.scoped(
601+
Effect.gen(function* () {
602+
let executions = 0;
603+
const settings = Atom.make({ theme: "dark" }).pipe(Atom.keepAlive);
604+
const trigger = Atom.make((get) => JSON.stringify([get(settings) !== null]));
605+
const harness = yield* makeEnvironmentQueryHarness(
606+
Effect.sync(() => (executions += 1)),
607+
{ refreshTrigger: trigger },
608+
);
609+
const registry = AtomRegistry.make();
610+
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
611+
const read = readEnvironmentQuery(registry, harness.atom);
612+
613+
const unmount = registry.mount(harness.atom);
614+
expect(yield* read).toBe(1);
615+
unmount();
616+
registry.set(settings, { theme: "light" });
617+
expect(yield* read).toBe(1);
618+
expect(executions).toBe(1);
619+
}),
620+
),
621+
);
622+
623+
it.effect("refreshes an idle query the registry swept and rebuilt after its trigger moved", () =>
624+
Effect.scoped(
625+
Effect.gen(function* () {
626+
// Only the registry's idle-TTL sweeps run on these; effects keep their own scheduler.
627+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] });
628+
yield* Effect.addFinalizer(() => Effect.sync(() => vi.useRealTimers()));
629+
let executions = 0;
630+
const trigger = Atom.make(0).pipe(Atom.keepAlive);
631+
const harness = yield* makeEnvironmentQueryHarness(
632+
Effect.sync(() => (executions += 1)),
633+
{ refreshTrigger: trigger, idleTtlMs: 1_000 },
634+
);
635+
const registry = AtomRegistry.make({ timeoutResolution: 100 });
636+
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
637+
const read = readEnvironmentQuery(registry, harness.atom);
638+
639+
const unmount = registry.mount(harness.atom);
640+
expect(yield* read).toBe(1);
641+
unmount();
642+
// Lets the registry start the query node's idle TTL.
643+
yield* Effect.yieldNow;
644+
vi.advanceTimersByTime(500);
645+
registry.set(trigger, 1);
646+
yield* Effect.yieldNow;
647+
// Sweeps the query node but not the data it read, whose idle TTL started with the trigger.
648+
vi.advanceTimersByTime(700);
649+
expect(executions).toBe(1);
650+
651+
expect(yield* read).toBe(2);
652+
}),
653+
),
654+
);
551655
});
552656

553657
describe("Atom.fn mutation semantics", () => {

‎packages/client-runtime/src/state/runtime.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,35 @@ export function followStreamInEnvironment<A, E, R>(
483483
);
484484
}
485485

486+
/**
487+
* Refreshes a query when `signal` changes, but only while something reads it.
488+
* `Atom.makeRefreshOnSignal` subscribes to the signal, so a query that outlives its view on an
489+
* idle TTL still re-runs on every change. Reading the signal as a dependency instead lets the
490+
* registry re-run a mounted query right away and only mark an idle one stale, which then
491+
* refreshes on its next read.
492+
*/
493+
const refreshOnSignalWhileRead =
494+
(signal: Atom.Atom<unknown>) =>
495+
<A>(self: Atom.Atom<A>): Atom.Atom<A> => {
496+
// The signal value each registry's data was read under. Held outside the node because the
497+
// registry can sweep this node while `self` keeps the data, and the rebuilt node must still
498+
// see a change it missed.
499+
const readUnder = new WeakMap<AtomRegistry.AtomRegistry, unknown>();
500+
return Atom.transform(
501+
self,
502+
(get) => {
503+
const current = get(signal);
504+
get.subscribe(self, (value) => get.setSelf(value));
505+
const changed =
506+
readUnder.has(get.registry) && !Object.is(readUnder.get(get.registry), current);
507+
readUnder.set(get.registry, current);
508+
if (changed) get.refresh(self);
509+
return get.once(self);
510+
},
511+
{ initialValueTarget: self },
512+
);
513+
};
514+
486515
export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
487516
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, ER>,
488517
options: EnvironmentQueryAtomOptions<
@@ -566,7 +595,7 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
566595
return (
567596
refreshTrigger === undefined
568597
? intervalQuery
569-
: intervalQuery.pipe(Atom.makeRefreshOnSignal(refreshTrigger))
598+
: intervalQuery.pipe(refreshOnSignalWhileRead(refreshTrigger))
570599
).pipe(Atom.setIdleTTL(idleTtlMs), Atom.withLabel(`${options.label}:${key}`));
571600
});
572601
return (target) => family(environmentRpcKey(target));

0 commit comments

Comments
 (0)