hermes_worker.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. """Hermes model loop with a closed, parent-owned tool dispatcher.
  2. Credentials and this process remain on the Runner. Tools operate through the
  3. parent's leased computer; no model-selected native Hermes handler is invoked.
  4. Only private stdin/stdout carry protocol frames. Ordinary logging uses stderr.
  5. """
  6. from __future__ import annotations
  7. import contextlib
  8. import copy
  9. import json
  10. import math
  11. import os
  12. from pathlib import Path
  13. import re
  14. import sys
  15. import threading
  16. import uuid
  17. WIRE = sys.stdout
  18. WRITE_LOCK = threading.Lock()
  19. BOOT = json.loads(sys.stdin.readline())
  20. NONCE = BOOT["nonce"]
  21. PROXY_ENV_KEYS = ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY",
  22. "http_proxy", "https_proxy", "all_proxy", "no_proxy")
  23. # Only context-management settings cross the Profile boundary. Plugins, tools,
  24. # arbitrary environment variables and unrelated credentials remain excluded.
  25. COMPRESSION_KEYS = {
  26. "enabled", "threshold", "threshold_tokens", "target_ratio",
  27. "protect_first_n", "protect_last_n", "min_tail_user_messages", "tail_mode",
  28. "max_attempts", "abort_on_summary_failure", "in_place", "checkpoint_required",
  29. "codex_gpt55_autoraise", "codex_gpt55_autoraise_notice",
  30. "codex_responses_native", "codex_responses_compact_threshold",
  31. "proactive_prune_tokens", "proactive_prune_min_result_chars",
  32. "proactive_prune_min_reclaim_tokens", "micro_compact",
  33. "micro_compact_every_n_turns", "micro_compact_defrag_threshold_tokens",
  34. "idle_compact_after_seconds", "context_timeout_seconds", "context_total_ceiling_seconds",
  35. }
  36. def context_configuration(config):
  37. raw = config.get("compression", {})
  38. if not isinstance(raw, dict):
  39. raw = {}
  40. compression = {key: value for key, value in raw.items()
  41. if key in COMPRESSION_KEYS
  42. and isinstance(value, (str, bool, int, float))
  43. and (not isinstance(value, float) or math.isfinite(value))}
  44. thresholds = raw.get("model_thresholds")
  45. if isinstance(thresholds, dict):
  46. compression["model_thresholds"] = {
  47. key: value for key, value in thresholds.items()
  48. if isinstance(key, str) and isinstance(value, (int, float))
  49. and not isinstance(value, bool) and math.isfinite(value) and 0 < value <= 1
  50. }
  51. model = config.get("model", {})
  52. limits = {key: model[key] for key in ("context_length", "max_tokens")
  53. if isinstance(model, dict) and isinstance(model.get(key), int)
  54. and not isinstance(model[key], bool) and model[key] > 0}
  55. return {"compression": compression, "model": limits}
  56. def emit(kind, **values):
  57. with WRITE_LOCK:
  58. WIRE.write(json.dumps({"nonce": NONCE, "type": kind, **values}, ensure_ascii=False) + "\n")
  59. WIRE.flush()
  60. class ConfigurationError(Exception):
  61. def __init__(self, code):
  62. self.code = code
  63. super().__init__(code)
  64. def load_profile():
  65. source = Path(BOOT["hermesSource"]).resolve()
  66. root = Path(BOOT["hermesHome"]).resolve()
  67. profile = BOOT["profile"]
  68. if not re.fullmatch(r"[a-z0-9][a-z0-9_-]{0,63}", profile):
  69. raise ConfigurationError("computer_profile_invalid")
  70. home = root if profile == "default" else root / "profiles" / profile
  71. if not (home / "config.yaml").is_file():
  72. raise ConfigurationError("computer_profile_missing")
  73. os.environ["HERMES_HOME"] = str(home)
  74. sys.path.insert(0, str(source))
  75. from dotenv import dotenv_values
  76. profile_env = dotenv_values(home / ".env")
  77. for key, value in profile_env.items():
  78. if value is not None:
  79. os.environ[key] = value
  80. # Capture only this Profile's values, before Hermes imports can load any
  81. # fallback environment. Preserve case and explicit empty values for its SDK.
  82. proxy_env = {key: profile_env[key] for key in PROXY_ENV_KEYS
  83. if isinstance(profile_env.get(key), str)}
  84. from hermes_constants import set_hermes_home_override
  85. set_hermes_home_override(str(home))
  86. from hermes_cli.config import load_config_readonly
  87. try:
  88. return load_config_readonly(), proxy_env
  89. except Exception:
  90. raise ConfigurationError("computer_profile_config_invalid") from None
  91. def resolve_workspace(config):
  92. terminal = config.get("terminal", {})
  93. if not isinstance(terminal, dict):
  94. raise ConfigurationError("computer_cwd_invalid")
  95. raw = terminal.get("cwd")
  96. if raw is None or raw == "":
  97. raw = "."
  98. if not isinstance(raw, str) or not raw.strip() or len(raw) > 4096 or re.search(r"[\x00-\x1f\x7f]", raw):
  99. raise ConfigurationError("computer_cwd_invalid")
  100. configured = raw.strip()
  101. # Placeholders refer to the assigned Linux workspace, never the Web/App
  102. # release directory inherited by this host-side configuration reader.
  103. base = BOOT.get("defaultCwd", "/home/cua/workspace")
  104. path = "." if configured in (".", "auto", "cwd") else os.path.expanduser(configured)
  105. cwd = os.path.normpath(path if os.path.isabs(path) else os.path.join(base, path))
  106. if not os.path.isabs(path) and os.path.commonpath([base, cwd]) != base:
  107. raise ConfigurationError("computer_cwd_invalid")
  108. return {"cwd": cwd, "configuredCwd": configured}
  109. def resolve_model():
  110. config, proxy_env = load_profile()
  111. workspace = resolve_workspace(config)
  112. model = config.get("model", {})
  113. if not isinstance(model, dict) or not model.get("default"):
  114. raise ConfigurationError("computer_model_missing")
  115. try:
  116. from hermes_cli.runtime_provider import resolve_runtime_provider
  117. runtime = resolve_runtime_provider(requested=model.get("provider"), explicit_api_key=model.get("api_key"), explicit_base_url=model.get("base_url"), target_model=model["default"])
  118. except Exception:
  119. raise ConfigurationError("computer_model_unavailable") from None
  120. if runtime.get("api_mode") not in ("chat_completions", "anthropic_messages", "codex_responses"):
  121. raise ConfigurationError("computer_model_unsupported")
  122. emit("resolved", model={key: runtime[key] for key in ("provider", "api_mode", "base_url", "api_key") if key in runtime} | {"model": model["default"]}, **workspace,
  123. contextConfig=context_configuration(config),
  124. **({"proxyEnv": proxy_env} if proxy_env else {}))
  125. def run_model():
  126. home = Path(BOOT["home"]).resolve()
  127. home.mkdir(parents=True, exist_ok=True, mode=0o700)
  128. os.chdir(home)
  129. os.environ["HOME"] = str(home)
  130. os.environ["HERMES_HOME"] = str(home)
  131. os.environ["HERMES_CWD"] = str(home)
  132. sys.path.insert(0, str(Path(BOOT["hermesSource"]).resolve()))
  133. context_config = context_configuration(BOOT.get("contextConfig", {}))
  134. # JSON is a YAML subset. An absent compression setting uses Hermes' own
  135. # defaults, while an explicit Profile false remains false.
  136. (home / "config.yaml").write_text(json.dumps({
  137. "plugins": {"enabled": []}, **context_config,
  138. "model": {**context_config["model"], "default": BOOT["model"]["model"],
  139. **{key: BOOT["model"][key] for key in ("provider", "base_url") if key in BOOT["model"]}},
  140. }), encoding="utf-8")
  141. (home / ".env").touch(mode=0o600)
  142. # Prevent the installed project's dotenv fallback from importing host secrets.
  143. import hermes_cli.env_loader as env_loader
  144. env_loader.load_hermes_dotenv = lambda *args, **kwargs: []
  145. import run_agent
  146. import model_tools
  147. supplied_tools = [{"type": "function", "function": {"name": item["name"], "description": item["description"], "parameters": item["inputSchema"]}} for item in BOOT["tools"]]
  148. # Hermes refreshes tools at compaction boundaries, including through
  149. # model_tools directly. Keep the parent-owned catalog on every rebuild;
  150. # neither drop the VM tools nor discover native handlers from the host.
  151. def closed_tool_definitions(*args, **kwargs):
  152. return copy.deepcopy(supplied_tools)
  153. run_agent.get_tool_definitions = closed_tool_definitions
  154. model_tools.get_tool_definitions = closed_tool_definitions
  155. pending = {}
  156. stopped = threading.Event()
  157. allowed = {item["name"] for item in BOOT["tools"]}
  158. class ClosedAgent(run_agent.AIAgent):
  159. def _compress_context(self, *args, **kwargs):
  160. result = super()._compress_context(*args, **kwargs)
  161. if not stopped.is_set() and isinstance(result, tuple) and result and isinstance(result[0], list):
  162. emit("checkpoint", messages=result[0])
  163. return result
  164. def _persist_session(self, messages, conversation_history=None):
  165. result = super()._persist_session(messages, conversation_history)
  166. if not stopped.is_set():
  167. emit("checkpoint", messages=messages)
  168. return result
  169. def _execute_tool_calls(self, assistant_message, messages, effective_task_id, api_call_count=0):
  170. # Intercept before Hermes' sequential, parallel and inline branches.
  171. # In particular message_agent/delegate_task must never reach native code.
  172. from agent.tool_dispatch_helpers import make_tool_result_message
  173. emit("checkpoint", messages=messages)
  174. for tool in assistant_message.tool_calls:
  175. name = tool.function.name
  176. try:
  177. arguments = json.loads(tool.function.arguments)
  178. if not isinstance(arguments, dict):
  179. raise ValueError("arguments must be an object")
  180. result = self._invoke_tool(name, arguments, effective_task_id, tool.id)
  181. except Exception:
  182. result = json.dumps({"error": "Worker tool failed or was cancelled"})
  183. if isinstance(result, dict) and result.get("_multimodal"):
  184. result = [{"type": "text", "text": result.get("text_summary", "")}, *result.get("content", [])]
  185. messages.append(make_tool_result_message(name, result, tool.id))
  186. emit("checkpoint", messages=messages)
  187. def _invoke_tool(self, function_name, function_args, effective_task_id, tool_call_id=None, **kwargs):
  188. if stopped.is_set() or function_name not in allowed:
  189. return json.dumps({"error": "This tool is not authorized for this worker"})
  190. call_id = str(uuid.uuid4())
  191. event = threading.Event()
  192. slot = {"event": event}
  193. pending[call_id] = slot
  194. emit("tool", id=call_id, callId=tool_call_id, name=function_name, arguments=function_args)
  195. try:
  196. if not event.wait(90) or stopped.is_set():
  197. stopped.set()
  198. emit("failed", message="Worker 工具调用已超时或停止")
  199. self.interrupt()
  200. raise RuntimeError("worker tool cancelled or timed out")
  201. result = slot.get("result", {"error": "No tool response"})
  202. return result if isinstance(result, str) or (isinstance(result, dict) and result.get("_multimodal")) else json.dumps(result, ensure_ascii=False)
  203. finally:
  204. pending.pop(call_id, None)
  205. from tools.registry import registry
  206. def no_native_dispatch(*args, **kwargs):
  207. raise RuntimeError("closed worker requires parent dispatch")
  208. for item in BOOT["tools"]:
  209. registry.register(name=item["name"], toolset="yaoyao_computer", schema={"name": item["name"], "description": item["description"], "parameters": item["inputSchema"]}, handler=no_native_dispatch)
  210. model = BOOT["model"]
  211. if model.get("api_mode") not in ("chat_completions", "anthropic_messages", "codex_responses"):
  212. raise ValueError("unsupported worker provider")
  213. agent = ClosedAgent(**model, enabled_toolsets=[], disabled_toolsets=None, quiet_mode=True,
  214. save_trajectories=False, skip_context_files=True, skip_memory=True,
  215. skip_background_review=True, checkpoints_enabled=False, session_id=BOOT["sessionId"],
  216. max_iterations=32, run_budget_seconds=900,
  217. stream_delta_callback=lambda text, **kwargs: emit("delta", text=str(text)) if text is not None else None,
  218. reasoning_callback=lambda text, **kwargs: emit("reasoning", text=str(text)) if text is not None else None)
  219. agent.tools = closed_tool_definitions()
  220. agent.valid_tool_names = set(allowed)
  221. def receive():
  222. try:
  223. for line in sys.stdin:
  224. frame = json.loads(line)
  225. if frame.get("nonce") != NONCE:
  226. continue
  227. if frame.get("type") == "result" and frame.get("id") in pending:
  228. slot = pending[frame["id"]]
  229. slot["result"] = frame.get("result")
  230. slot["event"].set()
  231. elif frame.get("type") == "interrupt":
  232. break
  233. finally:
  234. stopped.set()
  235. agent.interrupt()
  236. for slot in list(pending.values()):
  237. slot["event"].set()
  238. threading.Thread(target=receive, daemon=True).start()
  239. emit("ready", tools=sorted(allowed))
  240. environment = "Computer tools operate on your assigned isolated Linux computer, with working directory " + BOOT["cwd"] + ". Its network access follows the configured computer policy. "
  241. if BOOT.get("host"):
  242. environment += "The user also enabled host tools. host_* tools operate on the Runner host (" + BOOT["host"]["platform"] + "), with working directory " + BOOT["host"]["cwd"] + ". Use the appropriate OS syntax for each tool. computer_* tools operate in Linux; computer_copy_file explicitly copies between host and VM. These environments have separate files, processes and installed software. "
  243. else:
  244. environment += "Host files and commands are not available through computer tools. Never attempt to execute a host command through them. "
  245. result = agent.run_conversation(BOOT["prompt"], conversation_history=BOOT.get("history", []), task_id=BOOT["taskId"],
  246. system_message="You are a controlled Hermes worker. " + environment + "Other supplied application and team tools follow their descriptions. Use only supplied tools and follow the user's task and permission scope.")
  247. # These are engine result flags, never inferred from model-generated text.
  248. failure_code = ("context_compaction_disabled" if result.get("compaction_disabled") is True
  249. else "context_compaction_failed" if result.get("compression_exhausted") is True
  250. else None)
  251. interrupted = stopped.is_set() or result.get("interrupted") is True
  252. completed = result.get("completed", True) is not False and not result.get("failed") and failure_code is None and not interrupted
  253. emit("complete", text=str(result.get("final_response") or ""), messages=result.get("messages"),
  254. interrupted=interrupted, completed=completed,
  255. **({"failureCode": failure_code} if failure_code and not interrupted and not pending else {}))
  256. try:
  257. with contextlib.redirect_stdout(sys.stderr):
  258. if BOOT.get("mode") == "resolve-workspace":
  259. emit("resolved", **resolve_workspace(load_profile()[0]))
  260. elif BOOT.get("mode") == "resolve":
  261. resolve_model()
  262. else:
  263. run_model()
  264. except BaseException as error:
  265. if BOOT.get("diagnostic"):
  266. import traceback
  267. traceback.print_exc(file=sys.stderr)
  268. # Never serialize SDK exception strings, request headers, or credentials.
  269. emit("failed", code=error.code if isinstance(error, ConfigurationError) else "computer_worker_failed")
  270. sys.exit(1)