Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

fix(install): carry a download on when it moves to another mirror

RoversX45e70bfd5312
diff --git a/ci/test-remote-update.py b/ci/test-remote-update.py
index 3aa60ef0..3fbc971c 100644
--- a/ci/test-remote-update.py
+++ b/ci/test-remote-update.py
@@ -31,9 +31,12 @@ host = urlparse(url).hostname
name = url.rsplit("/", 1)[-1]
dest = Path(args[args.index("-o") + 1])
speed_limit = int(args[args.index("--speed-limit") + 1]) if "--speed-limit" in args else 0
+offset = dest.stat().st_size if "-C" in args and dest.exists() else 0
+# Which source wrote the archive last, so a resume can tell its own bytes.
+writer = root / "last-writer"
with (root / "requests" / f"{time.time_ns()}-{os.getpid()}").open("w") as log:
log.write(json.dumps({"host": host, "name": name, "probe": "--range" in args,
- "resume": "-C" in args, "speed_limit": speed_limit,
+ "resume": "-C" in args, "speed_limit": speed_limit, "offset": offset,
"auth": any("Authorization:" in x for x in args)}) + "\n")
if host == "api.github.com":
if config.get("api_failure", False):
@@ -64,22 +67,28 @@ attempt = int(counter.read_text()) if counter.exists() else 0
counter.write_text(str(attempt + 1))
if speed_limit > config.get("transfer_rates", {}).get(host, speed_limit):
dest.write_bytes(data[:20])
+ writer.write_text(host)
sys.exit(28)
if config.get("resume") and attempt == 0:
dest.write_bytes(data[:20])
+ writer.write_text(host)
sys.exit(18)
if config.get("no_range") and attempt == 1 and dest.exists() and dest.stat().st_size:
sys.exit(33)
if config.get("slow"):
dest.write_bytes(data[:20])
+ writer.write_text(host)
time.sleep(2.4)
if "-C" in args and dest.exists():
- offset = dest.stat().st_size
- assert dest.read_bytes() == data[:offset], "resume prefix corrupted"
+ # As curl does: what is there is taken as this source's first bytes.
+ # Bytes this same source wrote must be its own.
+ if writer.exists() and writer.read_text() == host:
+ assert dest.read_bytes() == data[:offset], "resume prefix corrupted"
with dest.open("ab") as output:
- output.write(data[offset:])
+ output.write(data[dest.stat().st_size:])
else:
dest.write_bytes(data)
+writer.write_text(host)
'''
@@ -229,8 +238,21 @@ class InstallerTests(unittest.TestCase):
output = self.run_install()
self.assertIn("mismatch", output)
downloads = [r for r in self.requests() if r["name"] == GZIP and not r["probe"]]
- self.assertEqual([r["host"] for r in downloads], mirrors + ["github.com", *mirrors[:2]])
- self.assertEqual([r["speed_limit"] for r in downloads[-2:]], [1, 1])
+ # The corrupt mirror goes on from another's bytes first, then has
+ # to bring the whole of its own before it is passed over.
+ self.assertEqual([r["host"] for r in downloads],
+ mirrors + ["github.com", mirrors[0], mirrors[0], mirrors[1]])
+ self.assertEqual([r["speed_limit"] for r in downloads[-3:]], [1, 1, 1])
+
+ def test_next_mirror_goes_on_from_a_slow_ones_bytes(self):
+ self.network.update(rates={"ghfast.top": 900000, "gh-proxy.org": 800000},
+ transfer_rates={"ghfast.top": 131072})
+ output = self.run_install()
+ downloads = [r for r in self.requests() if r["name"] == GZIP and not r["probe"]]
+ self.assertEqual([r["host"] for r in downloads], ["ghfast.top", "gh-proxy.org"])
+ self.assertEqual(downloads[1]["offset"], 20)
+ self.assertIn("going on from", output)
+ self.assertIn("verified SHA-256", output)
def test_http_failures_do_not_get_slow_download_retry(self):
mirrors = self.slow_mirrors()
diff --git a/install.sh b/install.sh
index bf9a5fdf..159d9999 100755
--- a/install.sh
+++ b/install.sh
@@ -940,6 +940,13 @@ rank_sources() {
cat "$tmp"/rate-* | sort -nr | awk '{print $2}' > "$tmp/ranked-sources"
}
+# The archive is all there and is the one GitHub recorded.
+archive_complete() {
+ [ -s "$archive" ] \
+ && { [ -z "$size" ] || [ "$(wc -c < "$archive")" -eq "$size" ]; } \
+ && [ "$(sha256 "$archive")" = "$digest" ]
+}
+
verified_download() {
source=$1
transfer_mode=${2:-}
@@ -947,10 +954,21 @@ verified_download() {
if [ "$source" != github ]; then
source_url="https://$source/$url"; source_via=$source; source_auth=""
fi
- rm -f "$archive" "$tmp/slow-$source"
+ rm -f "$tmp/slow-$source"
+ # What a source before this one brought is kept: they all serve the same
+ # bytes, so this one goes on from there, and the whole is checked at the
+ # end. One that will not go on from there starts it again.
+ carried=0
+ if [ -s "$archive" ]; then
+ archive_complete && return 0
+ carried=$(wc -c < "$archive")
+ note "going on from $(mb "$carried") MB with $source"
+ fi
attempt=0
while [ "$attempt" -lt 3 ]; do
attempt=$((attempt + 1))
+ had=0
+ [ ! -f "$archive" ] || had=$(wc -c < "$archive")
# If every measured link is slow, keep the fastest rather than refuse all
# of them. If it stalls or breaks, the next source still gets a chance.
transfer_rate=1; transfer_window=15
@@ -965,10 +983,18 @@ verified_download() {
if download "$archive" "$source_via" "$source_auth" --connect-timeout 4 \
--speed-limit "$transfer_rate" --speed-time "$transfer_window" --retry 0 \
-C - -o "$archive" "$source_url"; then
- if [ "$(sha256 "$archive")" = "$digest" ] \
- && { [ -z "$size" ] || [ "$(wc -c < "$archive")" -eq "$size" ]; }; then
+ if archive_complete; then
return 0
fi
+ # Bytes from another source may be what is wrong: this one gets to
+ # bring the whole before it is the one blamed.
+ if [ "$carried" -gt 0 ]; then
+ warn "checksum or size mismatch going on with $source; starting again"
+ rm -f "$archive"
+ carried=0
+ attempt=0
+ continue
+ fi
warn "checksum or size mismatch from $source; discarded"
rm -f "$archive"
return 1
@@ -981,13 +1007,14 @@ verified_download() {
22|63) break ;;
28)
if [ "$transfer_rate" -gt 1 ]; then
- if [ "$mirror" = auto ] && [ -s "$archive" ]; then
+ if [ "$mirror" = auto ] && [ -f "$archive" ] \
+ && [ "$(wc -c < "$archive")" -gt "$had" ]; then
: > "$tmp/slow-$source"
fi
break
fi
;;
- 33|36) rm -f "$archive" ;;
+ 33|36) rm -f "$archive"; carried=0 ;;
esac
[ "$attempt" -ge 3 ] || note "retrying $source (resuming when supported)"
fi