Skip to content

The agent that grows with you

refactor(gateway): generic bracket-group packing in hosted-room modules (AST-neutral)

Tekniumcf89a14df198
diff --git a/gateway/hosted_room_discussion.py b/gateway/hosted_room_discussion.py
index 274e0660ea6..2fc83178d71 100644
--- a/gateway/hosted_room_discussion.py
+++ b/gateway/hosted_room_discussion.py
@@ -47,10 +47,9 @@ _TURN_ID_RE = re.compile(
_LOCAL_TARGET_FIELDS = frozenset({"kind", "profile"})
_PEER_TARGET_FIELDS = frozenset({"kind", "peer_id", "installation_id", "profile", "capability_digest"})
_REMOTE_MEMBER_FIELDS = frozenset({
- "connectionId", "connectionKind", "connectionLabel", "connection_id",
- "connection_kind", "connection_label", "remoteSource", "route",
- "sourceMissing", "sourceReachable", "sourceScoped", "targetProfile",
- "target_profile"})
+ "connectionId", "connectionKind", "connectionLabel", "connection_id", "connection_kind", "connection_label",
+ "remoteSource", "route", "sourceMissing", "sourceReachable", "sourceScoped", "targetProfile", "target_profile",
+})
_USER_PAYLOAD_FIELDS = frozenset({"text", "thread_id"})
_TURN_COORDINATE_FIELDS = frozenset(
{"discussion_event_id", "member_id", "member_index", "round_index", "task_id", "thread_id", "turn_id", })
@@ -275,8 +274,7 @@ def validate_roster(value: Any, *, local_profiles: Iterable[str]) -> tuple[Discu
else "member targets must be unique")
for key, seen, message in (
(target_key, targets, target_message),
- (handle.casefold(), handles,
- "member handles must be unique and cannot reserve @all or @everyone"),
+ (handle.casefold(), handles, "member handles must be unique and cannot reserve @all or @everyone"),
(member_id.casefold(), member_ids, "member ids must be unique")):
if key in seen:
raise DiscussionValidationError(message)
@@ -618,15 +616,11 @@ def _make_task_plan(
f"d{discussion_event.seq}.r{round_index}.p{member_index}."
f"s{seen_through_seq}.m{_member_digest(member)}")
seed = compact_json({
- "discussion_event_id": discussion_event.event_id,
- "member_id": member.member_id,
- "member_index": member_index,
- "prompt_sha256": hashlib.sha256(prompt.encode("utf-8")).hexdigest(),
- "room_id": room.room_id,
- "round_index": round_index,
- "seen_through_seq": seen_through_seq,
- "source_event_seq": discussion_event.seq,
- "thread_id": discussion_event.payload["thread_id"]})
+ "discussion_event_id": discussion_event.event_id, "member_id": member.member_id, "member_index": member_index,
+ "prompt_sha256": hashlib.sha256(prompt.encode("utf-8")).hexdigest(), "room_id": room.room_id,
+ "round_index": round_index, "seen_through_seq": seen_through_seq, "source_event_seq": discussion_event.seq,
+ "thread_id": discussion_event.payload["thread_id"],
+ })
task_id = f"dtask:{hashlib.sha256(seed.encode('utf-8')).hexdigest()[:48]}"
identity = driver.TaskIdentity(
room_id=room.room_id, task_id=task_id,
@@ -836,14 +830,10 @@ def _settled_effects(
effects.append(EventPlan(
event_id=message_event_id, kind="message.member", actor=member_actor,
payload={
- "discussion_event_id": task.discussion_event_id,
- "member_id": task.member.member_id,
- "member_index": task.member_index,
- "round_index": task.round_index,
- "task_id": task.identity.task_id,
- "text": text,
- "thread_id": task.identity.thread_id,
- "turn_id": task.identity.turn_id},
+ "discussion_event_id": task.discussion_event_id, "member_id": task.member.member_id,
+ "member_index": task.member_index, "round_index": task.round_index, "task_id": task.identity.task_id,
+ "text": text, "thread_id": task.identity.thread_id, "turn_id": task.identity.turn_id,
+ },
authority_gateway_id=room.gateway_id, authority_epoch=room.authority_epoch))
return {"message_event_id": None if passed else message_event_id, "passed": passed}, effects
diff --git a/gateway/hosted_room_driver.py b/gateway/hosted_room_driver.py
index a38c1da5fce..739cb0d85df 100644
--- a/gateway/hosted_room_driver.py
+++ b/gateway/hosted_room_driver.py
@@ -87,8 +87,7 @@ _COMPLETE_STOP_SQL = _task_update(
# row stale message); the UPDATE is _generation_update(set_clause, status).
_INDETERMINATE_STALE = "indeterminate task generation changed"
_GENERATION_TRANSITIONS = {
- "resolve": (
- "indeterminate", _SETTLE_SET, _INDETERMINATE_STALE, "indeterminate task changed during reconciliation"),
+ "resolve": ("indeterminate", _SETTLE_SET, _INDETERMINATE_STALE, "indeterminate task changed during reconciliation"),
"resolve_cancel": (
"indeterminate", _CANCEL_SET, "indeterminate cancellation proof is stale",
"indeterminate cancellation proof lost its fence"),
diff --git a/gateway/hosted_room_links.py b/gateway/hosted_room_links.py
index 7daf74cf7b7..7658b6be9c4 100644
--- a/gateway/hosted_room_links.py
+++ b/gateway/hosted_room_links.py
@@ -160,14 +160,7 @@ def make_stored_link(
catalog: GatewayRoomCatalog, cancellation_scope_id: str, trace_id: str) -> StoredRoomLink:
target_url, transport_security = validate_room_link_url(target_url)
return StoredRoomLink.from_mapping({
- "room_id": room_id,
- "member_id": member_id,
- "target_url": target_url,
- "target_profile": target_profile,
- "grant": grant,
- "catalog": catalog.as_mapping(),
- "cancellation_scope_id": cancellation_scope_id,
- "trace_id": trace_id,
- "transport_security": transport_security,
- "status": "ready",
- "updated_at": time.time()})
+ "room_id": room_id, "member_id": member_id, "target_url": target_url, "target_profile": target_profile,
+ "grant": grant, "catalog": catalog.as_mapping(), "cancellation_scope_id": cancellation_scope_id,
+ "trace_id": trace_id, "transport_security": transport_security, "status": "ready", "updated_at": time.time(),
+ })
diff --git a/gateway/hosted_room_peer.py b/gateway/hosted_room_peer.py
index 3b641f1f0b5..4d89d47988b 100644
--- a/gateway/hosted_room_peer.py
+++ b/gateway/hosted_room_peer.py
@@ -281,8 +281,7 @@ def catalog_mapping(
raise HostedRoomPeerError("remote room execution requires manual or smart approvals")
value = {
"installation_id": _identifier(installation_id, field="installation_id"),
- "protocol_versions": sorted(
- {_positive_int(item, field="protocol_version") for item in protocol_versions}),
+ "protocol_versions": sorted({_positive_int(item, field="protocol_version") for item in protocol_versions}),
# Direct HTTPS/loopback is the only RoomLink transport implemented by
# this backend slice. Do not advertise pull/relay placeholders.
"link_modes": [mode for mode in dict.fromkeys(link_modes) if mode == "direct"],
diff --git a/gateway/hosted_room_policy_checkpoint.py b/gateway/hosted_room_policy_checkpoint.py
index 3106a922348..89dad1d4bb6 100644
--- a/gateway/hosted_room_policy_checkpoint.py
+++ b/gateway/hosted_room_policy_checkpoint.py
@@ -264,10 +264,8 @@ class HostedRoomPolicyCheckpoint:
(int(event["seq"]), str(event["room_id"])))
_APPLY_BY_KIND: dict[str, Callable[..., None]] = {
- "message.user": _apply_user_message,
- "message.member": _apply_discussion_event,
- **dict.fromkeys(_TERMINAL_KINDS, _apply_discussion_event),
- "room.activity": _apply_room_activity,
+ "message.user": _apply_user_message, "message.member": _apply_discussion_event,
+ **dict.fromkeys(_TERMINAL_KINDS, _apply_discussion_event), "room.activity": _apply_room_activity,
"room.stop_requested": _apply_stop_requested,
}
diff --git a/gateway/hosted_room_replicas.py b/gateway/hosted_room_replicas.py
index a26035b72a6..6dda485634e 100644
--- a/gateway/hosted_room_replicas.py
+++ b/gateway/hosted_room_replicas.py
@@ -268,11 +268,9 @@ def promote_replica(
claim_seq = int(replica["last_seq"]) + 1
claim_event_id = f"system:authority-claimed:{target_epoch}"
claim_actor_json, claim_payload_json = _control_event_json({
- "previous_gateway_id": previous_gateway,
- "authority_gateway_id": local_gateway,
- "authority_epoch": target_epoch,
- "promoted_from_replica": True,
- "reason": reason})
+ "previous_gateway_id": previous_gateway, "authority_gateway_id": local_gateway,
+ "authority_epoch": target_epoch, "promoted_from_replica": True, "reason": reason,
+ })
claim_bytes = utf8_len(claim_event_id, "authority.claimed", claim_actor_json, claim_payload_json)
conn.execute(
diff --git a/gateway/hosted_rooms.py b/gateway/hosted_rooms.py
index 2378bd82a0f..01e0c34e783 100644
--- a/gateway/hosted_rooms.py
+++ b/gateway/hosted_rooms.py
@@ -397,16 +397,12 @@ _LEGACY_COLUMN_DDL = (
(
"hosted_rooms", "authority_epoch",
"ALTER TABLE hosted_rooms ADD COLUMN authority_epoch INTEGER NOT NULL DEFAULT 1"),
- (
- "hosted_rooms", "event_bytes",
- "ALTER TABLE hosted_rooms ADD COLUMN event_bytes INTEGER NOT NULL DEFAULT 0"),
+ ("hosted_rooms", "event_bytes", "ALTER TABLE hosted_rooms ADD COLUMN event_bytes INTEGER NOT NULL DEFAULT 0"),
(
"hosted_room_events", "actor_json",
"ALTER TABLE hosted_room_events "
f"ADD COLUMN actor_json TEXT NOT NULL DEFAULT '{_LEGACY_ACTOR_JSON}'"),
- (
- "hosted_room_events", "authority_epoch",
- "ALTER TABLE hosted_room_events ADD COLUMN authority_epoch INTEGER"))
+ ("hosted_room_events", "authority_epoch", "ALTER TABLE hosted_room_events ADD COLUMN authority_epoch INTEGER"))
def _migrate_legacy_columns(conn: sqlite3.Connection) -> None:
@@ -764,11 +760,10 @@ def revoke_room_grant_scope(
AND member_id=? AND target_profile=? AND authority_gateway_id=?
AND authority_epoch=?""",
(
- timestamp, timestamp,
- str(claims.get("room_id") or ""), str(claims.get("member_id") or ""),
- str(claims.get("target_profile") or ""),
- str(claims.get("authority_gateway_id") or ""),
- int(claims.get("authority_epoch") or 0)))
+ timestamp, timestamp, str(claims.get("room_id") or ""), str(claims.get("member_id") or ""),
+ str(claims.get("target_profile") or ""), str(claims.get("authority_gateway_id") or ""),
+ int(claims.get("authority_epoch") or 0),
+ ))
def _reservation_claims(claims: Mapping[str, Any]) -> tuple[str, str, str, str, int]:
@@ -897,8 +892,7 @@ def list_remote_run_receipts(
"""Return remote run handles in durable task order."""
filters = [
(column, value)
- for column, value in (
- ("room_id", room_id), ("target_profile", target_profile), ("session_id", session_id))
+ for column, value in (("room_id", room_id), ("target_profile", target_profile), ("session_id", session_id))
if value is not None]
where = f" WHERE {' AND '.join(f'{column}=?' for column, _ in filters)}" if filters else ""
with _transaction(db_path) as conn: