Files
hyprland-headless-stream-pl…/src/hhstreamd/textio.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

52 lines
1.8 KiB
Python

"""Reading the tail of a file that may be arbitrarily large.
Both the audit log and an application's captured output are append-only files
with no upper bound in M0, and both are read by verbs that must stay cheap.
Reading either whole — ``readlines()`` on a gigabyte of Chromium stderr — would
turn a ``status`` into an out-of-memory event on a machine that already has no
resource caps (DESIGN.md §3.3). So every tail read is bounded in bytes, and
says when it truncated.
"""
from __future__ import annotations
import os
from pathlib import Path
#: The most any single tail read will pull into memory.
DEFAULT_TAIL_BYTES = 1 << 20 # 1 MiB
def tail_lines(
path: Path | str, limit: int, *, max_bytes: int = DEFAULT_TAIL_BYTES
) -> tuple[list[str], bool, int]:
"""The last ``limit`` lines of ``path``.
Returns ``(lines, truncated, size)``. ``truncated`` is true when the window
did not reach the start of the file, so a caller can say so rather than
implying it showed everything.
"""
if limit <= 0:
try:
return [], False, os.stat(path).st_size
except OSError:
return [], False, 0
try:
with open(path, "rb") as fh:
size = fh.seek(0, os.SEEK_END)
window = min(size, max_bytes)
fh.seek(size - window)
data = fh.read(window)
except OSError:
return [], False, 0
partial = window < size
if partial:
# The window almost certainly starts mid-line; drop that fragment
# rather than reporting a half line as if it were whole.
newline = data.find(b"\n")
data = data[newline + 1 :] if newline != -1 else b""
lines = data.decode("utf-8", "replace").splitlines()
truncated = partial or len(lines) > limit
return lines[-limit:], truncated, size