WIP: feat: Hermes confined to act only as an Endo guest #1
No files matched your search
@@ -18,6 +18,13 @@ python3 -m doctest fake_host.py fake_llm.py
|
|||||||
python3 demo.py path/to/hermes path/to/socat # ends with PASS
|
python3 demo.py path/to/hermes path/to/socat # ends with PASS
|
||||||
```
|
```
|
||||||
|
|
||||||
|
With a model and a proxy command, a real model drives it, through [llm-proxy.py](llm-proxy.py) as deployed. For example, with an [Inference Cooperative](https://docs.inference.coop/guides/api-access/) key:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
python3 demo.py path/to/hermes path/to/socat deepseek-flash-tee \
|
||||||
|
python3 ../llm-proxy.py https://gateway.inference.coop/v1 path/to/key
|
||||||
|
```
|
||||||
|
|
||||||
The demo leaves Hermes's detached helpers running; look for them with `pgrep -fl hermes_cli`. After a change to `nix/`, evaluate both platforms:
|
The demo leaves Hermes's detached helpers running; look for them with `pgrep -fl hermes_cli`. After a change to `nix/`, evaluate both platforms:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
|||||||
@@ -26,11 +26,13 @@ macOS: [NixOS microVM (vfkit) ]
|
|||||||
imports = [ hermes-guest.nixosModules.linux-host ];
|
imports = [ hermes-guest.nixosModules.linux-host ];
|
||||||
services.hermes-guest = {
|
services.hermes-guest = {
|
||||||
enable = true;
|
enable = true;
|
||||||
model = "anthropic/claude-sonnet-4";
|
# the Inference Cooperative: https://docs.inference.coop/guides/api-access/
|
||||||
|
model = "deepseek-flash-tee";
|
||||||
|
reasoningEffort = "low"; # DeepSeek V4.1 takes low, high, xhigh or max
|
||||||
hostSocketUnit = "endo-guest-hermes.socket";
|
hostSocketUnit = "endo-guest-hermes.socket";
|
||||||
};
|
};
|
||||||
services.hermes-llm-proxy = {
|
services.hermes-llm-proxy = {
|
||||||
upstream = "https://openrouter.ai/api/v1";
|
upstream = "https://gateway.inference.coop/v1";
|
||||||
keyFile = "/run/secrets/llm-key"; # a runtime path, not a nix path
|
keyFile = "/run/secrets/llm-key"; # a runtime path, not a nix path
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
@@ -42,8 +44,9 @@ Building the VM needs a Linux builder (for example nix-darwin's `nix.linux-build
|
|||||||
```sh
|
```sh
|
||||||
nix build --impure --expr '(builtins.getFlake (toString ./.)).lib.mkDarwin {
|
nix build --impure --expr '(builtins.getFlake (toString ./.)).lib.mkDarwin {
|
||||||
hostDir = "/Users/me/.local/state/hermes-guest";
|
hostDir = "/Users/me/.local/state/hermes-guest";
|
||||||
model = "anthropic/claude-sonnet-4";
|
model = "deepseek-flash-tee";
|
||||||
upstream = "https://openrouter.ai/api/v1";
|
reasoningEffort = "low";
|
||||||
|
upstream = "https://gateway.inference.coop/v1";
|
||||||
keyFile = "/Users/me/.config/hermes-guest/llm-key";
|
keyFile = "/Users/me/.config/hermes-guest/llm-key";
|
||||||
}.host'
|
}.host'
|
||||||
mkdir -p ~/.local/state/hermes-guest
|
mkdir -p ~/.local/state/hermes-guest
|
||||||
|
|||||||
@@ -59,6 +59,7 @@
|
|||||||
model,
|
model,
|
||||||
upstream,
|
upstream,
|
||||||
keyFile,
|
keyFile,
|
||||||
|
reasoningEffort ? null,
|
||||||
system ? "aarch64-darwin",
|
system ? "aarch64-darwin",
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
@@ -71,7 +72,7 @@
|
|||||||
microvm.vmHostPackages = hostPkgs;
|
microvm.vmHostPackages = hostPkgs;
|
||||||
services.hermes-guest = {
|
services.hermes-guest = {
|
||||||
enable = true;
|
enable = true;
|
||||||
inherit model;
|
inherit model reasoningEffort;
|
||||||
vm.hostDir = hostDir;
|
vm.hostDir = hostDir;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,6 +80,12 @@ in
|
|||||||
type = lib.types.str;
|
type = lib.types.str;
|
||||||
description = "The model name, as the proxy's upstream knows it.";
|
description = "The model name, as the proxy's upstream knows it.";
|
||||||
};
|
};
|
||||||
|
reasoningEffort = lib.mkOption {
|
||||||
|
type = lib.types.nullOr lib.types.str;
|
||||||
|
default = null;
|
||||||
|
example = "low";
|
||||||
|
description = "Hermes's agent.reasoning_effort, for models that take only some levels (DeepSeek V4.1: low, high, xhigh, max).";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
config = lib.mkIf cfg.enable {
|
config = lib.mkIf cfg.enable {
|
||||||
@@ -98,6 +104,9 @@ in
|
|||||||
# the guest's methods are its whole world: offer them to the model
|
# the guest's methods are its whole world: offer them to the model
|
||||||
# directly, not behind tool_search/tool_call
|
# directly, not behind tool_search/tool_call
|
||||||
tools.tool_search.enabled = "off";
|
tools.tool_search.enabled = "off";
|
||||||
|
}
|
||||||
|
// lib.optionalAttrs (cfg.reasoningEffort != null) {
|
||||||
|
agent.reasoning_effort = cfg.reasoningEffort;
|
||||||
};
|
};
|
||||||
mcpServers.host = {
|
mcpServers.host = {
|
||||||
command = "${pkgs.socat}/bin/socat";
|
command = "${pkgs.socat}/bin/socat";
|
||||||
|
|||||||
+43
-9
@@ -7,16 +7,24 @@ does (socat STDIO UNIX-CONNECT:host.sock). It also serves a scripted
|
|||||||
model, so the run needs no API key and does the same thing every time.
|
model, so the run needs no API key and does the same thing every time.
|
||||||
Then it checks what the host received.
|
Then it checks what the host received.
|
||||||
|
|
||||||
usage: demo.py HERMES SOCAT
|
With MODEL and PROXY, a real model drives it instead: each connection to
|
||||||
|
Hermes's model port is handed to PROXY as its stdin, as launchd and
|
||||||
|
systemd hand connections to llm-proxy.py. A real model needn't follow
|
||||||
|
the script, so only the outcome is checked: the pen comes back.
|
||||||
|
|
||||||
|
usage: demo.py HERMES SOCAT [MODEL PROXY...]
|
||||||
HERMES the hermes executable
|
HERMES the hermes executable
|
||||||
SOCAT the socat executable
|
SOCAT the socat executable
|
||||||
|
MODEL e.g. deepseek-flash-tee
|
||||||
|
PROXY e.g. python3 ../llm-proxy.py https://gateway.inference.coop/v1 KEY_FILE
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||||
from socketserver import StreamRequestHandler, ThreadingUnixStreamServer
|
from socketserver import (BaseRequestHandler, StreamRequestHandler,
|
||||||
|
ThreadingTCPServer, ThreadingUnixStreamServer)
|
||||||
|
|
||||||
from fake_host import HostSide, Mailbox, Ref, handle_rpc
|
from fake_host import HostSide, Mailbox, Ref, handle_rpc
|
||||||
from fake_llm import chunks, completion, next_step
|
from fake_llm import chunks, completion, next_step
|
||||||
@@ -92,12 +100,20 @@ class LlmHandler(BaseHTTPRequestHandler):
|
|||||||
logging.debug(format, *args)
|
logging.debug(format, *args)
|
||||||
|
|
||||||
|
|
||||||
def hermes_config(socat, host_sock, llm_port):
|
class InetdHandler(BaseRequestHandler):
|
||||||
|
"""One connection, handed to the proxy command as its stdin (inetd style)."""
|
||||||
|
|
||||||
|
def handle(self):
|
||||||
|
self.server.popen(self.server.command, stdin=self.request).wait()
|
||||||
|
|
||||||
|
|
||||||
|
def hermes_config(socat, host_sock, llm_port, model, extra=()):
|
||||||
"""config.yaml for the run (JSON is YAML): the deployed unit's settings."""
|
"""config.yaml for the run (JSON is YAML): the deployed unit's settings."""
|
||||||
return json.dumps({
|
return json.dumps({
|
||||||
|
**dict(extra),
|
||||||
"model": {
|
"model": {
|
||||||
"provider": "custom",
|
"provider": "custom",
|
||||||
"default": MODEL,
|
"default": model,
|
||||||
"base_url": f"http://127.0.0.1:{llm_port}/v1",
|
"base_url": f"http://127.0.0.1:{llm_port}/v1",
|
||||||
"api_key": "held-by-proxy",
|
"api_key": "held-by-proxy",
|
||||||
},
|
},
|
||||||
@@ -127,8 +143,19 @@ def check(host, mailbox, seen, stdout):
|
|||||||
return [what for what, ok in expect.items() if not ok]
|
return [what for what, ok in expect.items() if not ok]
|
||||||
|
|
||||||
|
|
||||||
|
def check_outcome(host):
|
||||||
|
"""What a real model should have brought about; the failures, if any."""
|
||||||
|
expect = {
|
||||||
|
"the host got the pen back, as a reference": Ref("Pen") in host.received.values(),
|
||||||
|
"in a reply to message 0": any(m.get("replyTo") == 0 for m in host.inbox),
|
||||||
|
}
|
||||||
|
return [what for what, ok in expect.items() if not ok]
|
||||||
|
|
||||||
|
|
||||||
def main(argv, make_tmpdir, run, popen, timeout_expired, environ):
|
def main(argv, make_tmpdir, run, popen, timeout_expired, environ):
|
||||||
hermes, socat = argv[1:3]
|
hermes, socat = argv[1:3]
|
||||||
|
live = argv[3:4]
|
||||||
|
model = argv[3] if live else MODEL
|
||||||
with make_tmpdir() as tmp:
|
with make_tmpdir() as tmp:
|
||||||
host = HostSide(on_request=lambda description: Ref("Pen"))
|
host = HostSide(on_request=lambda description: Ref("Pen"))
|
||||||
mailbox = Mailbox(host)
|
mailbox = Mailbox(host)
|
||||||
@@ -137,8 +164,12 @@ def main(argv, make_tmpdir, run, popen, timeout_expired, environ):
|
|||||||
host_sock = str(tmp / "host.sock")
|
host_sock = str(tmp / "host.sock")
|
||||||
mcp = ThreadingUnixStreamServer(host_sock, McpHandler)
|
mcp = ThreadingUnixStreamServer(host_sock, McpHandler)
|
||||||
mcp.mailbox = mailbox
|
mcp.mailbox = mailbox
|
||||||
llm = ThreadingHTTPServer(("127.0.0.1", 0), LlmHandler)
|
if live:
|
||||||
llm.script, llm.seen, llm.errors = SCRIPT, [], []
|
llm = ThreadingTCPServer(("127.0.0.1", 0), InetdHandler)
|
||||||
|
llm.popen, llm.command = popen, argv[4:]
|
||||||
|
else:
|
||||||
|
llm = ThreadingHTTPServer(("127.0.0.1", 0), LlmHandler)
|
||||||
|
llm.script, llm.seen, llm.errors = SCRIPT, [], []
|
||||||
for server in (mcp, llm):
|
for server in (mcp, llm):
|
||||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||||
|
|
||||||
@@ -146,7 +177,9 @@ def main(argv, make_tmpdir, run, popen, timeout_expired, environ):
|
|||||||
hermes_home = home / ".hermes"
|
hermes_home = home / ".hermes"
|
||||||
hermes_home.mkdir(parents=True)
|
hermes_home.mkdir(parents=True)
|
||||||
(hermes_home / "config.yaml").write_text(
|
(hermes_home / "config.yaml").write_text(
|
||||||
hermes_config(socat, host_sock, llm.server_address[1]))
|
hermes_config(socat, host_sock, llm.server_address[1], model,
|
||||||
|
# DeepSeek V4.1 takes low|high|xhigh|max, not Hermes's default
|
||||||
|
{"agent": {"reasoning_effort": "low"}} if live else ()))
|
||||||
|
|
||||||
env = dict(environ, HOME=str(home), HERMES_HOME=str(hermes_home))
|
env = dict(environ, HOME=str(home), HERMES_HOME=str(hermes_home))
|
||||||
# As in the deployed unit, the agent runs in `hermes gateway`, which
|
# As in the deployed unit, the agent runs in `hermes gateway`, which
|
||||||
@@ -159,7 +192,7 @@ def main(argv, make_tmpdir, run, popen, timeout_expired, environ):
|
|||||||
try:
|
try:
|
||||||
done = run([hermes, "chat", "--oneshot", "-q",
|
done = run([hermes, "chat", "--oneshot", "-q",
|
||||||
"Check your messages and do what your host asks."],
|
"Check your messages and do what your host asks."],
|
||||||
env=env, capture_output=True, text=True, timeout=120)
|
env=env, capture_output=True, text=True, timeout=300 if live else 120)
|
||||||
stdout = done.stdout
|
stdout = done.stdout
|
||||||
logging.info("hermes exited %s\n--- stdout\n%s\n--- stderr\n%s",
|
logging.info("hermes exited %s\n--- stdout\n%s\n--- stderr\n%s",
|
||||||
done.returncode, done.stdout[-3000:], done.stderr[-3000:])
|
done.returncode, done.stdout[-3000:], done.stderr[-3000:])
|
||||||
@@ -178,7 +211,8 @@ def main(argv, make_tmpdir, run, popen, timeout_expired, environ):
|
|||||||
logging.info("--- gateway log (tail)\n%s",
|
logging.info("--- gateway log (tail)\n%s",
|
||||||
(tmp / "gateway.log").read_text()[-2000:])
|
(tmp / "gateway.log").read_text()[-2000:])
|
||||||
|
|
||||||
failures = check(host, mailbox, llm.seen, stdout) + llm.errors
|
failures = (check_outcome(host) if live
|
||||||
|
else check(host, mailbox, llm.seen, stdout) + llm.errors)
|
||||||
logging.info("host inbox: %s", json.dumps(host.inbox))
|
logging.info("host inbox: %s", json.dumps(host.inbox))
|
||||||
for what in failures:
|
for what in failures:
|
||||||
logging.error("FAILED: %s", what)
|
logging.error("FAILED: %s", what)
|
||||||
|
|||||||
Reference in new issue
Block a user