@simplexai/localmodels-darwin-x64 0.1.0-dev.62 → 0.1.0-dev.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bootstrap.identity +1 -1
- package/bootstrap.sha256 +56 -54
- package/modules/localmodels/__init__.py +1 -1
- package/modules/localmodels/bifrost_checks.py +19 -13
- package/modules/localmodels/bifrost_clients.py +38 -36
- package/modules/localmodels/bifrost_config.py +19 -6
- package/modules/localmodels/bifrost_log.py +3 -3
- package/modules/localmodels/bifrost_models.py +9 -8
- package/modules/localmodels/bifrost_pi.py +27 -72
- package/modules/localmodels/catalog.json +51 -58
- package/modules/localmodels/custom_hub.py +10 -1
- package/modules/localmodels/custom_hub_http.py +14 -3
- package/modules/localmodels/debug_mode.py +3 -3
- package/modules/localmodels/gateway.py +56 -55
- package/modules/localmodels/gateway_bifrost.py +70 -83
- package/modules/localmodels/gateway_clients.py +33 -116
- package/modules/localmodels/gateway_home.py +5 -5
- package/modules/localmodels/gateway_keys.py +8 -6
- package/modules/localmodels/gateway_probe.py +4 -4
- package/modules/localmodels/gateway_server.py +4 -4
- package/modules/localmodels/gateway_speed.py +2 -2
- package/modules/localmodels/harness.py +185 -401
- package/modules/localmodels/harness_core.py +409 -0
- package/modules/localmodels/harness_gateway.py +75 -73
- package/modules/localmodels/harness_probe.py +19 -216
- package/modules/localmodels/library_custom.py +51 -16
- package/modules/localmodels/library_elastic.py +1 -1
- package/modules/localmodels/library_gateway.py +59 -176
- package/modules/localmodels/library_model.py +23 -0
- package/modules/localmodels/library_pricing.py +2 -2
- package/modules/localmodels/library_serve.py +5 -3
- package/modules/localmodels/library_smoke.py +33 -21
- package/modules/localmodels/library_status.py +330 -2
- package/modules/localmodels/llm_rank.py +14 -5
- package/modules/localmodels/magpie_setup.py +5 -4
- package/modules/localmodels/magpie_windows.py +1 -1
- package/modules/localmodels/pi_profile.py +384 -0
- package/modules/localmodels/process_identity.py +3 -3
- package/modules/localmodels/runtime_cli.py +32 -46
- package/modules/localmodels/runtime_pi.py +1 -1
- package/modules/localmodels/source_byok.py +9 -16
- package/modules/localmodels/stack_binding.py +9 -7
- package/modules/localmodels/stack_clients.py +137 -21
- package/modules/localmodels/stack_doctor.py +6 -3
- package/modules/localmodels/stack_hardware.py +18 -0
- package/modules/localmodels/stack_lifecycle.py +3 -2
- package/modules/localmodels/stack_remove.py +2 -1
- package/modules/localmodels/stack_verify.py +2 -2
- package/modules/localmodels/stack_view_events.py +2 -2
- package/modules/{simplex_localmodels-0.1.0.dev62.dist-info → simplex_localmodels-0.1.0.dev63.dist-info}/METADATA +1 -1
- package/modules/{simplex_localmodels-0.1.0.dev62.dist-info → simplex_localmodels-0.1.0.dev63.dist-info}/RECORD +53 -51
- package/package.json +1 -1
- package/tool.json +1 -1
- /package/modules/{simplex_localmodels-0.1.0.dev62.dist-info → simplex_localmodels-0.1.0.dev63.dist-info}/WHEEL +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev62.dist-info → simplex_localmodels-0.1.0.dev63.dist-info}/entry_points.txt +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev62.dist-info → simplex_localmodels-0.1.0.dev63.dist-info}/licenses/NOTICE +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev62.dist-info → simplex_localmodels-0.1.0.dev63.dist-info}/licenses/localmodels/HERMES-LICENSE +0 -0
|
@@ -3,30 +3,25 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import errno
|
|
6
|
-
import json
|
|
7
6
|
import os
|
|
8
7
|
import re
|
|
9
|
-
import shutil
|
|
10
8
|
import socket
|
|
11
9
|
import subprocess
|
|
12
10
|
import sys
|
|
13
11
|
from pathlib import Path
|
|
14
|
-
from typing import TYPE_CHECKING, TypedDict
|
|
15
12
|
|
|
16
|
-
from localmodels import
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
path: str | None
|
|
29
|
-
version: str | None
|
|
13
|
+
from localmodels import (
|
|
14
|
+
harness_core,
|
|
15
|
+
pi_profile,
|
|
16
|
+
process_identity,
|
|
17
|
+
process_identity_darwin,
|
|
18
|
+
remote_ops_service,
|
|
19
|
+
)
|
|
20
|
+
from localmodels.harness_core import ClientConfig as ClientConfig
|
|
21
|
+
from localmodels.harness_core import ClientState as ClientState
|
|
22
|
+
from localmodels.harness_core import gateway_environment as gateway_environment
|
|
23
|
+
from localmodels.pi_profile import client_environment as client_environment
|
|
24
|
+
from localmodels.pi_profile import gateway_home as gateway_home
|
|
30
25
|
|
|
31
26
|
|
|
32
27
|
def windows_program(path: str | None) -> str | None:
|
|
@@ -47,14 +42,7 @@ def first_line(out: subprocess.CompletedProcess[str]) -> str | None:
|
|
|
47
42
|
|
|
48
43
|
def pi_settings_model(home: str) -> str | None:
|
|
49
44
|
"""pi's own default when Magpie is not there to report it."""
|
|
50
|
-
|
|
51
|
-
doc = json.loads(Path(home, ".pi/agent/settings.json").read_text(encoding="utf-8"))
|
|
52
|
-
except (OSError, ValueError):
|
|
53
|
-
return None
|
|
54
|
-
if not isinstance(doc, dict) or not doc.get("defaultModel"):
|
|
55
|
-
return None
|
|
56
|
-
provider = doc.get("defaultProvider")
|
|
57
|
-
return f"{provider}/{doc['defaultModel']}" if provider else str(doc["defaultModel"])
|
|
45
|
+
return pi_profile.PiProfileStore(Path(home, ".pi/agent")).default_model()
|
|
58
46
|
|
|
59
47
|
|
|
60
48
|
def shown_pi_model(text: str) -> str | None:
|
|
@@ -67,38 +55,34 @@ def listener_uids(addr: str, tables: tuple[str, ...] | None = None) -> set[int]:
|
|
|
67
55
|
"""The users whose sockets listen on ``addr`` (matching its host and port, plus wildcard
|
|
68
56
|
listeners): the given TCP tables (/proc's by default on Linux), lsof on macOS when no table
|
|
69
57
|
is given."""
|
|
70
|
-
from localmodels.harness import HarnessError, run_tool
|
|
71
|
-
|
|
72
58
|
host, port = process_identity_darwin.parse_host_port(addr)
|
|
73
59
|
uids: set[int] = set()
|
|
74
60
|
if tables is None and sys.platform == "win32":
|
|
75
|
-
from localmodels import process_identity
|
|
76
|
-
|
|
77
61
|
pids = (
|
|
78
62
|
process_identity.listener_pids(port)
|
|
79
63
|
if host == "127.0.0.1"
|
|
80
64
|
else process_identity.listener_pids(port, host=host)
|
|
81
65
|
)
|
|
82
66
|
if pids is None:
|
|
83
|
-
raise HarnessError("cannot confirm who owns the Windows gateway listener")
|
|
67
|
+
raise harness_core.HarnessError("cannot confirm who owns the Windows gateway listener")
|
|
84
68
|
for pid in pids:
|
|
85
69
|
found = process_identity.inspect(pid)
|
|
86
70
|
uids.add(found["uid"] if found else -1) # 0: this user's process
|
|
87
71
|
return uids
|
|
88
72
|
if tables is None and sys.platform == "darwin":
|
|
89
|
-
out = run_tool(
|
|
73
|
+
out = harness_core.run_tool(
|
|
90
74
|
["/usr/sbin/lsof", "-wnP", f"-iTCP:{port}", "-sTCP:LISTEN", "-Fpun"], timeout=10
|
|
91
75
|
)
|
|
92
76
|
parsed = process_identity_darwin.parse_lsof_listeners(out.stdout, port, host)
|
|
93
77
|
if out.returncode not in (0, 1) or parsed is None:
|
|
94
|
-
raise HarnessError("cannot confirm who owns the macOS gateway listener")
|
|
78
|
+
raise harness_core.HarnessError("cannot confirm who owns the macOS gateway listener")
|
|
95
79
|
_, uids, _, saw_uid = parsed
|
|
96
80
|
if out.returncode == 0 and not saw_uid:
|
|
97
|
-
raise HarnessError("cannot confirm who owns the macOS gateway listener")
|
|
81
|
+
raise harness_core.HarnessError("cannot confirm who owns the macOS gateway listener")
|
|
98
82
|
if not uids:
|
|
99
83
|
try:
|
|
100
84
|
with socket.create_connection((host, port), timeout=1):
|
|
101
|
-
raise HarnessError(
|
|
85
|
+
raise harness_core.HarnessError(
|
|
102
86
|
"the macOS gateway port is listening, but its owner cannot be confirmed"
|
|
103
87
|
)
|
|
104
88
|
except OSError:
|
|
@@ -146,13 +130,9 @@ def _linux_socket_owner_pids(
|
|
|
146
130
|
def loopback_listener_pids(port: int, proc: str = _PROC) -> set[int] | None:
|
|
147
131
|
"""PIDs of processes listening on TCP ``port`` (this user's on Linux), or None when unknown."""
|
|
148
132
|
if sys.platform == "win32":
|
|
149
|
-
from localmodels import process_identity
|
|
150
|
-
|
|
151
133
|
return process_identity.listener_pids(port)
|
|
152
134
|
if sys.platform == "darwin":
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
out = run_tool(
|
|
135
|
+
out = harness_core.run_tool(
|
|
156
136
|
["/usr/sbin/lsof", "-wnP", f"-iTCP:{port}", "-sTCP:LISTEN", "-Fpn"], timeout=10
|
|
157
137
|
)
|
|
158
138
|
parsed = process_identity_darwin.parse_lsof_listeners(out.stdout, port, "127.0.0.1")
|
|
@@ -161,8 +141,6 @@ def loopback_listener_pids(port: int, proc: str = _PROC) -> set[int] | None:
|
|
|
161
141
|
return parsed[0]
|
|
162
142
|
if not sys.platform.startswith("linux"):
|
|
163
143
|
return None
|
|
164
|
-
from localmodels import remote_ops_service
|
|
165
|
-
|
|
166
144
|
inodes = remote_ops_service.loopback_listener_inodes(port)
|
|
167
145
|
if inodes is None:
|
|
168
146
|
return None
|
|
@@ -207,185 +185,10 @@ def _listdir(path: str) -> list[str]:
|
|
|
207
185
|
return []
|
|
208
186
|
|
|
209
187
|
|
|
210
|
-
def gateway_home(tools_dir: str | Path) -> Path:
|
|
211
|
-
"""The private gateway client context under ``tools_dir``. Existing parent links such as a
|
|
212
|
-
relocated ``~/.local`` resolve first; ``gateway-home`` itself is never resolved."""
|
|
213
|
-
return Path(hosts.tools_dir(tools_dir)).expanduser().resolve() / "gateway-home"
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
def client_environment(tools_dir: str) -> dict[str, str]:
|
|
217
|
-
"""Settings written by an automatic stack stay in its private gateway context."""
|
|
218
|
-
home = gateway_home(tools_dir)
|
|
219
|
-
return {
|
|
220
|
-
"PI_CODING_AGENT_DIR": str(home / ".pi/agent"),
|
|
221
|
-
"CODEX_HOME": str(home / ".codex"),
|
|
222
|
-
"CLAUDE_CONFIG_DIR": str(home / ".claude"),
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
def gateway_environment(
|
|
227
|
-
plan: harness.ToolPlan, *, base: dict[str, str] | None = None
|
|
228
|
-
) -> dict[str, str]:
|
|
229
|
-
"""Derive an execution environment without writing inherited credentials into a plan."""
|
|
230
|
-
from localmodels import harness
|
|
231
|
-
|
|
232
|
-
env = dict(
|
|
233
|
-
plan["environment"] if "environment" in plan else os.environ if base is None else base
|
|
234
|
-
)
|
|
235
|
-
action = next((t for t in plan["tools"] if t["tool"] == "gateway"), None)
|
|
236
|
-
if action is None:
|
|
237
|
-
return env
|
|
238
|
-
gateway = plan.get("gateway")
|
|
239
|
-
if isinstance(gateway, dict) and gateway.get("addr") != action["path"]:
|
|
240
|
-
raise harness.HarnessError("the gateway address in the approved plan does not match")
|
|
241
|
-
if isinstance(gateway, dict) and harness.gateway_kind(gateway) == "bifrost":
|
|
242
|
-
if "environment" not in plan:
|
|
243
|
-
env.update(harness.client_environment(plan["tools_dir"]))
|
|
244
|
-
return env
|
|
245
|
-
env["MAGPIE_ADDR"] = action["path"]
|
|
246
|
-
home = Path(env.get("HOME", str(Path.home())))
|
|
247
|
-
if "environment" not in plan and action["action"] != "daemon":
|
|
248
|
-
private = gateway_home(plan["tools_dir"])
|
|
249
|
-
env.update(
|
|
250
|
-
HOME=str(private),
|
|
251
|
-
USERPROFILE=str(private),
|
|
252
|
-
XDG_CONFIG_HOME=str(private / ".config"),
|
|
253
|
-
MAGPIE_CONFIG_DIR=str(private / ".config/magpie"),
|
|
254
|
-
XDG_DATA_HOME=str(private / ".local/share"),
|
|
255
|
-
XDG_CACHE_HOME=str(private / ".cache"),
|
|
256
|
-
APPDATA=str(private / "AppData/Roaming"),
|
|
257
|
-
LOCALAPPDATA=str(private / "AppData/Local"),
|
|
258
|
-
)
|
|
259
|
-
env.setdefault("PI_CODING_AGENT_DIR", str(home / ".pi/agent"))
|
|
260
|
-
env.setdefault("CODEX_HOME", str(home / ".codex"))
|
|
261
|
-
env.setdefault("CLAUDE_CONFIG_DIR", str(home / ".claude"))
|
|
262
|
-
if "gateway" in plan:
|
|
263
|
-
env.update(harness.client_environment(plan["tools_dir"]))
|
|
264
|
-
return env
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
def inspect(
|
|
268
|
-
env: dict[str, str] | None = None,
|
|
269
|
-
*,
|
|
270
|
-
tools_dir: str,
|
|
271
|
-
approved: harness.ToolPlan | None = None,
|
|
272
|
-
private_clients: bool = False,
|
|
273
|
-
) -> harness.ToolState:
|
|
274
|
-
"""Read-only facts about the tools on this computer, found on ``harness.search_path``."""
|
|
275
|
-
from localmodels import harness
|
|
276
|
-
|
|
277
|
-
if not (sys.platform.startswith("linux") or sys.platform in ("darwin", "win32")):
|
|
278
|
-
raise harness.HarnessError(
|
|
279
|
-
"the local stack tool check supports Linux, macOS and Windows; cannot confirm"
|
|
280
|
-
" gateway ownership on this platform."
|
|
281
|
-
)
|
|
282
|
-
from localmodels import harness_gateway, stack_clients
|
|
283
|
-
|
|
284
|
-
env = dict(os.environ if env is None else env)
|
|
285
|
-
tools_dir = os.path.expanduser(
|
|
286
|
-
hosts.tools_dir(approved["tools_dir"] if approved else tools_dir, env=env)
|
|
287
|
-
)
|
|
288
|
-
path = env.get("PATH", "")
|
|
289
|
-
home = env.get("HOME") or os.path.expanduser("~")
|
|
290
|
-
bin_dir = os.path.join(home, harness.BIN_DIR.removeprefix("~/"))
|
|
291
|
-
search = harness.search_path(env)
|
|
292
|
-
node, pi, magpie = (shutil.which(name, path=search) for name in ("node", "pi", "magpie"))
|
|
293
|
-
magpie = windows_program(magpie)
|
|
294
|
-
bifrost = bool(approved and harness.gateway_kind(approved.get("gateway")) == "bifrost")
|
|
295
|
-
bifrost_address = (
|
|
296
|
-
str(approved["gateway"]["addr"]) if bifrost and approved else harness.bifrost_addr(env)
|
|
297
|
-
)
|
|
298
|
-
requested = (
|
|
299
|
-
next(t["path"] for t in approved["tools"] if t["tool"] == "gateway")
|
|
300
|
-
if approved and not bifrost
|
|
301
|
-
else env.get("MAGPIE_ADDR") or None
|
|
302
|
-
)
|
|
303
|
-
addr = (
|
|
304
|
-
harness.GATEWAY_ADDR
|
|
305
|
-
if bifrost
|
|
306
|
-
else harness_gateway.select_address(
|
|
307
|
-
requested,
|
|
308
|
-
tools_dir,
|
|
309
|
-
owned=lambda addr: harness_gateway.owned_address(addr, tools_dir, magpie),
|
|
310
|
-
)
|
|
311
|
-
)
|
|
312
|
-
context: harness.ToolPlan = {
|
|
313
|
-
"tools_dir": tools_dir,
|
|
314
|
-
"tools": [
|
|
315
|
-
{"tool": "gateway", "action": "serve", "path": addr, "version": None, "artifacts": []}
|
|
316
|
-
],
|
|
317
|
-
"download_bytes": 0,
|
|
318
|
-
"warnings": [],
|
|
319
|
-
}
|
|
320
|
-
gateway_env = harness.gateway_environment(approved or context, base=env)
|
|
321
|
-
if private_clients:
|
|
322
|
-
gateway_env.update(harness.client_environment(tools_dir))
|
|
323
|
-
node_version = (
|
|
324
|
-
first_line(harness.run_tool([node, "--version"], timeout=10, env=env)) if node else None
|
|
325
|
-
)
|
|
326
|
-
pi_version = (
|
|
327
|
-
first_line(harness.run_tool([pi, "--version"], timeout=30, env=env)) if pi else None
|
|
328
|
-
)
|
|
329
|
-
if pi and pi_version and not runtime_discovery.pi_loads_extensions([pi], env):
|
|
330
|
-
# localmodels before 0.1.0.dev39 installed pi without jiti, so it cannot load the
|
|
331
|
-
# extension Linger starts it with: plan_tools installs the pinned tree over an owned one.
|
|
332
|
-
pi_version = None
|
|
333
|
-
magpie_version = pi_model = None
|
|
334
|
-
gateway_up = False
|
|
335
|
-
if magpie and not bifrost:
|
|
336
|
-
version = first_line(harness.run_tool([magpie, "--version"], timeout=20, env=env))
|
|
337
|
-
magpie_version = version.split()[-1] if version else None
|
|
338
|
-
if harness_gateway.owned_address(addr, tools_dir, magpie):
|
|
339
|
-
gateway_up = (
|
|
340
|
-
harness.run_tool([magpie, "healthcheck"], timeout=20, env=gateway_env).returncode
|
|
341
|
-
== 0
|
|
342
|
-
)
|
|
343
|
-
if not private_clients:
|
|
344
|
-
pi_model = shown_pi_model(
|
|
345
|
-
harness.run_tool([magpie, "pi"], timeout=20, env=gateway_env).stdout
|
|
346
|
-
)
|
|
347
|
-
clients = (
|
|
348
|
-
stack_clients.inspect(search, env, None)
|
|
349
|
-
if private_clients
|
|
350
|
-
else stack_clients.inspect(
|
|
351
|
-
search, gateway_env if gateway_up else env, magpie if gateway_up else None
|
|
352
|
-
)
|
|
353
|
-
)
|
|
354
|
-
if private_clients:
|
|
355
|
-
pi_model = pi_settings_model(str(gateway_home(tools_dir)))
|
|
356
|
-
for client, seen in clients.items():
|
|
357
|
-
seen["files"] = stack_clients.config_files(client, gateway_env)
|
|
358
|
-
if gateway_up and magpie and next(iter(seen["files"].values()), None) is not None:
|
|
359
|
-
out = harness.run_tool([magpie, client], timeout=20, env=gateway_env)
|
|
360
|
-
seen["model"] = (
|
|
361
|
-
stack_clients.shown_model(client, out.stdout) if out.returncode == 0 else None
|
|
362
|
-
)
|
|
363
|
-
return {
|
|
364
|
-
"inspection": "complete",
|
|
365
|
-
"gateway_other_user": False,
|
|
366
|
-
"tools_dir": tools_dir,
|
|
367
|
-
"bifrost_addr": bifrost_address,
|
|
368
|
-
"bifrost_listeners": sorted(listener_uids(bifrost_address)),
|
|
369
|
-
"node": node,
|
|
370
|
-
"node_version": node_version,
|
|
371
|
-
"pi": pi if pi_version else None,
|
|
372
|
-
"pi_version": pi_version,
|
|
373
|
-
"magpie": magpie if magpie_version else None,
|
|
374
|
-
"magpie_version": magpie_version,
|
|
375
|
-
"gateway_addr": addr,
|
|
376
|
-
"gateway_up": gateway_up,
|
|
377
|
-
"pi_model": pi_model if private_clients else pi_model or pi_settings_model(home),
|
|
378
|
-
"bin_on_path": bin_dir in path.split(os.pathsep),
|
|
379
|
-
"clients": clients,
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
|
|
383
188
|
def loopback_listening(port: int) -> bool:
|
|
384
189
|
"""Only connection refusal proves that a loopback port has no TCP listener. Windows reads
|
|
385
190
|
its listener table instead: a refused loopback connect retries there for about two seconds."""
|
|
386
191
|
if sys.platform == "win32":
|
|
387
|
-
from localmodels import process_identity
|
|
388
|
-
|
|
389
192
|
if (pids := process_identity.listener_pids(port)) is None:
|
|
390
193
|
raise OSError(f"cannot confirm whether port {port} is free")
|
|
391
194
|
return bool(pids)
|
|
@@ -12,7 +12,7 @@ from __future__ import annotations
|
|
|
12
12
|
import os
|
|
13
13
|
import re
|
|
14
14
|
import urllib.parse
|
|
15
|
-
from collections.abc import Mapping
|
|
15
|
+
from collections.abc import Mapping, Sequence
|
|
16
16
|
from pathlib import Path
|
|
17
17
|
from typing import Any, cast
|
|
18
18
|
|
|
@@ -32,6 +32,7 @@ from localmodels import (
|
|
|
32
32
|
library_status,
|
|
33
33
|
plan_envelope,
|
|
34
34
|
remote_ops,
|
|
35
|
+
remote_ops_download,
|
|
35
36
|
)
|
|
36
37
|
from localmodels.gguf import GGUFHeader
|
|
37
38
|
from localmodels.library_model import (
|
|
@@ -64,8 +65,15 @@ def _safe_rel_path(raw: str) -> str:
|
|
|
64
65
|
def _custom_local_name(hub: CustomHub, repo_id: str, revision: str, rel_path: str) -> str:
|
|
65
66
|
hub_short = "hf" if hub == "huggingface" else "ms"
|
|
66
67
|
owner, _, name = repo_id.partition("/")
|
|
67
|
-
rev_short = _REV_SAFE_RE.sub("-", (revision or "main").strip())[:12].strip("
|
|
68
|
-
|
|
68
|
+
rev_short = _REV_SAFE_RE.sub("-", (revision or "main").strip())[:12].strip(".-_")
|
|
69
|
+
if not rev_short or rev_short in (".", ".."):
|
|
70
|
+
rev_short = "main"
|
|
71
|
+
root = f"custom/{hub_short}/{_slug(owner)}/{_slug(name)}"
|
|
72
|
+
candidate = f"{root}/{rev_short}/{_safe_rel_path(rel_path)}"
|
|
73
|
+
parts = candidate.split("/")
|
|
74
|
+
if not candidate.startswith(root + "/") or any(p in ("", ".", "..") for p in parts):
|
|
75
|
+
raise LibraryError("INVALID_REPO_SPEC", f"unsafe custom target path: {candidate!r}")
|
|
76
|
+
return candidate
|
|
69
77
|
|
|
70
78
|
|
|
71
79
|
def _parse_download_payload(
|
|
@@ -369,7 +377,8 @@ def _build_download_plan(
|
|
|
369
377
|
}
|
|
370
378
|
for f in all_files
|
|
371
379
|
]
|
|
372
|
-
|
|
380
|
+
stem = re.sub(r"\.gguf$", "", Path(weight_files[0].path).name, flags=re.IGNORECASE)
|
|
381
|
+
model_alias = gguf.model_id_from_stem(stem)
|
|
373
382
|
where = plan_envelope.deployment(
|
|
374
383
|
catalog_id,
|
|
375
384
|
deploy.DEFAULT_PORT,
|
|
@@ -451,9 +460,13 @@ def _auth_headers_for_plan(
|
|
|
451
460
|
allowed = custom_hub.token_for_endpoint(hub, f["url"], token)
|
|
452
461
|
if not allowed:
|
|
453
462
|
continue
|
|
454
|
-
host = (urllib.parse.urlsplit(f["url"]).hostname or "").lower()
|
|
463
|
+
host = (urllib.parse.urlsplit(f["url"]).hostname or "").lower().rstrip(".")
|
|
455
464
|
if host:
|
|
456
465
|
hosts_seen[host] = f"Bearer {allowed}"
|
|
466
|
+
if hub == "huggingface":
|
|
467
|
+
official_tok = custom_hub.token_for_endpoint("huggingface", "https://huggingface.co", token)
|
|
468
|
+
if official_tok:
|
|
469
|
+
hosts_seen.setdefault("huggingface.co", f"Bearer {official_tok}")
|
|
457
470
|
return hosts_seen or None
|
|
458
471
|
|
|
459
472
|
|
|
@@ -482,9 +495,21 @@ def _read_local_header(first_dest: str) -> GGUFHeader | None:
|
|
|
482
495
|
return None
|
|
483
496
|
|
|
484
497
|
|
|
485
|
-
def _cleanup_aborted(
|
|
498
|
+
def _cleanup_aborted(
|
|
499
|
+
model_id: str,
|
|
500
|
+
job: Job,
|
|
501
|
+
handle: JobHandle,
|
|
502
|
+
base_dir: str,
|
|
503
|
+
*,
|
|
504
|
+
preserve_dests: Sequence[str] = (),
|
|
505
|
+
) -> None:
|
|
486
506
|
gone = library_jobs.leftovers(handle.job)
|
|
487
|
-
|
|
507
|
+
keep = library_jobs.in_use(model_id)
|
|
508
|
+
preserved_real = {os.path.realpath(p) for p in preserve_dests}
|
|
509
|
+
keep.update(preserved_real)
|
|
510
|
+
for path in preserve_dests:
|
|
511
|
+
remote_ops_download.discard(os.path.expanduser(path) + ".part")
|
|
512
|
+
library_jobs.delete_weights(gone, keep)
|
|
488
513
|
library_jobs.prune_empty_custom_dirs(gone, base_dir)
|
|
489
514
|
if library_state.current(job):
|
|
490
515
|
library_state.clear_job(model_id)
|
|
@@ -655,14 +680,16 @@ def download_custom(payload: Mapping[str, object], base_dir: str) -> Row:
|
|
|
655
680
|
f"{custom_model_id} is {existing['state']}; unload it before replacing",
|
|
656
681
|
)
|
|
657
682
|
old_dests = library_jobs.dests(existing["plan"])
|
|
683
|
+
old_real = {os.path.realpath(p) for p in old_dests}
|
|
658
684
|
others = library_jobs.claimed_elsewhere(custom_model_id)
|
|
659
|
-
|
|
685
|
+
fetch_targets = [f for f in files if os.path.realpath(f) not in others]
|
|
686
|
+
owned_new = [f for f in fetch_targets if os.path.realpath(f) not in old_real]
|
|
660
687
|
job = library_state.acquire(
|
|
661
688
|
"download",
|
|
662
689
|
custom_model_id,
|
|
663
690
|
pool_id,
|
|
664
691
|
est_memory,
|
|
665
|
-
|
|
692
|
+
owned_new,
|
|
666
693
|
files,
|
|
667
694
|
custom=custom_meta,
|
|
668
695
|
catalog_entry=provisional_entry,
|
|
@@ -680,7 +707,7 @@ def download_custom(payload: Mapping[str, object], base_dir: str) -> Row:
|
|
|
680
707
|
plan,
|
|
681
708
|
handle,
|
|
682
709
|
library_jobs.executor(),
|
|
683
|
-
set(
|
|
710
|
+
set(fetch_targets),
|
|
684
711
|
auth_headers=auth_headers,
|
|
685
712
|
shard_count=len(weight_files),
|
|
686
713
|
)
|
|
@@ -707,8 +734,10 @@ def download_custom(payload: Mapping[str, object], base_dir: str) -> Row:
|
|
|
707
734
|
)
|
|
708
735
|
except KeyboardInterrupt:
|
|
709
736
|
with library_state.locked():
|
|
710
|
-
if library_state.
|
|
711
|
-
_cleanup_aborted(
|
|
737
|
+
if library_state.current(job):
|
|
738
|
+
_cleanup_aborted(
|
|
739
|
+
custom_model_id, job, handle, base_dir, preserve_dests=old_dests
|
|
740
|
+
)
|
|
712
741
|
raise LibraryError("CANCELLED", "the download was cancelled") from None
|
|
713
742
|
except LibraryError as exc:
|
|
714
743
|
safe_detail = custom_hub.redact_secrets(
|
|
@@ -717,7 +746,9 @@ def download_custom(payload: Mapping[str, object], base_dir: str) -> Row:
|
|
|
717
746
|
with library_state.locked():
|
|
718
747
|
if library_state.current(job):
|
|
719
748
|
if library_state.cancelled(job):
|
|
720
|
-
_cleanup_aborted(
|
|
749
|
+
_cleanup_aborted(
|
|
750
|
+
custom_model_id, job, handle, base_dir, preserve_dests=old_dests
|
|
751
|
+
)
|
|
721
752
|
raise LibraryError("CANCELLED", "the download was cancelled") from None
|
|
722
753
|
if (
|
|
723
754
|
exc.code in library_jobs.KEEP_FAILED_DOWNLOAD_CODES
|
|
@@ -728,12 +759,16 @@ def download_custom(payload: Mapping[str, object], base_dir: str) -> Row:
|
|
|
728
759
|
)
|
|
729
760
|
claims.clear()
|
|
730
761
|
else:
|
|
731
|
-
_cleanup_aborted(
|
|
762
|
+
_cleanup_aborted(
|
|
763
|
+
custom_model_id, job, handle, base_dir, preserve_dests=old_dests
|
|
764
|
+
)
|
|
732
765
|
raise LibraryError(exc.code, safe_detail, failed=exc.failed, **exc.fields) from None
|
|
733
766
|
except Exception as exc:
|
|
734
767
|
with library_state.locked():
|
|
735
768
|
if library_state.current(job):
|
|
736
|
-
_cleanup_aborted(
|
|
769
|
+
_cleanup_aborted(
|
|
770
|
+
custom_model_id, job, handle, base_dir, preserve_dests=old_dests
|
|
771
|
+
)
|
|
737
772
|
safe_detail = custom_hub.redact_secrets(
|
|
738
773
|
remote_ops.redact_text(str(exc)), extra_token=resolved_token
|
|
739
774
|
)
|
|
@@ -819,7 +854,7 @@ def _commit_custom_record(
|
|
|
819
854
|
with library_state.locked():
|
|
820
855
|
found = library_state.read_job(custom_model_id)
|
|
821
856
|
if found is None or found.get("id") != job["id"] or library_state.cancelled(job):
|
|
822
|
-
_cleanup_aborted(custom_model_id, job, handle, base_dir)
|
|
857
|
+
_cleanup_aborted(custom_model_id, job, handle, base_dir, preserve_dests=old_dests)
|
|
823
858
|
raise LibraryError("CANCELLED", "the download was cancelled")
|
|
824
859
|
library_state.save(record)
|
|
825
860
|
library_state.clear_job(custom_model_id)
|