@simplexai/localmodels-linux-x64 0.1.0-dev.49 → 0.1.0-dev.50
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 +51 -45
- package/modules/localmodels/__init__.py +1 -1
- package/modules/localmodels/catalog.json +416 -2
- package/modules/localmodels/download-mirrors.json +47 -0
- package/modules/localmodels/engines-strata-poly-0.2.19-poly.1.json +79 -0
- package/modules/localmodels/gateway.py +23 -6
- package/modules/localmodels/gateway_bifrost.py +6 -2
- package/modules/localmodels/harness.py +8 -2
- package/modules/localmodels/harness_gateway.py +78 -17
- package/modules/localmodels/harness_probe.py +5 -1
- package/modules/localmodels/hosts.py +52 -5
- package/modules/localmodels/library_floor.py +76 -0
- package/modules/localmodels/library_jobs.py +38 -6
- package/modules/localmodels/library_poly.py +272 -0
- package/modules/localmodels/library_pricing.py +24 -35
- package/modules/localmodels/library_serve.py +7 -0
- package/modules/localmodels/library_state.py +16 -0
- package/modules/localmodels/library_status.py +27 -7
- package/modules/localmodels/llm_rank.py +14 -0
- package/modules/localmodels/magpie_windows.py +312 -0
- package/modules/localmodels/probe.py +1 -1
- package/modules/localmodels/remote_ops.py +11 -0
- package/modules/localmodels/remote_ops_download.py +30 -5
- package/modules/localmodels/reuse.py +13 -6
- package/modules/localmodels/runtime_io.py +32 -18
- package/modules/localmodels/setup_inspection.py +83 -79
- package/modules/localmodels/setup_inspection_harness.py +147 -0
- package/modules/localmodels/setup_text.py +12 -0
- package/modules/localmodels/setup_upgrade.py +11 -1
- package/modules/localmodels/stack.py +3 -1
- package/modules/localmodels/stack_auto.py +6 -2
- package/modules/localmodels/stack_clients.py +1 -1
- package/modules/localmodels/stack_down.py +25 -16
- package/modules/localmodels/stack_hardware.py +12 -1
- package/modules/localmodels/stack_picks.py +28 -13
- package/modules/localmodels/stack_remove.py +3 -1
- package/modules/localmodels/stack_suite.py +29 -8
- package/modules/localmodels/strata-poly-serve-0.2.19-poly.1.txt +428 -0
- package/modules/localmodels/strata_poly_server.py +14 -2
- package/modules/localmodels/suite.py +19 -3
- package/modules/localmodels/tool_archive.py +4 -0
- package/modules/localmodels/vision.py +4 -2
- package/modules/localmodels/win_probe.py +24 -7
- package/modules/{simplex_localmodels-0.1.0.dev49.dist-info → simplex_localmodels-0.1.0.dev50.dist-info}/METADATA +1 -1
- package/modules/{simplex_localmodels-0.1.0.dev49.dist-info → simplex_localmodels-0.1.0.dev50.dist-info}/RECORD +48 -42
- package/package.json +1 -1
- package/tool.json +1 -1
- /package/modules/{simplex_localmodels-0.1.0.dev49.dist-info → simplex_localmodels-0.1.0.dev50.dist-info}/WHEEL +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev49.dist-info → simplex_localmodels-0.1.0.dev50.dist-info}/entry_points.txt +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev49.dist-info → simplex_localmodels-0.1.0.dev50.dist-info}/licenses/NOTICE +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev49.dist-info → simplex_localmodels-0.1.0.dev50.dist-info}/licenses/localmodels/HERMES-LICENSE +0 -0
|
@@ -83,6 +83,10 @@ OWNED_WRAPPER = "# localmodels: pi wrapper"
|
|
|
83
83
|
class HarnessError(RuntimeError):
|
|
84
84
|
"""A tool could not be planned, installed or wired; the message says why."""
|
|
85
85
|
|
|
86
|
+
def __init__(self, message: str, *, code: str | None = None) -> None:
|
|
87
|
+
super().__init__(message)
|
|
88
|
+
self.code = code
|
|
89
|
+
|
|
86
90
|
|
|
87
91
|
class NodeLock(TypedDict):
|
|
88
92
|
version: str
|
|
@@ -321,7 +325,9 @@ def plan_tools(
|
|
|
321
325
|
"""Reuse what works, install the pinned version of what is missing, never replace: Node.js,
|
|
322
326
|
pi, the gateway's program (Magpie, or this tool's Bifrost) and the gateway, with no model
|
|
323
327
|
behind them (``localmodels gateway``)."""
|
|
324
|
-
|
|
328
|
+
resolved = hosts.tools_dir(state.get("tools_dir") or tools_dir)
|
|
329
|
+
cleaned = resolved.rstrip("/\\")
|
|
330
|
+
tools_dir = cleaned if cleaned and not cleaned.endswith(":") else resolved
|
|
325
331
|
lock = lock or load_lock()
|
|
326
332
|
bin_dir = BIN_DIR
|
|
327
333
|
actions: list[ToolAction] = []
|
|
@@ -647,7 +653,7 @@ def _install_pi(
|
|
|
647
653
|
def install_tools(plan: ToolPlan, log: Callable[[str], None] = lambda message: None) -> list[str]:
|
|
648
654
|
"""Install the tools the plan marks ``install``; returns the paths created (owned)."""
|
|
649
655
|
lock = load_lock()
|
|
650
|
-
tools_dir = Path(os.path.expanduser(plan["tools_dir"]))
|
|
656
|
+
tools_dir = Path(os.path.expanduser(hosts.tools_dir(plan["tools_dir"])))
|
|
651
657
|
tools_dir.mkdir(parents=True, exist_ok=True)
|
|
652
658
|
bin_dir = Path(os.path.expanduser(BIN_DIR))
|
|
653
659
|
owned: list[str] = []
|
|
@@ -16,6 +16,7 @@ from localmodels import (
|
|
|
16
16
|
harness,
|
|
17
17
|
harness_probe,
|
|
18
18
|
hosts,
|
|
19
|
+
magpie_windows,
|
|
19
20
|
process_identity,
|
|
20
21
|
remote_ops_service,
|
|
21
22
|
runtime_io,
|
|
@@ -43,19 +44,34 @@ def address_port(addr: str) -> int:
|
|
|
43
44
|
return port
|
|
44
45
|
|
|
45
46
|
|
|
47
|
+
def _tools_path(tools_dir: str | Path) -> Path:
|
|
48
|
+
return Path(hosts.tools_dir(tools_dir)).expanduser()
|
|
49
|
+
|
|
50
|
+
|
|
46
51
|
def owned_address(addr: str, tools_dir: str, magpie: str | None) -> bool:
|
|
47
52
|
"""A socket is ours only with this private context's live process receipt."""
|
|
48
|
-
|
|
53
|
+
owned_gw = hosts.load_owned(tools_dir)["gateway"]
|
|
54
|
+
if not magpie or owned_gw["started"] not in ("serve", "daemon"):
|
|
49
55
|
return False
|
|
50
|
-
receipt =
|
|
56
|
+
receipt = _tools_path(tools_dir) / SERVE_PID
|
|
57
|
+
port = address_port(addr)
|
|
58
|
+
owned_magpie = owned_gw.get("magpie")
|
|
59
|
+
if owned_magpie is not None:
|
|
60
|
+
try:
|
|
61
|
+
if address_port(owned_magpie["addr"]) != port:
|
|
62
|
+
return False
|
|
63
|
+
except harness.HarnessError:
|
|
64
|
+
return False
|
|
51
65
|
state, pid = process_identity.status(receipt)
|
|
52
|
-
if state != "active" and
|
|
66
|
+
if state != "active" and magpie_windows.adopt_magpie_successor(
|
|
67
|
+
receipt, magpie, port=port, gateway_home=harness_probe.gateway_home(tools_dir)
|
|
68
|
+
):
|
|
53
69
|
state, pid = process_identity.status(receipt) # restarted into an updated binary
|
|
54
70
|
if state != "active" or pid is None:
|
|
55
71
|
return False
|
|
56
72
|
if not remote_ops_service.command_matches(pid, [magpie, "serve"]):
|
|
57
73
|
return False
|
|
58
|
-
return remote_ops_service.listener_problem(
|
|
74
|
+
return remote_ops_service.listener_problem(port, [pid]) is None
|
|
59
75
|
|
|
60
76
|
|
|
61
77
|
def address_listening(
|
|
@@ -100,9 +116,12 @@ def select_address(
|
|
|
100
116
|
if requested:
|
|
101
117
|
raise harness.HarnessError(
|
|
102
118
|
f"Magpie gateway address {addr} is held by a program without proof of ownership;"
|
|
103
|
-
" that program is kept"
|
|
119
|
+
" that program is kept",
|
|
120
|
+
code="gateway-port-in-use",
|
|
104
121
|
)
|
|
105
|
-
raise harness.HarnessError(
|
|
122
|
+
raise harness.HarnessError(
|
|
123
|
+
"Magpie default ports 3425..3456 are all in use", code="gateway-port-in-use"
|
|
124
|
+
)
|
|
106
125
|
|
|
107
126
|
|
|
108
127
|
def require_target(plan: harness.ToolPlan) -> None:
|
|
@@ -129,6 +148,7 @@ def plan_gateway(
|
|
|
129
148
|
lock: harness.ToolLock | None = None,
|
|
130
149
|
) -> tuple[harness.ToolPlan, list[str]]:
|
|
131
150
|
"""The existing pinned Magpie installation without Agent clients or LLM routing."""
|
|
151
|
+
tools_dir = hosts.tools_dir(tools_dir)
|
|
132
152
|
lock = lock or harness.load_lock()
|
|
133
153
|
bin_dir = harness.BIN_DIR
|
|
134
154
|
actions: list[harness.ToolAction] = []
|
|
@@ -159,7 +179,8 @@ def plan_gateway(
|
|
|
159
179
|
f"Magpie gateway address {state['gateway_addr']} is held by another user's program;"
|
|
160
180
|
" wiring into it would send your requests through someone else's gateway. Ask them to"
|
|
161
181
|
" free the port, or set MAGPIE_ADDR=127.0.0.1:<free port> in the same terminal, then"
|
|
162
|
-
" plan and apply again"
|
|
182
|
+
" plan and apply again",
|
|
183
|
+
code="gateway-port-in-use",
|
|
163
184
|
)
|
|
164
185
|
if state["gateway_up"]:
|
|
165
186
|
actions.append(
|
|
@@ -202,9 +223,8 @@ def _standalone_target(tools_dir: str) -> harness.ToolPlan | None:
|
|
|
202
223
|
try:
|
|
203
224
|
saved = json.loads(path.read_text(encoding="utf-8"))
|
|
204
225
|
tools = decode(saved["tools"], harness.ToolPlan)
|
|
205
|
-
if (
|
|
206
|
-
|
|
207
|
-
or Path(tools["tools_dir"]).expanduser() != Path(tools_dir).expanduser()
|
|
226
|
+
if digest(saved) != saved["plan_id"] or _tools_path(tools["tools_dir"]) != _tools_path(
|
|
227
|
+
tools_dir
|
|
208
228
|
):
|
|
209
229
|
continue
|
|
210
230
|
addr = next(t["path"] for t in tools["tools"] if t["tool"] == "gateway")
|
|
@@ -231,7 +251,7 @@ def _recorded_target(view: object, plan: object, tools_dir: str) -> harness.Tool
|
|
|
231
251
|
and digest(plan) == plan.get("plan_id") == view.get("plan_id")
|
|
232
252
|
and isinstance(plan.get("harness"), dict)
|
|
233
253
|
and isinstance(plan["harness"].get("tools_dir"), str)
|
|
234
|
-
and
|
|
254
|
+
and _tools_path(plan["harness"]["tools_dir"]) == _tools_path(tools_dir)
|
|
235
255
|
):
|
|
236
256
|
try:
|
|
237
257
|
return decode(plan["harness"], harness.HarnessPlan)
|
|
@@ -280,7 +300,7 @@ def removal_target(
|
|
|
280
300
|
or magpie is None
|
|
281
301
|
or (binary := _magpie_tool(target)) is None
|
|
282
302
|
or os.path.realpath(binary) != os.path.realpath(magpie)
|
|
283
|
-
or
|
|
303
|
+
or _tools_path(target["tools_dir"]) != _tools_path(tools_dir)
|
|
284
304
|
):
|
|
285
305
|
return None
|
|
286
306
|
try:
|
|
@@ -290,6 +310,40 @@ def removal_target(
|
|
|
290
310
|
return target
|
|
291
311
|
|
|
292
312
|
|
|
313
|
+
def owned_magpie_target(record: hosts.OwnedMagpie) -> harness.ToolPlan:
|
|
314
|
+
"""Reconstruct the approved ToolPlan for a private ``magpie serve`` recorded in owned.json."""
|
|
315
|
+
return {
|
|
316
|
+
"tools_dir": record["tools_dir"],
|
|
317
|
+
"tools": [
|
|
318
|
+
{
|
|
319
|
+
"tool": "magpie",
|
|
320
|
+
"action": "reuse",
|
|
321
|
+
"path": record["binary"],
|
|
322
|
+
"version": None,
|
|
323
|
+
"artifacts": [],
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
"tool": "gateway",
|
|
327
|
+
"action": "serve",
|
|
328
|
+
"path": record["addr"],
|
|
329
|
+
"version": None,
|
|
330
|
+
"artifacts": [],
|
|
331
|
+
},
|
|
332
|
+
],
|
|
333
|
+
"download_bytes": 0,
|
|
334
|
+
"warnings": [],
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def _recorded_magpie_target(tools_dir: str) -> harness.ToolPlan | None:
|
|
339
|
+
record = hosts.load_owned(tools_dir)["gateway"].get("magpie")
|
|
340
|
+
if record is None:
|
|
341
|
+
return None
|
|
342
|
+
if _tools_path(record["tools_dir"]) != _tools_path(tools_dir):
|
|
343
|
+
return None
|
|
344
|
+
return owned_magpie_target(record)
|
|
345
|
+
|
|
346
|
+
|
|
293
347
|
def stop_gateway(
|
|
294
348
|
magpie: str,
|
|
295
349
|
started: str,
|
|
@@ -300,7 +354,7 @@ def stop_gateway(
|
|
|
300
354
|
) -> Check:
|
|
301
355
|
"""Signal only a process whose private receipt still proves the approved gateway.
|
|
302
356
|
``forget=False`` (``stack down``) keeps it recorded as started, so ``stack up`` starts it."""
|
|
303
|
-
target = plan or accepted_plan(tools_dir)
|
|
357
|
+
target = plan or accepted_plan(tools_dir) or _recorded_magpie_target(tools_dir)
|
|
304
358
|
failure: Check = {
|
|
305
359
|
"check": "gateway",
|
|
306
360
|
"ok": False,
|
|
@@ -312,15 +366,20 @@ def stop_gateway(
|
|
|
312
366
|
if (
|
|
313
367
|
target is None
|
|
314
368
|
or started != "serve"
|
|
315
|
-
or
|
|
369
|
+
or _tools_path(target["tools_dir"]) != _tools_path(tools_dir)
|
|
316
370
|
or (binary := _magpie_tool(target)) is None
|
|
317
371
|
or os.path.realpath(binary) != os.path.realpath(magpie)
|
|
318
372
|
):
|
|
319
373
|
return failure
|
|
320
|
-
|
|
374
|
+
gw_action = next((t for t in target["tools"] if t["tool"] == "gateway"), None)
|
|
375
|
+
gw_home = harness_probe.gateway_home(tools_dir)
|
|
376
|
+
pid_file = _tools_path(tools_dir) / SERVE_PID
|
|
321
377
|
try:
|
|
378
|
+
port = address_port(gw_action["path"]) if gw_action is not None else None
|
|
322
379
|
state, pid = process_identity.status(pid_file)
|
|
323
|
-
if state != "active" and
|
|
380
|
+
if state != "active" and magpie_windows.adopt_magpie_successor(
|
|
381
|
+
pid_file, magpie, port=port, gateway_home=gw_home
|
|
382
|
+
):
|
|
324
383
|
state, pid = process_identity.status(pid_file)
|
|
325
384
|
if state == "active" and pid is not None:
|
|
326
385
|
require_target(target)
|
|
@@ -342,6 +401,8 @@ def stop_gateway(
|
|
|
342
401
|
}
|
|
343
402
|
if forget:
|
|
344
403
|
hosts.own_gateway(tools_dir, "started", None)
|
|
404
|
+
if hosts.load_owned(tools_dir)["gateway"].get("magpie") is not None:
|
|
405
|
+
hosts.own_magpie(tools_dir, None)
|
|
345
406
|
return {"check": "gateway", "ok": True, "detail": SERVE_STOPPED[outcome]}
|
|
346
407
|
|
|
347
408
|
|
|
@@ -405,7 +466,7 @@ def purge_gateway_profile(plan: harness.ToolPlan | None, notes: list[str]) -> No
|
|
|
405
466
|
)
|
|
406
467
|
return
|
|
407
468
|
else:
|
|
408
|
-
pid_file =
|
|
469
|
+
pid_file = _tools_path(plan["tools_dir"]) / SERVE_PID
|
|
409
470
|
if process_identity.status(pid_file)[0] != "inactive":
|
|
410
471
|
notes.append("- private gateway config kept: process still running or of unknown owner")
|
|
411
472
|
return
|
|
@@ -242,6 +242,8 @@ def gateway_environment(
|
|
|
242
242
|
MAGPIE_CONFIG_DIR=str(private / ".config/magpie"),
|
|
243
243
|
XDG_DATA_HOME=str(private / ".local/share"),
|
|
244
244
|
XDG_CACHE_HOME=str(private / ".cache"),
|
|
245
|
+
APPDATA=str(private / "AppData/Roaming"),
|
|
246
|
+
LOCALAPPDATA=str(private / "AppData/Local"),
|
|
245
247
|
)
|
|
246
248
|
env.setdefault("PI_CODING_AGENT_DIR", str(home / ".pi/agent"))
|
|
247
249
|
env.setdefault("CODEX_HOME", str(home / ".codex"))
|
|
@@ -269,7 +271,9 @@ def inspect(
|
|
|
269
271
|
from localmodels import harness_gateway, stack_clients
|
|
270
272
|
|
|
271
273
|
env = dict(os.environ if env is None else env)
|
|
272
|
-
tools_dir = os.path.expanduser(
|
|
274
|
+
tools_dir = os.path.expanduser(
|
|
275
|
+
hosts.tools_dir(approved["tools_dir"] if approved else tools_dir, env=env)
|
|
276
|
+
)
|
|
273
277
|
path = env.get("PATH", "")
|
|
274
278
|
home = env.get("HOME") or os.path.expanduser("~")
|
|
275
279
|
bin_dir = os.path.join(home, harness.BIN_DIR.removeprefix("~/"))
|
|
@@ -12,7 +12,7 @@ import os
|
|
|
12
12
|
import re
|
|
13
13
|
import threading
|
|
14
14
|
import time
|
|
15
|
-
from collections.abc import Callable, Iterator
|
|
15
|
+
from collections.abc import Callable, Iterator, Mapping
|
|
16
16
|
from contextlib import contextmanager
|
|
17
17
|
from pathlib import Path
|
|
18
18
|
from typing import Any, Literal, TypedDict, TypeVar
|
|
@@ -346,6 +346,15 @@ class OwnedBifrost(_OwnedBifrostSince20261110):
|
|
|
346
346
|
binary: str
|
|
347
347
|
|
|
348
348
|
|
|
349
|
+
class OwnedMagpie(TypedDict):
|
|
350
|
+
"""A private ``magpie serve`` instance this tool launched for its own gateway-home."""
|
|
351
|
+
|
|
352
|
+
addr: str
|
|
353
|
+
binary: str
|
|
354
|
+
gateway_home: str
|
|
355
|
+
tools_dir: str
|
|
356
|
+
|
|
357
|
+
|
|
349
358
|
class _OwnedGatewaySince20261031(TypedDict, total=False):
|
|
350
359
|
caller_keys: list[str] # ids of the Magpie gateway keys (callers' keys) this tool added
|
|
351
360
|
# The Magpie search APIs this tool added for the web search service the stack used to run:
|
|
@@ -353,6 +362,7 @@ class _OwnedGatewaySince20261031(TypedDict, total=False):
|
|
|
353
362
|
search: str # the searxng search API URL
|
|
354
363
|
search_you: bool # Magpie's You.com search API, the free fallback after it
|
|
355
364
|
bifrost: OwnedBifrost # since 2026-11-02: the Bifrost gateway this tool runs
|
|
365
|
+
magpie: OwnedMagpie # since 2026-11-20: private magpie serve launched for this gateway-home
|
|
356
366
|
|
|
357
367
|
|
|
358
368
|
class OwnedGateway(_OwnedGatewaySince20261031):
|
|
@@ -424,12 +434,37 @@ def empty_owned() -> Owned:
|
|
|
424
434
|
}
|
|
425
435
|
|
|
426
436
|
|
|
427
|
-
def
|
|
437
|
+
def _strip_trailing_sep(path: str) -> str:
|
|
438
|
+
cleaned = path.rstrip("/\\")
|
|
439
|
+
return cleaned if cleaned and not cleaned.endswith(":") else path
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def _is_default_tools_dir(given: str | Path, env: Mapping[str, str] | None = None) -> bool:
|
|
443
|
+
raw = str(given)
|
|
444
|
+
if raw.replace("\\", "/").rstrip("/") == TOOLS_DIR:
|
|
445
|
+
return True
|
|
446
|
+
norm_given = os.path.normcase(os.path.normpath(os.path.expanduser(raw)))
|
|
447
|
+
norm_default = os.path.normcase(os.path.normpath(os.path.expanduser(TOOLS_DIR)))
|
|
448
|
+
if norm_given == norm_default:
|
|
449
|
+
return True
|
|
450
|
+
if env is not None:
|
|
451
|
+
home = env.get("HOME") or env.get("USERPROFILE")
|
|
452
|
+
if home:
|
|
453
|
+
env_default = os.path.normcase(
|
|
454
|
+
os.path.normpath(os.path.join(home, TOOLS_DIR.removeprefix("~/")))
|
|
455
|
+
)
|
|
456
|
+
if norm_given == env_default:
|
|
457
|
+
return True
|
|
458
|
+
return False
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
def tools_dir(given: str | Path = TOOLS_DIR, *, env: Mapping[str, str] | None = None) -> str:
|
|
428
462
|
"""The tools root: ``LOCALMODELS_TOOLS_DIR`` overrides the default ``TOOLS_DIR``."""
|
|
429
|
-
if
|
|
430
|
-
|
|
463
|
+
environ = os.environ if env is None else env
|
|
464
|
+
if (override := environ.get("LOCALMODELS_TOOLS_DIR", "").strip()) and _is_default_tools_dir(
|
|
465
|
+
given, env=env
|
|
431
466
|
):
|
|
432
|
-
return override
|
|
467
|
+
return _strip_trailing_sep(override)
|
|
433
468
|
return str(given)
|
|
434
469
|
|
|
435
470
|
|
|
@@ -520,6 +555,18 @@ def own_gateway(
|
|
|
520
555
|
update_owned(change, tools_dir)
|
|
521
556
|
|
|
522
557
|
|
|
558
|
+
def own_magpie(tools_dir: str | Path, record: OwnedMagpie | None) -> None:
|
|
559
|
+
"""Record or clear the private ``magpie serve`` instance launched by this tool."""
|
|
560
|
+
|
|
561
|
+
def change(doc: Owned) -> None:
|
|
562
|
+
if record is None:
|
|
563
|
+
doc["gateway"].pop("magpie", None)
|
|
564
|
+
else:
|
|
565
|
+
doc["gateway"]["magpie"] = record
|
|
566
|
+
|
|
567
|
+
update_owned(change, tools_dir)
|
|
568
|
+
|
|
569
|
+
|
|
523
570
|
def forget_search(tools_dir: str | Path, field: Literal["search", "search_you"]) -> None:
|
|
524
571
|
"""Forget a Magpie search API this tool added for the web search service it used to run."""
|
|
525
572
|
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Floor metadata for library models that do not fit this computer's pools."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from localmodels import budget, context_policy, library_pricing, metal, suite, vision
|
|
6
|
+
from localmodels.estimator import agent_footprint_bytes, eager
|
|
7
|
+
from localmodels.library_pricing import Model
|
|
8
|
+
from localmodels.probe import HostFacts
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def floor_metadata(
|
|
12
|
+
facts: HostFacts,
|
|
13
|
+
model: Model,
|
|
14
|
+
shortfall_bytes: int = 0,
|
|
15
|
+
*,
|
|
16
|
+
memory_kind: str = "",
|
|
17
|
+
) -> tuple[int, int, bool, int]:
|
|
18
|
+
"""Estimated floor memory, context window, vision flag and download bytes when a model does
|
|
19
|
+
not fit this computer's pools (``plan is None``), using the same agent posture.
|
|
20
|
+
|
|
21
|
+
Honors per-entry posture overrides (such as ``ubatch``), accounts for Metal and CUDA UMA
|
|
22
|
+
unified memory, and avoids mixing host RAM shortfalls (``memory_kind == "ram"``) into VRAM
|
|
23
|
+
floor bytes.
|
|
24
|
+
"""
|
|
25
|
+
if model.entry.runtime == "strata-poly":
|
|
26
|
+
recipe = suite.recipe(model.entry.id)
|
|
27
|
+
window = int(recipe.get("context") or model.entry.n_ctx_train)
|
|
28
|
+
active = model.entry.agent_posture(
|
|
29
|
+
context_policy.posture_for(library_pricing.posture(), "cuda")
|
|
30
|
+
)
|
|
31
|
+
vision_on = bool(recipe.get("vision")) and active.vision != "off"
|
|
32
|
+
download = sum(
|
|
33
|
+
int(f["size_bytes"]) for f in recipe["files"] if vision_on or not f.get("vision")
|
|
34
|
+
)
|
|
35
|
+
floor_bytes = int(recipe.get("min_vram_free_bytes") or recipe.get("vram_bytes") or 0)
|
|
36
|
+
effective_shortfall = 0 if memory_kind == "ram" else shortfall_bytes
|
|
37
|
+
return max(effective_shortfall, floor_bytes), window, vision_on, download
|
|
38
|
+
metal_unified = metal.supported(facts)
|
|
39
|
+
cuda_uma = not metal_unified and budget.unified_pool_bytes(facts) is not None
|
|
40
|
+
active = model.entry.agent_posture(
|
|
41
|
+
context_policy.posture_for(library_pricing.posture(), "metal" if metal_unified else "cuda")
|
|
42
|
+
)
|
|
43
|
+
profile = eager(model.entry.profile(model.variant))
|
|
44
|
+
window = profile.n_ctx_train if 0 < profile.n_ctx_train < active.floor else active.floor
|
|
45
|
+
vision_on = vision.llama_reader_enabled(model.entry.mmproj, active.vision)
|
|
46
|
+
mtp = bool(model.entry.mtp_capable and model.entry.agent_mtp and active.mtp_draft_depth > 0)
|
|
47
|
+
core = agent_footprint_bytes(
|
|
48
|
+
profile,
|
|
49
|
+
window,
|
|
50
|
+
slots=active.slots,
|
|
51
|
+
kv_types=active.kv_types[-1],
|
|
52
|
+
ubatch=active.ubatch,
|
|
53
|
+
mtp=mtp,
|
|
54
|
+
)
|
|
55
|
+
if metal_unified or cuda_uma:
|
|
56
|
+
vision_extra = (
|
|
57
|
+
vision.llama_device_bytes(model.entry.mmproj, "gpu")
|
|
58
|
+
if vision_on and model.entry.mmproj is not None
|
|
59
|
+
else 0
|
|
60
|
+
)
|
|
61
|
+
host_side = (
|
|
62
|
+
context_policy.cache_floor_bytes(active, profile)
|
|
63
|
+
+ context_policy.metal_host_bytes(profile, active)
|
|
64
|
+
if metal_unified
|
|
65
|
+
else 0
|
|
66
|
+
)
|
|
67
|
+
floor_bytes = core + vision_extra + host_side
|
|
68
|
+
else:
|
|
69
|
+
floor_bytes = core
|
|
70
|
+
download = sum(f.size_bytes for f in model.variant.files)
|
|
71
|
+
if vision_on and model.entry.mmproj is not None:
|
|
72
|
+
download += model.entry.mmproj.size_bytes
|
|
73
|
+
if model.entry.mtp_head is not None:
|
|
74
|
+
download += model.entry.mtp_head.size_bytes
|
|
75
|
+
effective_shortfall = 0 if memory_kind == "ram" else shortfall_bytes
|
|
76
|
+
return max(effective_shortfall, floor_bytes), window, vision_on, download
|
|
@@ -21,6 +21,7 @@ from pathlib import Path
|
|
|
21
21
|
from localmodels import (
|
|
22
22
|
app,
|
|
23
23
|
deploy_steps,
|
|
24
|
+
frontend,
|
|
24
25
|
hosts,
|
|
25
26
|
library_gateway,
|
|
26
27
|
library_pricing,
|
|
@@ -147,6 +148,11 @@ def delete_weights(files: list[str], keep: set[str]) -> list[str]:
|
|
|
147
148
|
return deleted
|
|
148
149
|
|
|
149
150
|
|
|
151
|
+
KEEP_FAILED_DOWNLOAD_CODES = frozenset(
|
|
152
|
+
("DOWNLOAD_FAILED", "NETWORK_UNAVAILABLE", "DISK_INSUFFICIENT")
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
|
|
150
156
|
def _code(message: str) -> str:
|
|
151
157
|
text = message.lower()
|
|
152
158
|
if "sha256 mismatch" in text:
|
|
@@ -173,16 +179,24 @@ def _download_telemetry(phase: str, event: dict[str, object]) -> tuple[str | Non
|
|
|
173
179
|
|
|
174
180
|
|
|
175
181
|
class _Counter:
|
|
176
|
-
"""Bytes of the whole model: the files already verified
|
|
177
|
-
moves its file's counter for the phase it reports."""
|
|
182
|
+
"""Bytes of the whole model: the files already verified (or partially downloaded on disk)
|
|
183
|
+
count as done; each progress event moves its file's counter for the phase it reports."""
|
|
178
184
|
|
|
179
185
|
def __init__(self, model: str, plan: Plan, handle: JobHandle) -> None:
|
|
180
186
|
self.model, self.handle = model, handle
|
|
181
187
|
self.sizes = {os.path.basename(f["dest"]): f["size_bytes"] for f in plan["files"]}
|
|
182
188
|
self.done = {
|
|
183
|
-
os.path.basename(f["dest"]):
|
|
189
|
+
os.path.basename(f["dest"]): (
|
|
190
|
+
f["size_bytes"]
|
|
191
|
+
if plan_envelope.verified_in_place(f)
|
|
192
|
+
else min(
|
|
193
|
+
f["size_bytes"],
|
|
194
|
+
remote_ops_download.kept_bytes(
|
|
195
|
+
os.path.expanduser(f["dest"]) + ".part", f["size_bytes"]
|
|
196
|
+
),
|
|
197
|
+
)
|
|
198
|
+
)
|
|
184
199
|
for f in plan["files"]
|
|
185
|
-
if plan_envelope.verified_in_place(f)
|
|
186
200
|
}
|
|
187
201
|
|
|
188
202
|
@property
|
|
@@ -250,7 +264,9 @@ def fetch(
|
|
|
250
264
|
"""The plan's engine (when it is a pinned release) and weights, as stack apply fetches them;
|
|
251
265
|
``only`` limits the weights to the files this job claims. Progress counts the whole model."""
|
|
252
266
|
counter = _Counter(model, plan, handle)
|
|
253
|
-
|
|
267
|
+
initial_done = sum(counter.done.values())
|
|
268
|
+
handle.update("download", initial_done, counter.total)
|
|
269
|
+
progress(model, "download", initial_done, counter.total)
|
|
254
270
|
files = [f for f in plan["files"] if only is None or os.path.expanduser(f["dest"]) in only]
|
|
255
271
|
steps: tuple[Action, ...] = ("download",) if files else ()
|
|
256
272
|
if engine and plan["engine"]["source"] == "release":
|
|
@@ -264,6 +280,11 @@ def fetch(
|
|
|
264
280
|
if result.returncode != 0 or outcome.get("event") != "result":
|
|
265
281
|
message = str(outcome.get("message") or result.stderr.strip()[-500:] or "failed")
|
|
266
282
|
raise LibraryError(_code(message), message, failed=True)
|
|
283
|
+
if engine and (reqs := plan["launch"].get("frontend_requirements")):
|
|
284
|
+
try:
|
|
285
|
+
frontend.install(str(plan["launch"]["frontend_base_dir"]), str(reqs))
|
|
286
|
+
except frontend.FrontendError as exc:
|
|
287
|
+
raise LibraryError("DOWNLOAD_FAILED", str(exc), failed=True) from None
|
|
267
288
|
progress(model, "verify", counter.total, counter.total)
|
|
268
289
|
|
|
269
290
|
|
|
@@ -426,7 +447,8 @@ def download(
|
|
|
426
447
|
others = claimed_elsewhere(model.id)
|
|
427
448
|
mine = [f for f in files if os.path.realpath(f) not in others]
|
|
428
449
|
job = library_state.acquire("download", model.id, priced.pool, memory, mine, files)
|
|
429
|
-
|
|
450
|
+
claims = [job]
|
|
451
|
+
with library_state.held(claims):
|
|
430
452
|
handle = library_state.JobHandle(job)
|
|
431
453
|
try:
|
|
432
454
|
_fetch_and_commit(model, plan, handle, priced.pool, memory, base_dir, mine, files)
|
|
@@ -437,6 +459,16 @@ def download(
|
|
|
437
459
|
if library_state.load(model.id) is None:
|
|
438
460
|
_give_up(model.id, job, handle)
|
|
439
461
|
raise LibraryError("CANCELLED", "the download was cancelled") from None
|
|
462
|
+
except LibraryError as exc:
|
|
463
|
+
if exc.code in KEEP_FAILED_DOWNLOAD_CODES:
|
|
464
|
+
with library_state.locked():
|
|
465
|
+
if library_state.load(model.id) is None and library_state.current(job):
|
|
466
|
+
if library_state.cancelled(job):
|
|
467
|
+
_give_up(model.id, job, handle)
|
|
468
|
+
raise LibraryError("CANCELLED", "the download was cancelled") from None
|
|
469
|
+
library_state.fail_job(handle.job, {"code": exc.code, "detail": exc.detail})
|
|
470
|
+
claims.clear()
|
|
471
|
+
raise
|
|
440
472
|
if not autoload:
|
|
441
473
|
return library_status.model_row(library_status.Library.read(probe(), base_dir), model.id)
|
|
442
474
|
try:
|