refactor(gateway): dedupe drain-timeout resolution + share active_agents parse

Follow-up cleanups on top of the busy/idle readout (PR #50103):

- web_server.py /api/status reused the single drain-timeout resolver
  hermes_cli.gateway._get_restart_drain_timeout() (HERMES_RESTART_DRAIN_TIMEOUT
  env -> agent.restart_drain_timeout config -> default) instead of inlining a
  third hand-rolled copy of that precedence chain. Also fixes a subtle
  divergence: the inline copy used os.environ.get() so a set-but-empty env var
  was treated as a value rather than falling through to config; the shared
  resolver .strip()s and falls through correctly.
- Added gateway.status.parse_active_agents() and routed BOTH HTTP surfaces
  (/api/status and /health/detailed) through it, so the exposed active_agents
  field is consistently clamped non-negative. Previously /api/status clamped
  while /health/detailed exposed the raw file value, diverging on a corrupt
  count.
- Added TestParseActiveAgents covering the shared coercion contract.
This commit is contained in:
kshitijk4poor 2026-06-21 16:37:42 +05:30
parent 0ee75469d7
commit b577f25100
4 changed files with 55 additions and 19 deletions

View file

@ -1093,6 +1093,34 @@ class TestCorruptStatusFiles:
assert status._read_pid_record(p) == {"pid": 4242}
class TestParseActiveAgents:
"""The shared read-side coercion used by BOTH HTTP surfaces (/api/status
and /health/detailed) so the exposed active_agents field is consistent and
never negative regardless of what the status file holds."""
def test_valid_int_passthrough(self):
assert status.parse_active_agents(3) == 3
def test_zero(self):
assert status.parse_active_agents(0) == 0
def test_numeric_string_coerced(self):
assert status.parse_active_agents("5") == 5
def test_negative_clamped_to_zero(self):
assert status.parse_active_agents(-3) == 0
def test_none_degrades_to_zero(self):
assert status.parse_active_agents(None) == 0
def test_garbage_string_degrades_to_zero(self):
assert status.parse_active_agents("garbage") == 0
def test_float_truncates(self):
# int() truncation, then clamp — never raises.
assert status.parse_active_agents(2.9) == 2
class TestActiveAgentsTurnBoundaryWrite:
"""The load-bearing Phase 1a contract: writing the in-flight count at a
turn boundary must PRESERVE the lifecycle gateway_state. The whole readout