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

111 lines
3.6 KiB
Python

"""Protocol framing tests (DESIGN.md §3.4)."""
from __future__ import annotations
import json
import pytest
from hhstreamd import protocol
from hhstreamd.protocol import MAX_LINE_BYTES, PROTOCOL_VERSION, ProtocolError
def test_a_message_is_exactly_one_newline_terminated_line():
frame = protocol.encode({"ok": True, "id": "foot-aaaaaa"})
assert frame.endswith(b"\n")
assert frame.count(b"\n") == 1
assert json.loads(frame) == {"ok": True, "id": "foot-aaaaaa"}
def test_round_trip():
message = {"v": 1, "op": "start", "app": "foot", "argv": ["--a", "--b"]}
assert protocol.decode(protocol.encode(message)) == message
def test_embedded_newlines_cannot_break_the_framing():
"""A session id or an argv element must never be able to split a frame."""
message = {"ok": False, "reason": "line one\nline two\r\nline three"}
frame = protocol.encode(message)
assert frame.count(b"\n") == 1
assert protocol.decode(frame) == message
def test_non_ascii_is_escaped_so_the_frame_stays_ascii():
frame = protocol.encode({"reason": "café — ✓"})
frame.decode("ascii") # must not raise
assert protocol.decode(frame)["reason"] == "café — ✓"
def test_decode_tolerates_a_missing_trailing_newline():
assert protocol.decode(b'{"ok":true}') == {"ok": True}
@pytest.mark.parametrize("line", [b"", b"\n", b" \n"])
def test_an_empty_frame_is_a_bad_request(line):
with pytest.raises(ProtocolError) as exc:
protocol.decode(line)
assert exc.value.error == "bad_request"
def test_malformed_json_is_a_bad_request():
with pytest.raises(ProtocolError) as exc:
protocol.decode(b"{not json\n")
assert exc.value.error == "bad_request"
assert "malformed JSON" in exc.value.reason
@pytest.mark.parametrize("payload", [b"[1,2,3]", b'"a string"', b"42", b"null"])
def test_a_frame_that_is_not_an_object_is_rejected(payload):
with pytest.raises(ProtocolError) as exc:
protocol.decode(payload)
assert exc.value.error == "bad_request"
def test_an_oversized_frame_is_refused_on_both_sides():
with pytest.raises(ProtocolError) as exc:
protocol.decode(b"x" * (MAX_LINE_BYTES + 1))
assert exc.value.error == "message_too_large"
with pytest.raises(ProtocolError) as exc:
protocol.encode({"argv": ["x" * MAX_LINE_BYTES]})
assert exc.value.error == "message_too_large"
def test_decode_request_requires_an_op():
with pytest.raises(ProtocolError) as exc:
protocol.decode_request(b'{"v":1}')
assert exc.value.error == "bad_request"
assert "no 'op'" in exc.value.reason
def test_decode_request_rejects_an_unknown_protocol_version():
with pytest.raises(ProtocolError) as exc:
protocol.decode_request(b'{"v":99,"op":"list"}')
assert exc.value.error == "unsupported_version"
def test_a_request_without_a_version_is_assumed_to_be_current():
assert protocol.decode_request(b'{"op":"list"}')["op"] == "list"
def test_request_attaches_the_version_and_drops_unset_fields():
message = protocol.request("start", app="foot", argv=None, idle_timeout_s=30.0)
assert message == {"v": PROTOCOL_VERSION, "op": "start", "app": "foot", "idle_timeout_s": 30.0}
def test_error_replies_carry_both_a_machine_field_and_a_human_field():
reply = protocol.error(
"not_allowlisted", "app 'sh' is not in the allowlist", hint="see the config"
)
assert reply == {
"ok": False,
"error": "not_allowlisted",
"reason": "app 'sh' is not in the allowlist",
"hint": "see the config",
}
def test_ok_replies_are_flagged():
assert protocol.ok(id="a")["ok"] is True