wire the broker to the compositor, containers and pointer

The four slices existed and nothing called them. Now the broker builds a
spatial container per session, reads back what the compositor actually did,
and aims the cursor into one container under a broker-wide lease.

Record schema 3 (registry.py): `container` {output, workspace, mode,
created_by, state}, `pointer` {bookmark, last_released_at, released_by} and
the compositor's `signature`. A v2 record is migrated on load, not refused —
its application is still running. Every field that could not be verified is
null; `container: null` means "no container", never "probably something".

Containers (new container_host.py, manager.py, launcher.py): `start`
allocates a slot from `WorkspaceAllocator`, runs `container_plan()`'s argv,
launches the app with the container stamp in its environment, then reads the
workspace, output and window back out of `clients -j`. Teardown removes the
output, releases the slot and reports survivors rather than claiming success
— and runs for an unprompted exit and for `gc` too, so no container is ever
left unattended. Broker start-up reconciles: adopt `hh-*` outputs a live
session claims, remove the rest, `contains_output()` being the only filter.
No IPC means `container: degraded` and lifecycle verbs unchanged.

The plans' argv are dispatched over the compositor's request socket rather
than by executing `hyprctl`, which is what hyprctl does with the same words;
the reply is checked, so a refused command is not recorded as a container.

Pointer (new driver.py, manager.py): a client for the driver's private
socket, and `pointer.acquire|move|button|click|release|state` on top of
`PointerLease`. The broker owns the lease, the driver executes. The bookmark
is marked on every move and before release and stored in the record, so it
survives a restart. On expiry `pressed()` is read before `expire()` and
`release_all` goes to the driver, because the lease cannot lift a button
itself; a sweep runs unprompted so the TTL is a promise.

containers.py to v2: `quarantine_plan()` -> `quarantine_argv()`, with the
before-any-per-session-rule ordering stated where the caller could get it
wrong. protocol.py: 9 NO_LEASE, 10 COMPOSITOR, the verb table, and the
`request_id` cache moved out of the server so the CLI agrees with it.

Tests: real AF_UNIX everywhere — a stateful fake compositor and the shipped
pointer driver behind a recording sink. No mocked sockets, no compositor, no
VM. 551 -> 693.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017mUq7QvQ9EZb2Wi35xj4cs
This commit is contained in:
HermenixandClaude Opus 5 committed 2026-10-02 19:47:33 +00:00
1 parent 20b2aee109
commit 129430bffd
20 files changed
+4805 -89

No files matched your search

+124 -1
View File
@@ -22,7 +22,12 @@ from .clock import Clock, iso
log = logging.getLogger("hhstreamd.registry")
SCHEMA = 2
#: 3 adds the container, the pointer bookmark and the compositor signature
#: (PLAN §7). The number is read, never written by anyone but this module: a
#: record carrying an older one is **migrated on load** (:func:`migrate`), not
#: refused, because the processes it describes are still running and refusing
#: the record would strand them.
SCHEMA = 3
#: Tombstones survive this long so a ``status`` after an unexplained failure
#: still answers the question. ``gc`` prunes them.
@@ -34,6 +39,28 @@ DEFAULT_TOMBSTONE_TTL_S = 3600.0
ACTIVITY_FLUSH_INTERVAL_S = 15.0
class ContainerState:
"""What the broker knows about a session's spatial container.
These are the only four answers. ``container: null`` is a fifth and
different thing — "this session has no container at all" — and nothing here
means "probably something": a field the compositor did not confirm stays
``None`` (PLAN §7).
"""
#: Created by us, and the compositor still shows the output.
OWNED = "owned"
#: No IPC, so nothing was created and nothing can be verified. The session
#: still starts, stops and is reaped by process identity (PLAN §4.4).
DEGRADED = "degraded"
#: Torn down on purpose: the output was removed and the slot released.
REMOVED = "removed"
#: The record and the compositor disagree — the output we recorded is not
#: there, or an output of ours has no live session. Never deleted on a
#: guess; reconciliation decides.
ORPHAN = "orphan"
class State:
STARTING = "starting"
READY = "ready"
@@ -102,6 +129,19 @@ class Session:
workspace: int | None = None
window: Any = None
foreground: bool | None = None
#: ``{output, workspace, mode, created_by, state}`` or ``None`` for "no
#: container". A dict rather than a nested dataclass because
#: :meth:`to_dict` is ``asdict`` and :meth:`from_dict` is ``cls(**data)``:
#: a dataclass would serialise and come back as a dict anyway, and the
#: asymmetry is where a half-migrated record would hide.
container: dict[str, Any] | None = None
#: ``{bookmark, last_released_at, released_by}``. Absent (``None``) means
#: the pointer was never aimed here — not that its position is unknown.
pointer: dict[str, Any] | None = None
#: ``HYPRLAND_INSTANCE_SIGNATURE`` of the compositor this session's
#: container lives in, so a restarted broker re-attaches to the same
#: instance instead of picking whichever one is there now.
signature: str | None = None
command_count: int = 0
exit_code: int | None = None
exit_detected_by: str | None = None
@@ -147,6 +187,7 @@ class Session:
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "Session":
data = migrate(data)
known = {f for f in cls.__dataclass_fields__}
unknown = set(data) - known
if unknown:
@@ -168,6 +209,88 @@ class Session:
return data
def container_record(
*,
output: str | None,
workspace: int | None,
mode: tuple[int, int] | None,
created_by: str,
state: str,
) -> dict[str, Any]:
"""One ``container`` sub-record (PLAN §7).
Every argument is explicit and may be ``None`` because the degraded case is
the common one: there is a container *state* to report long before there is
an output to name, and the alternative — leaving the key out — would read
as "no container" when what we mean is "a container we could not verify".
"""
return {
"output": output,
"workspace": workspace,
"mode": None if mode is None else {"width": mode[0], "height": mode[1]},
"created_by": created_by,
"state": state,
}
def pointer_record(
*,
bookmark: dict[str, Any] | None,
last_released_at: str | None,
released_by: str | None,
) -> dict[str, Any]:
"""One ``pointer`` sub-record: where the cursor was left, and by what.
Written on every move and on release. A session that has one has been
aimed at; a session that has none never was.
"""
return {
"bookmark": bookmark,
"last_released_at": last_released_at,
"released_by": released_by,
}
def migrate(data: dict[str, Any]) -> dict[str, Any]:
"""Bring a record read from disk up to :data:`SCHEMA`.
A schema-2 record is **adopted, not rejected**: its application is very
likely still running, and the daemon that wrote it knew nothing of
containers, so the honest value for every field schema 3 added is ``None``
— "no container", "the pointer was never used here", "we do not know which
compositor instance this was". Each of those is a defined value, which is
the whole reason the migration can be this short.
A record from a *newer* schema is left alone and loudly logged: the fields
this daemon does not know are dropped by :meth:`Session.from_dict`, and
guessing at what a future version meant would be worse than running with
the subset we understand.
"""
schema = data.get("schema")
if schema == SCHEMA:
return data
if isinstance(schema, int) and schema > SCHEMA:
log.error(
"record %s is schema %s, newer than this daemon's %s; reading the fields "
"this version knows and leaving the rest",
data.get("id"),
schema,
SCHEMA,
)
return data
upgraded = dict(data)
# Only ever *adds* keys. A v2 record that somehow carries a container must
# keep it: the field is new to the schema, not new to the session.
for key in ("container", "pointer", "signature"):
upgraded.setdefault(key, None)
upgraded["schema"] = SCHEMA
log.info("migrated record %s from schema %s to %s", data.get("id"), schema, SCHEMA)
return upgraded
def mint_id(app: str, taken: set[str], *, rng: Any = secrets) -> str:
"""``<app-slug>-<6 base32 chars>`` (DESIGN.md §2.2).