- sidecar/gen_progress.py, local_ready.py, local_room.py moved into the provider-evox2 plugin's new provider.sidecar module; the core reaches them through usage_loader.live_progress / before_launch and the module's extra_routers() (/local-ready, host only). No plugin: no-ops. - provider.sidecar: a provider sidecar module that is no usage reader (typed_manifest, store.sync_typed, DriverLoader code_key tuple). - backend/models/openrouter.py deleted: the APPE catalogue is the openrouter_appe discovery of plugins/providers.py (appe_catalogue, warm_discovery, legacy_appe_index). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
219 lines
12 KiB
Python
219 lines
12 KiB
Python
"""Local-model runs' live generation: which runs are local and where, what a
|
|
llm-server slot says (load → prefill → decode), the in-flight opencode tool,
|
|
the transcript's running tool, rates and the stall clock — and the guide a
|
|
local run is told on top.
|
|
|
|
``gen_progress`` is provider-evox2's sidecar module now (conftest aliases the
|
|
name to the plugin's)."""
|
|
|
|
import json
|
|
import pathlib
|
|
import sqlite3
|
|
|
|
import gen_progress as gp
|
|
import opencode_cli
|
|
|
|
SID = "11111111-2222-3333-4444-555555555555"
|
|
REG = {"evox2": {"baseUrl": "http://box:8090/v1", "apiKey": "k", "models": [
|
|
{"id": "qwen3.8-flash-next-coder"}, {"id": "ggml-org/qwen3.8-27b"}]},
|
|
"openrouter": {"baseUrl": "https://or/v1", "models": [{"id": "qwen/qwen3.8-27b"}]}}
|
|
|
|
|
|
# ── which runs are local ─────────────────────────────────────────────────────
|
|
def test_opencode_names_provider_and_id_the_id_may_hold_a_slash():
|
|
t = gp.local_target("opencode", "evox2/ggml-org/qwen3.8-27b", REG)
|
|
assert t == {"root": "http://box:8090", "key": "k", "model": "ggml-org/qwen3.8-27b"}
|
|
assert gp.local_target("opencode", "openai/gpt-5.5", REG) is None
|
|
assert gp.local_target("opencode", "gpt-5.5", REG) is None
|
|
|
|
|
|
def test_pi_resolves_the_provider_from_the_registry_like_the_runner():
|
|
assert gp.local_target("pi", "qwen3.8-flash-next-coder", REG)["model"] == "qwen3.8-flash-next-coder"
|
|
assert gp.local_target("pi", "ggml-org/qwen3.8-27b", REG)["root"] == "http://box:8090"
|
|
assert gp.local_target("pi", "qwen/qwen3.8-27b", REG) is None # OpenRouter's
|
|
assert gp.local_target("claude", "opus", REG) is None
|
|
|
|
|
|
# ── the server's side ────────────────────────────────────────────────────────
|
|
def _server(monkeypatch, models, slots):
|
|
calls = []
|
|
|
|
def fake_get(url, key):
|
|
calls.append(url)
|
|
if url.endswith("/models"):
|
|
return {"data": [{"id": m, "status": {"value": s}} for m, s in models.items()]}
|
|
return slots
|
|
monkeypatch.setattr(gp, "_get", fake_get)
|
|
return calls
|
|
|
|
|
|
def test_an_unloaded_model_is_never_asked_for_its_slot(monkeypatch):
|
|
calls = _server(monkeypatch, {"m": "unloaded"}, [])
|
|
assert gp.server_reading("http://box:8090", "k", "m", {}) == {"phase": "idle"}
|
|
assert calls == ["http://box:8090/models"] # /slots?model= would load it
|
|
_server(monkeypatch, {"m": "loading"}, [])
|
|
assert gp.server_reading("http://box:8090", "k", "m", {})["phase"] == "loading"
|
|
|
|
|
|
def test_a_slot_reads_as_prefill_then_decode(monkeypatch):
|
|
slot = {"id_task": 7, "is_processing": True, "n_prompt_tokens": 75000,
|
|
"n_prompt_tokens_cache": 68000, "n_prompt_tokens_processed": 2048,
|
|
"next_token": [{"n_decoded": 0, "n_remain": -1}]}
|
|
_server(monkeypatch, {"m": "loaded"}, [slot])
|
|
r = gp.server_reading("http://box:8090", "k", "m", {})
|
|
assert r == {"phase": "prefill", "task": 7, "promptTokens": 75000,
|
|
"promptCached": 68000, "promptDone": 2048}
|
|
# decoding: n_prompt_tokens counts what was written too
|
|
slot.update(n_prompt_tokens=75000 + 6000, next_token=[{"n_decoded": 6000, "n_remain": 10384}])
|
|
r = gp.server_reading("http://box:8090", "k", "m", {})
|
|
assert (r["phase"], r["decoded"], r["cap"], r["promptTokens"]) == ("decode", 6000, 16384, 75000)
|
|
|
|
|
|
def test_models_is_read_once_per_server_and_tick(monkeypatch):
|
|
calls = _server(monkeypatch, {"a": "loaded", "b": "loaded"}, [])
|
|
loaded: dict = {}
|
|
gp.server_reading("http://box:8090", "k", "a", loaded)
|
|
gp.server_reading("http://box:8090", "k", "b", loaded)
|
|
assert sum(c.endswith("/models") for c in calls) == 1
|
|
|
|
|
|
def test_an_unreachable_server_says_so(monkeypatch):
|
|
def boom(url, key):
|
|
raise OSError("no route")
|
|
monkeypatch.setattr(gp, "_get", boom)
|
|
assert gp.server_reading("http://box:8090", "k", "m", {}) == {"phase": "unreachable"}
|
|
|
|
|
|
# ── the run's own side ───────────────────────────────────────────────────────
|
|
def _jsonl(path: pathlib.Path, *recs):
|
|
path.write_text("".join(json.dumps(r) + "\n" for r in recs))
|
|
return path
|
|
|
|
|
|
def test_a_transcript_ending_on_a_tool_call_has_a_tool_running(tmp_path):
|
|
call = {"type": "assistant", "message": {"content": [{"type": "tool_use", "id": "t"}]}}
|
|
usage = {"type": "assistant", "message": {"usage": {"output_tokens": 3}}} # content-less
|
|
result = {"type": "user", "message": {"content": [{"type": "tool_result", "tool_use_id": "t"}]}}
|
|
assert gp.tool_running(_jsonl(tmp_path / "a.jsonl", call, usage))
|
|
assert not gp.tool_running(_jsonl(tmp_path / "b.jsonl", call, result))
|
|
assert not gp.tool_running(tmp_path / "missing.jsonl")
|
|
|
|
|
|
def test_a_v1_transcript_ending_on_a_tool_call_has_a_tool_running(tmp_path):
|
|
head = {"t": "session", "v": 1, "id": SID, "harness": "opencode"}
|
|
step = [{"t": "call", "id": "m"}, {"t": "text", "text": "Running it."},
|
|
{"t": "tool_call", "id": "t", "tool": "shell"}, {"t": "usage", "output": 3}]
|
|
result = {"t": "tool_result", "toolCall": "t", "text": "ok"}
|
|
answer = [{"t": "call", "id": "m2"}, {"t": "text", "text": "Done."}, {"t": "usage"}]
|
|
assert gp.tool_running(_jsonl(tmp_path / "a.jsonl", head, *step))
|
|
assert gp.tool_running(_jsonl(tmp_path / "b.jsonl", head, *step,
|
|
{"t": "call", "id": "m", "empty": True}, {"t": "usage"}))
|
|
assert not gp.tool_running(_jsonl(tmp_path / "c.jsonl", head, *step, result))
|
|
assert not gp.tool_running(_jsonl(tmp_path / "d.jsonl", head, *step, result, *answer))
|
|
assert not gp.tool_running(_jsonl(tmp_path / "e.jsonl", head,
|
|
{"t": "prompt", "text": "hi", "origin": "user"}))
|
|
|
|
|
|
def test_the_transcript_is_looked_up_where_each_harness_writes_it(monkeypatch, tmp_path):
|
|
"""pi's runner replaces only the slashes of the cwd, the opencode mirror
|
|
every non-alphanumeric — they differ on a cwd with a `.` or a `_`."""
|
|
import opencode_mirror
|
|
monkeypatch.setattr(gp, "PI_RUNNER_DIR", tmp_path / "pi")
|
|
monkeypatch.setattr(gp, "OPENCODE_RUNNER_DIR", tmp_path / "oc")
|
|
cwd = "/home/u/.local/share/my_app.v2"
|
|
assert gp.transcript_path(SID, {"harness": "pi", "cwd": cwd}) == \
|
|
tmp_path / "pi" / "transcripts" / "-home-u-.local-share-my_app.v2" / f"{SID}.jsonl"
|
|
oc = gp.transcript_path(SID, {"harness": "opencode", "cwd": cwd, "account": "work"})
|
|
assert oc == tmp_path / "oc" / "transcripts" / "work" / "-home-u--local-share-my-app-v2" / f"{SID}.jsonl"
|
|
assert oc.parent.name == opencode_mirror.cwd_slug(cwd)
|
|
assert gp.transcript_path(SID, {"harness": "claude", "cwd": cwd}) is None
|
|
|
|
|
|
def test_opencode_names_the_tool_call_being_streamed(monkeypatch, tmp_path):
|
|
db = tmp_path / "opencode.db"
|
|
con = sqlite3.connect(db)
|
|
con.execute("CREATE TABLE session_message (id TEXT, session_id TEXT, type TEXT, seq INTEGER, data TEXT)")
|
|
step = {"content": [{"type": "text", "text": ""},
|
|
{"type": "tool", "name": "write", "state": {"status": "streaming", "input": ""}}]}
|
|
con.execute("INSERT INTO session_message VALUES ('m1', ?, 'assistant', 1, ?)",
|
|
(f"ses_{SID}", json.dumps(step)))
|
|
con.commit()
|
|
con.close()
|
|
monkeypatch.setattr(opencode_cli, "db_path", lambda account: db)
|
|
rec = {"harness": "opencode", "account": "opencode-lab"}
|
|
assert gp.streaming_tool(rec, SID) == "write"
|
|
assert gp.streaming_tool({"harness": "pi"}, SID) is None
|
|
|
|
|
|
# ── a sequence of readings ───────────────────────────────────────────────────
|
|
def test_rate_and_phase_start_follow_the_same_request():
|
|
a = gp._advance(None, {"phase": "decode", "task": 1, "decoded": 100}, 1000.0, 900.0)
|
|
assert a["since"] == 1000.0 and "tokPerS" not in a and a["idleSince"] is None
|
|
b = gp._advance(a, {"phase": "decode", "task": 1, "decoded": 160}, 1003.0, 900.0)
|
|
assert b["since"] == 1000.0 and b["tokPerS"] == 20.0
|
|
c = gp._advance(b, {"phase": "prefill", "task": 2, "promptDone": 0}, 1006.0, 900.0)
|
|
assert c["since"] == 1006.0 and "tokPerS" not in c
|
|
|
|
|
|
def test_a_new_phase_starts_back_dated_by_the_runs_known_rate():
|
|
a = gp._advance(None, {"phase": "decode", "task": 1, "decoded": 100}, 1000.0, 0.0)
|
|
b = gp._advance(a, {"phase": "decode", "task": 1, "decoded": 120}, 1001.0, 0.0)
|
|
assert b["tokPerS"] == 20.0
|
|
# the tool ran, the next request already wrote 10 tokens: it began ~0.5 s ago
|
|
c = gp._advance(b, {"phase": "decode", "task": 2, "decoded": 10}, 1002.0, 0.0)
|
|
assert c["since"] == 1001.5
|
|
# never before the previous reading: the phase wasn't there yet
|
|
d = gp._advance(c, {"phase": "decode", "task": 3, "decoded": 400}, 1003.0, 0.0)
|
|
assert d["since"] == 1002.0
|
|
# no rate known for prefill yet: the poll's own time, as before
|
|
e = gp._advance(d, {"phase": "prefill", "task": 4, "promptDone": 500}, 1004.0, 0.0)
|
|
assert e["since"] == 1004.0
|
|
|
|
|
|
def test_the_rate_is_smoothed_over_time_not_per_poll():
|
|
a = gp._advance(None, {"phase": "decode", "task": 1, "decoded": 100}, 1000.0, 0.0)
|
|
b = gp._advance(a, {"phase": "decode", "task": 1, "decoded": 120}, 1001.0, 0.0)
|
|
c = gp._advance(b, {"phase": "decode", "task": 1, "decoded": 160}, 1002.0, 0.0)
|
|
assert c["tokPerS"] == 24.4 # a 1 s burst at 40 moves it a fifth of the way
|
|
slow = gp._advance(b, {"phase": "decode", "task": 1, "decoded": 280}, 1005.0, 0.0)
|
|
assert slow["tokPerS"] == 32.6 # 4 s at 40 tok/s: most of the way
|
|
|
|
|
|
def test_the_trail_carries_the_previous_readings_oldest_first():
|
|
r = None
|
|
for i in range(9):
|
|
r = gp._advance(r, {"phase": "decode", "task": 1, "decoded": 10 * i}, 1000.0 + i, 0.0)
|
|
assert [p["at"] for p in r["trail"]] == [1002.0 + i for i in range(gp.TRAIL_N)]
|
|
assert r["trail"][-1] == {"at": 1007.0, "phase": "decode", "since": 1000.0,
|
|
"decoded": 70, "tokPerS": 10.0}
|
|
assert all("trail" not in p and "rates" not in p and "task" not in p for p in r["trail"])
|
|
|
|
|
|
def test_idle_counts_from_the_last_sign_of_life_unless_a_tool_runs():
|
|
busy = gp._advance(None, {"phase": "decode", "task": 1, "decoded": 5}, 1000.0, 0.0)
|
|
idle = gp._advance(busy, {"phase": "idle"}, 1500.0, 1200.0) # transcript moved at 1200
|
|
assert idle["idleSince"] == 1200.0
|
|
later = gp._advance(idle, {"phase": "idle"}, 2500.0, 1200.0)
|
|
assert later["idleSince"] == 1200.0
|
|
waiting = gp._advance(later, {"phase": "idle", "toolRunning": True}, 2600.0, 1200.0)
|
|
assert waiting["idleSince"] is None # an ask-form wait, a build
|
|
down = gp._advance(None, {"phase": "unreachable"}, 3000.0, 2900.0)
|
|
assert down["idleSince"] == 2900.0
|
|
|
|
|
|
# ── the guide a local run gets ───────────────────────────────────────────────
|
|
def test_only_a_local_opencode_run_gets_the_local_model_guide(monkeypatch, tmp_path):
|
|
guide = tmp_path / "local-model.md"
|
|
guide.write_text("# small calls")
|
|
monkeypatch.setattr(opencode_cli, "LOCAL_MODEL_GUIDE", guide)
|
|
monkeypatch.setattr(opencode_cli, "OPENCODE_INSTRUCTIONS", ["CLAUDE.md"])
|
|
assert opencode_cli.run_config("evox2/qwen3.8-flash-next-coder")["instructions"] == ["CLAUDE.md", str(guide)]
|
|
assert opencode_cli.run_config("gpt-5.5")["instructions"] == ["CLAUDE.md"]
|
|
env = opencode_cli.run_env("default", {}, "/x", "evox2/qwen3.6-35b-a3b")
|
|
assert str(guide) in json.loads(env["OPENCODE_CONFIG_CONTENT"])["instructions"]
|
|
|
|
|
|
def test_the_shipped_guide_exists():
|
|
import config
|
|
assert config.LOCAL_MODEL_GUIDE.is_file()
|