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
297 lines
11 KiB
Python
297 lines
11 KiB
Python
"""Start-up reconciliation: adopt what is ours, remove what is unattended.
|
|
|
|
A container is a render target that costs CPU and a place windows can land, so
|
|
one with no live session must never be left behind — and a broker that lost its
|
|
registry recovers by exactly this scan. The danger is the other direction: the
|
|
scan deletes outputs, so everything here is also a test that it deletes only
|
|
what :func:`hhstreamd.containers.contains_output` claims.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from fakes import FakeCompositor
|
|
|
|
from hhstreamd import procmon
|
|
from hhstreamd.clock import ManualClock
|
|
from hhstreamd.manager import ManagerConfig, SessionManager
|
|
from hhstreamd.policy import Policy
|
|
from hhstreamd.registry import ContainerState, Session, State, container_record
|
|
|
|
|
|
def make_session(session_id: str, *, output: str | None, workspace: int | None, **kwargs) -> Session:
|
|
return Session(
|
|
id=session_id,
|
|
app="foot",
|
|
argv=["/usr/bin/foot"],
|
|
pid=4242,
|
|
pgid=4242,
|
|
sid=4242,
|
|
pid_starttime=1,
|
|
cmdline_sha256="9f2c",
|
|
home="/home/someone",
|
|
runtime_dir=f"/run/hhstream/sessions/{session_id}",
|
|
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,
|
|
container=container_record(
|
|
output=output,
|
|
workspace=workspace,
|
|
mode=(1280, 720),
|
|
created_by="broker",
|
|
state=ContainerState.OWNED,
|
|
),
|
|
**kwargs,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def compositor(short_tmpdir: Path):
|
|
fake = FakeCompositor(short_tmpdir)
|
|
try:
|
|
yield fake
|
|
finally:
|
|
fake.stop()
|
|
|
|
|
|
@pytest.fixture
|
|
def manager(short_tmpdir: Path, compositor: FakeCompositor) -> SessionManager:
|
|
return SessionManager(
|
|
ManagerConfig(
|
|
state_dir=short_tmpdir / "state",
|
|
runtime_dir=short_tmpdir / "run",
|
|
policy=Policy(),
|
|
compositor_runtime_dir=compositor.runtime_dir,
|
|
signature=compositor.signature,
|
|
),
|
|
clock=ManualClock(boottime=1000.0),
|
|
)
|
|
|
|
|
|
def test_an_unattended_output_is_removed_at_start_up(manager, compositor):
|
|
compositor.add_output("hh-ghost-aaaaaa")
|
|
|
|
summary = manager.reconcile_containers()
|
|
|
|
assert summary["removed"] == ["hh-ghost-aaaaaa"]
|
|
assert "hh-ghost-aaaaaa" not in compositor.outputs()
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["HEADLESS-1", "DP-1", "hh-", "hh-Upper", "hhother"])
|
|
def test_an_output_the_filter_rejects_is_never_touched(manager, compositor, name):
|
|
"""The one thing between this scan and somebody else's monitor."""
|
|
|
|
compositor.add_output(name)
|
|
|
|
summary = manager.reconcile_containers()
|
|
|
|
assert summary["removed"] == []
|
|
assert name in compositor.outputs()
|
|
|
|
|
|
def test_a_container_with_a_live_session_is_adopted_not_removed(manager, compositor):
|
|
compositor.add_output("hh-foot-aaaaaa")
|
|
manager.registry.add(
|
|
make_session("foot-aaaaaa", output="hh-foot-aaaaaa", workspace=1002,
|
|
signature=compositor.signature)
|
|
)
|
|
|
|
summary = manager.reconcile_containers()
|
|
|
|
assert summary["adopted"] == [{"id": "foot-aaaaaa", "output": "hh-foot-aaaaaa"}]
|
|
assert summary["removed"] == []
|
|
assert "hh-foot-aaaaaa" in compositor.outputs()
|
|
|
|
|
|
def test_an_adopted_workspace_is_not_handed_to_the_next_session(manager, compositor):
|
|
"""After a restart the records name slots that are still in use."""
|
|
|
|
compositor.add_output("hh-foot-aaaaaa")
|
|
manager.registry.add(
|
|
make_session("foot-aaaaaa", output="hh-foot-aaaaaa", workspace=1000,
|
|
signature=compositor.signature)
|
|
)
|
|
|
|
manager.reconcile_containers()
|
|
|
|
assert manager.host.allocator.in_use() == [1000]
|
|
assert manager.host.create("foot-bbbbbb", "hh-foot-bbbbbb").workspace == 1001
|
|
|
|
|
|
def test_a_record_naming_an_output_that_is_gone_is_marked_orphan(manager, compositor):
|
|
"""The application may still be running, so nothing is killed for this."""
|
|
|
|
session = manager.registry.add(
|
|
make_session("foot-aaaaaa", output="hh-foot-aaaaaa", workspace=1000,
|
|
signature=compositor.signature)
|
|
)
|
|
|
|
summary = manager.reconcile_containers()
|
|
|
|
assert session.container["state"] == ContainerState.ORPHAN
|
|
assert summary["orphaned"] == [{"id": "foot-aaaaaa", "output": "hh-foot-aaaaaa"}]
|
|
assert session.live, "losing a container is not losing the session"
|
|
|
|
|
|
def test_a_container_from_a_previous_compositor_instance_is_not_claimed(manager, compositor):
|
|
"""This is what the recorded signature is for.
|
|
|
|
The output name would match, and adopting it would hand this broker a
|
|
container that died with its compositor — and reserve a workspace slot in
|
|
an instance that never allocated one.
|
|
"""
|
|
|
|
compositor.add_output("hh-foot-aaaaaa")
|
|
session = manager.registry.add(
|
|
make_session("foot-aaaaaa", output="hh-foot-aaaaaa", workspace=1000,
|
|
signature="an-older-instance")
|
|
)
|
|
|
|
summary = manager.reconcile_containers()
|
|
|
|
assert session.container["state"] == ContainerState.ORPHAN
|
|
assert summary["orphaned"] == [{"id": "foot-aaaaaa", "why": "signature_changed"}]
|
|
# Unclaimed, so the scan takes it down like any other unattended output.
|
|
assert summary["removed"] == ["hh-foot-aaaaaa"]
|
|
assert manager.host.allocator.in_use() == []
|
|
|
|
|
|
def test_the_quarantine_backstop_is_installed_before_anything_else(manager, compositor):
|
|
manager.reconcile_containers()
|
|
|
|
keywords = [c for c in compositor.commands if c.startswith("keyword")]
|
|
assert keywords[0].startswith("keyword windowrule match:initial_class .*,")
|
|
|
|
|
|
def test_no_compositor_leaves_the_broker_degraded_rather_than_refusing(short_tmpdir: Path):
|
|
manager = SessionManager(
|
|
ManagerConfig(
|
|
state_dir=short_tmpdir / "state",
|
|
runtime_dir=short_tmpdir / "run",
|
|
policy=Policy(),
|
|
compositor_runtime_dir=short_tmpdir / "no-compositor",
|
|
),
|
|
clock=ManualClock(boottime=1000.0),
|
|
)
|
|
|
|
summary = manager.reconcile_containers()
|
|
|
|
assert summary["available"] is False
|
|
assert summary["removed"] == []
|
|
assert manager.containers_available is False
|
|
assert "no-compositor" in summary["error"]
|
|
|
|
|
|
def test_container_list_will_not_claim_there_are_no_orphans_without_looking(short_tmpdir):
|
|
"""`[]` would read as "nothing is out there", which we cannot know."""
|
|
|
|
manager = SessionManager(
|
|
ManagerConfig(
|
|
state_dir=short_tmpdir / "state",
|
|
runtime_dir=short_tmpdir / "run",
|
|
policy=Policy(),
|
|
compositor_runtime_dir=short_tmpdir / "no-compositor",
|
|
),
|
|
clock=ManualClock(boottime=1000.0),
|
|
)
|
|
manager.reconcile_containers()
|
|
|
|
listing = manager.container_list()
|
|
|
|
assert listing["orphans"] is None
|
|
assert listing["compositor"] == "absent"
|
|
|
|
|
|
def test_container_list_names_the_orphans_it_can_see(manager, compositor):
|
|
compositor.add_output("hh-ghost-aaaaaa")
|
|
manager.containers_available = True
|
|
manager.registry.add(
|
|
make_session("foot-aaaaaa", output="hh-foot-aaaaaa", workspace=1000,
|
|
signature=compositor.signature)
|
|
)
|
|
compositor.add_output("hh-foot-aaaaaa")
|
|
|
|
listing = manager.container_list()
|
|
|
|
assert listing["orphans"] == ["hh-ghost-aaaaaa"]
|
|
assert [row["id"] for row in listing["containers"]] == ["foot-aaaaaa"]
|
|
# Listing is not acting: the orphan is still there afterwards.
|
|
assert "hh-ghost-aaaaaa" in compositor.outputs()
|
|
|
|
|
|
def test_gc_takes_down_the_container_of_a_session_it_tombstones(manager, compositor):
|
|
"""`gc` is the last thing that will ever look at that session.
|
|
|
|
It tombstones a record whose process vanished without the watcher
|
|
noticing; if it did not also take the output down, nothing else would,
|
|
until the next broker start-up found it unattended.
|
|
"""
|
|
|
|
compositor.add_output("hh-foot-aaaaaa")
|
|
session = manager.registry.add(
|
|
make_session("foot-aaaaaa", output="hh-foot-aaaaaa", workspace=1000,
|
|
signature=compositor.signature)
|
|
)
|
|
manager.reconcile_containers() # adopts it, and reserves its slot
|
|
# A pid that is not a process: what `gc` is for.
|
|
session.pid = session.pgid = session.sid = 4_194_303
|
|
|
|
result = asyncio.run(manager.gc())
|
|
|
|
entry = {row["id"]: row for row in result["reconciled"]}["foot-aaaaaa"]
|
|
assert entry["container_removed"] is True
|
|
assert entry["container_survivors"] == []
|
|
assert "hh-foot-aaaaaa" not in compositor.outputs()
|
|
assert session.container["state"] == ContainerState.REMOVED
|
|
assert manager.host.allocator.in_use() == [], "the slot came back"
|
|
|
|
|
|
def test_gc_leaves_a_healthy_session_s_container_alone(manager, compositor):
|
|
compositor.add_output("hh-foot-aaaaaa")
|
|
session = manager.registry.add(
|
|
make_session("foot-aaaaaa", output="hh-foot-aaaaaa", workspace=1000,
|
|
signature=compositor.signature)
|
|
)
|
|
manager.reconcile_containers()
|
|
# This process: alive, and its own identity.
|
|
identity = procmon.identity(os.getpid())
|
|
session.pid, session.pgid, session.sid = os.getpid(), identity.pgid, identity.sid
|
|
session.pid_starttime = identity.starttime
|
|
session.cmdline_sha256 = identity.cmdline_sha256
|
|
|
|
asyncio.run(manager.gc())
|
|
|
|
assert "hh-foot-aaaaaa" in compositor.outputs()
|
|
assert session.container["state"] == ContainerState.OWNED
|
|
|
|
|
|
def test_reconciling_everything_rescans_the_outputs_and_every_session(manager, compositor):
|
|
"""`container.reconcile` with no id is the start-up scan, on demand."""
|
|
|
|
compositor.add_output("hh-foot-aaaaaa")
|
|
session = manager.registry.add(
|
|
make_session("foot-aaaaaa", output="hh-foot-aaaaaa", workspace=1000,
|
|
signature=compositor.signature)
|
|
)
|
|
manager.reconcile_containers()
|
|
compositor.add_output("hh-ghost-aaaaaa")
|
|
compositor.add_window("hh-foot-aaaaaa", 1000, "hh-foot-aaaaaa")
|
|
|
|
result = asyncio.run(manager.container_reconcile(None))
|
|
|
|
assert result["scan"]["removed"] == ["hh-ghost-aaaaaa"]
|
|
assert result["scan"]["adopted"] == [{"id": "foot-aaaaaa", "output": "hh-foot-aaaaaa"}]
|
|
# The window that appeared while nobody was looking is now in the record.
|
|
assert session.window["initial_class"] == "hh-foot-aaaaaa"
|
|
assert {d["field"] for d in result["diff"]} == {"workspace", "window"}
|
|
# The slot stays reserved across a second scan rather than being hunted
|
|
# for again in a band it is itself occupying.
|
|
assert manager.host.allocator.in_use() == [1000]
|