Skip to content

Commit 4b7a347

Browse files
only21milcodexmaria-rcks
authored
fix(composer): retry expired catalogs after failed discovery (pingdotgg#23174)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent c9d3f7c commit 4b7a347

2 files changed

Lines changed: 73 additions & 5 deletions

File tree

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,63 @@ describe("provider workspace snapshot refresh atom", () => {
199199
unmount();
200200
});
201201

202+
it.each(["already expired", "expires during discovery"])(
203+
"retries an unchanged catalog that %s after the short cooldown",
204+
async (expiry) => {
205+
const start = Date.parse("2026-01-01T00:05:00.000Z");
206+
vi.useFakeTimers({ now: expiry === "expires during discovery" ? start - 1 : start });
207+
const stale = withSnapshot("2026-01-01T00:00:00.000Z");
208+
const { registry, atom, responses, run, unmount } = setup(
209+
expiry === "expires during discovery"
210+
? withSnapshot("2026-01-01T00:00:00.000Z", true)
211+
: stale,
212+
[
213+
() => {
214+
if (expiry === "expires during discovery") vi.setSystemTime(start + 1);
215+
return stale;
216+
},
217+
],
218+
);
219+
await vi.advanceTimersByTimeAsync(0);
220+
await vi.advanceTimersByTimeAsync(9_999);
221+
registry.refresh(atom);
222+
expect(run).toHaveBeenCalledTimes(1);
223+
responses.push(() => withSnapshot("2026-01-01T00:05:10.000Z"));
224+
await vi.advanceTimersByTimeAsync(1);
225+
registry.refresh(atom);
226+
expect(run).toHaveBeenCalledTimes(2);
227+
unmount();
228+
},
229+
);
230+
231+
it("keeps the TTL guard for a newly scanned catalog when the client clock is ahead", async () => {
232+
vi.useFakeTimers({ now: Date.parse("2026-01-01T01:00:00.000Z") });
233+
const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"), [
234+
() => withSnapshot("2026-01-01T00:01:00.000Z"),
235+
]);
236+
await vi.advanceTimersByTimeAsync(10_000);
237+
registry.refresh(atom);
238+
expect(run).toHaveBeenCalledTimes(1);
239+
unmount();
240+
});
241+
242+
it("bounds retries when discovery repeatedly returns the same expired catalog", async () => {
243+
vi.useFakeTimers({ now: Date.parse("2026-01-01T00:05:00.000Z") });
244+
const stale = withSnapshot("2026-01-01T00:00:00.000Z");
245+
const { registry, atom, run, unmount } = setup(stale, [() => stale, () => stale, () => stale]);
246+
await vi.advanceTimersByTimeAsync(0);
247+
await vi.advanceTimersByTimeAsync(10_000);
248+
registry.refresh(atom);
249+
await vi.advanceTimersByTimeAsync(0);
250+
await vi.advanceTimersByTimeAsync(10_000);
251+
registry.refresh(atom);
252+
await vi.advanceTimersByTimeAsync(0);
253+
await vi.advanceTimersByTimeAsync(10_000);
254+
registry.refresh(atom);
255+
expect(run).toHaveBeenCalledTimes(3);
256+
unmount();
257+
});
258+
202259
it("does not rescan a current snapshot, and rescans once it ages past the TTL", () => {
203260
vi.useFakeTimers({ now: Date.parse("2026-01-01T00:01:00.000Z") });
204261
const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"));

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ interface ProviderWorkspaceSnapshotRefreshState {
144144
readonly requestedAt: number | null;
145145
/** A failed scan is not retried before this time. */
146146
readonly retryNotBefore: number | null;
147-
/** Scans in a row that ended without a complete snapshot. */
147+
/** Failed scans within the current request TTL. */
148148
readonly failedAttempts: number;
149149
readonly hadSnapshot: boolean;
150150
}
@@ -183,8 +183,10 @@ function planProviderWorkspaceSnapshotRefresh(
183183
const state = {
184184
requestedAt,
185185
retryNotBefore,
186-
// A complete snapshot ends the run of failures.
187-
failedAttempts: hasSnapshot ? 0 : previous.failedAttempts,
186+
failedAttempts:
187+
lost || (requestedAt !== null && now - requestedAt >= PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS)
188+
? 0
189+
: previous.failedAttempts,
188190
hadSnapshot: hasSnapshot,
189191
};
190192
const refresh =
@@ -305,9 +307,18 @@ export function createProviderWorkspaceSnapshotRefreshAtomFamily(input: {
305307
{ environmentId, input: { instanceId, cwd } },
306308
{ reportFailure: false },
307309
).then((result) => {
310+
const refreshedProvider =
311+
result._tag === "Success" ? findProvider(result.value.providers) : undefined;
312+
const snapshot =
313+
refreshedProvider && resolveProviderWorkspaceSnapshot(refreshedProvider, cwd);
314+
// A new server timestamp proves progress even when the client clock is ahead.
315+
// @effect-diagnostics-next-line globalDate:off
316+
const completedAt = Date.now();
308317
const refreshed =
309-
result._tag === "Success" &&
310-
hasCompleteProviderWorkspaceSnapshot(findProvider(result.value.providers), cwd);
318+
hasCompleteProviderWorkspaceSnapshot(refreshedProvider, cwd) &&
319+
(hasCurrentProviderWorkspaceSnapshot(refreshedProvider, cwd, completedAt) ||
320+
snapshot?.checkedAt !==
321+
(provider && resolveProviderWorkspaceSnapshot(provider, cwd))?.checkedAt);
311322
if (!refreshed) retryLater();
312323
}, retryLater);
313324
return plan.state;

0 commit comments

Comments
 (0)