The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 18:30 UTC
fix(server): server updates retry when a release download fails briefly (#33896)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
shivam committed
Nov 13, 2026 at 02:37 UTC
47a99882999c4927fcab8d27ed8d5c2587ccabc5
3 files changed
+158
-13
apps/server/src/cloud/pinnedRuntime.test.ts
+123
-2
@@ -7,9 +7,10 @@ import * as Effect from "effect/Effect";
7
import * as FileSystem from "effect/FileSystem";
8
import * as Fiber from "effect/Fiber";
9
import * as Path from "effect/Path";
10
+import * as Stream from "effect/Stream";
11
import * as NodePath from "node:path";
12
import * as TestClock from "effect/testing/TestClock";
12
-import { HttpClient, HttpClientResponse } from "effect/http";
13
+import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
14
import * as ChildProcess from "effect/process/ChildProcess";
15
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
16
@@ -189,7 +190,127 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
190
validate: () => Effect.die("must not validate a missing archive"),
191
}).pipe(Effect.flip);
192
assert.isTrue(isUnpublishedRelease(yield* failsWith(404)));
192
- assert.isFalse(isUnpublishedRelease(yield* failsWith(500)));
193
+ assert.isFalse(isUnpublishedRelease(yield* failsWith(403)));
194
+ }),
195
+ );
196
+
197
+ it.effect("retries a release download that GitHub answers with a gateway timeout", () =>
198
+ Effect.gen(function* () {
199
+ const fs = yield* FileSystem.FileSystem;
200
+ const path = yield* Path.Path;
201
+ const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-retry-" });
202
+ const checksums = yield* validChecksums;
203
+ const requests: string[] = [];
204
+ const gatewayTimeoutServed = yield* Deferred.make<void>();
205
+ const install = yield* ensurePinnedRuntimeInstalled({
206
+ baseDir,
207
+ version,
208
+ fs,
209
+ path,
210
+ platform: "linux",
211
+ arch: "x64",
212
+ httpClient: HttpClient.make((request) => {
213
+ requests.push(request.url);
214
+ const isChecksums = request.url.endsWith("/SHA256SUMS");
215
+ if (requests.length === 1) {
216
+ Deferred.doneUnsafe(gatewayTimeoutServed, Effect.void);
217
+ return Effect.succeed(
218
+ HttpClientResponse.fromWeb(request, new Response("", { status: 504 })),
219
+ );
220
+ }
221
+ return Effect.succeed(
222
+ HttpClientResponse.fromWeb(
223
+ request,
224
+ new Response(isChecksums ? checksums : archiveBytes),
225
+ ),
226
+ );
227
+ }),
228
+ runner: extractingRunner(fs, path),
229
+ validate: () => Effect.void,
230
+ }).pipe(Effect.forkScoped);
231
+ yield* Deferred.await(gatewayTimeoutServed);
232
+ yield* TestClock.adjust(Duration.seconds(2));
233
+ const paths = yield* Fiber.join(install);
234
+ assert.equal(yield* fs.readFileString(paths.sentinelPath), `${version}\n`);
235
+ assert.equal(requests.filter((url) => url.endsWith("/SHA256SUMS")).length, 2);
236
+ }),
237
+ );
238
+
239
+ it.effect("retries a release archive whose body fails after the host answered", () =>
240
+ Effect.gen(function* () {
241
+ const fs = yield* FileSystem.FileSystem;
242
+ const path = yield* Path.Path;
243
+ const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-retry-body-" });
244
+ const checksums = yield* validChecksums;
245
+ let archiveRequests = 0;
246
+ const archiveRequested = yield* Deferred.make<void>();
247
+ const install = yield* ensurePinnedRuntimeInstalled({
248
+ baseDir,
249
+ version,
250
+ fs,
251
+ path,
252
+ platform: "linux",
253
+ arch: "x64",
254
+ httpClient: HttpClient.make((request) => {
255
+ const isChecksums = request.url.endsWith("/SHA256SUMS");
256
+ const response = HttpClientResponse.fromWeb(
257
+ request,
258
+ new Response(isChecksums ? checksums : archiveBytes),
259
+ );
260
+ if (isChecksums || ++archiveRequests > 1) return Effect.succeed(response);
261
+ const failingBody = Stream.concat(
262
+ Stream.make(archiveBytes.slice(0, 4)),
263
+ Stream.fail(
264
+ new HttpClientError.HttpClientError({
265
+ reason: new HttpClientError.DecodeError({ request, response }),
266
+ }),
267
+ ),
268
+ );
269
+ Deferred.doneUnsafe(archiveRequested, Effect.void);
270
+ // TestClock follows the work a woken sleep starts, so one adjust reaches the retry wait.
271
+ return Effect.sleep("1 second").pipe(
272
+ Effect.as<HttpClientResponse.HttpClientResponse>(
273
+ Object.create(response, { stream: { value: failingBody } }),
274
+ ),
275
+ );
276
+ }),
277
+ runner: extractingRunner(fs, path),
278
+ validate: () => Effect.void,
279
+ }).pipe(Effect.forkScoped);
280
+ yield* Deferred.await(archiveRequested);
281
+ yield* TestClock.adjust(Duration.seconds(3));
282
+ const paths = yield* Fiber.join(install);
283
+ assert.equal(yield* fs.readFileString(paths.sentinelPath), `${version}\n`);
284
+ assert.equal(archiveRequests, 2);
285
+ }),
286
+ );
287
+
288
+ it.effect("fails at once when the release host cannot be reached", () =>
289
+ Effect.gen(function* () {
290
+ const fs = yield* FileSystem.FileSystem;
291
+ const path = yield* Path.Path;
292
+ const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-offline-" });
293
+ let requests = 0;
294
+ const error = yield* ensurePinnedRuntimeInstalled({
295
+ baseDir,
296
+ version,
297
+ fs,
298
+ path,
299
+ platform: "linux",
300
+ arch: "x64",
301
+ httpClient: HttpClient.make((request) => {
302
+ requests += 1;
303
+ return Effect.fail(
304
+ new HttpClientError.HttpClientError({
305
+ reason: new HttpClientError.TransportError({ request }),
306
+ }),
307
+ );
308
+ }),
309
+ runner: extractingRunner(fs, path),
310
+ validate: () => Effect.die("must not validate without a download"),
311
+ }).pipe(Effect.flip);
312
+ assert.instanceOf(error, PinnedRuntimeInstallError);
313
+ assert.equal(requests, 1);
314
}),
315
);
316
apps/server/src/cloud/pinnedRuntime.ts
+16
@@ -7,6 +7,7 @@ import * as FileSystem from "effect/FileSystem";
7
import * as Path from "effect/Path";
8
import * as Schema from "effect/Schema";
9
import * as Option from "effect/Option";
10
+import * as Schedule from "effect/Schedule";
11
import * as Stream from "effect/Stream";
12
import * as Semaphore from "effect/Semaphore";
13
import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http";
@@ -119,6 +120,20 @@ interface PinnedRuntimeInstallInput {
120
readonly onProgress?: (progress: PinnedRuntimeProgress) => void;
121
}
122
123
+// GitHub release downloads intermittently answer 5xx during incidents; a retry usually lands.
124
+const DOWNLOAD_RETRY = { times: 3, schedule: Schedule.exponential("2 seconds") } as const;
125
+
126
+const isTransientDownloadFailure = (
127
+ error: HttpClientError.HttpClientError | Cause.TimeoutError,
128
+): boolean => {
129
+ // No answer or a stall means the host cannot reach the release source; retrying only delays that.
130
+ if (error._tag === "TimeoutError") return false;
131
+ // Body bytes are read raw, so a DecodeError is a body that failed after the host answered.
132
+ if (error.reason._tag === "DecodeError") return true;
133
+ const status = error.response?.status;
134
+ return status !== undefined && (status >= 500 || status === 429 || status === 408);
135
+};
136
+
137
const fetchReleaseAsset = Effect.fn("cloud.pinned_runtime.fetch_release_asset")(function* (
138
httpClient: HttpClient.HttpClient,
139
url: string,
@@ -161,6 +176,7 @@ const fetchReleaseAsset = Effect.fn("cloud.pinned_runtime.fetch_release_asset")(
176
return bytes;
177
}),
178
),
179
+ Effect.retry({ ...DOWNLOAD_RETRY, while: isTransientDownloadFailure }),
180
Effect.mapError(
181
(cause) =>
182
new PinnedRuntimeInstallError({
apps/server/src/cloud/selfUpdate.test.ts
+19
-11
@@ -184,21 +184,29 @@ it.layer(NodeServices.layer)("server self update", (it) => {
184
])("reports an HTTP $status downloading $asset and allows retry", ({ asset, status }) =>
185
Effect.gen(function* () {
186
let failDownload = true;
187
+ const downloadFailed = yield* Deferred.make<void>();
188
const { selfUpdate, order, baseDir } = yield* makeHarness({
189
httpClient: releaseHttpClient([], []).pipe(
189
- HttpClient.transform((response, request) =>
190
- failDownload && request.url.endsWith("/SHA256SUMS") === (asset === "checksums")
191
- ? Effect.succeed(
192
- HttpClientResponse.fromWeb(
193
- HttpClientRequest.get("https://user:password@cdn.example/private?token=secret"),
194
- new Response("private response body", { status }),
195
- ),
196
- )
197
- : response,
198
- ),
190
+ HttpClient.transform((response, request) => {
191
+ if (!failDownload || request.url.endsWith("/SHA256SUMS") !== (asset === "checksums"))
192
+ return response;
193
+ Deferred.doneUnsafe(downloadFailed, Effect.void);
194
+ return Effect.succeed(
195
+ HttpClientResponse.fromWeb(
196
+ HttpClientRequest.get("https://user:password@cdn.example/private?token=secret"),
197
+ new Response("private response body", { status }),
198
+ ),
199
+ );
200
+ }),
201
),
202
});
201
- const error = yield* selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip);
203
+ const failing = yield* selfUpdate
204
+ .update({ targetVersion: "1.1.0" })
205
+ .pipe(Effect.flip, Effect.forkScoped);
206
+ yield* Deferred.await(downloadFailed);
207
+ // A 5xx is retried after 2, 4, then 8 seconds before the update gives up.
208
+ yield* TestClock.adjust("14 seconds");
209
+ const error = yield* Fiber.join(failing);
210
expect(error.reason).toBe(
211
`Could not prepare t3@1.1.0 while downloading the t3 release ${asset} (HTTP ${status} from cdn.example).`,
212
);