Use duplicate_repo for sandbox Spaces (#249)
Browse files* Use duplicate_repo for sandbox Spaces
Co-authored-by: OpenAI Codex <codex@openai.com>
* Tighten duplicate_repo sandbox fakes
Co-authored-by: OpenAI Codex <codex@openai.com>
---------
Co-authored-by: OpenAI Codex <codex@openai.com>
agent/tools/sandbox_client.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# /// script
|
| 3 |
# requires-python = ">=3.10"
|
| 4 |
-
# dependencies = ["huggingface_hub>=
|
| 5 |
# ///
|
| 6 |
"""
|
| 7 |
Sandbox Tools — Agent-native primitives for HF Space dev-mode sandboxes.
|
|
@@ -615,18 +615,19 @@ class Sandbox:
|
|
| 615 |
kwargs = {
|
| 616 |
"from_id": template,
|
| 617 |
"to_id": space_id,
|
|
|
|
| 618 |
"private": private,
|
| 619 |
-
"
|
| 620 |
}
|
| 621 |
if sleep_time is not None:
|
| 622 |
-
kwargs["
|
| 623 |
|
| 624 |
-
api.
|
| 625 |
_log(f"Space created: https://huggingface.co/spaces/{space_id}")
|
| 626 |
|
| 627 |
_check_cancel()
|
| 628 |
|
| 629 |
-
# ``
|
| 630 |
# initial create request. Avoid a second /hardware call: deployed HF
|
| 631 |
# OAuth tokens can 401 on that endpoint for a just-created private
|
| 632 |
# Space even though duplication itself succeeded. We rely on the
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# /// script
|
| 3 |
# requires-python = ">=3.10"
|
| 4 |
+
# dependencies = ["huggingface_hub>=1.12.0", "httpx>=0.27.0"]
|
| 5 |
# ///
|
| 6 |
"""
|
| 7 |
Sandbox Tools — Agent-native primitives for HF Space dev-mode sandboxes.
|
|
|
|
| 615 |
kwargs = {
|
| 616 |
"from_id": template,
|
| 617 |
"to_id": space_id,
|
| 618 |
+
"repo_type": "space",
|
| 619 |
"private": private,
|
| 620 |
+
"space_hardware": hardware,
|
| 621 |
}
|
| 622 |
if sleep_time is not None:
|
| 623 |
+
kwargs["space_sleep_time"] = sleep_time
|
| 624 |
|
| 625 |
+
api.duplicate_repo(**kwargs)
|
| 626 |
_log(f"Space created: https://huggingface.co/spaces/{space_id}")
|
| 627 |
|
| 628 |
_check_cancel()
|
| 629 |
|
| 630 |
+
# ``duplicate_repo`` sends hardware and sleepTimeSeconds in the
|
| 631 |
# initial create request. Avoid a second /hardware call: deployed HF
|
| 632 |
# OAuth tokens can 401 on that endpoint for a just-created private
|
| 633 |
# Space even though duplication itself succeeded. We rely on the
|
tests/unit/test_sandbox_private_spaces.py
CHANGED
|
@@ -13,6 +13,28 @@ def _fail_metadata_update(*args, **kwargs):
|
|
| 13 |
raise AssertionError("sandbox creation should not update Space metadata")
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def test_sandbox_client_defaults_to_private_spaces(monkeypatch):
|
| 17 |
duplicate_kwargs = {}
|
| 18 |
logs: list[str] = []
|
|
@@ -22,8 +44,25 @@ def test_sandbox_client_defaults_to_private_spaces(monkeypatch):
|
|
| 22 |
def __init__(self, token=None):
|
| 23 |
self.token = token
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def request_space_hardware(self, space_id, hardware, sleep_time=None):
|
| 29 |
requested_hardware.append((space_id, hardware, sleep_time))
|
|
@@ -45,8 +84,9 @@ def test_sandbox_client_defaults_to_private_spaces(monkeypatch):
|
|
| 45 |
|
| 46 |
Sandbox.create(owner="alice", token="hf-token", log=logs.append)
|
| 47 |
|
|
|
|
| 48 |
assert duplicate_kwargs["private"] is True
|
| 49 |
-
assert duplicate_kwargs["
|
| 50 |
assert requested_hardware == []
|
| 51 |
assert not any("sleep time" in log for log in logs)
|
| 52 |
|
|
@@ -67,7 +107,16 @@ def test_sandbox_client_retries_transient_runtime_404(monkeypatch):
|
|
| 67 |
def __init__(self, token=None):
|
| 68 |
self.token = token
|
| 69 |
|
| 70 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
pass
|
| 72 |
|
| 73 |
def request_space_hardware(self, space_id, hardware, sleep_time=None):
|
|
@@ -107,8 +156,25 @@ def test_sandbox_client_configures_gpu_at_duplication(monkeypatch):
|
|
| 107 |
def __init__(self, token=None):
|
| 108 |
self.token = token
|
| 109 |
|
| 110 |
-
def
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
def request_space_hardware(self, space_id, hardware, sleep_time=None):
|
| 114 |
requested_hardware.append((space_id, hardware, sleep_time))
|
|
@@ -137,8 +203,9 @@ def test_sandbox_client_configures_gpu_at_duplication(monkeypatch):
|
|
| 137 |
)
|
| 138 |
|
| 139 |
assert sandbox.space_id.startswith("alice/sandbox-")
|
| 140 |
-
assert duplicate_kwargs["
|
| 141 |
-
assert duplicate_kwargs["
|
|
|
|
| 142 |
assert requested_hardware == []
|
| 143 |
assert "Using duplicated Space hardware: t4-small" in logs
|
| 144 |
assert "Using duplicated Space sleep time: 2700s" in logs
|
|
@@ -153,8 +220,25 @@ def test_sandbox_client_logs_cpu_sleep_time_as_hub_fixed(monkeypatch):
|
|
| 153 |
def __init__(self, token=None):
|
| 154 |
self.token = token
|
| 155 |
|
| 156 |
-
def
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
def request_space_hardware(self, space_id, hardware, sleep_time=None):
|
| 160 |
requested_hardware.append((space_id, hardware, sleep_time))
|
|
@@ -180,8 +264,9 @@ def test_sandbox_client_logs_cpu_sleep_time_as_hub_fixed(monkeypatch):
|
|
| 180 |
log=logs.append,
|
| 181 |
)
|
| 182 |
|
| 183 |
-
assert duplicate_kwargs["
|
| 184 |
-
assert duplicate_kwargs["
|
|
|
|
| 185 |
assert requested_hardware == []
|
| 186 |
assert "Using duplicated Space hardware: cpu-basic" in logs
|
| 187 |
assert (
|
|
|
|
| 13 |
raise AssertionError("sandbox creation should not update Space metadata")
|
| 14 |
|
| 15 |
|
| 16 |
+
def _capture_duplicate_repo_call(
|
| 17 |
+
captured,
|
| 18 |
+
*,
|
| 19 |
+
from_id,
|
| 20 |
+
to_id,
|
| 21 |
+
repo_type,
|
| 22 |
+
private,
|
| 23 |
+
space_hardware,
|
| 24 |
+
space_sleep_time=None,
|
| 25 |
+
):
|
| 26 |
+
captured.update(
|
| 27 |
+
{
|
| 28 |
+
"from_id": from_id,
|
| 29 |
+
"to_id": to_id,
|
| 30 |
+
"repo_type": repo_type,
|
| 31 |
+
"private": private,
|
| 32 |
+
"space_hardware": space_hardware,
|
| 33 |
+
"space_sleep_time": space_sleep_time,
|
| 34 |
+
}
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
def test_sandbox_client_defaults_to_private_spaces(monkeypatch):
|
| 39 |
duplicate_kwargs = {}
|
| 40 |
logs: list[str] = []
|
|
|
|
| 44 |
def __init__(self, token=None):
|
| 45 |
self.token = token
|
| 46 |
|
| 47 |
+
def duplicate_repo(
|
| 48 |
+
self,
|
| 49 |
+
*,
|
| 50 |
+
from_id,
|
| 51 |
+
to_id,
|
| 52 |
+
repo_type,
|
| 53 |
+
private,
|
| 54 |
+
space_hardware,
|
| 55 |
+
space_sleep_time=None,
|
| 56 |
+
):
|
| 57 |
+
_capture_duplicate_repo_call(
|
| 58 |
+
duplicate_kwargs,
|
| 59 |
+
from_id=from_id,
|
| 60 |
+
to_id=to_id,
|
| 61 |
+
repo_type=repo_type,
|
| 62 |
+
private=private,
|
| 63 |
+
space_hardware=space_hardware,
|
| 64 |
+
space_sleep_time=space_sleep_time,
|
| 65 |
+
)
|
| 66 |
|
| 67 |
def request_space_hardware(self, space_id, hardware, sleep_time=None):
|
| 68 |
requested_hardware.append((space_id, hardware, sleep_time))
|
|
|
|
| 84 |
|
| 85 |
Sandbox.create(owner="alice", token="hf-token", log=logs.append)
|
| 86 |
|
| 87 |
+
assert duplicate_kwargs["repo_type"] == "space"
|
| 88 |
assert duplicate_kwargs["private"] is True
|
| 89 |
+
assert duplicate_kwargs["space_hardware"] == "cpu-basic"
|
| 90 |
assert requested_hardware == []
|
| 91 |
assert not any("sleep time" in log for log in logs)
|
| 92 |
|
|
|
|
| 107 |
def __init__(self, token=None):
|
| 108 |
self.token = token
|
| 109 |
|
| 110 |
+
def duplicate_repo(
|
| 111 |
+
self,
|
| 112 |
+
*,
|
| 113 |
+
from_id,
|
| 114 |
+
to_id,
|
| 115 |
+
repo_type,
|
| 116 |
+
private,
|
| 117 |
+
space_hardware,
|
| 118 |
+
space_sleep_time=None,
|
| 119 |
+
):
|
| 120 |
pass
|
| 121 |
|
| 122 |
def request_space_hardware(self, space_id, hardware, sleep_time=None):
|
|
|
|
| 156 |
def __init__(self, token=None):
|
| 157 |
self.token = token
|
| 158 |
|
| 159 |
+
def duplicate_repo(
|
| 160 |
+
self,
|
| 161 |
+
*,
|
| 162 |
+
from_id,
|
| 163 |
+
to_id,
|
| 164 |
+
repo_type,
|
| 165 |
+
private,
|
| 166 |
+
space_hardware,
|
| 167 |
+
space_sleep_time=None,
|
| 168 |
+
):
|
| 169 |
+
_capture_duplicate_repo_call(
|
| 170 |
+
duplicate_kwargs,
|
| 171 |
+
from_id=from_id,
|
| 172 |
+
to_id=to_id,
|
| 173 |
+
repo_type=repo_type,
|
| 174 |
+
private=private,
|
| 175 |
+
space_hardware=space_hardware,
|
| 176 |
+
space_sleep_time=space_sleep_time,
|
| 177 |
+
)
|
| 178 |
|
| 179 |
def request_space_hardware(self, space_id, hardware, sleep_time=None):
|
| 180 |
requested_hardware.append((space_id, hardware, sleep_time))
|
|
|
|
| 203 |
)
|
| 204 |
|
| 205 |
assert sandbox.space_id.startswith("alice/sandbox-")
|
| 206 |
+
assert duplicate_kwargs["repo_type"] == "space"
|
| 207 |
+
assert duplicate_kwargs["space_hardware"] == "t4-small"
|
| 208 |
+
assert duplicate_kwargs["space_sleep_time"] == 2700
|
| 209 |
assert requested_hardware == []
|
| 210 |
assert "Using duplicated Space hardware: t4-small" in logs
|
| 211 |
assert "Using duplicated Space sleep time: 2700s" in logs
|
|
|
|
| 220 |
def __init__(self, token=None):
|
| 221 |
self.token = token
|
| 222 |
|
| 223 |
+
def duplicate_repo(
|
| 224 |
+
self,
|
| 225 |
+
*,
|
| 226 |
+
from_id,
|
| 227 |
+
to_id,
|
| 228 |
+
repo_type,
|
| 229 |
+
private,
|
| 230 |
+
space_hardware,
|
| 231 |
+
space_sleep_time=None,
|
| 232 |
+
):
|
| 233 |
+
_capture_duplicate_repo_call(
|
| 234 |
+
duplicate_kwargs,
|
| 235 |
+
from_id=from_id,
|
| 236 |
+
to_id=to_id,
|
| 237 |
+
repo_type=repo_type,
|
| 238 |
+
private=private,
|
| 239 |
+
space_hardware=space_hardware,
|
| 240 |
+
space_sleep_time=space_sleep_time,
|
| 241 |
+
)
|
| 242 |
|
| 243 |
def request_space_hardware(self, space_id, hardware, sleep_time=None):
|
| 244 |
requested_hardware.append((space_id, hardware, sleep_time))
|
|
|
|
| 264 |
log=logs.append,
|
| 265 |
)
|
| 266 |
|
| 267 |
+
assert duplicate_kwargs["repo_type"] == "space"
|
| 268 |
+
assert duplicate_kwargs["space_hardware"] == "cpu-basic"
|
| 269 |
+
assert duplicate_kwargs["space_sleep_time"] == 2700
|
| 270 |
assert requested_hardware == []
|
| 271 |
assert "Using duplicated Space hardware: cpu-basic" in logs
|
| 272 |
assert (
|