Skip to content

The agent that grows with you

refactor(kanban_db_notify): share sub-key predicate/CAS helper, collapse filter loops (467->426 LOC)

Tekniume1b85ab6629d
diff --git a/hermes_cli/kanban_db_notify.py b/hermes_cli/kanban_db_notify.py
index 7a5151fd0ce..077fad8e0ee 100644
--- a/hermes_cli/kanban_db_notify.py
+++ b/hermes_cli/kanban_db_notify.py
@@ -21,14 +21,20 @@ if TYPE_CHECKING:
from hermes_cli.kanban_db import Event
-# ---------------------------------------------------------------------------
-# Notification subscriptions (used by the gateway kanban-notifier)
-# ---------------------------------------------------------------------------
-
# Notifier reaction to a terminal event: "notify" = passive adapter.send only
# (default); "notify+wake" = send AND wake the destination agent; "wake" = wake only.
_NOTIFY_DELIVERY_MODES = ("notify", "notify+wake", "wake")
+_SCALAR_TYPES = (str, int, float, bool)
+
+# Subscription primary key predicate; every per-row statement below binds
+# ``(task_id, platform, chat_id, thread_id or "")`` against it.
+_SUB_KEY_WHERE = "WHERE task_id = ? AND platform = ? AND chat_id = ? AND thread_id = ?"
+
+
+def _sub_key(task_id: str, platform: str, chat_id: str, thread_id: Optional[str]) -> tuple:
+ return (task_id, platform, chat_id, thread_id or "")
+
def _encode_notify_delivery_metadata(
metadata: Optional[Mapping[str, Any]],
@@ -36,12 +42,11 @@ def _encode_notify_delivery_metadata(
"""Serialize platform send metadata stored on notification subscriptions."""
if not isinstance(metadata, Mapping):
return None
- clean: dict[str, Any] = {}
- for key, value in metadata.items():
- if value is None:
- continue
- if isinstance(value, (str, int, float, bool)):
- clean[str(key)] = value
+ clean = {
+ str(key): value
+ for key, value in metadata.items()
+ if value is not None and isinstance(value, _SCALAR_TYPES)
+ }
if not clean:
return None
return json.dumps(clean, sort_keys=True, separators=(",", ":"))
@@ -58,11 +63,7 @@ def _decode_notify_delivery_metadata(raw: Any) -> dict[str, Any]:
return {}
if not isinstance(data, dict):
return {}
- return {
- str(key): value
- for key, value in data.items()
- if isinstance(value, (str, int, float, bool))
- }
+ return {str(key): value for key, value in data.items() if isinstance(value, _SCALAR_TYPES)}
def add_notify_sub(
@@ -95,15 +96,13 @@ def add_notify_sub(
current ``MAX(task_events.id)``, not 0) — otherwise the notifier replays
every historical terminal event on its next tick (boot-time bursts).
"""
- insert_mode = delivery_mode if delivery_mode in _NOTIFY_DELIVERY_MODES else (
- # api_server is stateless: the adapter has no send(), the wake self-post
- # IS the delivery. A plain 'notify' default would leave those subs with
- # no delivery mechanism at all. Explicit modes still win.
- "notify+wake" if platform == "api_server" else "notify"
- )
- insert_chat_type = chat_type or "dm"
- now = int(time.time())
+ valid_mode = delivery_mode if delivery_mode in _NOTIFY_DELIVERY_MODES else None
+ # api_server is stateless: the adapter has no send(), the wake self-post IS
+ # the delivery. A plain 'notify' default would leave those subs with no
+ # delivery mechanism at all. Explicit modes still win.
+ insert_mode = valid_mode or ("notify+wake" if platform == "api_server" else "notify")
metadata_json = _encode_notify_delivery_metadata(delivery_metadata)
+ key = _sub_key(task_id, platform, chat_id, thread_id)
with _kb.write_txn(conn):
conn.execute(
"""
@@ -115,37 +114,24 @@ def add_notify_sub(
COALESCE((SELECT MAX(id) FROM task_events WHERE task_id = ?), 0))
""",
(
- task_id,
- platform,
- chat_id,
- thread_id or "",
- user_id,
- user_id_alt,
- insert_chat_type,
- notifier_profile,
- insert_mode,
- metadata_json,
- now,
- task_id,
+ *key, user_id, user_id_alt, chat_type or "dm", notifier_profile,
+ insert_mode, metadata_json, int(time.time()), task_id,
),
)
# chat_type / delivery_mode / delivery_metadata are last-write-wins;
# user_id_alt and notifier_profile only self-heal legacy rows lacking one.
- key = (task_id, platform, chat_id, thread_id or "")
for column, value, fill_only in (
("chat_type", chat_type, False),
("user_id_alt", user_id_alt, True),
("notifier_profile", notifier_profile, True),
- ("delivery_mode", delivery_mode if delivery_mode in _NOTIFY_DELIVERY_MODES else None, False),
+ ("delivery_mode", valid_mode, False),
("delivery_metadata", metadata_json, False),
):
if not value:
continue
guard = f" AND ({column} IS NULL OR {column} = '')" if fill_only else ""
conn.execute(
- f"UPDATE kanban_notify_subs SET {column} = ? "
- "WHERE task_id = ? AND platform = ? AND chat_id = ? AND thread_id = ?"
- + guard,
+ f"UPDATE kanban_notify_subs SET {column} = ? " + _SUB_KEY_WHERE + guard,
(value, *key),
)
@@ -159,19 +145,11 @@ def _notify_profile_filter(
if notifier_profiles is None:
return "", []
- profiles = sorted(
- {
- str(profile).strip()
- for profile in notifier_profiles
- if str(profile).strip()
- }
- )
+ profiles = sorted({str(p).strip() for p in notifier_profiles if str(p).strip()})
clauses: list[str] = []
params: list[str] = []
if profiles:
- clauses.append(
- "notifier_profile IN (" + ",".join("?" for _ in profiles) + ")"
- )
+ clauses.append("notifier_profile IN (" + ",".join("?" for _ in profiles) + ")")
params.extend(profiles)
if include_unowned:
clauses.append("notifier_profile IS NULL OR notifier_profile = ''")
@@ -207,14 +185,11 @@ def list_notify_subs(
sql = "SELECT * FROM kanban_notify_subs"
if where:
sql += " WHERE " + " AND ".join(f"({clause})" for clause in where)
- rows = conn.execute(sql, params).fetchall()
out: list[dict] = []
- for row in rows:
+ for row in conn.execute(sql, params).fetchall():
item = dict(row)
if "delivery_metadata" in item:
- item["delivery_metadata"] = _decode_notify_delivery_metadata(
- item.get("delivery_metadata")
- )
+ item["delivery_metadata"] = _decode_notify_delivery_metadata(item.get("delivery_metadata"))
out.append(item)
return out
@@ -244,29 +219,28 @@ def count_notify_subs(
path = db_path if db_path is not None else _kb.kanban_db_path(board=board)
if not path.exists():
return 0
+ owner_where, owner_params = _notify_profile_filter(
+ notifier_profiles, include_unowned=include_unowned,
+ )
+ clauses: list[str] = []
+ params: list[Any] = []
+ if owner_where:
+ clauses.append(f"({owner_where})")
+ params.extend(owner_params)
+ for clause, value in (
+ ("LOWER(platform) = LOWER(?)", platform),
+ ("chat_id = ?", chat_id),
+ ("thread_id = ?", thread_id),
+ ):
+ if value is not None:
+ clauses.append(clause)
+ params.append(value)
+ query = "SELECT COUNT(*) FROM kanban_notify_subs"
+ if clauses:
+ query += " WHERE " + " AND ".join(clauses)
conn = sqlite3.connect(path.resolve().as_uri() + "?mode=ro", uri=True)
try:
try:
- owner_where, owner_params = _notify_profile_filter(
- notifier_profiles, include_unowned=include_unowned,
- )
- clauses: list[str] = []
- params: list[Any] = []
- if owner_where:
- clauses.append(f"({owner_where})")
- params.extend(owner_params)
- if platform is not None:
- clauses.append("LOWER(platform) = LOWER(?)")
- params.append(platform)
- if chat_id is not None:
- clauses.append("chat_id = ?")
- params.append(chat_id)
- if thread_id is not None:
- clauses.append("thread_id = ?")
- params.append(thread_id)
- query = "SELECT COUNT(*) FROM kanban_notify_subs"
- if clauses:
- query += " WHERE " + " AND ".join(clauses)
row = conn.execute(query, params).fetchone()
except sqlite3.OperationalError as exc:
if "no such table" in str(exc).lower():
@@ -287,9 +261,8 @@ def remove_notify_sub(
) -> bool:
with _kb.write_txn(conn):
cur = conn.execute(
- "DELETE FROM kanban_notify_subs WHERE task_id = ? "
- "AND platform = ? AND chat_id = ? AND thread_id = ?",
- (task_id, platform, chat_id, thread_id or ""),
+ "DELETE FROM kanban_notify_subs " + _SUB_KEY_WHERE,
+ _sub_key(task_id, platform, chat_id, thread_id),
)
return cur.rowcount > 0
@@ -338,9 +311,8 @@ def _notify_cursor(
) -> Optional[int]:
"""``last_event_id`` of one subscription row, or ``None`` when unsubscribed."""
row = conn.execute(
- "SELECT last_event_id FROM kanban_notify_subs "
- "WHERE task_id = ? AND platform = ? AND chat_id = ? AND thread_id = ?",
- (task_id, platform, chat_id, thread_id or ""),
+ "SELECT last_event_id FROM kanban_notify_subs " + _SUB_KEY_WHERE,
+ _sub_key(task_id, platform, chat_id, thread_id),
).fetchone()
return None if row is None else int(row["last_event_id"])
@@ -370,11 +342,8 @@ def unseen_events_for_sub(
if kind_list:
params.extend(kind_list)
rows = conn.execute(q, params).fetchall()
- out: list[Event] = []
- max_id = cursor
- for r in rows:
- out.append(_kb.Event.from_row(r))
- max_id = max(max_id, int(r["id"]))
+ out = [_kb.Event.from_row(r) for r in rows]
+ max_id = max([cursor, *(int(r["id"]) for r in rows)])
return max_id, out
@@ -401,24 +370,23 @@ def claim_unseen_events_for_sub(
if old_cursor is None:
return 0, 0, []
new_cursor, events = unseen_events_for_sub(
- conn,
- task_id=task_id,
- platform=platform,
- chat_id=chat_id,
- thread_id=thread_id,
- kinds=kinds,
+ conn, task_id=task_id, platform=platform, chat_id=chat_id,
+ thread_id=thread_id, kinds=kinds,
)
if not events:
return old_cursor, old_cursor, []
- conn.execute(
- "UPDATE kanban_notify_subs SET last_event_id = ? "
- "WHERE task_id = ? AND platform = ? AND chat_id = ? AND thread_id = ? "
- "AND last_event_id = ?",
- (int(new_cursor), task_id, platform, chat_id, thread_id or "", int(old_cursor)),
- )
+ _cas_cursor(conn, _sub_key(task_id, platform, chat_id, thread_id), new_cursor, old_cursor)
return old_cursor, new_cursor, events
+def _cas_cursor(conn: sqlite3.Connection, key: tuple, new_cursor: int, expected: int) -> sqlite3.Cursor:
+ """Move ``last_event_id`` only if it still equals ``expected``."""
+ return conn.execute(
+ "UPDATE kanban_notify_subs SET last_event_id = ? " + _SUB_KEY_WHERE + " AND last_event_id = ?",
+ (int(new_cursor), *key, int(expected)),
+ )
+
+
def advance_notify_cursor(
conn: sqlite3.Connection,
*,
@@ -430,9 +398,8 @@ def advance_notify_cursor(
) -> None:
with _kb.write_txn(conn):
conn.execute(
- "UPDATE kanban_notify_subs SET last_event_id = ? "
- "WHERE task_id = ? AND platform = ? AND chat_id = ? AND thread_id = ?",
- (int(new_cursor), task_id, platform, chat_id, thread_id or ""),
+ "UPDATE kanban_notify_subs SET last_event_id = ? " + _SUB_KEY_WHERE,
+ (int(new_cursor), *_sub_key(task_id, platform, chat_id, thread_id)),
)
@@ -450,18 +417,10 @@ def rewind_notify_cursor(
notifier advanced the row, so retries never clobber newer progress.
"""
with _kb.write_txn(conn):
- cur = conn.execute(
- "UPDATE kanban_notify_subs SET last_event_id = ? "
- "WHERE task_id = ? AND platform = ? AND chat_id = ? AND thread_id = ? "
- "AND last_event_id = ?",
- (
- int(old_cursor), task_id, platform, chat_id, thread_id or "",
- int(claimed_cursor),
- ),
- )
+ cur = _cas_cursor(conn, _sub_key(task_id, platform, chat_id, thread_id), old_cursor, claimed_cursor)
return cur.rowcount > 0
-# Late-bound origin namespace (see module docstring). Imported LAST so this
+# Late-bound origin namespace (see module docstring); imported LAST so this
# module is fully populated before ``kanban_db`` re-exports from it.
from hermes_cli import kanban_db as _kb # noqa: E402