Comprehensive cleanup across 80 files based on automated (ruff, pyflakes, vulture)
and manual analysis of the entire codebase.
Changes by category:
Unused imports removed (~95 across 55 files):
- Removed genuinely unused imports from all major subsystems
- agent/, hermes_cli/, tools/, gateway/, plugins/, cron/
- Includes imports in try/except blocks that were truly unused
(vs availability checks which were left alone)
Unused variables removed (~25):
- Removed dead variables: connected, inner, channels, last_exc,
source, new_server_names, verify, pconfig, default_terminal,
result, pending_handled, temperature, loop
- Dropped unused argparse subparser assignments in hermes_cli/main.py
(12 instances of add_parser() where result was never used)
Dead code removed:
- run_agent.py: Removed dead ternary (None if False else None) and
surrounding unreachable branch in identity fallback
- run_agent.py: Removed write-only attribute _last_reported_tool
- hermes_cli/providers.py: Removed dead @property decorator on
module-level function (decorator has no effect outside a class)
- gateway/run.py: Removed unused MCP config load before reconnect
- gateway/platforms/slack.py: Removed dead SessionSource construction
Undefined name bugs fixed (would cause NameError at runtime):
- batch_runner.py: Added missing logger = logging.getLogger(__name__)
- tools/environments/daytona.py: Added missing Dict and Path imports
Unnecessary global statements removed (14):
- tools/terminal_tool.py: 5 functions declared global for dicts
they only mutated via .pop()/[key]=value (no rebinding)
- tools/browser_tool.py: cleanup thread loop only reads flag
- tools/rl_training_tool.py: 4 functions only do dict mutations
- tools/mcp_oauth.py: only reads the global
- hermes_time.py: only reads cached values
Inefficient patterns fixed:
- startswith/endswith tuple form: 15 instances of
x.startswith('a') or x.startswith('b') consolidated to
x.startswith(('a', 'b'))
- len(x)==0 / len(x)>0: 13 instances replaced with pythonic
truthiness checks (not x / bool(x))
- in dict.keys(): 5 instances simplified to in dict
- Redefined unused name: removed duplicate _strip_mdv2 import in
send_message_tool.py
Other fixes:
- hermes_cli/doctor.py: Replaced undefined logger.debug() with pass
- hermes_cli/config.py: Consolidated chained .endswith() calls
Test results: 3934 passed, 17 failed (all pre-existing on main),
19 skipped. Zero regressions.
86 lines
2.4 KiB
Python
86 lines
2.4 KiB
Python
"""CLI entry point for the hermes-agent ACP adapter.
|
|
|
|
Loads environment variables from ``~/.hermes/.env``, configures logging
|
|
to write to stderr (so stdout is reserved for ACP JSON-RPC transport),
|
|
and starts the ACP agent server.
|
|
|
|
Usage::
|
|
|
|
python -m acp_adapter.entry
|
|
# or
|
|
hermes acp
|
|
# or
|
|
hermes-acp
|
|
"""
|
|
|
|
import asyncio
|
|
import logging
|
|
import sys
|
|
from pathlib import Path
|
|
from hermes_constants import get_hermes_home
|
|
|
|
|
|
def _setup_logging() -> None:
|
|
"""Route all logging to stderr so stdout stays clean for ACP stdio."""
|
|
handler = logging.StreamHandler(sys.stderr)
|
|
handler.setFormatter(
|
|
logging.Formatter(
|
|
"%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
)
|
|
)
|
|
root = logging.getLogger()
|
|
root.handlers.clear()
|
|
root.addHandler(handler)
|
|
root.setLevel(logging.INFO)
|
|
|
|
# Quiet down noisy libraries
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
logging.getLogger("httpcore").setLevel(logging.WARNING)
|
|
logging.getLogger("openai").setLevel(logging.WARNING)
|
|
|
|
|
|
def _load_env() -> None:
|
|
"""Load .env from HERMES_HOME (default ``~/.hermes``)."""
|
|
from hermes_cli.env_loader import load_hermes_dotenv
|
|
|
|
hermes_home = get_hermes_home()
|
|
loaded = load_hermes_dotenv(hermes_home=hermes_home)
|
|
if loaded:
|
|
for env_file in loaded:
|
|
logging.getLogger(__name__).info("Loaded env from %s", env_file)
|
|
else:
|
|
logging.getLogger(__name__).info(
|
|
"No .env found at %s, using system env", hermes_home / ".env"
|
|
)
|
|
|
|
|
|
def main() -> None:
|
|
"""Entry point: load env, configure logging, run the ACP agent."""
|
|
_setup_logging()
|
|
_load_env()
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logger.info("Starting hermes-agent ACP adapter")
|
|
|
|
# Ensure the project root is on sys.path so ``from run_agent import AIAgent`` works
|
|
project_root = str(Path(__file__).resolve().parent.parent)
|
|
if project_root not in sys.path:
|
|
sys.path.insert(0, project_root)
|
|
|
|
import acp
|
|
from .server import HermesACPAgent
|
|
|
|
agent = HermesACPAgent()
|
|
try:
|
|
asyncio.run(acp.run_agent(agent, use_unstable_protocol=True))
|
|
except KeyboardInterrupt:
|
|
logger.info("Shutting down (KeyboardInterrupt)")
|
|
except Exception:
|
|
logger.exception("ACP agent crashed")
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|