Files
hermes-agent/tests/gateway/test_session_authority.py
Teknium e1a10b0bc2 ci: bring the branch's own code under main's strict ruff config
Main made the strict ruff config the default (`ruff check .`, blocking), and
the branch's code carried 278 findings under it (main itself is clean). Fixed:

- PLW1510 (86): `check=False` on each subprocess.run, preserving behaviour,
  as main's own sweep (0006a5e229) did.
- ASYNC220 (9, test fixtures only): `noqa` with the reason, main's convention
  for a Popen that returns at once and whose handle the test drives.
- ruff's safe autofixes plus the narrow unsafe ones (UP006/UP037/RUF100/RUF013/
  PLR0402/RUF059 underscore renames/UP031/UP041/UP017/FURB167/PIE807/RUF007/
  RUF015/RUF022/FURB162), and by hand: four %-formats to f-strings, three
  unused unpacked names, one fixture loop over `.items()`.

No behaviour change. `ruff check .` passes; scripts/check passes; the 412 test
files mirroring every edited file pass (3 tests/pm files that fail only under
that batch's uv lookup pass solo, before and after this commit).
2026-10-10 17:59:08 -07:00

71 lines
3.2 KiB
Python

"""Real messaging ingress and authenticated WS must share one warm agent."""
import json
import os
from pathlib import Path
import subprocess
import sys
import time
import pytest
def test_messaging_then_authenticated_ws_reuses_live_agent(tmp_path):
repo = Path(__file__).resolve().parents[2]
home = tmp_path / "home"
home.mkdir()
state = tmp_path / "state"
state.mkdir()
# A fresh interpreter keeps import-time profile caches and background workers
# away from both the user's state and pytest's process-global fixtures.
env = {key: os.environ[key] for key in ("PATH", "SYSTEMROOT", "LANG", "TZ") if key in os.environ}
env.update(HOME=str(home), USERPROFILE=str(home), HERMES_HOME=str(state),
PYTHONPATH=str(repo), PYTHONUNBUFFERED="1")
result = subprocess.run(
[sys.executable, str(Path(__file__).parent / "fixtures" / "shared_authority_peer.py")],
cwd=repo, env=env, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=100, check=False
)
assert result.returncode == 0, result.stdout + "\n" + result.stderr
receipt = json.loads((state / "receipt.json").read_text())
print(json.dumps(receipt, indent=2))
assert receipt["messaging_completed"], receipt
assert receipt["unauthenticated_rejected"], receipt
assert receipt["ws_completed"], receipt
assert receipt["same_agent"], receipt
@pytest.mark.platforms("linux")
def test_native_queue_survives_owner_kill_without_replaying_unknown(tmp_path):
repo = Path(__file__).resolve().parents[2]
home, state = tmp_path / 'home', tmp_path / 'state'
home.mkdir()
state.mkdir()
env = {key: os.environ[key] for key in ('PATH', 'LANG', 'TZ') if key in os.environ}
env.update(HOME=str(home), USERPROFILE=str(home), HERMES_HOME=str(state),
PYTHONPATH=str(repo), PYTHONUNBUFFERED='1', AUTHORITY_PROBE_MODE='crash')
command = [sys.executable, str(Path(__file__).parent / 'fixtures' / 'shared_authority_peer.py')]
with (state / 'owner.log').open('w+') as log:
child = subprocess.Popen(command, cwd=repo, env=env, stdin=subprocess.DEVNULL,
stdout=log, stderr=subprocess.STDOUT)
try:
deadline = time.monotonic() + 90
while not (state / 'crash-ready.json').exists():
if child.poll() is not None or time.monotonic() > deadline:
log.seek(0)
pytest.fail(log.read())
time.sleep(0.05)
child.kill()
assert child.wait(timeout=10) < 0
finally:
if child.poll() is None:
child.kill()
child.wait(timeout=10)
env['AUTHORITY_PROBE_MODE'] = 'recover'
for expected_calls in (1, 0):
result = subprocess.run(command, cwd=repo, env=env, stdin=subprocess.DEVNULL,
capture_output=True, text=True, timeout=70, check=False)
assert result.returncode == 0, result.stdout + '\n' + result.stderr
receipt = json.loads((state / 'recovery-receipt.json').read_text())
assert receipt['model_calls'] == expected_calls, receipt
print(json.dumps(receipt, indent=2))