Spaces:
Running

hermes-deploy commited on
Commit
a527efd
·
1 Parent(s): ad7dd76

v11: boot-time log filtering + probe token budget fix (thinking models)

Browse files
__pycache__/health_server.cpython-313.pyc CHANGED
Binary files a/__pycache__/health_server.cpython-313.pyc and b/__pycache__/health_server.cpython-313.pyc differ
 
health_server.py CHANGED
@@ -61,30 +61,62 @@ def gate_tail(n: int = 200) -> str:
61
  return "(gateway has not written a log yet)\n"
62
 
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  def gateway_status():
65
  alive = os.system(
66
  "pgrep -f 'hermes gateway|hermes_cli' >/dev/null 2>&1") == 0
 
67
  last_err = None
68
- for ln in reversed(gate_tail().splitlines()):
 
 
 
 
 
 
 
69
  low = ln.lower()
70
  if any(w in low for w in ("traceback", "error", "exception", "exited",
71
  "exit code")):
72
  last_err = ln.strip()[:300]
73
- break
74
  return alive, last_err
75
 
76
 
77
  def telegram_status():
78
- """Read the gateway log: connected / connecting / dead."""
79
- tail = gate_tail(350)
80
- i_ok = tail.rfind("Connected to Telegram")
81
- i_fail = max(tail.rfind("Failed to connect"), tail.rfind("token rejected"),
82
- tail.rfind("exited (code"))
 
83
  if i_ok > i_fail:
84
  return "connected"
85
  if i_fail > -1:
86
  return "failed"
87
- if "Connecting to Telegram" in tail:
88
  return "connecting"
89
  return "unknown"
90
 
@@ -108,19 +140,26 @@ def router_probe():
108
  out["models_error"] = f"{type(e).__name__}: {e}"[:200]
109
  try:
110
  body = json.dumps({
111
- "model": "fast", "temperature": 0, "max_tokens": 12,
112
- "messages": [{"role": "user", "content": "Reply with exactly: pong"}],
 
113
  }).encode()
114
  req = urllib.request.Request(
115
  "http://127.0.0.1:4000/v1/chat/completions",
116
  data=body, headers=hdrs, method="POST")
117
  t0 = time.time()
118
- with urllib.request.urlopen(req, timeout=40) as r:
119
  j = json.loads(r.read())
120
- ch = (j.get("choices") or [{}])[0].get("message", {}) or {}
 
 
 
 
121
  out["fast_completion"] = {
122
  "ok": True, "elapsed_s": round(time.time() - t0, 1),
123
- "content": (ch.get("content") or "")[:200],
 
 
124
  "model_served": j.get("model"),
125
  }
126
  except urllib.error.HTTPError as e:
 
61
  return "(gateway has not written a log yet)\n"
62
 
63
 
64
+ def _log_ts(ln):
65
+ """Parse 'YYYY-MM-DD HH:MM:SS,uuu' at line start -> epoch, or None."""
66
+ import re as _re
67
+ from datetime import datetime as _dt
68
+ m = _re.match(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}", ln)
69
+ if not m:
70
+ return None
71
+ try:
72
+ return _dt.strptime(m.group(0), "%Y-%m-%d %H:%M:%S,%f").timestamp()
73
+ except Exception:
74
+ return None
75
+
76
+
77
+ def _fresh_lines(lines):
78
+ """Drop lines written before THIS container booted (the HF image build
79
+ bakes the old /tmp into the layer, so stale tails can survive a reboot)."""
80
+ out = []
81
+ for ln in lines:
82
+ ts = _log_ts(ln)
83
+ if ts is None or ts > START - 30:
84
+ out.append(ln)
85
+ return out
86
+
87
+
88
  def gateway_status():
89
  alive = os.system(
90
  "pgrep -f 'hermes gateway|hermes_cli' >/dev/null 2>&1") == 0
91
+ lines = _fresh_lines(gate_tail().splitlines())
92
  last_err = None
93
+ tg_connected = False
94
+ tg_failed = False
95
+ for ln in lines:
96
+ if "Connected to Telegram" in ln:
97
+ tg_connected, tg_failed = True, False
98
+ if "Failed to connect" in ln or "token rejected" in ln or \
99
+ "exited (code" in ln:
100
+ tg_failed, tg_connected = True, False
101
  low = ln.lower()
102
  if any(w in low for w in ("traceback", "error", "exception", "exited",
103
  "exit code")):
104
  last_err = ln.strip()[:300]
 
105
  return alive, last_err
106
 
107
 
108
  def telegram_status():
109
+ lines = _fresh_lines(gate_tail(350).splitlines())
110
+ i_ok = max((i for i, ln in enumerate(lines)
111
+ if "Connected to Telegram" in ln), default=-1)
112
+ i_fail = max((i for i, ln in enumerate(lines)
113
+ if ("Failed to connect" in ln or "token rejected" in ln
114
+ or "exited (code" in ln)), default=-1)
115
  if i_ok > i_fail:
116
  return "connected"
117
  if i_fail > -1:
118
  return "failed"
119
+ if any("Connecting to Telegram" in ln for ln in lines):
120
  return "connecting"
121
  return "unknown"
122
 
 
140
  out["models_error"] = f"{type(e).__name__}: {e}"[:200]
141
  try:
142
  body = json.dumps({
143
+ "model": "fast", "temperature": 0, "max_tokens": 120,
144
+ "messages": [{"role": "user",
145
+ "content": "Say just the word: pong."}],
146
  }).encode()
147
  req = urllib.request.Request(
148
  "http://127.0.0.1:4000/v1/chat/completions",
149
  data=body, headers=hdrs, method="POST")
150
  t0 = time.time()
151
+ with urllib.request.urlopen(req, timeout=60) as r:
152
  j = json.loads(r.read())
153
+ ch = (j.get("choices") or [{}])[0]
154
+ msg = ch.get("message", {}) or {}
155
+ usage = j.get("usage") or {}
156
+ # empty visible content + tokens spent = "thinking" model burns
157
+ # its budget on hidden reasoning; finish_reason tells us why
158
  out["fast_completion"] = {
159
  "ok": True, "elapsed_s": round(time.time() - t0, 1),
160
+ "content": (msg.get("content") or "")[:200],
161
+ "finish_reason": ch.get("finish_reason"),
162
+ "completion_tokens": usage.get("completion_tokens"),
163
  "model_served": j.get("model"),
164
  }
165
  except urllib.error.HTTPError as e: