Skip to content

Automation foundation model for tiny devices: 2-bit, 8-29 MB, tool calls, ASR, structured extraction and embeddings on phones, wearables, smart homes, robots, cars and microcontrollers.

fix(platform): keep the old file when a download is cut short (#160)

kevinf1bb1a99a776
diff --git a/needle/platform.py b/needle/platform.py
index d0b3955..c3c5418 100644
--- a/needle/platform.py
+++ b/needle/platform.py
@@ -1,8 +1,10 @@
from __future__ import annotations
+import http.client
import json
import os
import shutil
+import socket
import ssl
import time
import urllib.error
@@ -122,10 +124,35 @@ class Platform:
return link
def _fetch_to(self, link, dest):
+ """Download `link` to `dest`. The bytes land in a `.part` file that
+ replaces `dest` only once complete, so a download that fails keeps the
+ file it would have replaced, and the failure is a PlatformError like
+ every other call's."""
os.makedirs(os.path.dirname(os.path.abspath(dest)), exist_ok=True)
+ partial = dest + ".part"
request = urllib.request.Request(link, method="GET")
- with self._follower.open(request, timeout=self.timeout) as response, open(dest, "wb") as out:
- shutil.copyfileobj(response, out)
+ name = os.path.basename(dest)
+ try:
+ with self._follower.open(request, timeout=self.timeout) as response, open(partial, "wb") as out:
+ shutil.copyfileobj(response, out)
+ # http.client's read(n) returns what arrived when the server
+ # hangs up early, without the IncompleteRead a plain read()
+ # raises, so a cut download looks complete unless its length
+ # is checked against the one the server announced.
+ expected = response.headers.get("Content-Length")
+ if expected is not None and out.tell() != int(expected):
+ raise PlatformError("network_error", f"downloading {name} ended after "
+ f"{out.tell()} of {int(expected)} bytes")
+ os.replace(partial, dest)
+ except urllib.error.HTTPError as error:
+ raise PlatformError(f"http_{error.code}", f"downloading {name} failed with HTTP {error.code}",
+ error.code) from None
+ except (urllib.error.URLError, http.client.HTTPException, ConnectionError, socket.timeout) as error:
+ raise PlatformError("network_error", f"downloading {name} failed: "
+ f"{getattr(error, 'reason', None) or error!r}") from None
+ finally:
+ if os.path.exists(partial):
+ os.remove(partial)
return dest
def plans(self):
diff --git a/tests/test_platform.py b/tests/test_platform.py
index f7df2d2..e2fcd6f 100644
--- a/tests/test_platform.py
+++ b/tests/test_platform.py
@@ -31,9 +31,15 @@ class FakePlatform(BaseHTTPRequestHandler):
def do_GET(self):
s = self.state
s["requests"].append(("GET", self.path, self.headers.get("Authorization")))
- if self.path.startswith("/blob/"):
+ if self.path == "/blob/expired":
+ self.send_response(403)
+ self.send_header("Content-Length", "0")
+ self.end_headers()
+ elif self.path.startswith("/blob/"):
self.send_response(200)
- self.send_header("Content-Length", "5")
+ # "cut" promises ten bytes and hangs up after five, the way a
+ # dropped connection ends a download.
+ self.send_header("Content-Length", "10" if self.path == "/blob/cut" else "5")
self.end_headers()
self.wfile.write(b"CACT!")
elif self.path.startswith("/v1/") and self.headers.get("Authorization") != "Bearer needle_ft_test":
@@ -170,6 +176,31 @@ def test_errors_carry_code_param_and_url(server, tmp_path):
assert client.billing()["plan"] == "standard"
+def test_a_failed_download_keeps_the_file_it_would_replace(server, tmp_path):
+ # The download opened its destination for writing before the first byte
+ # arrived, so a dropped connection left the previous model truncated to
+ # whatever came through, and the failure escaped as a raw urllib or
+ # http.client exception that `needle platform` does not catch.
+ from needle.platform import Platform, PlatformError
+
+ base, state = server
+ host = base[:-len("/v1")]
+ client = Platform(api_key="needle_ft_test", base_url=base)
+ dest = tmp_path / "smart-home-8L.cact"
+ dest.write_bytes(b"previous model")
+
+ for blob, code in (("cut", "network_error"), ("expired", "http_403")):
+ with pytest.raises(PlatformError) as caught:
+ client._fetch_to(f"{host}/blob/{blob}", str(dest))
+ assert caught.value.code == code
+ assert dest.read_bytes() == b"previous model"
+ assert sorted(p.name for p in tmp_path.iterdir()) == ["smart-home-8L.cact"]
+
+ assert client._fetch_to(f"{host}/blob/model-1-8", str(dest)) == str(dest)
+ assert dest.read_bytes() == b"CACT!"
+ assert sorted(p.name for p in tmp_path.iterdir()) == ["smart-home-8L.cact"]
+
+
def test_download_target_routes_hosted_models():
from needle.cli import _download_target