Files
PaperClip/services/hermes/patch_hermes_queryfile.py
Jannik 8a492afa00 fix(hermes): patch --query-file support and surface provider errors
- Add idempotent Hermes CLI patch for --query-file (E2BIG fix) and wire it into setup_hermes.sh
- Mark runs failed when Hermes exits 0 after a terminal provider error (e.g. HTTP 429)
- Prefer MiniMax as default OPENAI_* provider in service.pre_start
- Replace dead OpenRouter model IDs with models that currently exist
- Ignore Playwright MCP artifacts
2026-11-15 20:23:17 +01:00

129 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""Add --query-file support to the installed hermes_cli.
PaperClip's hermes adapter passes the (potentially large) prompt via
`hermes chat --query-file <path>`. Upstream hermes-agent 0.19.0 (latest on
PyPI at the time of writing) only supports `-q/--query`, and passing a prompt
larger than ~128 KB as a single argv string fails with E2BIG (MAX_ARG_STRLEN).
This patch teaches the CLI to read the query from a file, which removes the
argv size limit and keeps the PaperClip adapter patch working.
Idempotent: safe to run multiple times, and safe if hermes is not installed.
"""
from __future__ import annotations
import sys
from pathlib import Path
MARKER = "# [paperclip-patch] --query-file support"
def find_site_packages() -> Path | None:
"""Locate the site-packages dir that contains hermes_cli."""
candidates: list[Path] = []
# 1) Ask the venv interpreter that owns the hermes entrypoint.
try:
import subprocess
out = subprocess.run(
[
"/opt/hermes-venv/bin/python3",
"-c",
"import hermes_cli, os; print(os.path.dirname(os.path.dirname(hermes_cli.__file__)))",
],
capture_output=True,
text=True,
timeout=30,
)
if out.returncode == 0 and out.stdout.strip():
candidates.append(Path(out.stdout.strip()))
except Exception:
pass
# 2) Fall back to the current interpreter's site-packages.
try:
import hermes_cli # type: ignore
candidates.append(Path(hermes_cli.__file__).resolve().parent.parent)
except Exception:
pass
for candidate in candidates:
if (candidate / "hermes_cli" / "_parser.py").is_file():
return candidate
return None
def patch_parser(site_packages: Path) -> str:
parser_path = site_packages / "hermes_cli" / "_parser.py"
src = parser_path.read_text(encoding="utf-8")
if "--query-file" in src:
return "parser: already patched"
needle = ''' chat_parser.add_argument(
"-q", "--query", help="Single query (non-interactive mode)"
)'''
if needle not in src:
return "parser: ANCHOR NOT FOUND (skipped)"
addition = needle + '''
chat_parser.add_argument(
"--query-file",
dest="query_file",
default=None,
help="Read the single query from a file (non-interactive mode).",
)'''
parser_path.write_text(src.replace(needle, addition, 1), encoding="utf-8")
return "parser: patched"
def patch_main(site_packages: Path) -> str:
main_path = site_packages / "hermes_cli" / "main.py"
src = main_path.read_text(encoding="utf-8")
if MARKER in src:
return "main: already patched"
needle = "def cmd_chat(args):\n"
count = src.count(needle)
if count != 1:
return f"main: ANCHOR COUNT={count} (expected 1, skipped)"
injection = (
"def cmd_chat(args):\n"
f" {MARKER}\n"
' _qf = getattr(args, "query_file", None)\n'
' if _qf and not getattr(args, "query", None):\n'
" try:\n"
' with open(_qf, "r", encoding="utf-8", errors="replace") as _fh:\n'
" args.query = _fh.read()\n"
" except OSError as _exc:\n"
' print(f"Error: could not read --query-file {_qf}: {_exc}")\n'
" sys.exit(1)\n"
)
main_path.write_text(src.replace(needle, injection, 1), encoding="utf-8")
return "main: patched"
def main() -> int:
site_packages = find_site_packages()
if site_packages is None:
print("hermes_cli not found (skipped)")
return 0
parser_status = patch_parser(site_packages)
main_status = patch_main(site_packages)
print(f"site-packages: {site_packages}")
print(parser_status)
print(main_status)
if "ANCHOR" in parser_status or "ANCHOR" in main_status:
return 2
return 0
if __name__ == "__main__":
sys.exit(main())