Skip to content

Commit f619a97

Browse files
committed
fix(client-runtime): share ref refresh freshness across list keys
1 parent b3d3eab commit f619a97

2 files changed

Lines changed: 45 additions & 10 deletions

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,40 @@ describe("cached VCS refs", () => {
209209
invalidateVcsRefs(registry, TARGET);
210210

211211
expect(yield* Queue.take(requests)).toEqual({ cwd: "/repo", limit: 100, refresh: true });
212+
213+
yield* Effect.acquireRelease(
214+
Effect.sync(() =>
215+
registry.mount(
216+
atoms.listRefs({
217+
environmentId: TARGET.environmentId,
218+
input: { cwd: "/other-repo", limit: 100 },
219+
}),
220+
),
221+
),
222+
(unmount) => Effect.sync(unmount),
223+
);
224+
expect(yield* Queue.take(requests)).toEqual({
225+
cwd: "/other-repo",
226+
limit: 100,
227+
refresh: true,
228+
});
229+
230+
yield* Effect.acquireRelease(
231+
Effect.sync(() =>
232+
registry.mount(
233+
atoms.listRefs({
234+
environmentId: TARGET.environmentId,
235+
input: { cwd: "/other-repo", query: "release", limit: 100 },
236+
}),
237+
),
238+
),
239+
(unmount) => Effect.sync(unmount),
240+
);
241+
expect(yield* Queue.take(requests)).toEqual({
242+
cwd: "/other-repo",
243+
query: "release",
244+
limit: 100,
245+
});
212246
}),
213247
),
214248
);

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import {
3333
withVcsRefsPersistenceLock,
3434
} from "./vcsRefInvalidation.ts";
3535

36+
// Share freshness across search and pagination keys, but keep it local to this registry.
37+
const serverRefsRevisionAtom = Atom.family((_key: string) => Atom.make(0).pipe(Atom.keepAlive));
38+
3639
const OFFLINE_BRANCH_LIST_LIMIT = 100;
3740
const VCS_REFS_IDLE_TTL_MS = 30_000;
3841
const VCS_STATUS_IDLE_TTL_MS = 10_000;
@@ -122,7 +125,6 @@ export const makeCachedVcsRefsChanges = Effect.fn("CachedVcsRefsState.makeChange
122125
expectedRevision?: number,
123126
registry?: AtomRegistry.AtomRegistry,
124127
persistedCacheReadable = true,
125-
bypassServerSnapshot = false,
126128
) {
127129
const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
128130
const cache = yield* Persistence.EnvironmentCacheStore;
@@ -144,6 +146,13 @@ export const makeCachedVcsRefsChanges = Effect.fn("CachedVcsRefsState.makeChange
144146
)
145147
: Option.none<VcsListRefsResult>();
146148
const refresh = Effect.fn("CachedVcsRefsState.refresh")(function* () {
149+
const revisionAtom = serverRefsRevisionAtom(
150+
`${environmentId.length}:${environmentId}${input.cwd}`,
151+
);
152+
const bypassServerSnapshot =
153+
expectedRevision !== undefined &&
154+
registry !== undefined &&
155+
expectedRevision > registry.get(revisionAtom);
147156
const refs = yield* request(
148157
WS_METHODS.vcsListRefs,
149158
bypassServerSnapshot ? { ...input, refresh: true } : input,
@@ -170,6 +179,7 @@ export const makeCachedVcsRefsChanges = Effect.fn("CachedVcsRefsState.makeChange
170179
expectedRevision,
171180
persist: useCache,
172181
});
182+
if (committed && bypassServerSnapshot) registry.set(revisionAtom, expectedRevision);
173183
return committed ? Option.some(refs) : Option.none<VcsListRefsResult>();
174184
});
175185

@@ -221,7 +231,6 @@ function cachedVcsRefsChanges(
221231
input: VcsListRefsInput,
222232
expectedRevision: number,
223233
persistedCacheReadable: boolean,
224-
bypassServerSnapshot: boolean,
225234
) {
226235
return followStreamInEnvironment(
227236
environmentId,
@@ -233,7 +242,6 @@ function cachedVcsRefsChanges(
233242
expectedRevision,
234243
registry,
235244
persistedCacheReadable,
236-
bypassServerSnapshot,
237245
);
238246
}),
239247
),
@@ -250,21 +258,14 @@ export function createVcsEnvironmentAtoms<R, E>(
250258
*/
251259
const listRefsFamily = Atom.family((key: string) => {
252260
const [environmentId, input] = JSON.parse(key) as [EnvironmentId, VcsListRefsInput];
253-
let builtRevision: number | undefined;
254261
return runtime
255262
.atom((get) => {
256263
const state = get(vcsRefsCacheStateAtom({ environmentId }));
257-
// The server snapshots refs for minutes and only drops them for its own Git
258-
// writes; an invalidation also covers terminal and agent changes, so skip it.
259-
const bypassServerSnapshot =
260-
builtRevision !== undefined && builtRevision !== state.revision;
261-
builtRevision = state.revision;
262264
return cachedVcsRefsChanges(
263265
environmentId,
264266
input,
265267
state.revision,
266268
state.persistedCacheReadable,
267-
bypassServerSnapshot,
268269
);
269270
})
270271
.pipe(

0 commit comments

Comments
 (0)