Compare commits
2 Commits
claude/iss
...
fix/457-ss
| Author | SHA1 | Date | |
|---|---|---|---|
| 66b0febdfb | |||
| 6d79bf7783 |
@@ -182,6 +182,15 @@ _SCRIPT_FAILURE_PHRASES = (
|
||||
"exit status",
|
||||
"non-zero exit",
|
||||
"did not complete",
|
||||
# SSH-specific failure patterns (#350)
|
||||
"no such file or directory",
|
||||
"command not found",
|
||||
"hermes binary not found",
|
||||
"hermes not found",
|
||||
"ssh: connect to host",
|
||||
"connection timed out",
|
||||
"host key verification failed",
|
||||
"no route to host",
|
||||
"could not run",
|
||||
"unable to execute",
|
||||
"permission denied",
|
||||
|
||||
212
cron/ssh_dispatch.py
Normal file
212
cron/ssh_dispatch.py
Normal file
@@ -0,0 +1,212 @@
|
||||
"""
|
||||
SSH dispatch utilities for cron jobs.
|
||||
|
||||
Provides validated remote execution so broken hermes binary paths
|
||||
are caught before draining the dispatch queue.
|
||||
|
||||
Usage:
|
||||
from cron.ssh_dispatch import SSHEnvironment, format_dispatch_report
|
||||
|
||||
ssh = SSHEnvironment(host="root@ezra", agent="allegro")
|
||||
result = ssh.dispatch("cron tick")
|
||||
if not result.success:
|
||||
print(result.failure_reason)
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import shutil
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class DispatchResult:
|
||||
"""Structured result of a remote command dispatch."""
|
||||
host: str
|
||||
command: str
|
||||
success: bool
|
||||
exit_code: Optional[int] = None
|
||||
stdout: str = ""
|
||||
stderr: str = ""
|
||||
failure_reason: Optional[str] = None
|
||||
duration_s: float = 0.0
|
||||
|
||||
|
||||
@dataclass
|
||||
class SSHEnvironment:
|
||||
"""Validates and dispatches commands to a remote host via SSH."""
|
||||
|
||||
host: str # e.g. "root@ezra" or "192.168.1.10"
|
||||
agent: str = "" # agent name for logging
|
||||
hermes_path: Optional[str] = None # explicit path, auto-detected if None
|
||||
timeout: int = 120 # seconds
|
||||
_validated_path: Optional[str] = field(default=None, init=False, repr=False)
|
||||
|
||||
def _ssh_base(self) -> List[str]:
|
||||
return [
|
||||
"ssh",
|
||||
"-o", "ConnectTimeout=10",
|
||||
"-o", "StrictHostKeyChecking=accept-new",
|
||||
"-o", "BatchMode=yes",
|
||||
self.host,
|
||||
]
|
||||
|
||||
def _probe_remote_binary(self, candidate: str) -> bool:
|
||||
"""Check if a hermes binary exists and is executable on the remote host."""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
self._ssh_base() + [f"test -x {candidate}"],
|
||||
capture_output=True, timeout=15,
|
||||
)
|
||||
return result.returncode == 0
|
||||
except (subprocess.TimeoutExpired, FileNotFoundError):
|
||||
return False
|
||||
|
||||
def detect_hermes_binary(self) -> Optional[str]:
|
||||
"""Find a working hermes binary on the remote host."""
|
||||
if self._validated_path:
|
||||
return self._validated_path
|
||||
|
||||
candidates = []
|
||||
if self.hermes_path:
|
||||
candidates.append(self.hermes_path)
|
||||
|
||||
# Common locations
|
||||
candidates.extend([
|
||||
"hermes", # on PATH
|
||||
"~/.local/bin/hermes",
|
||||
"/usr/local/bin/hermes",
|
||||
f"~/wizards/{self.agent}/venv/bin/hermes" if self.agent else "",
|
||||
f"/root/wizards/{self.agent}/venv/bin/hermes" if self.agent else "",
|
||||
])
|
||||
candidates = [c for c in candidates if c]
|
||||
|
||||
for candidate in candidates:
|
||||
if self._probe_remote_binary(candidate):
|
||||
self._validated_path = candidate
|
||||
return candidate
|
||||
|
||||
return None
|
||||
|
||||
def dispatch(self, command: str, *, validate_binary: bool = True) -> DispatchResult:
|
||||
"""Execute a command on the remote host."""
|
||||
import time
|
||||
start = time.monotonic()
|
||||
|
||||
if validate_binary:
|
||||
binary = self.detect_hermes_binary()
|
||||
if not binary:
|
||||
return DispatchResult(
|
||||
host=self.host,
|
||||
command=command,
|
||||
success=False,
|
||||
failure_reason=f"No working hermes binary found on {self.host}",
|
||||
duration_s=time.monotonic() - start,
|
||||
)
|
||||
|
||||
try:
|
||||
result = subprocess.run(
|
||||
self._ssh_base() + [command],
|
||||
capture_output=True,
|
||||
timeout=self.timeout,
|
||||
)
|
||||
duration = time.monotonic() - start
|
||||
stdout = result.stdout.decode("utf-8", errors="replace")
|
||||
stderr = result.stderr.decode("utf-8", errors="replace")
|
||||
|
||||
failure_reason = None
|
||||
if result.returncode != 0:
|
||||
failure_reason = _classify_ssh_error(stderr, result.returncode)
|
||||
|
||||
return DispatchResult(
|
||||
host=self.host,
|
||||
command=command,
|
||||
success=result.returncode == 0,
|
||||
exit_code=result.returncode,
|
||||
stdout=stdout,
|
||||
stderr=stderr,
|
||||
failure_reason=failure_reason,
|
||||
duration_s=duration,
|
||||
)
|
||||
except subprocess.TimeoutExpired:
|
||||
return DispatchResult(
|
||||
host=self.host,
|
||||
command=command,
|
||||
success=False,
|
||||
failure_reason=f"SSH command timed out after {self.timeout}s",
|
||||
duration_s=time.monotonic() - start,
|
||||
)
|
||||
except FileNotFoundError:
|
||||
return DispatchResult(
|
||||
host=self.host,
|
||||
command=command,
|
||||
success=False,
|
||||
failure_reason="ssh binary not found on local system",
|
||||
duration_s=time.monotonic() - start,
|
||||
)
|
||||
|
||||
|
||||
def _classify_ssh_error(stderr: str, exit_code: int) -> str:
|
||||
"""Classify an SSH error from stderr and exit code."""
|
||||
lower = stderr.lower()
|
||||
|
||||
if "no such file or directory" in lower:
|
||||
return f"Remote binary or file not found (exit {exit_code})"
|
||||
if "command not found" in lower:
|
||||
return f"Command not found on remote host (exit {exit_code})"
|
||||
if "permission denied" in lower:
|
||||
return f"Permission denied (exit {exit_code})"
|
||||
if "connection timed out" in lower or "connection refused" in lower:
|
||||
return f"SSH connection failed (exit {exit_code})"
|
||||
if "host key verification failed" in lower:
|
||||
return f"Host key verification failed (exit {exit_code})"
|
||||
if "no route to host" in lower:
|
||||
return f"No route to host (exit {exit_code})"
|
||||
if exit_code == 127:
|
||||
return f"Command not found (exit 127)"
|
||||
if exit_code == 126:
|
||||
return f"Command not executable (exit 126)"
|
||||
|
||||
return f"Command failed with exit code {exit_code}: {stderr[:200]}"
|
||||
|
||||
|
||||
def dispatch_to_hosts(
|
||||
hosts: List[str],
|
||||
command: str,
|
||||
agent: str = "",
|
||||
timeout: int = 120,
|
||||
) -> List[DispatchResult]:
|
||||
"""Dispatch a command to multiple hosts and return results."""
|
||||
results = []
|
||||
for host in hosts:
|
||||
ssh = SSHEnvironment(host=host, agent=agent, timeout=timeout)
|
||||
result = ssh.dispatch(command)
|
||||
results.append(result)
|
||||
return results
|
||||
|
||||
|
||||
def format_dispatch_report(results: List[DispatchResult]) -> str:
|
||||
"""Format a human-readable report of dispatch results."""
|
||||
lines = ["## Dispatch Report", ""]
|
||||
|
||||
succeeded = [r for r in results if r.success]
|
||||
failed = [r for r in results if not r.success]
|
||||
|
||||
lines.append(f"**Total:** {len(results)} hosts | "
|
||||
f"**OK:** {len(succeeded)} | **Failed:** {len(failed)}")
|
||||
lines.append("")
|
||||
|
||||
for r in results:
|
||||
status = "OK" if r.success else "FAIL"
|
||||
lines.append(f"### {r.host} [{status}]")
|
||||
lines.append(f"- Command: `{r.command}`")
|
||||
lines.append(f"- Duration: {r.duration_s:.1f}s")
|
||||
if r.exit_code is not None:
|
||||
lines.append(f"- Exit code: {r.exit_code}")
|
||||
if r.failure_reason:
|
||||
lines.append(f"- **Failure:** {r.failure_reason}")
|
||||
if r.stderr and not r.success:
|
||||
lines.append(f"- Stderr: `{r.stderr[:300]}`")
|
||||
lines.append("")
|
||||
|
||||
return "\n".join(lines)
|
||||
@@ -517,71 +517,3 @@ def resolve_provider_full(
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
# -- Runtime classification ---------------------------------------------------
|
||||
|
||||
# Providers that are definitively cloud-hosted (not local).
|
||||
# Used by _classify_runtime() to distinguish cloud vs unknown.
|
||||
_CLOUD_PREFIXES: frozenset[str] = frozenset(HERMES_OVERLAYS.keys()) | frozenset({
|
||||
# Common aliases that normalize to cloud providers
|
||||
"openai", "gemini", "google", "google-gemini", "google-ai-studio",
|
||||
"claude", "claude-code", "copilot", "github", "github-copilot",
|
||||
"glm", "z-ai", "z.ai", "zhipu", "zai",
|
||||
"kimi", "kimi-coding", "moonshot",
|
||||
"minimax", "minimax-china", "minimax_cn",
|
||||
"deep-seek",
|
||||
"dashscope", "aliyun", "qwen", "alibaba-cloud", "alibaba",
|
||||
"hf", "hugging-face", "huggingface-hub", "huggingface",
|
||||
"ai-gateway", "aigateway", "vercel-ai-gateway",
|
||||
"opencode-zen", "zen",
|
||||
"opencode-go-sub",
|
||||
"kilocode", "kilo-code", "kilo-gateway", "kilo",
|
||||
})
|
||||
|
||||
# Providers that are definitively local (self-hosted, no external API).
|
||||
_LOCAL_PROVIDERS: frozenset[str] = frozenset({
|
||||
"ollama", "local",
|
||||
"vllm", "llamacpp", "llama.cpp", "llama-cpp", "lmstudio", "lm-studio",
|
||||
})
|
||||
|
||||
|
||||
def _classify_runtime(provider: Optional[str], model: str) -> str:
|
||||
"""Classify a provider/model pair into a runtime category.
|
||||
|
||||
Returns one of:
|
||||
``"cloud"`` — the request targets a known remote/hosted provider.
|
||||
``"local"`` — the request targets a self-hosted/local inference server.
|
||||
``"unknown"`` — provider is unrecognised or not specified without enough
|
||||
context to determine the runtime type.
|
||||
|
||||
Edge-case rules (in order):
|
||||
1. If *provider* is set and is a known local provider → ``"local"``.
|
||||
2. If *provider* is set and is a known cloud provider → ``"cloud"``.
|
||||
3. If *provider* is set but **not** in either known set → ``"unknown"``.
|
||||
(Previously fell through to ``"local"`` — this was the bug.)
|
||||
4. If *provider* is empty/None, inspect the model string for a recognised
|
||||
cloud prefix (e.g. ``"openai/gpt-4o"`` → ``"cloud"``).
|
||||
5. Everything else → ``"unknown"``.
|
||||
"""
|
||||
p = (provider or "").strip().lower()
|
||||
|
||||
if p:
|
||||
# Rule 1: known local provider
|
||||
if p in _LOCAL_PROVIDERS:
|
||||
return "local"
|
||||
# Rule 2: known cloud provider
|
||||
if p in _CLOUD_PREFIXES:
|
||||
return "cloud"
|
||||
# Rule 3: provider is set but unrecognised — do NOT default to "local"
|
||||
return "unknown"
|
||||
|
||||
# Rule 4: no provider — try to infer from the model string
|
||||
m = (model or "").strip().lower()
|
||||
if "/" in m:
|
||||
model_prefix = m.split("/", 1)[0]
|
||||
if model_prefix in _CLOUD_PREFIXES:
|
||||
return "cloud"
|
||||
|
||||
# Rule 5: insufficient context
|
||||
return "unknown"
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
"""Tests for _classify_runtime() edge cases.
|
||||
|
||||
Covers the bug reported in #556: unknown provider with a model string
|
||||
incorrectly returned "local" instead of "unknown".
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from hermes_cli.providers import _classify_runtime
|
||||
|
||||
|
||||
class TestClassifyRuntimeLocalProviders:
|
||||
def test_ollama_no_model(self):
|
||||
assert _classify_runtime("ollama", "") == "local"
|
||||
|
||||
def test_ollama_with_model(self):
|
||||
assert _classify_runtime("ollama", "llama3:8b") == "local"
|
||||
|
||||
def test_local_provider_no_model(self):
|
||||
assert _classify_runtime("local", "") == "local"
|
||||
|
||||
def test_local_provider_with_model(self):
|
||||
assert _classify_runtime("local", "my-model") == "local"
|
||||
|
||||
def test_vllm_provider(self):
|
||||
assert _classify_runtime("vllm", "meta/llama-3") == "local"
|
||||
|
||||
def test_llamacpp_provider(self):
|
||||
assert _classify_runtime("llamacpp", "mistral") == "local"
|
||||
|
||||
|
||||
class TestClassifyRuntimeCloudProviders:
|
||||
def test_anthropic_provider(self):
|
||||
assert _classify_runtime("anthropic", "claude-opus-4-6") == "cloud"
|
||||
|
||||
def test_openrouter_provider(self):
|
||||
assert _classify_runtime("openrouter", "anthropic/claude-opus-4-6") == "cloud"
|
||||
|
||||
def test_nous_provider(self):
|
||||
assert _classify_runtime("nous", "hermes-3") == "cloud"
|
||||
|
||||
def test_gemini_provider(self):
|
||||
assert _classify_runtime("gemini", "gemini-pro") == "cloud"
|
||||
|
||||
def test_deepseek_provider(self):
|
||||
assert _classify_runtime("deepseek", "deepseek-chat") == "cloud"
|
||||
|
||||
|
||||
class TestClassifyRuntimeUnknownProviders:
|
||||
"""Regression tests for #556: unknown provider should return 'unknown', not 'local'."""
|
||||
|
||||
def test_unknown_provider_with_model(self):
|
||||
"""Core bug: 'custom' provider with model must not return 'local'."""
|
||||
assert _classify_runtime("custom", "my-model") == "unknown"
|
||||
|
||||
def test_unknown_provider_no_model(self):
|
||||
"""Unknown provider with no model should return 'unknown'."""
|
||||
assert _classify_runtime("custom", "") == "unknown"
|
||||
|
||||
def test_arbitrary_provider_with_model(self):
|
||||
"""Any unrecognised provider string with a model returns 'unknown'."""
|
||||
assert _classify_runtime("my-private-llm", "some-model") == "unknown"
|
||||
|
||||
def test_arbitrary_provider_no_model(self):
|
||||
assert _classify_runtime("my-private-llm", "") == "unknown"
|
||||
|
||||
def test_whitespace_only_provider_treated_as_empty(self):
|
||||
"""Provider with only whitespace is treated as absent."""
|
||||
# No model either → unknown
|
||||
assert _classify_runtime(" ", "") == "unknown"
|
||||
|
||||
|
||||
class TestClassifyRuntimeEmptyProvider:
|
||||
def test_empty_provider_cloud_prefixed_model(self):
|
||||
"""Empty provider with cloud-prefixed model returns 'cloud'."""
|
||||
assert _classify_runtime("", "openrouter/gpt-4o") == "cloud"
|
||||
|
||||
def test_none_provider_cloud_prefixed_model(self):
|
||||
assert _classify_runtime(None, "anthropic/claude-opus-4-6") == "cloud"
|
||||
|
||||
def test_empty_provider_no_model(self):
|
||||
assert _classify_runtime("", "") == "unknown"
|
||||
|
||||
def test_none_provider_no_model(self):
|
||||
assert _classify_runtime(None, "") == "unknown"
|
||||
|
||||
def test_empty_provider_non_cloud_prefixed_model(self):
|
||||
"""No provider, model without a recognized prefix → unknown."""
|
||||
assert _classify_runtime("", "my-model") == "unknown"
|
||||
|
||||
def test_empty_provider_model_with_unknown_prefix(self):
|
||||
"""Model prefix that isn't a known cloud provider → unknown."""
|
||||
assert _classify_runtime("", "myprivate/llm-7b") == "unknown"
|
||||
Reference in New Issue
Block a user