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:
1 parent
20b2aee109
commit
129430bffd
20 files changed
+4805
-89
No files matched your search
@@ -9,6 +9,7 @@ rest of ``hhstreamd``.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import collections
|
||||
import enum
|
||||
import json
|
||||
from typing import Any
|
||||
@@ -48,6 +49,14 @@ class ExitCode(enum.IntEnum):
|
||||
POLICY = 6
|
||||
TIMEOUT = 7
|
||||
BUSY = 8
|
||||
#: The verb needs the pointer lease and this session does not hold it.
|
||||
#: Distinct from BUSY, which is "someone else holds it": an agent that
|
||||
#: sees 9 must acquire, one that sees 8 must wait.
|
||||
NO_LEASE = 9
|
||||
#: An IPC or output operation failed. **The session is not lost** — it
|
||||
#: keeps running in degraded mode — so this must never be confused with a
|
||||
#: session failure, which is what a shared code would do.
|
||||
COMPOSITOR = 10
|
||||
|
||||
|
||||
#: Machine-readable error names, grouped by the exit code they map to. Every
|
||||
@@ -80,6 +89,9 @@ EXIT_CODE_FOR_ERROR: dict[str, ExitCode] = {
|
||||
"not_allowlisted": ExitCode.POLICY,
|
||||
"argument_refused": ExitCode.POLICY,
|
||||
"session_cap_reached": ExitCode.POLICY,
|
||||
# The workspace band is full. A policy refusal and not a compositor
|
||||
# failure: the compositor is fine, the broker is at its budget.
|
||||
"container_budget": ExitCode.POLICY,
|
||||
"refused": ExitCode.POLICY,
|
||||
# 7 — timed out
|
||||
"timeout": ExitCode.TIMEOUT,
|
||||
@@ -87,6 +99,15 @@ EXIT_CODE_FOR_ERROR: dict[str, ExitCode] = {
|
||||
# 8 — busy
|
||||
"busy": ExitCode.BUSY,
|
||||
"already_managed": ExitCode.BUSY,
|
||||
"pointer_busy": ExitCode.BUSY,
|
||||
# 9 — the lease is required and not held
|
||||
"no_lease": ExitCode.NO_LEASE,
|
||||
# 10 — the compositor side failed; the session itself is fine
|
||||
"compositor": ExitCode.COMPOSITOR,
|
||||
"compositor_unavailable": ExitCode.COMPOSITOR,
|
||||
"no_container": ExitCode.COMPOSITOR,
|
||||
"driver_unavailable": ExitCode.COMPOSITOR,
|
||||
"driver_refused": ExitCode.COMPOSITOR,
|
||||
}
|
||||
|
||||
|
||||
@@ -103,6 +124,99 @@ def exit_code_for(error: str | None) -> ExitCode:
|
||||
return EXIT_CODE_FOR_ERROR.get(error, ExitCode.BAD_STATE)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Verbs and `request_id` idempotency (PLAN §8)
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
#: Every verb the daemon answers. The container and pointer verbs are dotted
|
||||
#: and the original eight are not, because the original eight are frozen
|
||||
#: (CONTRACT-sessions.md §4: "existing verbs are unchanged") and renaming them
|
||||
#: would break every agent for a cosmetic gain.
|
||||
VERBS = (
|
||||
"ping",
|
||||
"start",
|
||||
"list",
|
||||
"status",
|
||||
"touch",
|
||||
"kill",
|
||||
"gc",
|
||||
"adopt",
|
||||
"logs",
|
||||
"container.list",
|
||||
"container.reconcile",
|
||||
"pointer.acquire",
|
||||
"pointer.move",
|
||||
"pointer.button",
|
||||
"pointer.click",
|
||||
"pointer.release",
|
||||
"pointer.state",
|
||||
)
|
||||
|
||||
#: Verbs whose reply is remembered by ``request_id``, so a caller that died
|
||||
#: before reading the reply can retry without the mutation happening twice.
|
||||
#:
|
||||
#: The read verbs are absent deliberately: they are idempotent already, so a
|
||||
#: retry costs nothing, and a cached ``list`` would be a stale one. Every
|
||||
#: ``pointer.*`` verb that moves something is here, because a retried click is
|
||||
#: a second click — the one failure an agent cannot undo.
|
||||
MUTATING_VERBS = frozenset(
|
||||
{
|
||||
"start",
|
||||
"touch",
|
||||
"kill",
|
||||
"adopt",
|
||||
"gc",
|
||||
"container.reconcile",
|
||||
"pointer.acquire",
|
||||
"pointer.move",
|
||||
"pointer.button",
|
||||
"pointer.click",
|
||||
"pointer.release",
|
||||
}
|
||||
)
|
||||
|
||||
#: How many completed replies are kept. Bounded because a client chooses the
|
||||
#: ids: an unbounded cache is memory a peer controls.
|
||||
DEDUP_ENTRIES = 64
|
||||
|
||||
|
||||
class IdempotencyCache:
|
||||
"""The last N mutating replies, by ``request_id``.
|
||||
|
||||
A retry returns *the recorded answer*, including a recorded failure: an
|
||||
agent that retries a ``start`` which was refused by policy must see the
|
||||
refusal again, not a second attempt that might succeed and leave it with
|
||||
an application it has no id for. Replies are returned with
|
||||
``deduplicated: true`` so the caller can tell a replay from a fresh answer
|
||||
rather than having to infer it.
|
||||
"""
|
||||
|
||||
def __init__(self, capacity: int = DEDUP_ENTRIES) -> None:
|
||||
self.capacity = capacity
|
||||
self._entries: collections.OrderedDict[str, dict[str, Any]] = collections.OrderedDict()
|
||||
|
||||
def lookup(self, op: str, request_id: Any) -> dict[str, Any] | None:
|
||||
"""The recorded reply for this id, or ``None`` to go ahead and do it."""
|
||||
|
||||
if not isinstance(request_id, str) or op not in MUTATING_VERBS:
|
||||
return None
|
||||
recorded = self._entries.get(request_id)
|
||||
if recorded is None:
|
||||
return None
|
||||
return {**recorded, "deduplicated": True}
|
||||
|
||||
def remember(self, op: str, request_id: Any, response: dict[str, Any]) -> None:
|
||||
if not isinstance(request_id, str) or op not in MUTATING_VERBS:
|
||||
return
|
||||
self._entries[request_id] = dict(response)
|
||||
self._entries.move_to_end(request_id)
|
||||
while len(self._entries) > self.capacity:
|
||||
self._entries.popitem(last=False)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self._entries)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Message construction
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user