Files
hermes-agent/hermes_cli/cli_process_dock.py
alt-glitch 40492aca4e refactor(environments-terminal): delete unreached vercel helpers, CredentialRef, failed_start and exit-137 hint
Removed. A search for callers by name, attribute and string (Python, TypeScript,
docs, the plugin API list and the frozen old-updater list) finds no supported
path that reaches any of them:
- tools/environments/vercel_sandbox.py: _exception_chain, _extract_status_code,
  _RETRY_BACKOFF_STEP, _MIN_SANDBOX_TIMEOUT, _MIN_RUNNING_WAIT,
  _RUNNING_WAIT_POLL_INTERVAL, _STOP_TIMEOUT, _STOP_POLL_INTERVAL,
  _SNAPSHOT_STORE_NAME
- tools/environments/sandbox_scope.py: CredentialRef (docstring in
  agent/terminal_env_provider.py and the terminal-environment-plugin guide no
  longer name it)
- tools/environments/streams.py: supports_streams() (the provider attribute of
  the same name stays)
- completion_reason "failed_start": its producer left in cd818a4d054; drop the
  lookups in process_registry_notifications._REASON_STATUS (and its
  "Failed to Start" branch), cli_process_dock._STATUS_BY_REASON,
  ui-tui processRoster REASON_STATUS, and the field comments
- tools/terminal_hints.py: _EXIT_CODE_HINTS[137] (exit 137 always carries a
  SIGKILL exit note, which gates annotate_failure off)
- ProcessRegistry._config_value: unread `fallback` parameter
2026-11-16 15:23:37 +05:30

113 lines
4.4 KiB
Python

"""Background-process rows for the classic CLI live-work dock.
Sibling of ``cli_subagent_monitor``: the dock paints one "Processes" block under the
subagent rows from the same snapshot the ``process_manage`` tool reads, so a
``terminal(background=true)`` spawn is visible the moment it starts instead of only
when its completion notification lands.
"""
from __future__ import annotations
import time
from agent.i18n import t
# A finished process stays on the dock long enough to read its exit line, then
# leaves; the completion notification in the transcript is the durable record.
RETAIN_SECONDS = 60
# Reader-thread exits report "exited"; `kill_process` / `kill_all` stamp "killed".
_STATUS_BY_REASON = {"killed": "killed", "lost": "lost"}
def _last_output_line(buffer: str) -> str:
for line in reversed((buffer or "").splitlines()):
text = " ".join(line.split())
if text:
return text
return ""
def process_status(entry: dict) -> str:
if entry.get("status") != "exited":
return "running"
reason = _STATUS_BY_REASON.get(entry.get("completion_reason") or "")
if reason:
return reason
return "done" if not entry.get("exit_code") else "failed"
def process_rows(now: float | None = None) -> list:
"""Running processes plus those that exited within ``RETAIN_SECONDS``, newest-running first.
The classic CLI is one conversation per OS process, so every registry entry (its own
spawns and any a child handed back) belongs to this dock; gateway-hosted surfaces scope
by session key in ``tui_gateway`` instead.
"""
from hermes_cli.gateway_tools import process_registry as _registry
process_registry = _registry() # the gateway session's processes (rewired CLI)
now = time.time() if now is None else now
rows = []
for entry in process_registry.list_sessions():
proc = process_registry.get(entry["session_id"])
if proc is None:
continue
status = process_status(entry)
exited_at = proc.exited_at if status != "running" else 0.0
if status != "running" and now - exited_at > RETAIN_SECONDS:
continue
end = exited_at or now
rows.append({
"kind": "process",
"key": proc.id,
"id": proc.id,
"command": " ".join((proc.command or "").split()) or t("cli.dock.process_goal_label"),
"status": status,
"exit_code": proc.exit_code,
"elapsed": max(0, int(end - proc.started_at)),
"since_exit": max(0, int(now - exited_at)) if exited_at else 0,
"detail": _last_output_line(proc.output_buffer),
})
rows.sort(key=lambda r: (r["status"] != "running", -r["elapsed"] if r["status"] == "running" else r["since_exit"]))
return rows
_GLYPH = {"running": "⚙", "done": "✔", "failed": "✘", "killed": "✘", "lost": "?"}
# Finished-status id → catalog key of the exit verdict (looked up at paint time).
_VERDICT_KEYS = {"done": "cli.dock.exit_code", "failed": "cli.dock.exit_code",
"killed": "cli.dock.verdict_killed", "lost": "cli.dock.verdict_lost"}
def process_activity(row: dict) -> str:
"""The trailing ``· …`` part of a dock line: elapsed + latest output while running, the exit
verdict + age once finished."""
if row["status"] == "running":
return f"{row['elapsed']}s · " + (
t("cli.dock.last_output", detail=row["detail"]) if row["detail"] else t("cli.dock.starting"))
verdict = _VERDICT_KEYS[row["status"]]
verdict = t(verdict, code=row["exit_code"])
return t("cli.dock.verdict_ago", verdict=verdict, seconds=row["since_exit"])
def process_glyph(row: dict) -> str:
return _GLYPH.get(row["status"], "?")
def process_tail(process_id: str) -> str:
from hermes_cli.gateway_tools import process_registry as _registry
process_registry = _registry() # the gateway session's processes (rewired CLI)
proc = process_registry.get(process_id)
if proc is None:
return t("cli.dock.process_gone")
text = (proc.output_buffer or "")[-32768:]
text = "".join(c for c in text if c.isprintable() or c in "\n\t")
return text or t("cli.dock.no_output")
def kill(process_id: str) -> dict:
from hermes_cli.gateway_tools import process_registry as _registry
process_registry = _registry() # the gateway session's processes (rewired CLI)
return process_registry.kill_process(process_id)