diff --git a/scripts/debrand.py b/scripts/debrand.py index f220b77..df4b424 100755 --- a/scripts/debrand.py +++ b/scripts/debrand.py @@ -327,6 +327,40 @@ async def _stop_unwatched(authority, actor, session_id, orphaned): r'print\("Detached; accepted work continues at the gateway\.", file=sys\.stderr\)', 'print("Stopped.", file=sys.stderr) @NOTE', "the gateway stops what this chat started"), + # -- errors name who said no: the engine calls any endpoint it doesn't know "custom", and ghost has two (its bridge, + # and Ollama for the local model). Each turn says where its chat goes, and the name comes from ghost's config. + ("hermes_cli/models.py", + r'(def provider_label\(provider: Optional\[str\]\) -> str:\n(?: .*\n)*? normalized = normalize_provider\(normalized\)\n)' + r'( return _PROVIDER_LABELS\.get\(normalized, original or "OpenRouter"\)\n)', + r'GHOST_ROUTE = contextvars.ContextVar("ghost_route", default=None) @NOTE' "\n" + r'_GHOST_NAMES = {"opengradient": "DreamFort", "ollama-local": "Ollama"}' "\n\n\n" + r'def _ghost_name(base_url):' "\n" + r' """DreamFort or Ollama, when *base_url* is where the config sends that provider."""' "\n" + r' try:' "\n" + r' from hermes_cli.config import load_config' "\n" + r' providers = load_config().get("providers") or {}' "\n" + r' except Exception:' "\n" + r' return None' "\n" + r' url = str(base_url or "").rstrip("/")' "\n" + r' return next((_GHOST_NAMES[name] for name, cfg in providers.items() if name in _GHOST_NAMES and isinstance(cfg, dict)' "\n" + r' and url and str(cfg.get("base_url") or "").rstrip("/") == url), None)' "\n\n\n" + r'\1 if normalized == "custom" and GHOST_ROUTE.get():' "\n" + r' named = _ghost_name(GHOST_ROUTE.get())' "\n" + r' if named:' "\n" + r' return named' "\n" + r'\2', + "ghost's providers by name"), + ("agent/conversation_loop.py", + r'( from agent\.turn_context import export_current_turn_boundary\n)', + r' from hermes_cli.models import GHOST_ROUTE' "\n" + r' GHOST_ROUTE.set(getattr(agent, "base_url", None)) @NOTE' "\n" + r'\1', + "where this turn's chat goes, for its errors"), + ("agent/billing_links.py", + r' return slug\.replace\("_", " "\)\.replace\("-", " "\)\.strip\(\)\.title\(\) or "your provider", None\n', + ' from hermes_cli.models import _ghost_name\n' + ' return _ghost_name(base) or slug.replace("_", " ").replace("-", " ").strip().title() or "your provider", None @NOTE\n', + "out of credits names who said so"), ] # Upstream defaults ghost flips off for EVERY profile. The shipped profile sets these too; baking them diff --git a/tests/e2e.py b/tests/e2e.py index 2398e26..0fe2621 100644 --- a/tests/e2e.py +++ b/tests/e2e.py @@ -118,15 +118,18 @@ def main(): code, out = run("ghost", "-z", "[[fail long 1]]") asked = [r for r in requests() if r.get("tools")] good &= check("a reply that runs out of room is asked for again, in smaller steps", code == 0 and "a fake reply" in out and len(asked) == 2, out + str(asked)) + code, out = run("ghost", "-z", "[[fail long]]") + requests() + good &= check("and when it runs out again, says so", "again when asked for smaller steps" in out, out) code, out = run("ghost", "-z", "[[fail 402]]") - good &= check("no credits is said plainly", "credits" in out and not requests()[0].get("billed"), out) + good &= check("no credits is said plainly", "DreamFort credits have run out" in out and not requests()[0].get("billed"), out) code, out = run("ghost", "sessions", "list") good &= check("the chat is there", code == 0 and "hello from the stand-in" in out, out) code, out = run("ghost", "doctor") good &= check("doctor is content", code == 0 and "login accepted" in out and "1 in the registry" in out, out) code, out = run("ghost", "usage") - good &= check("usage counts", code == 0 and "Last hour" in out and "0 replies" not in out.splitlines()[0], out) + good &= check("usage counts", code == 0 and "Last hour" in out and " 0 replies" not in out.splitlines()[0], out) code, out = run("ghost", "logout") good &= check("logged out", code == 0 and run("ghost-login", "--status")[0] != 0, out) finally: diff --git a/tests/test_engine.py b/tests/test_engine.py index 052e59b..48200b2 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -293,6 +293,29 @@ def test_the_engine_accepts_the_local_model(engine): assert engine(WINDOW + "out = window('ghost-tool:latest', 'ollama-local')", home=home) == 65536 +LABEL = ("from hermes_cli.models import GHOST_ROUTE, provider_label\nfrom hermes_cli.config import load_config\n" + "def label(provider):\n" + " GHOST_ROUTE.set((load_config()['providers'].get(provider) or {}).get('base_url'))\n" + " return provider_label('custom')\n") + + +def test_errors_say_who_refused(engine): + """The engine calls an endpoint it doesn't know "custom". A chat that goes to the bridge is DreamFort's.""" + out = engine(LABEL + "import inspect, agent.conversation_loop as loop\n" + "from agent.billing_links import build_billing_block\n" + "url = load_config()['providers']['opengradient']['base_url']\n" + "out = [label('opengradient'), provider_label('openrouter'), 'GHOST_ROUTE.set' in inspect.getsource(loop.run_conversation),\n" + " build_billing_block(provider='custom', base_url=url, model='m').provider_label]\n" + "GHOST_ROUTE.set('http://127.0.0.1:9/v1')\nout.append(provider_label('custom'))") + assert out == ["DreamFort", "OpenRouter", True, "DreamFort", "custom"] + + +def test_the_local_models_errors_say_ollama(engine): + home = engine.home.parent / "local" + _write_profile(home, "--local-model", "ghost-tool:latest") + assert engine(LABEL + "out = [label('ollama-local'), label('opengradient')]", home=home) == ["Ollama", "DreamFort"] + + def test_model_picker_offers_what_the_bridge_serves(engine): """And no local provider, unless ghost was installed with one.""" out = engine("from hermes_cli.config import load_config\nout = sorted(load_config()['providers'])")