Skip to content

The agent that grows with you

refactor(gateway): compact hosted-room docstrings by hand (all WHY sentences kept)

Teknium9589f562ee02
diff --git a/gateway/hosted_room_driver.py b/gateway/hosted_room_driver.py
index 739cb0d85df..ec294db1644 100644
--- a/gateway/hosted_room_driver.py
+++ b/gateway/hosted_room_driver.py
@@ -306,8 +306,7 @@ def _migrate_task_status_constraint(conn: sqlite3.Connection) -> None:
def _connect(db_path: Path | str) -> sqlite3.Connection:
"""Open the store; existing tables are validated (after the status-constraint migration when needed).
- The driver schema never shipped, so an incompatible draft fails closed in
- ``_validate_schema`` instead of attempting a partial in-place migration.
+ The driver schema never shipped, so an incompatible draft fails closed in ``_validate_schema``.
"""
existing: list[bool] = []
@@ -492,10 +491,9 @@ def _transition(
guard: Callable[[sqlite3.Row], None] | None = None) -> dict[str, Any]:
"""Run one fenced task transition: load -> idempotent replay -> lease/fence guard -> UPDATE.
- ``sql`` binds ``(*set_params, room_id, task_id, *fence_params)`` and must hit exactly one row
- or ``stale`` is raised. ``lease_first`` puts the active-lease check before the row load
- (recovery paths) instead of after the replay (settlement paths: an identical replay still
- succeeds after the lease moved on).
+ ``sql`` binds ``(*set_params, room_id, task_id, *fence_params)`` and must hit exactly one row or
+ ``stale`` is raised. ``lease_first`` checks the lease before the row load (recovery paths) instead
+ of after the replay (settlement paths: an identical replay still succeeds after the lease moved on).
"""
params = (*set_params, identity.room_id, identity.task_id, *fence_params)
with _transaction(db_path) as conn:
@@ -537,8 +535,7 @@ def _run_fence_transition(
lease_generation: Callable[[Any], int] = int, **transition: Any) -> dict[str, Any]:
"""Transition fenced on this attempt's running generation under its exact lease (row guard + SQL fence).
- ``lease_generation`` casts the stored run_lease_generation: ``int`` raises on NULL,
- ``int(v or 0)`` reads it as generation 0.
+ ``lease_generation`` casts the stored run_lease_generation: ``int`` raises on NULL, ``int(v or 0)`` reads 0.
"""
lease = attempt.lease
diff --git a/gateway/hosted_room_peer.py b/gateway/hosted_room_peer.py
index 4d89d47988b..ab532d8c9f7 100644
--- a/gateway/hosted_room_peer.py
+++ b/gateway/hosted_room_peer.py
@@ -103,10 +103,9 @@ def _gateway_room_grant_secret_for_home(home_value: str) -> bytes:
def gateway_room_grant_secret(root: Path | str | None = None) -> bytes:
"""Load or atomically mint the gateway-only RoomLink signing secret.
- API keys are bearer credentials known to clients and may be profile scoped;
- they must never become grant-signing authority. This secret lives in the
- installation root, is not exposed by configuration or capability RPCs, and
- is shared only by the gateway processes that serve this installation.
+ API keys are client-known, possibly profile-scoped bearer credentials and must never become
+ grant-signing authority; this secret lives in the installation root, is never exposed by
+ config or capability RPCs, and is shared only by this installation's gateway processes.
"""
if root is None:
from hermes_constants import get_hermes_home
@@ -120,8 +119,7 @@ def gateway_room_grant_secret(root: Path | str | None = None) -> bytes:
def derive_room_grant_secret(api_key: str) -> bytes:
"""Domain-separate room grants from the configured API key.
- The API-server startup guard enforces the production key-strength policy;
- this helper keeps a lower structural floor for isolated contract tests.
+ Production key strength is enforced by the API-server startup guard; this floor is for contract tests.
"""
if not isinstance(api_key, str) or len(api_key) < 8:
raise HostedRoomGrantError("room grants require a strong gateway API key")
@@ -352,11 +350,7 @@ def _configured_room_link_url() -> str | None:
def validate_room_link_url(value: Any) -> tuple[str, TransportSecurity]:
- """Validate a RoomLink endpoint and classify its transport protection.
-
- Plaintext HTTP is allowed only toward the local loopback interface; every
- non-loopback endpoint must use HTTPS.
- """
+ """Validate a RoomLink endpoint and classify its transport: plaintext HTTP only toward loopback."""
raw = str(value or "").strip().rstrip("/")
try:
parsed = urllib.parse.urlsplit(raw)
@@ -545,11 +539,7 @@ def decode_room_grant(secret: bytes, token: str, *, permission: str, now: float
def room_grant_needs_dispatch_refresh(token: str, *, now: float | None = None, leeway_seconds: float = 5 * 60) -> bool:
- """Read only grant timing to schedule target-validated refresh.
-
- This deliberately does not establish trust; the target validates the
- signature and immutable scope before issuing a replacement.
- """
+ """Read only grant timing to schedule refresh; trust is established by the target, not here."""
try:
payload = json.loads(_b64decode(_split_token(token)[0]).decode("ascii"))
expires_at = float(payload["expires_at"])
diff --git a/gateway/hosted_room_replicas.py b/gateway/hosted_room_replicas.py
index 90163b183b4..815c8a0284d 100644
--- a/gateway/hosted_room_replicas.py
+++ b/gateway/hosted_room_replicas.py
@@ -78,8 +78,7 @@ def _initialize_replica_schema(conn: sqlite3.Connection) -> None:
def _replica_transaction(db_path: Path | str) -> Iterator[sqlite3.Connection]:
"""Ensure the replica schema (own autocommit connection), then open an IMMEDIATE transaction.
- The schema DDL is re-run inside the transaction too; that double init is the
- established statement order and is intentionally preserved.
+ The DDL is deliberately re-run inside the transaction: that double init is the established statement order.
"""
conn = _connect(db_path)
try:
@@ -171,11 +170,7 @@ def _store_replica(
def ingest_page(
db_path: Path | str, *, room_id: Any, room_name: Any, members: Any, page: Any, now: float | None = None
) -> dict[str, Any]:
- """Persist one replay page idempotently; refuses seq gaps and epoch regressions.
-
- ``page`` is the verbatim ``groups.log`` (``read_events()``) result whose
- ``authority`` stamp proves lineage.
- """
+ """Persist one verbatim ``read_events()`` page idempotently; refuses seq gaps and epoch regressions."""
room_id = _room_id(room_id)
room_name = _validate_room_name(room_name)
_, members_json = _validate_members(members)
@@ -236,10 +231,9 @@ def promote_replica(
) -> dict[str, Any]:
"""Continue a replicated room on THIS gateway at ``epoch + 1``.
- Copies the replica log into the authoritative store and appends a
- lineage-proving ``authority.claimed`` event; wherever the claim replicates,
- the old epoch is stale and every fenced primitive rejects it. The caller
- decides takeover is safe; this only makes it atomic and provable.
+ Copies the replica log into the authoritative store and appends a lineage-proving
+ ``authority.claimed`` event, so wherever the claim replicates the old epoch is stale and every
+ fenced primitive rejects it. The caller decides takeover is safe; this makes it atomic and provable.
"""
room_id = _room_id(room_id)
if not isinstance(reason, str) or not reason or len(reason) > 200:
@@ -301,10 +295,9 @@ def demote_room(
) -> dict[str, Any]:
"""Fence THIS gateway's stale room authority against a proven newer epoch.
- Called when a returning gateway observes (replicated ``authority.claimed``
- or a transport rejection) that another gateway owns the room at a higher
- epoch. Appends ``authority.lost`` and adopts the observed lineage so no
- local send can commit at the stale epoch. Idempotent per lineage.
+ When a returning gateway observes (replicated ``authority.claimed`` or a transport rejection) that
+ another gateway owns the room at a higher epoch, append ``authority.lost`` and adopt the observed
+ lineage so no local send can commit at the stale epoch. Idempotent per lineage.
"""
room_id = _room_id(room_id)
observed_gateway_id = _validate_identifier(
diff --git a/gateway/hosted_rooms.py b/gateway/hosted_rooms.py
index dc439801b39..a3f96ea7bb5 100644
--- a/gateway/hosted_rooms.py
+++ b/gateway/hosted_rooms.py
@@ -1047,8 +1047,7 @@ def append_event(
authority_gateway_id: Any = None, authority_epoch: Any = None, now: float | None = None) -> dict[str, Any]:
"""Append one immutable event and allocate its per-room sequence atomically.
- Repeating the same ``event_id`` and immutable content returns the original
- event. Reusing the id for different content fails closed.
+ Repeating an ``event_id`` with identical content returns the original; different content fails closed.
"""
room_id = _room_id(room_id)
event_id = _event_id(event_id)
@@ -1113,9 +1112,8 @@ def _probe(path: Path, table: str, query: str, params: tuple[Any, ...], unavaila
def probe_hosted_room(db_path: Path | str, *, room_id: Any) -> bool:
"""Check room ownership without creating or migrating the shared store.
- This runs on the synchronous prompt-admission path for older Desktop
- clients, so it fails quickly under contention instead of blocking the
- WebSocket reader for SQLite's normal ten-second timeout.
+ Runs on the synchronous prompt-admission path for older Desktop clients, so it fails fast
+ under contention instead of blocking the WebSocket reader for SQLite's ten-second timeout.
"""
checked_room_id = _room_id(room_id)
return _probe(
@@ -1189,9 +1187,8 @@ def claim_authority(
new_gateway_id: Any, event_id: Any, now: float | None = None) -> dict[str, Any]:
"""Fence a verified authority transfer with a compare-and-swap epoch.
- This storage primitive does not decide *when* takeover is safe. A
- replicated driver must call it only after its lease/quorum policy has
- established that the previous owner can no longer commit.
+ Does not decide *when* takeover is safe: a replicated driver calls it only after its
+ lease/quorum policy established that the previous owner can no longer commit.
"""
room_id = _room_id(room_id)
expected_gateway_id = _actor_id(expected_gateway_id, "expected_gateway_id")