Files
hyprland-headless-stream-pl…/tests/sessions/unit/test_registry.py
T
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

271 lines
9.2 KiB
Python

"""Registry unit tests: atomic persistence, adoption, tombstones."""
from __future__ import annotations
import base64
import json
import os
from pathlib import Path
import pytest
from hhstreamd.clock import ManualClock
from hhstreamd.registry import (
Registry,
Session,
State,
atomic_write_json,
mint_id,
)
def make_session(sid: str = "foot-aaaaaa", **overrides) -> Session:
base = dict(
id=sid,
app="foot",
argv=["/usr/bin/foot"],
pid=4242,
pgid=4242,
sid=4242,
pid_starttime=918342,
cmdline_sha256="9f2c",
home="/home/someone",
runtime_dir="/run/hhstream/sessions/" + sid,
created_at="2026-10-01T09:14:02Z",
created_boottime=1000.0,
last_activity_at="2026-10-01T09:14:02Z",
last_activity_boottime=1000.0,
idle_timeout_s=10800.0,
state=State.READY,
)
base.update(overrides)
return Session(**base)
@pytest.fixture
def registry(tmp_path: Path) -> Registry:
reg = Registry(tmp_path, ManualClock(boottime=1000.0))
reg.ensure_dirs()
return reg
# ---------------------------------------------------------------------------
# atomic persistence
# ---------------------------------------------------------------------------
def test_atomic_write_leaves_no_temporary_files_behind(tmp_path: Path):
target = tmp_path / "rec.json"
atomic_write_json(target, {"hello": "world"})
assert json.loads(target.read_text()) == {"hello": "world"}
assert [p.name for p in tmp_path.iterdir()] == ["rec.json"]
def test_a_failed_write_leaves_the_previous_record_intact(tmp_path: Path, monkeypatch):
"""The point of tmp + os.replace: a reader never sees a half-written record."""
target = tmp_path / "rec.json"
atomic_write_json(target, {"generation": 1})
def explode(*args, **kwargs):
raise OSError("disk full")
monkeypatch.setattr(json, "dump", explode)
with pytest.raises(OSError):
atomic_write_json(target, {"generation": 2})
assert json.loads(target.read_text()) == {"generation": 1}
assert [p.name for p in tmp_path.iterdir()] == ["rec.json"] # temp cleaned up
def test_the_temp_file_is_written_in_the_target_directory(tmp_path: Path, monkeypatch):
"""os.replace is only atomic within one filesystem, so the temp must be adjacent."""
seen: list[str] = []
real_replace = os.replace
def spy(src, dst, *args, **kwargs):
seen.append(str(src))
return real_replace(src, dst, *args, **kwargs)
monkeypatch.setattr(os, "replace", spy)
target = tmp_path / "deep" / "rec.json"
atomic_write_json(target, {"a": 1})
assert Path(seen[0]).parent == target.parent
def test_persist_writes_the_record_and_it_round_trips(registry: Registry):
session = registry.add(make_session())
path = registry.record_path(session.id)
assert path.exists()
restored = Session.from_dict(json.loads(path.read_text()))
assert restored == session
def test_a_mirror_write_failure_never_fails_the_command(registry: Registry, monkeypatch, caplog):
session = registry.add(make_session())
monkeypatch.setattr(
"hhstreamd.registry.atomic_write_json",
lambda *a, **k: (_ for _ in ()).throw(OSError("read-only filesystem")),
)
registry.touch(session, force_flush=True) # must not raise
assert session.command_count == 1
assert "mirror write failed" in caplog.text
def test_activity_flushes_are_rate_limited_but_transitions_are_not(registry: Registry):
clock: ManualClock = registry.clock # type: ignore[assignment]
session = registry.add(make_session())
registry.touch(session) # within the 15 s window: memory only
on_disk = Session.from_dict(json.loads(registry.record_path(session.id).read_text()))
assert session.command_count == 1
assert on_disk.command_count == 0
clock.advance(16.0)
registry.touch(session)
on_disk = Session.from_dict(json.loads(registry.record_path(session.id).read_text()))
assert on_disk.command_count == 2
# ---------------------------------------------------------------------------
# handles
# ---------------------------------------------------------------------------
def test_a_colliding_draw_is_discarded_and_another_taken():
"""Handles are never reused: a draw already in the set is refused."""
class TwoDraws:
def __init__(self):
self.calls = 0
def token_bytes(self, n):
self.calls += 1
return b"\x00" * n if self.calls == 1 else b"\xff" * n
rng = TwoDraws()
collision = f"foot-{base64.b32encode(b'\x00' * 5).decode().rstrip('=').lower()[:6]}"
minted = mint_id("foot", {collision}, rng=rng)
assert rng.calls == 2
assert minted != collision
assert minted.startswith("foot-")
def test_a_tombstoned_id_still_blocks_reuse(registry: Registry):
session = registry.add(make_session())
registry.terminate(session, State.KILLED)
assert session.id in registry.taken_ids()
assert registry.new_id("foot") != session.id
def test_the_slug_is_sanitised_but_never_parsed():
assert mint_id("Chromium", set()).startswith("chromium-")
assert mint_id("/bin/odd name", set()).startswith("bin-odd-name-")
assert mint_id("!!!", set()).startswith("app-")
# ---------------------------------------------------------------------------
# tombstones
# ---------------------------------------------------------------------------
def test_terminate_writes_a_tombstone_with_an_expiry(registry: Registry):
clock: ManualClock = registry.clock # type: ignore[assignment]
session = registry.add(make_session())
registry.terminate(session, State.EXITED, exit_code=0, exit_detected_by="sigchld")
assert not session.live
assert session.exit_code == 0
assert session.tombstoned_boottime == clock.boottime()
assert session.tombstone_expires() == clock.boottime() + 3600.0
assert session.deadline() is None # a tombstone can never be idle-reaped
assert registry.tombstones() == [session]
assert registry.live() == []
def test_an_adopted_exit_records_no_exit_code_rather_than_inventing_a_zero(registry: Registry):
session = registry.add(make_session(adopted=True))
registry.terminate(session, State.EXITED, exit_detected_by="pidfd")
assert session.exit_code is None
assert session.exit_detected_by == "pidfd"
def test_drop_removes_the_record_and_its_mirror(registry: Registry):
session = registry.add(make_session())
path = registry.record_path(session.id)
registry.drop(session.id)
assert not path.exists()
assert registry.get(session.id) is None
# ---------------------------------------------------------------------------
# loading / adoption
# ---------------------------------------------------------------------------
def test_load_records_reads_back_what_was_persisted(tmp_path: Path):
writer = Registry(tmp_path, ManualClock())
writer.ensure_dirs()
writer.add(make_session("foot-aaaaaa"))
writer.add(make_session("foot-bbbbbb", pid=99))
reader = Registry(tmp_path, ManualClock())
records, quarantined = reader.load_records()
assert quarantined == []
assert sorted(r.id for r in records) == ["foot-aaaaaa", "foot-bbbbbb"]
def test_a_corrupt_record_is_quarantined_not_deleted_and_not_acted_on(tmp_path: Path, caplog):
registry = Registry(tmp_path, ManualClock())
registry.ensure_dirs()
bad = registry.sessions_dir / "foot-broken.json"
bad.write_text("{this is not json")
records, quarantined = registry.load_records()
assert records == []
assert len(quarantined) == 1
assert not bad.exists()
moved = list(registry.quarantine_dir.iterdir())
assert len(moved) == 1
assert moved[0].read_text() == "{this is not json"
assert "quarantining unreadable record" in caplog.text
def test_a_record_that_is_not_an_object_is_quarantined(tmp_path: Path):
registry = Registry(tmp_path, ManualClock())
registry.ensure_dirs()
(registry.sessions_dir / "x.json").write_text("[1, 2, 3]")
records, quarantined = registry.load_records()
assert records == [] and len(quarantined) == 1
def test_unknown_fields_from_a_newer_schema_are_dropped_not_fatal(caplog):
data = make_session().to_dict()
data["invented_by_a_later_version"] = True
session = Session.from_dict(data)
assert session.id == "foot-aaaaaa"
assert "dropping unknown record fields" in caplog.text
def test_a_record_from_a_previous_boot_is_recognised_as_stale(tmp_path: Path):
"""Pids restart from zero at boot, so /proc cannot be trusted about them."""
registry = Registry(tmp_path, ManualClock())
this_boot = make_session("foot-aaaaaa")
this_boot.boot_id = registry._boot_id
previous_boot = make_session("foot-bbbbbb")
previous_boot.boot_id = "00000000-0000-0000-0000-000000000000"
assert registry.stale_boot(this_boot) is False
assert registry.stale_boot(previous_boot) is True
def test_derived_fields_reported_to_callers(registry: Registry):
session = registry.add(make_session())
public = session.public(now=1000.0 + 600.0)
assert public["idle_s"] == 600.0
assert public["expires_in_s"] == 10800.0 - 600.0
assert public["live"] is True
assert public["schema"] == 2