Compare commits
2 Commits
fix/468-cr
...
burn/320-1
| Author | SHA1 | Date | |
|---|---|---|---|
| f1626a932c | |||
| d68ab4cff4 |
192
cli.py
192
cli.py
@@ -3134,6 +3134,196 @@ class HermesCLI:
|
||||
print(f" Home: {display}")
|
||||
print()
|
||||
|
||||
def _handle_debug_command(self, command: str):
|
||||
"""Generate a debug report with system info and logs, upload to paste service."""
|
||||
import platform
|
||||
import sys
|
||||
import time as _time
|
||||
|
||||
# Parse optional lines argument
|
||||
parts = command.split(maxsplit=1)
|
||||
log_lines = 50
|
||||
if len(parts) > 1:
|
||||
try:
|
||||
log_lines = min(int(parts[1]), 500)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
_cprint(" Collecting debug info...")
|
||||
|
||||
# Collect system info
|
||||
lines = []
|
||||
lines.append("=== HERMES DEBUG REPORT ===")
|
||||
lines.append(f"Generated: {_time.strftime('%Y-%m-%d %H:%M:%S %z')}")
|
||||
lines.append("")
|
||||
|
||||
lines.append("--- System ---")
|
||||
lines.append(f"Python: {sys.version}")
|
||||
lines.append(f"Platform: {platform.platform()}")
|
||||
lines.append(f"Architecture: {platform.machine()}")
|
||||
lines.append(f"Hostname: {platform.node()}")
|
||||
lines.append("")
|
||||
|
||||
# Hermes info
|
||||
lines.append("--- Hermes ---")
|
||||
try:
|
||||
from hermes_constants import get_hermes_home, display_hermes_home
|
||||
lines.append(f"Home: {display_hermes_home()}")
|
||||
except Exception:
|
||||
lines.append("Home: unknown")
|
||||
|
||||
try:
|
||||
from hermes_constants import __version__
|
||||
lines.append(f"Version: {__version__}")
|
||||
except Exception:
|
||||
lines.append("Version: unknown")
|
||||
|
||||
lines.append(f"Profile: {getattr(self, '_profile_name', 'default')}")
|
||||
lines.append(f"Session: {self.session_id}")
|
||||
lines.append(f"Model: {self.model}")
|
||||
lines.append(f"Provider: {getattr(self, '_provider_name', 'unknown')}")
|
||||
|
||||
try:
|
||||
lines.append(f"Working dir: {os.getcwd()}")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Config (redacted)
|
||||
lines.append("")
|
||||
lines.append("--- Config (redacted) ---")
|
||||
try:
|
||||
from hermes_constants import get_hermes_home
|
||||
config_path = get_hermes_home() / "config.yaml"
|
||||
if config_path.exists():
|
||||
import yaml
|
||||
with open(config_path) as f:
|
||||
cfg = yaml.safe_load(f) or {}
|
||||
# Redact secrets
|
||||
for key in ("api_key", "token", "secret", "password"):
|
||||
if key in cfg:
|
||||
cfg[key] = "***REDACTED***"
|
||||
lines.append(yaml.dump(cfg, default_flow_style=False)[:2000])
|
||||
else:
|
||||
lines.append("(no config file found)")
|
||||
except Exception as e:
|
||||
lines.append(f"(error reading config: {e})")
|
||||
|
||||
# Recent logs
|
||||
lines.append("")
|
||||
lines.append(f"--- Recent Logs (last {log_lines} lines) ---")
|
||||
try:
|
||||
from hermes_constants import get_hermes_home
|
||||
log_dir = get_hermes_home() / "logs"
|
||||
if log_dir.exists():
|
||||
for log_file in sorted(log_dir.glob("*.log")):
|
||||
try:
|
||||
content = log_file.read_text(encoding="utf-8", errors="replace")
|
||||
tail = content.strip().split("\n")[-log_lines:]
|
||||
if tail:
|
||||
lines.append(f"\n[{log_file.name}]")
|
||||
lines.extend(tail)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
lines.append("(no logs directory)")
|
||||
except Exception:
|
||||
lines.append("(error reading logs)")
|
||||
|
||||
# Tool info
|
||||
lines.append("")
|
||||
lines.append("--- Enabled Toolsets ---")
|
||||
try:
|
||||
lines.append(", ".join(self.enabled_toolsets) if self.enabled_toolsets else "(none)")
|
||||
except Exception:
|
||||
lines.append("(unknown)")
|
||||
|
||||
report = "\n".join(lines)
|
||||
report_size = len(report)
|
||||
|
||||
# Try to upload to paste services
|
||||
paste_url = None
|
||||
services = [
|
||||
("dpaste", _upload_dpaste),
|
||||
("0x0.st", _upload_0x0st),
|
||||
]
|
||||
|
||||
for name, uploader in services:
|
||||
try:
|
||||
url = uploader(report)
|
||||
if url:
|
||||
paste_url = url
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
print()
|
||||
if paste_url:
|
||||
_cprint(f" Debug report uploaded: {paste_url}")
|
||||
_cprint(f" Size: {report_size} bytes, {len(lines)} lines")
|
||||
else:
|
||||
# Fallback: save locally
|
||||
try:
|
||||
from hermes_constants import get_hermes_home
|
||||
debug_path = get_hermes_home() / "debug-report.txt"
|
||||
debug_path.write_text(report, encoding="utf-8")
|
||||
_cprint(f" Paste services unavailable. Report saved to: {debug_path}")
|
||||
_cprint(f" Size: {report_size} bytes, {len(lines)} lines")
|
||||
except Exception as e:
|
||||
_cprint(f" Failed to save report: {e}")
|
||||
_cprint(f" Report ({report_size} bytes):")
|
||||
print(report)
|
||||
print()
|
||||
|
||||
|
||||
def _upload_dpaste(content: str) -> str | None:
|
||||
"""Upload content to dpaste.org. Returns URL or None."""
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
data = urllib.parse.urlencode({
|
||||
"content": content,
|
||||
"syntax": "text",
|
||||
"expiry_days": 7,
|
||||
}).encode()
|
||||
req = urllib.request.Request(
|
||||
"https://dpaste.org/api/",
|
||||
data=data,
|
||||
headers={"User-Agent": "hermes-agent/debug"},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
url = resp.read().decode().strip()
|
||||
if url.startswith("http"):
|
||||
return url
|
||||
return None
|
||||
|
||||
|
||||
def _upload_0x0st(content: str) -> str | None:
|
||||
"""Upload content to 0x0.st. Returns URL or None."""
|
||||
import urllib.request
|
||||
import io
|
||||
# 0x0.st expects multipart form with a file field
|
||||
boundary = "----HermesDebugBoundary"
|
||||
body = (
|
||||
f"--{boundary}\r\n"
|
||||
f'Content-Disposition: form-data; name="file"; filename="debug.txt"\r\n'
|
||||
f"Content-Type: text/plain\r\n\r\n"
|
||||
f"{content}\r\n"
|
||||
f"--{boundary}--\r\n"
|
||||
).encode()
|
||||
req = urllib.request.Request(
|
||||
"https://0x0.st",
|
||||
data=body,
|
||||
headers={
|
||||
"Content-Type": f"multipart/form-data; boundary={boundary}",
|
||||
"User-Agent": "hermes-agent/debug",
|
||||
},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
url = resp.read().decode().strip()
|
||||
if url.startswith("http"):
|
||||
return url
|
||||
return None
|
||||
|
||||
|
||||
def show_config(self):
|
||||
"""Display current configuration with kawaii ASCII art."""
|
||||
# Get terminal config from environment (which was set from cli-config.yaml)
|
||||
@@ -4321,6 +4511,8 @@ class HermesCLI:
|
||||
self.show_help()
|
||||
elif canonical == "profile":
|
||||
self._handle_profile_command()
|
||||
elif canonical == "debug":
|
||||
self._handle_debug_command(cmd_original)
|
||||
elif canonical == "tools":
|
||||
self._handle_tools_command(cmd_original)
|
||||
elif canonical == "toolsets":
|
||||
|
||||
@@ -13,7 +13,6 @@ import concurrent.futures
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
@@ -157,27 +156,6 @@ _KNOWN_DELIVERY_PLATFORMS = frozenset({
|
||||
|
||||
from cron.jobs import get_due_jobs, mark_job_run, save_job_output, advance_next_run
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Model context guard
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
CRON_MIN_CONTEXT_TOKENS = 4096
|
||||
|
||||
|
||||
class ModelContextError(ValueError):
|
||||
"""Raised when a job's model has insufficient context for cron execution."""
|
||||
pass
|
||||
|
||||
|
||||
def _check_model_context_compat(model: str, context_length: int) -> None:
|
||||
"""Raise ModelContextError if the model context is below the cron minimum."""
|
||||
if context_length < CRON_MIN_CONTEXT_TOKENS:
|
||||
raise ModelContextError(
|
||||
f"Model '{model}' context ({context_length} tokens) is below the "
|
||||
f"minimum {CRON_MIN_CONTEXT_TOKENS} tokens required for cron jobs."
|
||||
)
|
||||
|
||||
|
||||
# Sentinel: when a cron agent has nothing new to report, it can start its
|
||||
# response with this marker to suppress delivery. Output is still saved
|
||||
# locally for audit.
|
||||
@@ -566,55 +544,6 @@ def _run_job_script(script_path: str) -> tuple[bool, str]:
|
||||
return False, f"Script execution failed: {exc}"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Cloud context warning — detect local service refs in cloud cron prompts
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_LOCAL_SERVICE_PATTERNS = [
|
||||
r'localhost:\d{2,5}',
|
||||
r'127\.0\.0\.\d{1,3}:\d{2,5}',
|
||||
r'0\.0\.0\.0:\d{2,5}',
|
||||
r'\bollama\b',
|
||||
r'curl\s+.*localhost',
|
||||
r'wget\s+.*localhost',
|
||||
r'http://localhost',
|
||||
r'https?://127\.',
|
||||
r'https?://0\.0\.0\.0',
|
||||
r'check.*ollama',
|
||||
r'connect.*local',
|
||||
r'hermes.*gateway.*local',
|
||||
]
|
||||
|
||||
_LOCAL_SERVICE_RE = [re.compile(p, re.IGNORECASE) for p in _LOCAL_SERVICE_PATTERNS]
|
||||
|
||||
|
||||
def _detect_local_service_refs(prompt: str) -> list[str]:
|
||||
"""Scan a prompt for references to local services (Ollama, localhost, etc.).
|
||||
|
||||
Returns list of matched patterns for logging.
|
||||
"""
|
||||
matches = []
|
||||
for pattern_re in _LOCAL_SERVICE_RE:
|
||||
if pattern_re.search(prompt):
|
||||
matches.append(pattern_re.pattern)
|
||||
return matches
|
||||
|
||||
|
||||
def _inject_cloud_context(prompt: str, local_refs: list[str]) -> str:
|
||||
"""Prepend a warning when cron runs on cloud but prompt refs local services.
|
||||
|
||||
The agent reports the limitation instead of wasting iterations on doomed connections.
|
||||
"""
|
||||
warning = (
|
||||
"[SYSTEM NOTE: You are running on a cloud endpoint, but your prompt references "
|
||||
"local services (localhost/Ollama). You cannot reach localhost from a cloud "
|
||||
"endpoint. Report this limitation to the user and suggest running the job on "
|
||||
"a local endpoint instead. Do NOT attempt to connect to localhost — it will "
|
||||
"timeout and waste your iteration budget.]\n\n"
|
||||
)
|
||||
return warning + prompt
|
||||
|
||||
|
||||
def _build_job_prompt(job: dict) -> str:
|
||||
"""Build the effective prompt for a cron job, optionally loading one or more skills first."""
|
||||
prompt = job.get("prompt", "")
|
||||
@@ -833,16 +762,6 @@ def run_job(job: dict) -> tuple[bool, str, str, Optional[str]]:
|
||||
message = format_runtime_provider_error(exc)
|
||||
raise RuntimeError(message) from exc
|
||||
|
||||
# Cloud context warning: if running on cloud but prompt refs local services,
|
||||
# inject a warning so the agent reports the limitation instead of wasting
|
||||
# iterations on doomed connections. (Fixes #378, #456)
|
||||
base_url = runtime.get("base_url") or ""
|
||||
is_cloud = not any(h in base_url for h in ("localhost", "127.0.0.1", "0.0.0.0", "::1"))
|
||||
local_refs = _detect_local_service_refs(prompt)
|
||||
if is_cloud and local_refs:
|
||||
logger.info("Job '%s': cloud endpoint + local service refs detected, injecting warning", job_name)
|
||||
prompt = _inject_cloud_context(prompt, local_refs)
|
||||
|
||||
from agent.smart_model_routing import resolve_turn_route
|
||||
turn_route = resolve_turn_route(
|
||||
prompt,
|
||||
|
||||
@@ -1,120 +0,0 @@
|
||||
"""Tests for cron cloud context warning injection (fix #378, #456).
|
||||
|
||||
When a cron job runs on a cloud endpoint but its prompt references local
|
||||
services (Ollama, localhost, etc.), inject a warning so the agent reports
|
||||
the limitation instead of wasting iterations on doomed connections.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from cron.scheduler import (
|
||||
_detect_local_service_refs,
|
||||
_inject_cloud_context,
|
||||
_LOCAL_SERVICE_PATTERNS,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Pattern detection
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestDetectLocalServiceRefs:
|
||||
def test_localhost_with_port(self):
|
||||
refs = _detect_local_service_refs("Check http://localhost:8080/status")
|
||||
assert len(refs) > 0
|
||||
assert any("localhost" in r for r in refs)
|
||||
|
||||
def test_127_address(self):
|
||||
refs = _detect_local_service_refs("Connect to 127.0.0.1:11434")
|
||||
assert len(refs) > 0
|
||||
|
||||
def test_ollama_reference(self):
|
||||
refs = _detect_local_service_refs("Run this on Ollama with gemma3")
|
||||
assert len(refs) > 0
|
||||
assert any("ollama" in r.lower() for r in refs)
|
||||
|
||||
def test_curl_localhost(self):
|
||||
refs = _detect_local_service_refs("curl localhost:3000/api/data")
|
||||
assert len(refs) > 0
|
||||
|
||||
def test_wget_localhost(self):
|
||||
refs = _detect_local_service_refs("wget http://localhost/file.txt")
|
||||
assert len(refs) > 0
|
||||
|
||||
def test_http_localhost(self):
|
||||
refs = _detect_local_service_refs("http://localhost:8642/health")
|
||||
assert len(refs) > 0
|
||||
|
||||
def test_https_127(self):
|
||||
refs = _detect_local_service_refs("https://127.0.0.1:443/secure")
|
||||
assert len(refs) > 0
|
||||
|
||||
def test_0000_address(self):
|
||||
refs = _detect_local_service_refs("Bind to 0.0.0.0:9090")
|
||||
assert len(refs) > 0
|
||||
|
||||
def test_no_match_for_remote(self):
|
||||
refs = _detect_local_service_refs("Check https://api.openai.com/v1/models")
|
||||
assert len(refs) == 0
|
||||
|
||||
def test_no_match_for_gitea(self):
|
||||
refs = _detect_local_service_refs("Query forge.alexanderwhitestone.com for issues")
|
||||
assert len(refs) == 0
|
||||
|
||||
def test_no_match_empty(self):
|
||||
refs = _detect_local_service_refs("")
|
||||
assert len(refs) == 0
|
||||
|
||||
def test_check_ollama_phrase(self):
|
||||
refs = _detect_local_service_refs("First check Ollama is running")
|
||||
assert len(refs) > 0
|
||||
|
||||
def test_connect_local_phrase(self):
|
||||
refs = _detect_local_service_refs("Connect to local Ollama server")
|
||||
assert len(refs) > 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Warning injection
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestInjectCloudContext:
|
||||
def test_prepends_warning(self):
|
||||
original = "Run a health check on localhost:8080"
|
||||
refs = _detect_local_service_refs(original)
|
||||
result = _inject_cloud_context(original, refs)
|
||||
assert "SYSTEM NOTE" in result
|
||||
assert "cloud endpoint" in result
|
||||
assert original in result
|
||||
|
||||
def test_warning_is_first(self):
|
||||
original = "Check localhost:11434"
|
||||
refs = _detect_local_service_refs(original)
|
||||
result = _inject_cloud_context(original, refs)
|
||||
assert result.startswith("[SYSTEM NOTE")
|
||||
|
||||
def test_preserves_original_prompt(self):
|
||||
original = "Do something with Ollama and then report results"
|
||||
refs = _detect_local_service_refs(original)
|
||||
result = _inject_cloud_context(original, refs)
|
||||
assert "Do something with Ollama" in result
|
||||
|
||||
def test_mentions_cannot_reach(self):
|
||||
original = "curl localhost:8080"
|
||||
refs = _detect_local_service_refs(original)
|
||||
result = _inject_cloud_context(original, refs)
|
||||
assert "cannot reach" in result.lower() or "cannot" in result.lower()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Pattern coverage
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestPatternCoverage:
|
||||
def test_at_least_10_patterns(self):
|
||||
assert len(_LOCAL_SERVICE_PATTERNS) >= 10
|
||||
|
||||
def test_patterns_are_strings(self):
|
||||
for p in _LOCAL_SERVICE_PATTERNS:
|
||||
assert isinstance(p, str)
|
||||
assert len(p) > 0
|
||||
Reference in New Issue
Block a user