refactor(gateway): pack dict/list literal statements in hosted-room modules (AST-neutral)

This commit is contained in:
Teknium
2026-09-02 18:34:23 -07:00
parent aa6365a022
commit fdc6beefcd
7 changed files with 59 additions and 91 deletions
+16 -24
View File
@@ -57,10 +57,9 @@ _TURN_COORDINATE_FIELDS = frozenset(
_MEMBER_MESSAGE_FIELDS = _TURN_COORDINATE_FIELDS | {"text"}
_TERMINAL_COMMON_FIELDS = _TURN_COORDINATE_FIELDS | {"seen_through_seq"}
_TERMINAL_EXTRA_FIELDS = {
"turn.settled": frozenset({"message_event_id", "passed"}),
"turn.failed": frozenset({"error"}),
"turn.cancelled": frozenset({"reason"}),
"turn.deferred": frozenset({"execution_generation", "reason"})}
"turn.settled": frozenset({"message_event_id", "passed"}), "turn.failed": frozenset({"error"}),
"turn.cancelled": frozenset({"reason"}), "turn.deferred": frozenset({"execution_generation", "reason"}),
}
_TERMINAL_OPTIONAL_FIELDS = {"turn.failed": frozenset({"reason_code"})}
_TERMINAL_EVENT_KINDS = frozenset(_TERMINAL_EXTRA_FIELDS)
# Gateway-authored control events: kind -> (exact payload fields, identifier fields).
@@ -396,10 +395,9 @@ def _validate_member_message(
member = _member_by_id(room, payload.get("member_id"))
peer = _peer_target(member)
expected = {
"kind": "member",
"id": member.member_id,
"profile": member.profile,
"connection_id": peer.get("peer_id") if peer else None}
"kind": "member", "id": member.member_id, "profile": member.profile,
"connection_id": peer.get("peer_id") if peer else None,
}
if any(actor.get(key) != value for key, value in expected.items()):
raise DiscussionValidationError("message.member actor does not match roster")
return payload
@@ -634,10 +632,9 @@ def _make_task_plan(
room_id=room.room_id, task_id=task_id,
thread_id=str(discussion_event.payload["thread_id"]), turn_id=turn_id)
payload = {
"target_member_id": member.member_id,
"target_profile": member.profile,
"prompt": prompt,
"source_event_seq": discussion_event.seq}
"target_member_id": member.member_id, "target_profile": member.profile, "prompt": prompt,
"source_event_seq": discussion_event.seq,
}
return DiscussionTaskPlan(
identity=identity, payload=payload, discussion_event_id=discussion_event.event_id, member=member,
member_index=member_index, round_index=round_index, seen_through_seq=seen_through_seq)
@@ -883,10 +880,9 @@ def _deferred_effects(
_TERMINAL_EFFECTS = {
"settled": _settled_effects,
"failed": _failed_effects,
"cancelled": _cancelled_effects,
"deferred": _deferred_effects}
"settled": _settled_effects, "failed": _failed_effects, "cancelled": _cancelled_effects,
"deferred": _deferred_effects,
}
def plan_publication(
@@ -921,14 +917,10 @@ def plan_publication(
f"ddeferred:{digest}:g{execution_generation}"
if effective_status == "deferred" else f"dterminal:{digest}")
common_payload = {
"discussion_event_id": task.discussion_event_id,
"member_id": task.member.member_id,
"member_index": task.member_index,
"round_index": task.round_index,
"seen_through_seq": task.seen_through_seq,
"task_id": task.identity.task_id,
"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, "seen_through_seq": task.seen_through_seq,
"task_id": task.identity.task_id, "thread_id": task.identity.thread_id, "turn_id": task.identity.turn_id,
}
extra, effects = _TERMINAL_EFFECTS[effective_status](
result, task=task, room=room, message_event_id=f"dmessage:{digest}",
newer_same_thread=newer_same_thread, execution_generation=execution_generation)
+2 -1
View File
@@ -354,7 +354,8 @@ def _optional(cast: Callable[[Any], Any]) -> Callable[[Any], Any]:
_TASK_VIEW_CASTS: dict[str, Callable[[Any], Any]] = {
"execution_generation": int, "cancel_generation": int, "run_lease_generation": _optional(int),
"result_json": _optional(json.loads), "created_at": float, "updated_at": float,
"started_at": _optional(float), "terminal_at": _optional(float), "indeterminate_at": _optional(float)}
"started_at": _optional(float), "terminal_at": _optional(float), "indeterminate_at": _optional(float),
}
def _task_from_row(row: sqlite3.Row, *, idempotent: bool = False) -> dict[str, Any]:
+11 -18
View File
@@ -68,11 +68,9 @@ class RoomExecutionPolicy:
or not 1 <= max_iterations <= MAX_POLICY_ITERATIONS):
raise RoomExecutionPolicyError("max_iterations is invalid")
unsigned = {
"version": POLICY_VERSION,
"target_profile": target_profile,
"enabled_toolsets": list(toolsets),
"approval_mode": approval_mode,
"max_iterations": max_iterations}
"version": POLICY_VERSION, "target_profile": target_profile, "enabled_toolsets": list(toolsets),
"approval_mode": approval_mode, "max_iterations": max_iterations,
}
supplied = str(value["policy_digest"] or "").strip().lower()
if supplied != _policy_digest(unsigned):
raise RoomExecutionPolicyError("policy_digest does not match the execution policy")
@@ -99,12 +97,11 @@ def execution_policy_mapping(*, target_profile: str, config: Mapping[str, Any] |
agent = config.get("agent") if isinstance(config.get("agent"), Mapping) else {}
approvals = config.get("approvals") if isinstance(config.get("approvals"), Mapping) else {}
unsigned = {
"version": POLICY_VERSION,
"target_profile": _identifier(target_profile, field="target_profile"),
"version": POLICY_VERSION, "target_profile": _identifier(target_profile, field="target_profile"),
"enabled_toolsets": toolsets,
"approval_mode": (
"off" if _YOLO_MODE_FROZEN else _normalize_approval_mode(approvals.get("mode", "manual"))),
"max_iterations": min(resolve_turn_limit(agent.get("max_turns")), MAX_POLICY_ITERATIONS)}
"approval_mode": ("off" if _YOLO_MODE_FROZEN else _normalize_approval_mode(approvals.get("mode", "manual"))),
"max_iterations": min(resolve_turn_limit(agent.get("max_turns")), MAX_POLICY_ITERATIONS),
}
value = {**unsigned, "policy_digest": _policy_digest(unsigned)}
return RoomExecutionPolicy.from_mapping(value).as_mapping()
@@ -125,11 +122,7 @@ def current_room_execution_policy() -> RoomExecutionPolicy | None:
__all__ = [
"MAX_POLICY_ITERATIONS",
"POLICY_VERSION",
"RoomExecutionPolicy",
"RoomExecutionPolicyError",
"bind_room_execution_policy",
"current_room_execution_policy",
"execution_policy_mapping",
"reset_room_execution_policy"]
"MAX_POLICY_ITERATIONS", "POLICY_VERSION", "RoomExecutionPolicy", "RoomExecutionPolicyError",
"bind_room_execution_policy", "current_room_execution_policy", "execution_policy_mapping",
"reset_room_execution_policy",
]
+2 -1
View File
@@ -393,7 +393,8 @@ _DISPATCH_FIELDS: dict[str, Callable[..., Any]] = dict(
authority_gateway_id=_identifier, authority_epoch=_positive_int, member_id=_identifier,
target_install_id=_identifier, target_profile=_identifier, task_id=_identifier,
execution_generation=_positive_int, source_event_seq=_positive_int, cancellation_scope_id=_identifier,
capability_digest=_digest, execution_policy_digest=_digest, trace_id=_identifier)
capability_digest=_digest, execution_policy_digest=_digest, trace_id=_identifier,
)
@dataclass(frozen=True)
+2 -1
View File
@@ -269,7 +269,8 @@ class HostedRoomPolicyCheckpoint:
"message.member": _apply_discussion_event,
**dict.fromkeys(_TERMINAL_KINDS, _apply_discussion_event),
"room.activity": _apply_room_activity,
"room.stop_requested": _apply_stop_requested}
"room.stop_requested": _apply_stop_requested,
}
def _apply_event(self, conn: sqlite3.Connection, event: Mapping[str, Any]) -> None:
handler = self._APPLY_BY_KIND.get(str(event.get("kind") or ""))
+17 -28
View File
@@ -217,11 +217,9 @@ def ingest_page(
conn, is_new=row is None, room_id=room_id, room_name=room_name, members_json=members_json,
authority=authority, new_last=new_last, latest_seq=latest_seq, added_bytes=added_bytes, now=now)
return {
"room_id": room_id,
"stored_seq": new_last,
"ingested": len(new_events),
"authority": authority,
"caught_up": new_last >= max(latest_seq, new_last)}
"room_id": room_id, "stored_seq": new_last, "ingested": len(new_events), "authority": authority,
"caught_up": new_last >= max(latest_seq, new_last),
}
def replica_state(db_path: Path | str, *, room_id: Any) -> dict[str, Any]:
@@ -232,15 +230,11 @@ def replica_state(db_path: Path | str, *, room_id: Any) -> dict[str, Any]:
if row is None:
raise ReplicaError("replica not found")
return {
"room_id": row["room_id"],
"name": row["name"],
"members": json.loads(row["members_json"]),
"room_id": row["room_id"], "name": row["name"], "members": json.loads(row["members_json"]),
"authority": {"gateway_id": row["authority_gateway_id"], "epoch": int(row["authority_epoch"])},
"last_seq": int(row["last_seq"]),
"latest_seq": int(row["latest_seq"]),
"event_bytes": int(row["event_bytes"]),
"created_at": float(row["created_at"]),
"updated_at": float(row["updated_at"])}
"last_seq": int(row["last_seq"]), "latest_seq": int(row["latest_seq"]), "event_bytes": int(row["event_bytes"]),
"created_at": float(row["created_at"]), "updated_at": float(row["updated_at"]),
}
def promote_replica(
@@ -304,13 +298,10 @@ def promote_replica(
conn.execute("DELETE FROM hosted_room_replica_events WHERE room_id=?", (room_id,))
conn.execute("DELETE FROM hosted_room_replicas WHERE room_id=?", (room_id,))
return {
"room_id": room_id,
"authority_gateway_id": local_gateway,
"authority_epoch": target_epoch,
"previous_gateway_id": previous_gateway,
"previous_epoch": previous_epoch,
"claim_seq": claim_seq,
"latest_seq": claim_seq}
"room_id": room_id, "authority_gateway_id": local_gateway, "authority_epoch": target_epoch,
"previous_gateway_id": previous_gateway, "previous_epoch": previous_epoch, "claim_seq": claim_seq,
"latest_seq": claim_seq,
}
def demote_room(
@@ -341,10 +332,9 @@ def demote_room(
current_epoch = int(row["authority_epoch"])
if current_gateway == observed_gateway_id and current_epoch == observed_epoch:
return {
"room_id": room_id,
"authority_gateway_id": current_gateway,
"authority_epoch": current_epoch,
"idempotent": True}
"room_id": room_id, "authority_gateway_id": current_gateway, "authority_epoch": current_epoch,
"idempotent": True,
}
if observed_epoch <= current_epoch:
raise ReplicaEpochRegressionError("observed epoch does not supersede the stored authority")
if current_gateway != local_gateway:
@@ -364,7 +354,6 @@ def demote_room(
WHERE room_id=?""",
(observed_gateway_id, observed_epoch, now, room_id))
return {
"room_id": room_id,
"authority_gateway_id": observed_gateway_id,
"authority_epoch": observed_epoch,
"idempotent": False}
"room_id": room_id, "authority_gateway_id": observed_gateway_id, "authority_epoch": observed_epoch,
"idempotent": False,
}
+9 -18
View File
@@ -528,15 +528,11 @@ def _reload(conn: sqlite3.Connection, sql: str, params: tuple, missing: str) ->
def _room_from_row(row: sqlite3.Row, *, idempotent: bool = False) -> dict[str, Any]:
room = {
"room_id": row["room_id"],
"name": row["name"],
"members": json.loads(row["members_json"]),
"authority_gateway_id": row["authority_gateway_id"],
"authority_epoch": int(row["authority_epoch"]),
"revision": int(row["revision"]),
"created_at": float(row["created_at"]),
"updated_at": float(row["updated_at"]),
"idempotent": idempotent}
"room_id": row["room_id"], "name": row["name"], "members": json.loads(row["members_json"]),
"authority_gateway_id": row["authority_gateway_id"], "authority_epoch": int(row["authority_epoch"]),
"revision": int(row["revision"]), "created_at": float(row["created_at"]),
"updated_at": float(row["updated_at"]), "idempotent": idempotent,
}
# sqlite3.Row: ``x in row`` scans values, so ``.keys()`` is load-bearing.
if "disbanded_at" in row.keys() and row["disbanded_at"] is not None: # noqa: SIM118
room["disbanded_at"] = float(row["disbanded_at"])
@@ -548,15 +544,10 @@ def _room_from_row(row: sqlite3.Row, *, idempotent: bool = False) -> dict[str, A
def _event_from_row(row: sqlite3.Row, *, idempotent: bool = False) -> dict[str, Any]:
epoch = row["authority_epoch"]
return {
"room_id": row["room_id"],
"seq": int(row["seq"]),
"event_id": row["event_id"],
"kind": row["kind"],
"actor": json.loads(row["actor_json"]),
"authority_epoch": int(epoch) if epoch is not None else None,
"payload": json.loads(row["payload_json"]),
"created_at": float(row["created_at"]),
"idempotent": idempotent}
"room_id": row["room_id"], "seq": int(row["seq"]), "event_id": row["event_id"], "kind": row["kind"],
"actor": json.loads(row["actor_json"]), "authority_epoch": int(epoch) if epoch is not None else None,
"payload": json.loads(row["payload_json"]), "created_at": float(row["created_at"]), "idempotent": idempotent,
}
def _load_event(conn: sqlite3.Connection, room_id: str, event_id: str) -> sqlite3.Row | None: