@simplexai/localmodels-linux-x64 0.1.0-dev.36 → 0.1.0-dev.37
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 +119 -98
- package/kit.py +1 -0
- package/modules/localmodels/__init__.py +1 -1
- package/modules/localmodels/__main__.py +9 -3
- package/modules/localmodels/bifrost_config.py +188 -0
- package/modules/localmodels/budget.py +13 -2
- package/modules/localmodels/catalog.py +51 -320
- package/modules/localmodels/catalog_add.py +2 -8
- package/modules/localmodels/catalog_recipes.py +321 -0
- package/modules/localmodels/cli_parser.py +4 -1
- package/modules/localmodels/console.py +5 -4
- package/modules/localmodels/contracts.py +13 -3
- package/modules/localmodels/debug_cli.py +158 -0
- package/modules/localmodels/debug_mode.py +727 -0
- package/modules/localmodels/debug_stub_server.py +192 -0
- package/modules/localmodels/decode_calibration.json +1 -1
- package/modules/localmodels/decode_calibration.py +34 -17
- package/modules/localmodels/gateway.py +21 -155
- package/modules/localmodels/gateway_accept.py +201 -0
- package/modules/localmodels/gateway_bifrost.py +19 -184
- package/modules/localmodels/gateway_cli.py +11 -4
- package/modules/localmodels/gateway_foundations.py +2 -4
- package/modules/localmodels/gateway_probe.py +0 -1
- package/modules/localmodels/gateway_server.py +2 -3
- package/modules/localmodels/gpu_specs.py +12 -0
- package/modules/localmodels/guard.py +2 -7
- package/modules/localmodels/harness.py +16 -129
- package/modules/localmodels/harness_cleanup.py +135 -0
- package/modules/localmodels/harness_gateway.py +16 -14
- package/modules/localmodels/harness_probe.py +12 -4
- package/modules/localmodels/hosts.py +25 -2
- package/modules/localmodels/http_local.py +59 -0
- package/modules/localmodels/install.py +23 -8
- package/modules/localmodels/library_gateway.py +9 -9
- package/modules/localmodels/library_jobs.py +76 -27
- package/modules/localmodels/library_serve.py +12 -1
- package/modules/localmodels/library_state.py +6 -1
- package/modules/localmodels/llm_rank.py +102 -9
- package/modules/localmodels/probe.py +108 -174
- package/modules/localmodels/process_identity.py +109 -154
- package/modules/localmodels/process_windows.py +86 -0
- package/modules/localmodels/recommend.py +1 -1
- package/modules/localmodels/recommend_cli.py +14 -6
- package/modules/localmodels/remote_ops.py +65 -0
- package/modules/localmodels/remote_ops_service.py +14 -2
- package/modules/localmodels/runtime_args.py +10 -3
- package/modules/localmodels/runtime_auth.py +5 -9
- package/modules/localmodels/runtime_cli.py +128 -129
- package/modules/localmodels/runtime_discovery.py +84 -0
- package/modules/localmodels/runtime_install.py +101 -0
- package/modules/localmodels/runtime_releases.py +302 -0
- package/modules/localmodels/service_stop.py +8 -108
- package/modules/localmodels/service_upgrade.py +394 -63
- package/modules/localmodels/services_cli.py +24 -14
- package/modules/localmodels/setup_inspection.py +97 -9
- package/modules/localmodels/setup_text.py +7 -0
- package/modules/localmodels/sizecalc.py +0 -1
- package/modules/localmodels/stack.py +14 -221
- package/modules/localmodels/stack_arena.py +732 -0
- package/modules/localmodels/stack_args.py +15 -9
- package/modules/localmodels/stack_auto.py +384 -3
- package/modules/localmodels/stack_aux_gpu.py +4 -3
- package/modules/localmodels/stack_capacity.py +138 -0
- package/modules/localmodels/stack_cleanup.py +9 -4
- package/modules/localmodels/stack_cli.py +286 -0
- package/modules/localmodels/stack_compose.py +58 -25
- package/modules/localmodels/stack_console.py +9 -6
- package/modules/localmodels/stack_hardware.py +92 -263
- package/modules/localmodels/stack_hardware_text.py +263 -0
- package/modules/localmodels/stack_journal.py +3 -9
- package/modules/localmodels/stack_ledger.py +48 -18
- package/modules/localmodels/stack_lifecycle.py +33 -312
- package/modules/localmodels/stack_mcp.py +729 -0
- package/modules/localmodels/stack_memory.py +0 -2
- package/modules/localmodels/stack_picks.py +59 -12
- package/modules/localmodels/stack_plan.py +19 -1
- package/modules/localmodels/stack_preflight.py +11 -19
- package/modules/localmodels/stack_remove.py +373 -0
- package/modules/localmodels/stack_route.py +101 -17
- package/modules/localmodels/stack_suite.py +11 -7
- package/modules/localmodels/stack_summary.py +14 -0
- package/modules/localmodels/stack_try.py +238 -24
- package/modules/localmodels/stack_try_client.py +53 -15
- package/modules/localmodels/stack_try_docs.py +2 -2
- package/modules/localmodels/stack_view.py +24 -171
- package/modules/localmodels/stack_view_access.py +114 -1
- package/modules/localmodels/stack_view_agent.py +3 -13
- package/modules/localmodels/stack_view_hub.py +9 -10
- package/modules/localmodels/stack_view_llm.py +1 -1
- package/modules/localmodels/stack_view_sandbox.py +2 -2
- package/modules/localmodels/stack_view_schema.py +183 -0
- package/modules/localmodels/stack_view_serve.py +7 -1
- package/modules/localmodels/stack_view_service.py +1 -2
- package/modules/localmodels/stack_view_text.py +13 -9
- package/modules/localmodels/stack_warm.py +3 -0
- package/modules/localmodels/stack_warm_store.py +517 -0
- package/modules/localmodels/tui.py +28 -4
- package/modules/localmodels/tui_dialogs.py +136 -0
- package/modules/localmodels/tui_modals.py +279 -0
- package/modules/localmodels/tui_screen.py +113 -154
- package/modules/localmodels/tui_views.py +134 -203
- package/modules/localmodels/web/index.html +510 -101
- package/modules/localmodels/web/sizecalc/app.js +363 -10
- package/modules/localmodels/web/sizecalc/page.html +27 -3
- package/modules/localmodels/web/view/fleet.html +11 -2
- package/modules/localmodels/web/view/fleet.js +191 -17
- package/modules/localmodels/web/view/i18n.js +3 -2
- package/modules/localmodels/web/view/index.html +1 -1
- package/modules/localmodels/web/view/nav.js +22 -3
- package/modules/localmodels/web/view/view.css +63 -18
- package/modules/localmodels/web/view/view.js +169 -32
- package/modules/{simplex_localmodels-0.1.0.dev36.dist-info → simplex_localmodels-0.1.0.dev37.dist-info}/METADATA +1 -1
- package/modules/{simplex_localmodels-0.1.0.dev36.dist-info → simplex_localmodels-0.1.0.dev37.dist-info}/RECORD +115 -94
- package/package.json +1 -1
- package/tool.json +1 -1
- package/modules/localmodels/engines-strata-0.1.49.1.json +0 -31
- /package/modules/{simplex_localmodels-0.1.0.dev36.dist-info → simplex_localmodels-0.1.0.dev37.dist-info}/WHEEL +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev36.dist-info → simplex_localmodels-0.1.0.dev37.dist-info}/entry_points.txt +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev36.dist-info → simplex_localmodels-0.1.0.dev37.dist-info}/licenses/NOTICE +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev36.dist-info → simplex_localmodels-0.1.0.dev37.dist-info}/licenses/localmodels/HERMES-LICENSE +0 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Executable candidates from the user's computer and previous native installations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import platform
|
|
7
|
+
import re
|
|
8
|
+
import shutil
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from localmodels import runtime_process
|
|
12
|
+
from localmodels.runtime_io import private_permissions
|
|
13
|
+
from localmodels.runtime_releases import MINIMUM, codex_member, version_key
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def private_file(path: Path) -> bool:
|
|
17
|
+
try:
|
|
18
|
+
if not path.is_file() or path.is_symlink():
|
|
19
|
+
return False
|
|
20
|
+
private_permissions(path)
|
|
21
|
+
return True
|
|
22
|
+
except (OSError, ValueError):
|
|
23
|
+
return False
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def managed_commands(root: Path, runtime: str, native_platform: str) -> list[list[str]]:
|
|
27
|
+
"""Find previously installed releases without contacting the registry."""
|
|
28
|
+
try:
|
|
29
|
+
member = (
|
|
30
|
+
codex_member(native_platform)
|
|
31
|
+
if runtime == "codex"
|
|
32
|
+
else runtime + (".exe" if os.name == "nt" else "")
|
|
33
|
+
)
|
|
34
|
+
except KeyError:
|
|
35
|
+
return []
|
|
36
|
+
parent = root / "tools" / runtime
|
|
37
|
+
versions = [
|
|
38
|
+
path
|
|
39
|
+
for path in parent.glob("*")
|
|
40
|
+
if re.fullmatch(r"\d+\.\d+\.\d+", path.name)
|
|
41
|
+
and not path.is_symlink()
|
|
42
|
+
and private_file(path / "localmodels-runtime.json")
|
|
43
|
+
and (path / member).is_file()
|
|
44
|
+
]
|
|
45
|
+
versions.sort(key=lambda path: tuple(map(int, path.name.split("."))), reverse=True)
|
|
46
|
+
return [[str(path / member)] for path in versions]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def host_commands(runtime: str, native_platform: str) -> list[str]:
|
|
50
|
+
home = Path.home()
|
|
51
|
+
directories = [*os.get_exec_path(), str(home / ".local/bin")]
|
|
52
|
+
if os.name == "nt" and (appdata := os.environ.get("APPDATA")):
|
|
53
|
+
directories.append(str(Path(appdata) / "npm"))
|
|
54
|
+
commands = [
|
|
55
|
+
executable
|
|
56
|
+
for directory in dict.fromkeys(directories)
|
|
57
|
+
if Path(directory).is_absolute()
|
|
58
|
+
and (executable := shutil.which(runtime, path=directory))
|
|
59
|
+
and Path(executable).is_absolute()
|
|
60
|
+
]
|
|
61
|
+
if runtime in ("codex", "claude") and native_platform.startswith("darwin-"):
|
|
62
|
+
for directory in (Path("/Applications"), home / "Applications"):
|
|
63
|
+
bundle = directory / f"{runtime.capitalize()}.app/Contents/Resources/{runtime}"
|
|
64
|
+
if bundle.is_file():
|
|
65
|
+
commands.append(str(bundle))
|
|
66
|
+
return list(dict.fromkeys(commands)) or [runtime]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def platform_tag() -> str:
|
|
70
|
+
system = {"Windows": "win32", "Darwin": "darwin", "Linux": "linux"}.get(
|
|
71
|
+
platform.system(), "unknown"
|
|
72
|
+
)
|
|
73
|
+
arch = {"x86_64": "x64", "AMD64": "x64", "aarch64": "arm64", "arm64": "arm64"}.get(
|
|
74
|
+
platform.machine(), "unknown"
|
|
75
|
+
)
|
|
76
|
+
return f"{system}-{arch}"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def compatible_version(
|
|
80
|
+
runtime: str, version: str | None, *, before_version: str | None = None
|
|
81
|
+
) -> bool:
|
|
82
|
+
if version is None or not runtime_process.version_ok(version, MINIMUM[runtime]):
|
|
83
|
+
return False
|
|
84
|
+
return before_version is None or version_key(version) < version_key(before_version)
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""Artifact caching, unpacking and staged tree installation for native agent runtimes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import shutil
|
|
7
|
+
from collections.abc import Callable
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from localmodels import http_local
|
|
12
|
+
from localmodels.runtime_io import (
|
|
13
|
+
Event,
|
|
14
|
+
cached_download,
|
|
15
|
+
download,
|
|
16
|
+
extract_archive,
|
|
17
|
+
extract_binary,
|
|
18
|
+
private_directory,
|
|
19
|
+
private_permissions,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def bootstrap(path: Path, destination: str) -> dict[str, str]:
|
|
24
|
+
if path.is_symlink():
|
|
25
|
+
raise ValueError("Unsafe bootstrap executable")
|
|
26
|
+
return dict(path=str(path), destination=destination, digest=http_local.sha256_file(path))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def downloads_dir(root: Path, runtime: str) -> Path:
|
|
30
|
+
"""Where apply keeps verified and partial downloads until a run uses them, so a retry after
|
|
31
|
+
a network failure resumes instead of starting over."""
|
|
32
|
+
return root / "tools" / ".downloads" / runtime
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def fetch_all(
|
|
36
|
+
plan: dict[str, Any], event: Event, *, download_fn: Callable[..., Any] = download
|
|
37
|
+
) -> list[Path]:
|
|
38
|
+
"""Download every artifact first: one that failed leaves the others ready for the retry."""
|
|
39
|
+
cache = downloads_dir(Path(plan["root"]), plan["runtime"])
|
|
40
|
+
wanted = [*plan["artifacts"], plan.get("nodeArtifact"), plan.get("launcherArtifact")]
|
|
41
|
+
return [cached_download(cache, a, event, download_fn) if a else Path() for a in wanted]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def take(kept: Path, file: Path) -> None:
|
|
45
|
+
kept.rename(file)
|
|
46
|
+
private_permissions(file, created=True)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def install_tree(
|
|
50
|
+
plan: dict[str, Any],
|
|
51
|
+
stage: Path,
|
|
52
|
+
event: Event,
|
|
53
|
+
*,
|
|
54
|
+
download_fn: Callable[..., Any] = download,
|
|
55
|
+
) -> Path:
|
|
56
|
+
tree = stage / "runtime"
|
|
57
|
+
private_directory(tree)
|
|
58
|
+
fetched = fetch_all(plan, event, download_fn=download_fn)
|
|
59
|
+
for index, artifact in enumerate(plan["artifacts"]):
|
|
60
|
+
file = stage / f"artifact-{index}"
|
|
61
|
+
take(fetched[index], file)
|
|
62
|
+
event({"kind": "stage", "stage": "extract", "state": "running"})
|
|
63
|
+
if artifact["format"] == "binary":
|
|
64
|
+
shutil.copyfile(file, tree / artifact["executable"])
|
|
65
|
+
(tree / artifact["executable"]).chmod(0o700)
|
|
66
|
+
private_permissions(tree / artifact["executable"], created=True)
|
|
67
|
+
else:
|
|
68
|
+
unpack = stage / f"unpack-{index}"
|
|
69
|
+
extract_archive(file, unpack)
|
|
70
|
+
if plan["runtime"] == "pi" and index == 1:
|
|
71
|
+
target = tree / artifact["destination"]
|
|
72
|
+
private_directory(target.parent)
|
|
73
|
+
(unpack / "package").rename(target)
|
|
74
|
+
else:
|
|
75
|
+
for entry in unpack.iterdir():
|
|
76
|
+
entry.rename(tree / entry.name)
|
|
77
|
+
if plan.get("nodeArtifact"):
|
|
78
|
+
node_archive = stage / "node-archive"
|
|
79
|
+
take(fetched[-2], node_archive)
|
|
80
|
+
extract_binary(
|
|
81
|
+
node_archive,
|
|
82
|
+
plan["nodeArtifact"]["member"],
|
|
83
|
+
tree / ("node.exe" if os.name == "nt" else "node"),
|
|
84
|
+
)
|
|
85
|
+
if plan.get("launcherArtifact"):
|
|
86
|
+
take(fetched[-1], tree / "pi.exe")
|
|
87
|
+
for item in plan["bootstrap"]:
|
|
88
|
+
source = Path(item["path"])
|
|
89
|
+
if source.is_symlink() or http_local.sha256_file(source) != item["digest"]:
|
|
90
|
+
raise ValueError("Bootstrap artifact changed; create a new plan")
|
|
91
|
+
destination = tree / item["destination"]
|
|
92
|
+
shutil.copyfile(source, destination)
|
|
93
|
+
destination.chmod(0o700)
|
|
94
|
+
private_permissions(destination, created=True)
|
|
95
|
+
if plan["runtime"] == "pi" and os.name != "nt":
|
|
96
|
+
wrapper = tree / "pi"
|
|
97
|
+
wrapper.write_text(
|
|
98
|
+
'#!/bin/sh\nHERE=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)\nexec "$HERE/node" "$HERE/package/dist/bundle/cli.js" "$@"\n'
|
|
99
|
+
)
|
|
100
|
+
wrapper.chmod(0o700)
|
|
101
|
+
return tree
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
"""Official stable release metadata for native runtime installation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import json
|
|
7
|
+
import re
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
from urllib.error import HTTPError
|
|
11
|
+
from urllib.parse import quote, urlsplit
|
|
12
|
+
from urllib.request import Request
|
|
13
|
+
|
|
14
|
+
from localmodels import npm_transport, runtime_process
|
|
15
|
+
|
|
16
|
+
CLAUDE_BASE = "https://downloads.claude.ai/claude-code-releases"
|
|
17
|
+
CLAUDE_PACKAGE = "@anthropic-ai/claude-code"
|
|
18
|
+
PI_PACKAGE = "@earendil-works/pi-coding-agent"
|
|
19
|
+
PI_IMAGE_MODULE = "@silvia-odwyer/photon-node"
|
|
20
|
+
PI_ENTRY = "dist/bundle/cli.js"
|
|
21
|
+
MINIMUM = {"codex": "0.100.0", "claude": "2.1.282", "pi": "1.2.0"}
|
|
22
|
+
STABLE = re.compile(r"[0-9]+\.[0-9]+\.[0-9]+")
|
|
23
|
+
REGISTRY = "https://registry.npmjs.org"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _object(value: Any) -> dict[str, Any]:
|
|
27
|
+
if not isinstance(value, dict):
|
|
28
|
+
raise ValueError("Invalid runtime release metadata")
|
|
29
|
+
return value
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _text(value: Any) -> str:
|
|
33
|
+
if not isinstance(value, str):
|
|
34
|
+
raise ValueError("Invalid runtime release metadata")
|
|
35
|
+
return value
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _read(url: str, limit: int, accept: str = "application/json") -> bytes:
|
|
39
|
+
request = Request(url, headers={"Accept": accept})
|
|
40
|
+
with npm_transport.https_opener().open(request, timeout=12) as response:
|
|
41
|
+
if urlsplit(response.geturl()).netloc != urlsplit(url).netloc:
|
|
42
|
+
raise ValueError("Unexpected runtime metadata host")
|
|
43
|
+
data: bytes = response.read(limit + 1)
|
|
44
|
+
if len(data) > limit:
|
|
45
|
+
raise ValueError("Runtime metadata exceeds the size limit")
|
|
46
|
+
return data
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _npm(package: str, tag: str | None) -> dict[str, Any]:
|
|
50
|
+
base = f"{REGISTRY}/{quote(package, safe='@')}"
|
|
51
|
+
value = _object(
|
|
52
|
+
json.loads(
|
|
53
|
+
_read(base, 16 * 1024 * 1024, "application/vnd.npm.install-v1+json")
|
|
54
|
+
if tag is None
|
|
55
|
+
else _read(f"{base}/{quote(tag, safe='')}", 1024 * 1024)
|
|
56
|
+
)
|
|
57
|
+
)
|
|
58
|
+
if value.get("name") != package:
|
|
59
|
+
raise ValueError("Unexpected npm package")
|
|
60
|
+
return value
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _archive(release: dict[str, Any], destination: str) -> dict[str, Any]:
|
|
64
|
+
package = _text(release.get("name"))
|
|
65
|
+
version = _text(release.get("version"))
|
|
66
|
+
url = f"{REGISTRY}/{package}/-/{package.rsplit('/', 1)[-1]}-{version}.tgz"
|
|
67
|
+
dist = _object(release.get("dist"))
|
|
68
|
+
algorithm, _, value = _text(dist.get("integrity")).partition("-")
|
|
69
|
+
digest = base64.b64decode(value, validate=True)
|
|
70
|
+
if algorithm != "sha512" or len(digest) != 64 or _text(dist.get("tarball")) != url:
|
|
71
|
+
raise ValueError("Invalid npm artifact integrity")
|
|
72
|
+
return dict(
|
|
73
|
+
url=url,
|
|
74
|
+
algorithm=algorithm,
|
|
75
|
+
digest=digest.hex(),
|
|
76
|
+
maxBytes=64 * 1024**2,
|
|
77
|
+
format="tar",
|
|
78
|
+
destination=destination,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _claude_artifacts(platform: str, version: str) -> list[dict[str, Any]] | None:
|
|
83
|
+
if platform.startswith("linux-") and any(Path("/lib").glob("ld-musl-*.so.1")):
|
|
84
|
+
platform += "-musl"
|
|
85
|
+
manifest = _object(json.loads(_read(f"{CLAUDE_BASE}/{version}/manifest.json", 1024 * 1024)))
|
|
86
|
+
if manifest.get("version") != version:
|
|
87
|
+
raise ValueError("Claude manifest version differs from the requested release")
|
|
88
|
+
platforms = _object(manifest.get("platforms"))
|
|
89
|
+
if platform not in platforms:
|
|
90
|
+
return None
|
|
91
|
+
artifact = _object(platforms[platform])
|
|
92
|
+
executable = "claude.exe" if platform.startswith("win32-") else "claude"
|
|
93
|
+
digest, size = _text(artifact.get("checksum")), artifact.get("size")
|
|
94
|
+
if (
|
|
95
|
+
artifact.get("binary") != executable
|
|
96
|
+
or not re.fullmatch(r"[0-9a-f]{64}", digest)
|
|
97
|
+
or type(size) is not int
|
|
98
|
+
or not 0 < size <= 512 * 1024**2
|
|
99
|
+
):
|
|
100
|
+
raise ValueError("Invalid Claude platform checksums")
|
|
101
|
+
return [
|
|
102
|
+
dict(
|
|
103
|
+
url=f"{CLAUDE_BASE}/{version}/{platform}/{executable}",
|
|
104
|
+
algorithm="sha256",
|
|
105
|
+
digest=digest,
|
|
106
|
+
maxBytes=size,
|
|
107
|
+
format="binary",
|
|
108
|
+
executable=executable,
|
|
109
|
+
)
|
|
110
|
+
]
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _pi_artifacts(version: str, node_version: str | None) -> list[dict[str, Any]] | None:
|
|
114
|
+
release = _npm(PI_PACKAGE, version)
|
|
115
|
+
minimum = re.fullmatch(
|
|
116
|
+
r">=\s*([0-9]+\.[0-9]+\.[0-9]+)",
|
|
117
|
+
_text(_object(release.get("engines", {})).get("node", "")),
|
|
118
|
+
)
|
|
119
|
+
if (
|
|
120
|
+
release["version"] != version
|
|
121
|
+
or release.get("deprecated")
|
|
122
|
+
or not minimum
|
|
123
|
+
or (node_version is not None and not runtime_process.version_ok(node_version, minimum[1]))
|
|
124
|
+
or _object(release.get("bin", {})).get("pi") != PI_ENTRY
|
|
125
|
+
):
|
|
126
|
+
return None
|
|
127
|
+
# Pi ships a bundled CLI; the image module it loads at runtime is published
|
|
128
|
+
# separately. Resolve the exact dependency declared by each Pi release.
|
|
129
|
+
image_version = _text(_object(release.get("dependencies")).get(PI_IMAGE_MODULE, ""))
|
|
130
|
+
if not STABLE.fullmatch(image_version):
|
|
131
|
+
return None
|
|
132
|
+
image = _npm(PI_IMAGE_MODULE, image_version)
|
|
133
|
+
if image["version"] != image_version or image.get("deprecated"):
|
|
134
|
+
return None
|
|
135
|
+
return [_archive(release, ""), _archive(image, f"package/node_modules/{PI_IMAGE_MODULE}")]
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def version_key(version: str) -> tuple[int, ...]:
|
|
139
|
+
if not isinstance(version, str) or not STABLE.fullmatch(version):
|
|
140
|
+
raise ValueError("Not a stable runtime version")
|
|
141
|
+
return tuple(map(int, version.split(".")))
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def resolve(
|
|
145
|
+
runtime: str,
|
|
146
|
+
platform: str,
|
|
147
|
+
version: str | None = None,
|
|
148
|
+
*,
|
|
149
|
+
before_version: str | None = None,
|
|
150
|
+
node_version: str | None = None,
|
|
151
|
+
) -> tuple[str, list[dict[str, Any]]]:
|
|
152
|
+
package = CLAUDE_PACKAGE if runtime == "claude" else PI_PACKAGE
|
|
153
|
+
|
|
154
|
+
def artifacts(candidate: str) -> list[dict[str, Any]] | None:
|
|
155
|
+
version_key(candidate)
|
|
156
|
+
if not runtime_process.version_ok(candidate, MINIMUM[runtime]):
|
|
157
|
+
return None
|
|
158
|
+
try:
|
|
159
|
+
return (
|
|
160
|
+
_claude_artifacts(platform, candidate)
|
|
161
|
+
if runtime == "claude"
|
|
162
|
+
else _pi_artifacts(candidate, node_version)
|
|
163
|
+
)
|
|
164
|
+
except HTTPError as error:
|
|
165
|
+
if error.code == 404:
|
|
166
|
+
return None
|
|
167
|
+
raise
|
|
168
|
+
|
|
169
|
+
if version is not None:
|
|
170
|
+
exact = artifacts(version)
|
|
171
|
+
if exact:
|
|
172
|
+
return version, exact
|
|
173
|
+
raise ValueError(f"Approved {runtime} release is unavailable")
|
|
174
|
+
|
|
175
|
+
latest = (
|
|
176
|
+
_read(f"{CLAUDE_BASE}/stable", 100, "text/plain").decode().strip()
|
|
177
|
+
if runtime == "claude"
|
|
178
|
+
else _text(_npm(package, "latest").get("version"))
|
|
179
|
+
)
|
|
180
|
+
bound = version_key(latest)
|
|
181
|
+
if before_version is None or bound < version_key(before_version):
|
|
182
|
+
first = artifacts(latest)
|
|
183
|
+
if first:
|
|
184
|
+
return latest, first
|
|
185
|
+
elif before_version is not None:
|
|
186
|
+
bound = version_key(before_version)
|
|
187
|
+
|
|
188
|
+
versions = _object(_npm(package, None).get("versions"))
|
|
189
|
+
candidates = sorted(
|
|
190
|
+
(
|
|
191
|
+
tag
|
|
192
|
+
for tag in versions
|
|
193
|
+
if STABLE.fullmatch(tag)
|
|
194
|
+
and version_key(tag) < bound
|
|
195
|
+
and not _object(versions[tag]).get("deprecated")
|
|
196
|
+
),
|
|
197
|
+
key=version_key,
|
|
198
|
+
reverse=True,
|
|
199
|
+
)
|
|
200
|
+
for candidate in candidates:
|
|
201
|
+
if found := artifacts(candidate):
|
|
202
|
+
return candidate, found
|
|
203
|
+
raise ValueError(f"No compatible stable {runtime} release is available for {platform}")
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def codex_member(native_platform: str) -> str:
|
|
207
|
+
target = {
|
|
208
|
+
"darwin-arm64": "aarch64-apple-darwin",
|
|
209
|
+
"darwin-x64": "x86_64-apple-darwin",
|
|
210
|
+
"linux-arm64": "aarch64-unknown-linux-musl",
|
|
211
|
+
"linux-x64": "x86_64-unknown-linux-musl",
|
|
212
|
+
"win32-arm64": "aarch64-pc-windows-msvc",
|
|
213
|
+
"win32-x64": "x86_64-pc-windows-msvc",
|
|
214
|
+
}[native_platform]
|
|
215
|
+
return f"package/vendor/{target}/bin/codex" + (
|
|
216
|
+
".exe" if native_platform.startswith("win32-") else ""
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _codex_metadata(tag: str | None) -> dict[str, Any]:
|
|
221
|
+
return _npm("@openai/codex", tag)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _codex_platform_release(native_platform: str, version: str) -> dict[str, Any] | None:
|
|
225
|
+
platform_name, arch = native_platform.split("-", 1)
|
|
226
|
+
platform_version = f"{version}-{native_platform}"
|
|
227
|
+
try:
|
|
228
|
+
release = _codex_metadata(platform_version)
|
|
229
|
+
except HTTPError as error:
|
|
230
|
+
if error.code == 404:
|
|
231
|
+
return None
|
|
232
|
+
raise
|
|
233
|
+
if (
|
|
234
|
+
release["version"] != platform_version
|
|
235
|
+
or not runtime_process.version_ok(version, MINIMUM["codex"])
|
|
236
|
+
or release.get("deprecated")
|
|
237
|
+
or platform_name not in release.get("os", [])
|
|
238
|
+
or arch not in release.get("cpu", [])
|
|
239
|
+
):
|
|
240
|
+
return None
|
|
241
|
+
dist = _object(release.get("dist"))
|
|
242
|
+
url = f"{REGISTRY}/@openai/codex/-/codex-{platform_version}.tgz"
|
|
243
|
+
integrity = _text(dist.get("integrity"))
|
|
244
|
+
digest = base64.b64decode(integrity.removeprefix("sha512-"), validate=True)
|
|
245
|
+
if (
|
|
246
|
+
_text(dist.get("tarball")) != url
|
|
247
|
+
or len(digest) != 64
|
|
248
|
+
or not integrity.startswith("sha512-")
|
|
249
|
+
):
|
|
250
|
+
raise ValueError("Codex registry integrity metadata is invalid")
|
|
251
|
+
return dict(
|
|
252
|
+
url=url,
|
|
253
|
+
algorithm="sha512",
|
|
254
|
+
digest=digest.hex(),
|
|
255
|
+
maxBytes=268435456,
|
|
256
|
+
format="tar",
|
|
257
|
+
executable=codex_member(native_platform),
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def codex_release(
|
|
262
|
+
native_platform: str, version: str | None = None, *, before_version: str | None = None
|
|
263
|
+
) -> tuple[str, list[dict[str, Any]]]:
|
|
264
|
+
"""Resolve the publisher's stable channel for this platform, then bind its exact npm artifact."""
|
|
265
|
+
try:
|
|
266
|
+
if version is not None:
|
|
267
|
+
version_key(version)
|
|
268
|
+
artifact = _codex_platform_release(native_platform, version)
|
|
269
|
+
if artifact is not None:
|
|
270
|
+
return version, [artifact]
|
|
271
|
+
raise ValueError("The approved Codex version is no longer available for this platform")
|
|
272
|
+
latest = _text(_codex_metadata("latest").get("version"))
|
|
273
|
+
latest_key = version_key(latest)
|
|
274
|
+
if before_version is None or latest_key < version_key(before_version):
|
|
275
|
+
artifact = _codex_platform_release(native_platform, latest)
|
|
276
|
+
if artifact is not None:
|
|
277
|
+
return latest, [artifact]
|
|
278
|
+
bound = (
|
|
279
|
+
latest_key if before_version is None else min(latest_key, version_key(before_version))
|
|
280
|
+
)
|
|
281
|
+
versions = _object(_codex_metadata(None).get("versions"))
|
|
282
|
+
older = sorted(
|
|
283
|
+
(
|
|
284
|
+
tag
|
|
285
|
+
for tag in versions
|
|
286
|
+
if STABLE.fullmatch(tag)
|
|
287
|
+
and version_key(tag) < bound
|
|
288
|
+
and not _object(versions[tag]).get("deprecated")
|
|
289
|
+
and f"{tag}-{native_platform}" in versions
|
|
290
|
+
),
|
|
291
|
+
key=version_key,
|
|
292
|
+
reverse=True,
|
|
293
|
+
)
|
|
294
|
+
for tag in older:
|
|
295
|
+
artifact = _codex_platform_release(native_platform, tag)
|
|
296
|
+
if artifact is not None:
|
|
297
|
+
return tag, [artifact]
|
|
298
|
+
raise ValueError("No stable Codex build is available for this platform")
|
|
299
|
+
except (KeyError, OSError, TypeError, ValueError) as error:
|
|
300
|
+
raise ValueError(
|
|
301
|
+
f"Could not verify a stable Codex release for {native_platform}"
|
|
302
|
+
) from error
|
|
@@ -1,116 +1,20 @@
|
|
|
1
1
|
"""``foundations stop``: end this root's controller and its autostart, for an uninstaller.
|
|
2
2
|
|
|
3
|
-
A controller started by
|
|
4
|
-
|
|
5
|
-
prove it is this root's controller,
|
|
3
|
+
A controller started by 0.1.0.dev34 or later exits cleanly when asked over its loopback API.
|
|
4
|
+
An earlier one has no such request: ``service_upgrade`` signals it only after its lock and exact
|
|
5
|
+
command prove it is this root's controller, on Linux, macOS and Windows. Model services the
|
|
6
6
|
controller started keep running; it never owned their processes.
|
|
7
7
|
"""
|
|
8
8
|
|
|
9
9
|
from __future__ import annotations
|
|
10
10
|
|
|
11
|
-
import errno
|
|
12
|
-
import json
|
|
13
|
-
import signal
|
|
14
11
|
import subprocess
|
|
15
|
-
import sys
|
|
16
|
-
import time
|
|
17
|
-
import urllib.error
|
|
18
|
-
import urllib.request
|
|
19
12
|
from pathlib import Path
|
|
20
13
|
from typing import Any
|
|
21
14
|
|
|
22
15
|
from localmodels import runtime_io, service_autostart, service_upgrade
|
|
23
16
|
|
|
24
17
|
SCHEMA = "localmodels.service-controller-stop/v1"
|
|
25
|
-
GRACE = 15.0 # an install in progress finishes its current step before the worker exits
|
|
26
|
-
KILL_AFTER = 10.0
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def _idle(root: Path) -> bool:
|
|
30
|
-
try:
|
|
31
|
-
with runtime_io.private_lock(root / "worker.lock", blocking=False):
|
|
32
|
-
return True
|
|
33
|
-
except OSError as error:
|
|
34
|
-
# flock answers EAGAIN (BlockingIOError); msvcrt.locking EACCES or EDEADLOCK.
|
|
35
|
-
if error.errno in (errno.EAGAIN, errno.EACCES, errno.EDEADLK):
|
|
36
|
-
return False
|
|
37
|
-
raise
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def _wait(root: Path, seconds: float) -> bool:
|
|
41
|
-
deadline = time.monotonic() + seconds
|
|
42
|
-
while not _idle(root):
|
|
43
|
-
if time.monotonic() >= deadline:
|
|
44
|
-
return False
|
|
45
|
-
time.sleep(0.1)
|
|
46
|
-
return True
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
def _request(root: Path) -> bool:
|
|
50
|
-
"""Ask the controller to exit; False when it does not answer or predates the request."""
|
|
51
|
-
from localmodels.services_cli import read_configuration
|
|
52
|
-
|
|
53
|
-
try:
|
|
54
|
-
config = read_configuration(root)
|
|
55
|
-
runtime_io.private_permissions(root / "control.key")
|
|
56
|
-
key = json.loads((root / "control.key").read_text())
|
|
57
|
-
request = urllib.request.Request(
|
|
58
|
-
f"http://127.0.0.1:{config['port']}/v1/controller/stop",
|
|
59
|
-
data=b"{}",
|
|
60
|
-
headers={"Authorization": "Bearer " + key, "Content-Type": "application/json"},
|
|
61
|
-
method="POST",
|
|
62
|
-
)
|
|
63
|
-
with urllib.request.build_opener(urllib.request.ProxyHandler({})).open(
|
|
64
|
-
request, timeout=2
|
|
65
|
-
) as response:
|
|
66
|
-
return bool(response.status == 202)
|
|
67
|
-
except (OSError, ValueError, KeyError, urllib.error.URLError):
|
|
68
|
-
return False
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
def _unproven() -> ValueError:
|
|
72
|
-
return ValueError("Cannot verify the existing model controller")
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
def _identity(root: Path) -> tuple[int, str]:
|
|
76
|
-
if sys.platform == "linux":
|
|
77
|
-
return service_upgrade.controller_identity(root)
|
|
78
|
-
# Neither a command-line match nor an open lock file proves its lock holder.
|
|
79
|
-
raise _unproven()
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
def _send(root: Path, identity: tuple[int, str], force: bool) -> None:
|
|
83
|
-
if sys.platform != "linux":
|
|
84
|
-
raise _unproven()
|
|
85
|
-
signum = signal.SIGKILL if force else signal.SIGTERM
|
|
86
|
-
service_upgrade.signal_controller(root, identity, signum)
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
def _signal(root: Path) -> None:
|
|
90
|
-
"""SIGTERM (the worker's clean shutdown), SIGKILL after KILL_AFTER, each re-proven."""
|
|
91
|
-
deadline = time.monotonic() + KILL_AFTER
|
|
92
|
-
sent: tuple[int, str] | None = None
|
|
93
|
-
killed = False
|
|
94
|
-
while not _idle(root):
|
|
95
|
-
try:
|
|
96
|
-
identity = _identity(root)
|
|
97
|
-
if sent is None:
|
|
98
|
-
_send(root, identity, sys.platform == "win32")
|
|
99
|
-
sent = identity
|
|
100
|
-
elif identity != sent:
|
|
101
|
-
raise ValueError("Model controller changed during stop")
|
|
102
|
-
elif time.monotonic() >= deadline:
|
|
103
|
-
if killed:
|
|
104
|
-
raise ValueError("Model controller did not stop")
|
|
105
|
-
_send(root, identity, True)
|
|
106
|
-
killed = True
|
|
107
|
-
deadline = time.monotonic() + 5
|
|
108
|
-
except (OSError, ValueError, subprocess.SubprocessError) as error:
|
|
109
|
-
# The owner may exit between the lock attempt and its proof; a free lock settles it.
|
|
110
|
-
if _idle(root):
|
|
111
|
-
return
|
|
112
|
-
raise ValueError(str(error) or "Cannot verify the existing model controller") from None
|
|
113
|
-
time.sleep(0.05)
|
|
114
18
|
|
|
115
19
|
|
|
116
20
|
def stop(root: Path) -> dict[str, Any]:
|
|
@@ -128,6 +32,8 @@ def stop(root: Path) -> dict[str, Any]:
|
|
|
128
32
|
_stop(root, result)
|
|
129
33
|
except (OSError, ValueError, subprocess.SubprocessError) as error:
|
|
130
34
|
result.update(ok=False, stopped=False, error=str(error))
|
|
35
|
+
if isinstance(error, service_upgrade.ControllerError):
|
|
36
|
+
result["code"] = error.code
|
|
131
37
|
return result
|
|
132
38
|
|
|
133
39
|
|
|
@@ -138,15 +44,9 @@ def _stop(root: Path, result: dict[str, Any]) -> None:
|
|
|
138
44
|
config = read_configuration(root)
|
|
139
45
|
config["stopped"] = True
|
|
140
46
|
runtime_io.write_private_json(root / "control.json", config)
|
|
141
|
-
running = not
|
|
47
|
+
running = not service_upgrade.idle(root)
|
|
142
48
|
result["autostart"] = service_autostart.unregister(root)
|
|
143
|
-
if not running and
|
|
49
|
+
if not running and service_upgrade.idle(root):
|
|
144
50
|
return
|
|
145
51
|
result["stopped"] = True
|
|
146
|
-
|
|
147
|
-
result["method"] = "systemd"
|
|
148
|
-
elif _request(root) and _wait(root, GRACE):
|
|
149
|
-
result["method"] = "request"
|
|
150
|
-
else:
|
|
151
|
-
result["method"] = "signal"
|
|
152
|
-
_signal(root)
|
|
52
|
+
result["method"] = service_upgrade.end_controller(root) or "systemd"
|