@simplexai/localmodels-darwin-x64 0.1.0-dev.60 → 0.1.0-dev.62
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 +78 -64
- package/modules/localmodels/__init__.py +1 -1
- package/modules/localmodels/__main__.py +5 -3
- package/modules/localmodels/auth_proxy.py +185 -19
- package/modules/localmodels/catalog.json +363 -0
- package/modules/localmodels/catalog.py +1 -1
- package/modules/localmodels/catalog_add.py +5 -5
- package/modules/localmodels/catalog_license.py +2 -0
- package/modules/localmodels/catalog_recipes.py +2 -0
- package/modules/localmodels/cli_parser.py +6 -2
- package/modules/localmodels/context_policy.py +40 -4
- package/modules/localmodels/custom_hub_http.py +27 -104
- package/modules/localmodels/debug_mode.py +13 -0
- package/modules/localmodels/debug_stub_server.py +156 -1
- package/modules/localmodels/deploy.py +11 -2
- package/modules/localmodels/download-mirrors.json +12 -0
- package/modules/localmodels/engines-b12757-mix-ef2df21.json +26 -0
- package/modules/localmodels/engines-whisper-b5454.json +120 -1
- package/modules/localmodels/gateway_app_bifrost.py +14 -14
- package/modules/localmodels/gateway_bifrost.py +4 -1
- package/modules/localmodels/gateway_cli.py +14 -80
- package/modules/localmodels/gateway_server.py +88 -16
- package/modules/localmodels/gpu_specs.py +35 -1
- package/modules/localmodels/harness.py +1 -2
- package/modules/localmodels/hf_repo.py +12 -4
- package/modules/localmodels/install.py +4 -4
- package/modules/localmodels/library_jobs.py +4 -0
- package/modules/localmodels/library_serve.py +69 -13
- package/modules/localmodels/memcalc_load.py +9 -4
- package/modules/localmodels/npm_transport.py +4 -58
- package/modules/localmodels/recommend_cli.py +2 -1
- package/modules/localmodels/remote_ops.py +120 -713
- package/modules/localmodels/remote_ops_common.py +670 -0
- package/modules/localmodels/remote_ops_download.py +399 -381
- package/modules/localmodels/remote_ops_mirrors.py +27 -66
- package/modules/localmodels/remote_ops_prepare.py +43 -35
- package/modules/localmodels/remote_ops_ranges.py +102 -63
- package/modules/localmodels/remote_ops_redirect.py +393 -0
- package/modules/localmodels/remote_ops_remove.py +30 -22
- package/modules/localmodels/remote_ops_service.py +102 -95
- package/modules/localmodels/remote_ops_verify.py +94 -63
- package/modules/localmodels/runtime_cli.py +15 -5
- package/modules/localmodels/runtime_io.py +54 -1
- package/modules/localmodels/source_cli.py +136 -23
- package/modules/localmodels/speech-sv.txt +93 -0
- package/modules/localmodels/speech_admission.py +437 -0
- package/modules/localmodels/speech_catalog.py +170 -0
- package/modules/localmodels/speech_cli.py +273 -0
- package/modules/localmodels/speech_jobs.py +776 -0
- package/modules/localmodels/speech_model.py +404 -0
- package/modules/localmodels/speech_serve.py +287 -0
- package/modules/localmodels/speech_server.py +837 -0
- package/modules/localmodels/speech_state.py +697 -0
- package/modules/localmodels/speech_worker_sensevoice.py +255 -0
- package/modules/localmodels/speech_workers.py +783 -0
- package/modules/localmodels/stack.py +0 -33
- package/modules/localmodels/stack_auto.py +5 -3
- package/modules/localmodels/stack_aux_gpu.py +45 -4
- package/modules/localmodels/stack_capacity.py +77 -8
- package/modules/localmodels/stack_cli.py +16 -1
- package/modules/localmodels/stack_compose.py +1 -1
- package/modules/localmodels/stack_down.py +24 -1
- package/modules/localmodels/stack_hardware.py +57 -88
- package/modules/localmodels/stack_hardware_text.py +4 -59
- package/modules/localmodels/stack_memory.py +13 -3
- package/modules/localmodels/stack_suite.py +9 -10
- package/modules/localmodels/stack_summary.py +4 -4
- package/modules/localmodels/stack_view_llm.py +1 -1
- package/modules/localmodels/transport.py +4 -2
- package/modules/localmodels/tui_views.py +2 -2
- package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/METADATA +1 -1
- package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/RECORD +75 -61
- package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/licenses/NOTICE +9 -0
- package/package.json +1 -1
- package/tool.json +1 -1
- /package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/WHEEL +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/entry_points.txt +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/licenses/localmodels/HERMES-LICENSE +0 -0
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
"""remote_ops ``verify`` action: readiness is a real request for the contract, never /health alone.
|
|
3
3
|
|
|
4
4
|
Part of the remote_ops bundle shipped to the target as one stdin payload (transport.BUNDLES):
|
|
5
|
-
standard library only, Python 3.8+. Shared primitives
|
|
6
|
-
here
|
|
7
|
-
private-name imports.
|
|
5
|
+
standard library only, Python 3.8+. Shared primitives live in ``remote_ops_common`` and are used
|
|
6
|
+
here via ``remote_ops_common.<name>`` so the bundle stays acyclic with no private-name accesses.
|
|
8
7
|
"""
|
|
9
8
|
|
|
10
9
|
from __future__ import annotations
|
|
@@ -19,16 +18,21 @@ import struct
|
|
|
19
18
|
import sys
|
|
20
19
|
import time
|
|
21
20
|
import urllib.error
|
|
22
|
-
import urllib.parse
|
|
23
21
|
import wave
|
|
24
22
|
import zlib
|
|
25
23
|
from typing import Callable, cast
|
|
26
24
|
|
|
27
25
|
try:
|
|
28
|
-
from localmodels import
|
|
26
|
+
from localmodels import (
|
|
27
|
+
process_identity,
|
|
28
|
+
remote_ops_common,
|
|
29
|
+
remote_ops_download,
|
|
30
|
+
remote_ops_launchd,
|
|
31
|
+
)
|
|
29
32
|
except ImportError: # run as a bare file from this directory
|
|
30
33
|
import process_identity # type: ignore[import-not-found,no-redef]
|
|
31
|
-
import
|
|
34
|
+
import remote_ops_common # type: ignore[import-not-found,no-redef]
|
|
35
|
+
import remote_ops_download # type: ignore[import-not-found,no-redef]
|
|
32
36
|
import remote_ops_launchd # type: ignore[import-not-found,no-redef]
|
|
33
37
|
|
|
34
38
|
|
|
@@ -37,24 +41,26 @@ LIMIT_UNIT = " apple" # repeated to size inputs; /tokenize gives the real count
|
|
|
37
41
|
LIMIT_MARGIN = 32 # tokens either side of the limit: room for the special tokens added
|
|
38
42
|
|
|
39
43
|
|
|
40
|
-
def _restarts(p:
|
|
44
|
+
def _restarts(p: remote_ops_common.ServiceParams) -> int:
|
|
41
45
|
"""systemd's restart count for the unit; it survives stop/start, so callers take deltas."""
|
|
42
|
-
_, out =
|
|
46
|
+
_, out = remote_ops_common.systemctl(
|
|
47
|
+
"show", "-p", "NRestarts", "--value", p["service"] + ".service"
|
|
48
|
+
)
|
|
43
49
|
return int(out) if out.isdigit() else 0
|
|
44
50
|
|
|
45
51
|
|
|
46
|
-
def _health_ready(root: str, health_path: str, p:
|
|
47
|
-
if
|
|
52
|
+
def _health_ready(root: str, health_path: str, p: remote_ops_common.VerifyParams) -> bool:
|
|
53
|
+
if remote_ops_common.receipts():
|
|
48
54
|
state, pid = (
|
|
49
55
|
remote_ops_launchd.status(p["deploy_dir"])
|
|
50
56
|
if p["service_kind"] == "launchd-user"
|
|
51
|
-
else process_identity.status(
|
|
57
|
+
else process_identity.status(remote_ops_common.paths(p)["pid"])
|
|
52
58
|
)
|
|
53
59
|
if state != "active" or not process_identity.listener_owned(p["port"], pid):
|
|
54
60
|
return False
|
|
55
61
|
return (
|
|
56
|
-
|
|
57
|
-
in
|
|
62
|
+
remote_ops_common.http_json(root + health_path, p["api_key"], timeout=5).get("status", "ok")
|
|
63
|
+
in remote_ops_common.READY
|
|
58
64
|
)
|
|
59
65
|
|
|
60
66
|
|
|
@@ -64,15 +70,15 @@ def _evaluate_chat_answer(content: str, reasoning: str, *, smoke: bool) -> tuple
|
|
|
64
70
|
return bool(text), text[:200], "the smoke completion was empty"
|
|
65
71
|
blob = content + " " + reasoning
|
|
66
72
|
return (
|
|
67
|
-
|
|
73
|
+
remote_ops_common.TOUCH_EXPECT in blob.lower(),
|
|
68
74
|
content[:200],
|
|
69
|
-
f"the answer lacks {
|
|
75
|
+
f"the answer lacks {remote_ops_common.TOUCH_EXPECT!r}",
|
|
70
76
|
)
|
|
71
77
|
|
|
72
78
|
|
|
73
|
-
def act_verify(p:
|
|
79
|
+
def act_verify(p: remote_ops_common.VerifyParams) -> None:
|
|
74
80
|
"""Readiness is a real generation, never /health alone (Hermes' touch generation)."""
|
|
75
|
-
paths =
|
|
81
|
+
paths = remote_ops_common.paths(p)
|
|
76
82
|
base_restarts = _restarts(p) if p["service_kind"] == "systemd-user" else 0
|
|
77
83
|
root = f"http://127.0.0.1:{p['port']}"
|
|
78
84
|
deadline = time.monotonic() + p.get("timeout_s", 900)
|
|
@@ -84,30 +90,33 @@ def act_verify(p: remote_ops.VerifyParams) -> None:
|
|
|
84
90
|
except (urllib.error.URLError, OSError, ValueError):
|
|
85
91
|
pass
|
|
86
92
|
if p["service_kind"] == "systemd-user":
|
|
87
|
-
_, state =
|
|
93
|
+
_, state = remote_ops_common.systemctl("is-active", p["service"] + ".service")
|
|
88
94
|
if state in ("failed", "inactive"):
|
|
89
95
|
raise RuntimeError(
|
|
90
|
-
f"the service exited ({state}). Log tail:\n{
|
|
96
|
+
f"the service exited ({state}). Log tail:\n{remote_ops_common.log_tail(paths)}"
|
|
91
97
|
)
|
|
92
98
|
restarts = _restarts(p) - base_restarts
|
|
93
|
-
if restarts >=
|
|
99
|
+
if restarts >= remote_ops_common.CRASH_RESTARTS:
|
|
94
100
|
# Restart=on-failure would retry forever; this service is ours to stop.
|
|
95
|
-
|
|
101
|
+
remote_ops_common.systemctl("stop", p["service"] + ".service")
|
|
96
102
|
raise RuntimeError(
|
|
97
103
|
f"the service kept exiting after start ({restarts} restarts this time) and was stopped. "
|
|
98
|
-
f"Log tail:\n{
|
|
104
|
+
f"Log tail:\n{remote_ops_common.log_tail(paths)}"
|
|
99
105
|
)
|
|
100
106
|
elif (
|
|
101
107
|
remote_ops_launchd.status(p["deploy_dir"])[0] != "active"
|
|
102
108
|
if p["service_kind"] == "launchd-user"
|
|
103
|
-
else
|
|
109
|
+
else remote_ops_common.pid_alive(paths["pid"]) is None
|
|
104
110
|
):
|
|
105
|
-
raise RuntimeError(
|
|
111
|
+
raise RuntimeError(
|
|
112
|
+
"the process exited. Log tail:\n" + remote_ops_common.log_tail(paths)
|
|
113
|
+
)
|
|
106
114
|
if time.monotonic() > deadline:
|
|
107
115
|
raise RuntimeError(
|
|
108
|
-
"timed out waiting for the model to load. Log tail:\n"
|
|
116
|
+
"timed out waiting for the model to load. Log tail:\n"
|
|
117
|
+
+ remote_ops_common.log_tail(paths)
|
|
109
118
|
)
|
|
110
|
-
|
|
119
|
+
remote_ops_common.emit("progress", phase="loading")
|
|
111
120
|
time.sleep(3)
|
|
112
121
|
contract = p.get("contract")
|
|
113
122
|
if contract in CONTRACTS:
|
|
@@ -115,7 +124,7 @@ def act_verify(p: remote_ops.VerifyParams) -> None:
|
|
|
115
124
|
return
|
|
116
125
|
started = time.monotonic()
|
|
117
126
|
smoke = bool(p.get("smoke"))
|
|
118
|
-
prompt = "Reply with OK." if smoke else
|
|
127
|
+
prompt = "Reply with OK." if smoke else remote_ops_common.TOUCH_PROMPT
|
|
119
128
|
messages = [{"role": "user", "content": prompt}]
|
|
120
129
|
body = {
|
|
121
130
|
"model": p["model_alias"],
|
|
@@ -123,7 +132,9 @@ def act_verify(p: remote_ops.VerifyParams) -> None:
|
|
|
123
132
|
"max_tokens": 16 if smoke else 512,
|
|
124
133
|
"temperature": 0,
|
|
125
134
|
}
|
|
126
|
-
resp =
|
|
135
|
+
resp = remote_ops_common.http_json(
|
|
136
|
+
root + "/v1/chat/completions", p["api_key"], body, timeout=300
|
|
137
|
+
)
|
|
127
138
|
choices = resp.get("choices")
|
|
128
139
|
if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict):
|
|
129
140
|
raise ValueError("missing completion choices")
|
|
@@ -146,7 +157,7 @@ def act_verify(p: remote_ops.VerifyParams) -> None:
|
|
|
146
157
|
_report(ok, answer, started, failure, timings=resp.get("timings"), **extra, **_released(ok, p))
|
|
147
158
|
|
|
148
159
|
|
|
149
|
-
def _released(ok: bool, p:
|
|
160
|
+
def _released(ok: bool, p: remote_ops_common.VerifyParams) -> dict[str, int]:
|
|
150
161
|
"""Windows keeps the pages a model was read through in the server's working set after the
|
|
151
162
|
weights are in VRAM, until memory runs short: 4.9 GiB for a 4.1 GiB model. Given back once
|
|
152
163
|
the server has answered, they left 4.5 GB more available in 0.4 s and the next request was
|
|
@@ -154,23 +165,23 @@ def _released(ok: bool, p: remote_ops.VerifyParams) -> dict[str, int]:
|
|
|
154
165
|
for a model with weights in host memory, a failed check, or another system."""
|
|
155
166
|
if not ok or not p.get("weights_on_gpu") or sys.platform != "win32":
|
|
156
167
|
return {}
|
|
157
|
-
state, pid = process_identity.status(
|
|
168
|
+
state, pid = process_identity.status(remote_ops_common.paths(p)["pid"])
|
|
158
169
|
freed = process_identity.release_working_set(pid) if state == "active" and pid else 0
|
|
159
170
|
return {"released_bytes": freed}
|
|
160
171
|
|
|
161
172
|
|
|
162
|
-
def _served(root: str, p:
|
|
173
|
+
def _served(root: str, p: remote_ops_common.VerifyParams) -> tuple[object, object]:
|
|
163
174
|
"""llama-server's build and per-slot window from /props; (None, None) when it does not say."""
|
|
164
175
|
props = {}
|
|
165
176
|
try:
|
|
166
|
-
props =
|
|
177
|
+
props = remote_ops_common.http_json(root + "/props", p["api_key"], timeout=5)
|
|
167
178
|
except (urllib.error.URLError, OSError, ValueError):
|
|
168
179
|
pass
|
|
169
180
|
dgs = props.get("default_generation_settings")
|
|
170
181
|
return props.get("build_info"), dgs.get("n_ctx") if isinstance(dgs, dict) else None
|
|
171
182
|
|
|
172
183
|
|
|
173
|
-
def _short_window(p:
|
|
184
|
+
def _short_window(p: remote_ops_common.VerifyParams, n_ctx: object) -> str:
|
|
174
185
|
"""Why the served window falls short of the planned one, "" when it does not or is unknown.
|
|
175
186
|
llama-server pads -c up to a multiple of 256 (200000 serves 200192), so at least the plan."""
|
|
176
187
|
window = p.get("window")
|
|
@@ -198,7 +209,7 @@ def _solid_png(rgb: tuple[int, int, int], side: int = 224) -> bytes:
|
|
|
198
209
|
)
|
|
199
210
|
|
|
200
211
|
|
|
201
|
-
def _picture_answer(root: str, p:
|
|
212
|
+
def _picture_answer(root: str, p: remote_ops_common.VerifyParams) -> str:
|
|
202
213
|
"""The model's answer, with its reasoning, about a synthetic red picture (decision 31)."""
|
|
203
214
|
image = "data:image/png;base64," + base64.b64encode(_solid_png((220, 20, 20))).decode()
|
|
204
215
|
parts = [
|
|
@@ -211,7 +222,9 @@ def _picture_answer(root: str, p: remote_ops.VerifyParams) -> str:
|
|
|
211
222
|
"max_tokens": 512,
|
|
212
223
|
"temperature": 0,
|
|
213
224
|
}
|
|
214
|
-
resp =
|
|
225
|
+
resp = remote_ops_common.http_json(
|
|
226
|
+
root + "/v1/chat/completions", p["api_key"], body, timeout=300
|
|
227
|
+
)
|
|
215
228
|
choices = resp.get("choices")
|
|
216
229
|
if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict):
|
|
217
230
|
raise ValueError("missing picture completion choices")
|
|
@@ -224,12 +237,16 @@ def _picture_answer(root: str, p: remote_ops.VerifyParams) -> str:
|
|
|
224
237
|
return content + " " + reasoning
|
|
225
238
|
|
|
226
239
|
|
|
227
|
-
def _verify_decision(root: str, p:
|
|
240
|
+
def _verify_decision(root: str, p: remote_ops_common.VerifyParams) -> None:
|
|
228
241
|
"""Readiness of a System One model is a probability for an unambiguous question."""
|
|
229
242
|
started = time.monotonic()
|
|
230
|
-
body = {
|
|
231
|
-
|
|
232
|
-
|
|
243
|
+
body = {
|
|
244
|
+
"model": p["model_alias"],
|
|
245
|
+
"state": remote_ops_common.DECISION_STATE,
|
|
246
|
+
"questions": {},
|
|
247
|
+
}
|
|
248
|
+
body["questions"] = {"outage": remote_ops_common.DECISION_QUESTION}
|
|
249
|
+
resp = remote_ops_common.http_json(root + "/v1/systemone", p["api_key"], body, timeout=300)
|
|
233
250
|
answers = resp.get("answers")
|
|
234
251
|
answer = answers.get("outage") if isinstance(answers, dict) else None
|
|
235
252
|
noul = answer.get("noul") if isinstance(answer, dict) else None
|
|
@@ -253,18 +270,20 @@ def _finite(values: object) -> list[float]:
|
|
|
253
270
|
|
|
254
271
|
|
|
255
272
|
def _report(ok: bool, answer: str, started: float, failure: str, **extra: object) -> None:
|
|
256
|
-
|
|
273
|
+
remote_ops_common.emit(
|
|
257
274
|
"result", ready=ok, answer=answer, seconds=round(time.monotonic() - started, 2), **extra
|
|
258
275
|
)
|
|
259
276
|
if not ok:
|
|
260
277
|
raise RuntimeError("readiness check failed: " + failure)
|
|
261
278
|
|
|
262
279
|
|
|
263
|
-
def _sized(root: str, p:
|
|
280
|
+
def _sized(root: str, p: remote_ops_common.VerifyParams, tokens: int) -> tuple[str, int]:
|
|
264
281
|
"""Text of about ``tokens`` tokens and its count, both from the server's own tokenizer."""
|
|
265
282
|
|
|
266
283
|
def count(text: str) -> int:
|
|
267
|
-
found =
|
|
284
|
+
found = remote_ops_common.http_json(
|
|
285
|
+
root + "/tokenize", p["api_key"], {"content": text}, timeout=60
|
|
286
|
+
)
|
|
268
287
|
if not isinstance(found.get("tokens"), list):
|
|
269
288
|
raise ValueError("invalid /tokenize response")
|
|
270
289
|
return len(cast("list[object]", found["tokens"]))
|
|
@@ -273,7 +292,9 @@ def _sized(root: str, p: remote_ops.VerifyParams, tokens: int) -> tuple[str, int
|
|
|
273
292
|
return text, count(text)
|
|
274
293
|
|
|
275
294
|
|
|
276
|
-
def _input_limit(
|
|
295
|
+
def _input_limit(
|
|
296
|
+
root: str, p: remote_ops_common.VerifyParams, send: Callable[[str], object]
|
|
297
|
+
) -> str:
|
|
277
298
|
"""The declared token limit holds: an input just under it is served; one just over it
|
|
278
299
|
refuses the whole request with an HTTP error instead of being truncated, and the service
|
|
279
300
|
keeps answering."""
|
|
@@ -301,12 +322,18 @@ def _input_limit(root: str, p: remote_ops.VerifyParams, send: Callable[[str], ob
|
|
|
301
322
|
return f" limit={limit}: {served} served, {refused} refused"
|
|
302
323
|
|
|
303
324
|
|
|
304
|
-
def _verify_embedding(root: str, p:
|
|
325
|
+
def _verify_embedding(root: str, p: remote_ops_common.VerifyParams) -> None:
|
|
305
326
|
"""Finite unit vectors of one width; a paraphrase lands nearer than an unrelated sentence."""
|
|
306
327
|
started = time.monotonic()
|
|
307
|
-
texts = [
|
|
328
|
+
texts = [
|
|
329
|
+
remote_ops_common.EMBED_QUERY,
|
|
330
|
+
remote_ops_common.EMBED_NEAR,
|
|
331
|
+
remote_ops_common.EMBED_FAR,
|
|
332
|
+
]
|
|
308
333
|
body = {"model": p["model_alias"], "input": texts}
|
|
309
|
-
data =
|
|
334
|
+
data = remote_ops_common.http_json(
|
|
335
|
+
root + "/v1/embeddings", p["api_key"], body, timeout=120
|
|
336
|
+
).get("data")
|
|
310
337
|
if (
|
|
311
338
|
not isinstance(data, list)
|
|
312
339
|
or len(data) != len(texts)
|
|
@@ -332,24 +359,26 @@ def _verify_embedding(root: str, p: remote_ops.VerifyParams) -> None:
|
|
|
332
359
|
answer += _input_limit(
|
|
333
360
|
root,
|
|
334
361
|
p,
|
|
335
|
-
lambda text:
|
|
362
|
+
lambda text: remote_ops_common.http_json(
|
|
336
363
|
root + "/v1/embeddings", p["api_key"], {**body, "input": [text]}, timeout=300
|
|
337
364
|
),
|
|
338
365
|
)
|
|
339
366
|
_report(all(abs(n - 1) < 1e-2 for n in norms) and near > far, answer, started, answer)
|
|
340
367
|
|
|
341
368
|
|
|
342
|
-
def _verify_rerank(root: str, p:
|
|
369
|
+
def _verify_rerank(root: str, p: remote_ops_common.VerifyParams) -> None:
|
|
343
370
|
"""The known relevant document, not placed first, must rank first. llama-server returns the
|
|
344
371
|
results sorted by score and cut to ``top_n``, so ask for every document."""
|
|
345
|
-
started, docs = time.monotonic(),
|
|
372
|
+
started, docs = time.monotonic(), remote_ops_common.RERANK_DOCS
|
|
346
373
|
body = {
|
|
347
374
|
"model": p["model_alias"],
|
|
348
|
-
"query":
|
|
375
|
+
"query": remote_ops_common.RERANK_QUERY,
|
|
349
376
|
"documents": docs,
|
|
350
377
|
"top_n": len(docs),
|
|
351
378
|
}
|
|
352
|
-
results =
|
|
379
|
+
results = remote_ops_common.http_json(root + "/v1/rerank", p["api_key"], body, timeout=120).get(
|
|
380
|
+
"results"
|
|
381
|
+
)
|
|
353
382
|
if not isinstance(results, list) or not all(isinstance(r, dict) for r in results):
|
|
354
383
|
raise ValueError("invalid rerank results")
|
|
355
384
|
scored: list[tuple[float, int]] = []
|
|
@@ -358,7 +387,7 @@ def _verify_rerank(root: str, p: remote_ops.VerifyParams) -> None:
|
|
|
358
387
|
if type(index) is not int:
|
|
359
388
|
raise ValueError("invalid rerank index")
|
|
360
389
|
scored.append((_finite([result.get("relevance_score")])[0], index))
|
|
361
|
-
if sorted(i for _, i in scored) != list(range(len(
|
|
390
|
+
if sorted(i for _, i in scored) != list(range(len(remote_ops_common.RERANK_DOCS))):
|
|
362
391
|
raise ValueError("rerank indices do not cover every document")
|
|
363
392
|
if any(scored[i][0] < scored[i + 1][0] for i in range(len(scored) - 1)):
|
|
364
393
|
raise ValueError("rerank results are not sorted by descending score")
|
|
@@ -368,7 +397,7 @@ def _verify_rerank(root: str, p: remote_ops.VerifyParams) -> None:
|
|
|
368
397
|
limit = _input_limit(
|
|
369
398
|
root,
|
|
370
399
|
p,
|
|
371
|
-
lambda text:
|
|
400
|
+
lambda text: remote_ops_common.http_json(
|
|
372
401
|
root + "/v1/rerank",
|
|
373
402
|
p["api_key"],
|
|
374
403
|
{**body, "documents": [text], "top_n": 1},
|
|
@@ -376,18 +405,20 @@ def _verify_rerank(root: str, p: remote_ops.VerifyParams) -> None:
|
|
|
376
405
|
),
|
|
377
406
|
)
|
|
378
407
|
_report(
|
|
379
|
-
best ==
|
|
408
|
+
best == remote_ops_common.RERANK_RELEVANT,
|
|
380
409
|
f"top={best}{limit}",
|
|
381
410
|
started,
|
|
382
411
|
f"document {best} ranked first",
|
|
383
412
|
)
|
|
384
413
|
|
|
385
414
|
|
|
386
|
-
def
|
|
415
|
+
def verify_stt(root: str, p: remote_ops_common.VerifyParams) -> None:
|
|
387
416
|
"""A pinned recording transcribes to text containing its known phrase."""
|
|
388
417
|
fx = p.get("fixture") or {}
|
|
389
|
-
dest = os.path.join(
|
|
390
|
-
|
|
418
|
+
dest = os.path.join(remote_ops_common.expand(p["deploy_dir"]), str(fx["name"]))
|
|
419
|
+
remote_ops_download.fetch(
|
|
420
|
+
str(fx["url"]), dest, str(fx["sha256"]), int(str(fx["size_bytes"])), dest
|
|
421
|
+
)
|
|
391
422
|
with open(dest, "rb") as f:
|
|
392
423
|
audio = f.read()
|
|
393
424
|
mark = "--localmodels" + hashlib.sha256(audio).hexdigest()[:16]
|
|
@@ -397,7 +428,7 @@ def _verify_stt(root: str, p: remote_ops.VerifyParams) -> None:
|
|
|
397
428
|
body += f"\r\n{mark}--\r\n".encode()
|
|
398
429
|
kind, started = "multipart/form-data; boundary=" + mark[2:], time.monotonic()
|
|
399
430
|
resp = json.loads(
|
|
400
|
-
|
|
431
|
+
remote_ops_common.request(root + str(fx["path"]), p["api_key"], body, kind, 300) or b"{}"
|
|
401
432
|
)
|
|
402
433
|
text = resp.get("text") if isinstance(resp, dict) else None
|
|
403
434
|
if not isinstance(text, str):
|
|
@@ -407,15 +438,15 @@ def _verify_stt(root: str, p: remote_ops.VerifyParams) -> None:
|
|
|
407
438
|
_report(ok, text.strip()[:120], started, f"the transcript lacks {expect!r}")
|
|
408
439
|
|
|
409
440
|
|
|
410
|
-
def _verify_tts(root: str, p:
|
|
441
|
+
def _verify_tts(root: str, p: remote_ops_common.VerifyParams) -> None:
|
|
411
442
|
"""Each fixture sentence (Chinese and English: both G2P paths) comes back as audible 16-bit
|
|
412
443
|
mono WAV of a sentence's length. Whether it says the words is a round trip through STT."""
|
|
413
444
|
started, heard = time.monotonic(), []
|
|
414
445
|
samples = cast("list[dict[str, str]]", (p.get("fixture") or {}).get("samples") or [])
|
|
415
446
|
for s in samples:
|
|
416
447
|
body = json.dumps({"model": p["model_alias"], "voice": s["voice"], "input": s["input"]})
|
|
417
|
-
audio =
|
|
418
|
-
root + "/v1/audio/speech", p["api_key"], body.encode(),
|
|
448
|
+
audio = remote_ops_common.request(
|
|
449
|
+
root + "/v1/audio/speech", p["api_key"], body.encode(), remote_ops_common.JSON, 300
|
|
419
450
|
)
|
|
420
451
|
with wave.open(io.BytesIO(audio)) as w:
|
|
421
452
|
mono16, frames = (w.getnchannels(), w.getsampwidth()) == (1, 2), w.getnframes()
|
|
@@ -434,11 +465,11 @@ def _verify_tts(root: str, p: remote_ops.VerifyParams) -> None:
|
|
|
434
465
|
|
|
435
466
|
CONTRACTS = {
|
|
436
467
|
"systemone": _verify_decision,
|
|
437
|
-
"health": lambda root, p:
|
|
468
|
+
"health": lambda root, p: remote_ops_common.emit(
|
|
438
469
|
"result", ready=True
|
|
439
470
|
), # behind a front that checks it
|
|
440
471
|
"embedding": _verify_embedding,
|
|
441
472
|
"rerank": _verify_rerank,
|
|
442
|
-
"stt":
|
|
473
|
+
"stt": verify_stt,
|
|
443
474
|
"tts": _verify_tts,
|
|
444
475
|
}
|
|
@@ -47,18 +47,25 @@ RUNTIMES = ("codex", "claude", "pi")
|
|
|
47
47
|
LOCK = Path(__file__).with_name("runtime-lock.json")
|
|
48
48
|
PROFILE_KEYS = {"codex": "CODEX_HOME", "claude": "CLAUDE_CONFIG_DIR", "pi": "PI_CODING_AGENT_DIR"}
|
|
49
49
|
PROFILE_DIRS = {"codex": ".codex", "claude": ".claude", "pi": ".pi/agent"}
|
|
50
|
-
|
|
50
|
+
platform = runtime_discovery.platform_tag
|
|
51
|
+
_platform = platform
|
|
51
52
|
|
|
52
53
|
|
|
53
|
-
def
|
|
54
|
+
def owned_profile(directory: Path) -> bool:
|
|
54
55
|
files = owned_files(directory) if _private_file(directory / "localmodels-auth.json") else {}
|
|
55
56
|
return bool(files) and all(_private_file(directory / name) for name in files)
|
|
56
57
|
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
_owned_profile = owned_profile
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def active_profile(runtime: str, env: dict[str, str]) -> Path:
|
|
59
63
|
return Path(env.get(PROFILE_KEYS[runtime], str(Path.home() / PROFILE_DIRS[runtime])))
|
|
60
64
|
|
|
61
65
|
|
|
66
|
+
_active_profile = active_profile
|
|
67
|
+
|
|
68
|
+
|
|
62
69
|
def _probe(
|
|
63
70
|
argv: list[str], env: dict[str, str] | None = None, *, timeout: float = 5
|
|
64
71
|
) -> tuple[int, str]:
|
|
@@ -139,15 +146,18 @@ def _managed_command(root: Path, runtime: str, node: str | None = None) -> list[
|
|
|
139
146
|
return [str(target / artifacts[0]["executable"])] if artifacts else []
|
|
140
147
|
|
|
141
148
|
|
|
142
|
-
def
|
|
149
|
+
def runtime_env(root: Path, runtime: str) -> dict[str, str]:
|
|
143
150
|
env = dict(os.environ)
|
|
144
151
|
directory = root / "runtime" / runtime
|
|
145
152
|
validate_runtime_root(directory)
|
|
146
|
-
if
|
|
153
|
+
if owned_profile(directory):
|
|
147
154
|
env[PROFILE_KEYS[runtime]] = str(directory)
|
|
148
155
|
return env
|
|
149
156
|
|
|
150
157
|
|
|
158
|
+
_runtime_env = runtime_env
|
|
159
|
+
|
|
160
|
+
|
|
151
161
|
def _credential_ambiguous(
|
|
152
162
|
runtime: str, profile: Path, owned: dict[str, str], env: dict[str, str]
|
|
153
163
|
) -> bool:
|
|
@@ -21,7 +21,7 @@ import urllib.parse
|
|
|
21
21
|
import urllib.request
|
|
22
22
|
import zipfile
|
|
23
23
|
from collections import deque
|
|
24
|
-
from collections.abc import Callable
|
|
24
|
+
from collections.abc import Callable, Mapping
|
|
25
25
|
from pathlib import Path
|
|
26
26
|
from typing import Any, cast
|
|
27
27
|
|
|
@@ -654,6 +654,59 @@ def cached_download(
|
|
|
654
654
|
return kept
|
|
655
655
|
|
|
656
656
|
|
|
657
|
+
def _archive_pin(build: Mapping[str, Any]) -> tuple[str, int]:
|
|
658
|
+
"""The npm tarball's SHA256 and size, which every Bifrost build of the tool lock carries."""
|
|
659
|
+
pin = build["npm"]
|
|
660
|
+
digest, size = pin.get("sha256"), pin.get("bytes")
|
|
661
|
+
if (
|
|
662
|
+
not isinstance(digest, str)
|
|
663
|
+
or not re.fullmatch("[0-9a-f]{64}", digest)
|
|
664
|
+
or type(size) is not int
|
|
665
|
+
or not 0 < size <= npm_transport.archive_size_cap(build["size_bytes"])
|
|
666
|
+
):
|
|
667
|
+
raise ValueError(
|
|
668
|
+
"Bifrost npm archive pin is missing or invalid (the tool lock needs npm sha256 and"
|
|
669
|
+
" bytes)"
|
|
670
|
+
)
|
|
671
|
+
return digest, size
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
def _sha256(path: Path) -> str:
|
|
675
|
+
digest = hashlib.sha256()
|
|
676
|
+
with path.open("rb") as stream:
|
|
677
|
+
for chunk in iter(lambda: stream.read(1 << 20), b""):
|
|
678
|
+
digest.update(chunk)
|
|
679
|
+
return digest.hexdigest()
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
def fetch_bifrost(
|
|
683
|
+
build: Mapping[str, Any],
|
|
684
|
+
destination: Path,
|
|
685
|
+
archive: Path,
|
|
686
|
+
event: Event,
|
|
687
|
+
) -> None:
|
|
688
|
+
"""Put the pinned Bifrost binary of ``build`` at ``destination``. The npm tarball downloads
|
|
689
|
+
to ``archive``, which must outlive this call: an interrupted download keeps
|
|
690
|
+
``archive.part`` for the next call to resume (``download``, which also moves between the npm
|
|
691
|
+
registry and its mirror), and a complete tarball left by a crash before extraction is reused
|
|
692
|
+
when it matches the pin. The tarball is deleted once extracted."""
|
|
693
|
+
digest, size = _archive_pin(build)
|
|
694
|
+
if archive.exists() or archive.is_symlink():
|
|
695
|
+
if archive.is_file() and not archive.is_symlink() and _sha256(archive) == digest:
|
|
696
|
+
event({"kind": "stage", "stage": "download", "state": "reused"})
|
|
697
|
+
else:
|
|
698
|
+
archive.unlink()
|
|
699
|
+
if not archive.exists():
|
|
700
|
+
pin = build["npm"]
|
|
701
|
+
url = npm_transport.check_url(
|
|
702
|
+
npm_transport.package_url(pin["package"], pin["version"], "global", os.environ)
|
|
703
|
+
)
|
|
704
|
+
artifact = {"url": url, "algorithm": "sha256", "digest": digest, "maxBytes": size}
|
|
705
|
+
download(artifact, archive, event)
|
|
706
|
+
npm_transport.extract_bifrost(archive, destination, build["sha256"], build["size_bytes"])
|
|
707
|
+
archive.unlink()
|
|
708
|
+
|
|
709
|
+
|
|
657
710
|
def error_cause(error: BaseException) -> str:
|
|
658
711
|
"""What went wrong, without native tool output or anything read from the network."""
|
|
659
712
|
if isinstance(error, RuntimeDownloadError):
|