Files
Hermenix 30f456c07e integrate the session broker: one CLI, a module unit, and a VM check
M0 of the sessions design, wired into the flake and gated by a real check.

What is here now:

- `src/hhstreamd/` — the broker: session registry, lifecycle verbs, process-group
  teardown, app allowlist, audit log, idle reaper on CLOCK_BOOTTIME.
- `src/hhstream_session/` — the CLI handlers, registered into the one front door.
- `src/hhstream/cli.py` — `hhstream status|shot|run|record|session …`, so the
  frame verbs and the session namespace are one binary.
- `nix/module.nix` — `sessions.*` options and the `hhstream-sessions.service`
  unit: the operator's own account (never a service account, because launched
  applications should see that account's home), the compositor's group as a
  supplementary group (without it a launched GUI client cannot open the Wayland
  socket), the generated app allowlist, and `--compositor-runtime-dir` so
  applications are handed the compositor's session rather than the broker's.
- `checks/vm-sessions.nix` — 18 subtests on a real compositor in a VM.

Two bugs found by driving it, both fixed:

- BUG-01: launched applications could not reach the compositor at all — the
  broker spawned them with its own inherited environment, which names the login
  session. `hhstreamd/compositor.py` discovers the socket and builds the child
  environment; a directory with no usable socket is a startup refusal, not a
  warning.
- BUG-02: `kill` refused to signal a live application and said nothing was
  wrong. The identity gate hashed `/proc/<pid>/cmdline`, which every `execve`
  replaces — and on NixOS applications are reached through wrapper scripts that
  do exactly that. The gate now rests on `starttime` (+ boot id, + session id
  where it is immutable) and records command-line drift as evidence of nothing.

KillMode=process is set on the unit on purpose: the default control-group kill
would take the applications down with the broker, which would make adoption and
a broker restart pointless.
2026-10-01 12:23:16 +00:00

81 lines
2.1 KiB
Python

"""Shared fixtures.
Two things are worth knowing:
* **Nothing here sleeps for real time** except where the brief requires it: the
one integration test that proves a real three-second idle reap. Everything
else either injects a clock or polls with a deadline.
* **Unix socket paths are short by force.** ``AF_UNIX`` caps a path at 108
bytes and ``pytest``'s ``tmp_path`` can exceed that on a deep ``TMPDIR``, so
sockets live in a shallow ``mkdtemp`` of their own rather than under
``tmp_path``.
"""
from __future__ import annotations
import os
import shutil
import socket
import sys
import tempfile
import time
from pathlib import Path
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
#: Linux's sun_path is 108 bytes including the NUL.
SUN_PATH_MAX = 107
@pytest.fixture
def short_tmpdir() -> Path:
"""A temp directory shallow enough to hold a unix socket path."""
path = Path(tempfile.mkdtemp(prefix="hh"))
try:
yield path
finally:
shutil.rmtree(path, ignore_errors=True)
def socket_path(directory: Path, name: str = "c.sock") -> str:
path = str(directory / name)
if len(path.encode()) > SUN_PATH_MAX:
pytest.skip(f"socket path too long for AF_UNIX ({len(path)} bytes): {path}")
return path
def wait_until(predicate, timeout: float, interval: float = 0.02, what: str = "condition"):
"""Poll ``predicate`` until it is truthy. Returns its value, or fails.
This is how the integration tests wait: a deadline and a tight poll, never
a fixed sleep sized by guesswork.
"""
deadline = time.monotonic() + timeout
while True:
value = predicate()
if value:
return value
if time.monotonic() > deadline:
pytest.fail(f"timed out after {timeout}s waiting for {what}")
time.sleep(interval)
def pid_alive(pid: int) -> bool:
return os.path.isdir(f"/proc/{pid}")
def socket_is_live(path: str) -> bool:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
sock.settimeout(0.5)
sock.connect(path)
return True
except OSError:
return False
finally:
sock.close()