Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
fix(cli): always persist lastChecked timestamp to prevent retry loops
When parseLatestVersionFromRefs returned undefined or the fetch failed, checkForUpdate never wrote lastChecked to the state file. This caused the throttle check to fire another fetch on every CLI invocation for users behind firewalls or experiencing persistent GitHub API errors. Now the timestamp is always persisted regardless of outcome, preserving the previous latestVersion on failure or falling back to currentVersion.
jkomyno committed
Mar 11, 2026 at 02:22 UTC
be8a4ac76b7a5124c0f1c955c3418fc9ed9f3943
2 files changed
+65
-33
ts/packages/cli/src/services/update-check.ts
+31
-27
@@ -114,11 +114,13 @@ export function createUpdateChecker(config: UpdateCheckConfig) {
114
function checkForUpdate(): Promise<void> | undefined {
115
try {
116
// Throttle: skip if checked recently.
117
+ let previousLatestVersion: string | undefined;
118
try {
119
const state: UpdateCheckState = JSON.parse(readFileSync(config.stateFile, 'utf-8'));
120
if (Date.now() - new Date(state.lastChecked).getTime() < config.checkIntervalMs) {
121
return undefined;
122
}
123
+ previousLatestVersion = state.latestVersion;
124
} catch {
125
// ENOENT or corrupt file — re-check.
126
}
@@ -132,33 +134,35 @@ export function createUpdateChecker(config: UpdateCheckConfig) {
134
headers.Authorization = `Bearer ${config.accessToken}`;
135
}
136
135
- return (
136
- config
137
- .fetchFn(config.refsUrl, { headers, signal: AbortSignal.timeout(10_000) })
138
- .then(res => {
139
- if (!res.ok) throw new Error(`HTTP ${res.status}`);
140
- return res.json();
141
- })
142
- .then((refs: unknown) => {
143
- const latestVersion = parseLatestVersionFromRefs(refs);
144
- if (!latestVersion) return;
145
-
146
- const stateDir = dirname(config.stateFile);
147
- mkdirSync(stateDir, { recursive: true });
148
-
149
- const state: UpdateCheckState = {
150
- lastChecked: new Date().toISOString(),
151
- latestVersion,
152
- };
153
-
154
- return writeFile(config.stateFile, JSON.stringify(state, null, 2));
155
- })
156
- // Normalize return type to Promise<void> (writeFile returns void, early-return yields undefined).
157
- .then(() => {})
158
- .catch(() => {
159
- // Silently ignore — never block the CLI.
160
- })
161
- );
137
+ // Always persist lastChecked to prevent retry loops when the fetch
138
+ // fails or returns no matching tags.
139
+ const writeState = (latestVersion?: string): Promise<void> => {
140
+ const stateDir = dirname(config.stateFile);
141
+ mkdirSync(stateDir, { recursive: true });
142
+
143
+ const state: UpdateCheckState = {
144
+ lastChecked: new Date().toISOString(),
145
+ latestVersion: latestVersion ?? previousLatestVersion ?? config.currentVersion,
146
+ };
147
+
148
+ return writeFile(config.stateFile, JSON.stringify(state, null, 2)).then(() => {});
149
+ };
150
+
151
+ return config
152
+ .fetchFn(config.refsUrl, { headers, signal: AbortSignal.timeout(10_000) })
153
+ .then(res => {
154
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
155
+ return res.json();
156
+ })
157
+ .then((refs: unknown) => {
158
+ const latestVersion = parseLatestVersionFromRefs(refs);
159
+ return writeState(latestVersion);
160
+ })
161
+ .catch(() => {
162
+ // Silently ignore fetch/parse errors — never block the CLI.
163
+ // Still update the timestamp to prevent unbounded retry loops.
164
+ return writeState().catch(() => {});
165
+ });
166
} catch {
167
// Silently ignore.
168
return undefined;
ts/packages/cli/test/src/services/update-check.test.ts
+34
-6
@@ -247,7 +247,7 @@ describe('checkForUpdate', () => {
247
expect(state.latestVersion).toBe('0.5.0');
248
});
249
250
- it('does not write state when no CLI tags are found', async () => {
250
+ it('still writes lastChecked when no CLI tags are found', async () => {
251
const config = makeConfig({
252
fetchFn: vi.fn().mockResolvedValue({
253
ok: true,
@@ -258,10 +258,32 @@ describe('checkForUpdate', () => {
258
259
await checkForUpdate();
260
261
- expect(existsSync(config.stateFile)).toBe(false);
261
+ expect(existsSync(config.stateFile)).toBe(true);
262
+ const state: UpdateCheckState = JSON.parse(readFileSync(config.stateFile, 'utf-8'));
263
+ expect(state.lastChecked).toBeDefined();
264
+ // Falls back to currentVersion since no previous state and no tags found
265
+ expect(state.latestVersion).toBe(config.currentVersion);
266
+ });
267
+
268
+ it('preserves previous latestVersion when no CLI tags are found', async () => {
269
+ const config = makeConfig({
270
+ fetchFn: vi.fn().mockResolvedValue({
271
+ ok: true,
272
+ json: () => Promise.resolve([{ ref: 'refs/tags/@composio/core@1.0.0' }]),
273
+ }) as unknown as typeof fetch,
274
+ });
275
+ const stale = new Date(Date.now() - 25 * 60 * 60 * 1000).toISOString();
276
+ writeState(config, { lastChecked: stale, latestVersion: '0.3.0' });
277
+ const { checkForUpdate } = createUpdateChecker(config);
278
+
279
+ await checkForUpdate();
280
+
281
+ const state: UpdateCheckState = JSON.parse(readFileSync(config.stateFile, 'utf-8'));
282
+ expect(state.latestVersion).toBe('0.3.0');
283
+ expect(new Date(state.lastChecked).getTime()).toBeGreaterThan(new Date(stale).getTime());
284
});
285
264
- it('silently ignores HTTP errors', async () => {
286
+ it('writes lastChecked on HTTP errors to prevent retry loops', async () => {
287
const config = makeConfig({
288
fetchFn: vi.fn().mockResolvedValue({
289
ok: false,
@@ -273,10 +295,13 @@ describe('checkForUpdate', () => {
295
// Should not throw
296
await checkForUpdate();
297
276
- expect(existsSync(config.stateFile)).toBe(false);
298
+ expect(existsSync(config.stateFile)).toBe(true);
299
+ const state: UpdateCheckState = JSON.parse(readFileSync(config.stateFile, 'utf-8'));
300
+ expect(state.lastChecked).toBeDefined();
301
+ expect(state.latestVersion).toBe(config.currentVersion);
302
});
303
279
- it('silently ignores network errors', async () => {
304
+ it('writes lastChecked on network errors to prevent retry loops', async () => {
305
const config = makeConfig({
306
fetchFn: vi.fn().mockRejectedValue(new Error('DNS failed')) as unknown as typeof fetch,
307
});
@@ -284,7 +309,10 @@ describe('checkForUpdate', () => {
309
310
await checkForUpdate();
311
287
- expect(existsSync(config.stateFile)).toBe(false);
312
+ expect(existsSync(config.stateFile)).toBe(true);
313
+ const state: UpdateCheckState = JSON.parse(readFileSync(config.stateFile, 'utf-8'));
314
+ expect(state.lastChecked).toBeDefined();
315
+ expect(state.latestVersion).toBe(config.currentVersion);
316
});
317
318
it('sends Authorization header when accessToken is set', async () => {