Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Keep the re-check fingerprint in its own module, for the other re-checks to use

Abel Dantasff58a73d1542
diff --git a/core/switch_core/db/fingerprint.py b/core/switch_core/db/fingerprint.py
new file mode 100644
index 000000000..cb9560b4e
--- /dev/null
+++ b/core/switch_core/db/fingerprint.py
@@ -0,0 +1,40 @@
+"""What a periodic re-check compares before reading a whole table again.
+
+A re-check exists for the signal that never arrived, and on a quiet
+deployment it finds nothing to repair. Reading every row to learn that costs
+the same on every process every time, so a re-check first asks the database
+for a fingerprint of the rows it would read: their count and the sum of a
+60-bit hash of the columns it acts on. Equal fingerprints mean no row it acts
+on was added, removed or changed since the read the stored one came from.
+Computed by the database, so asking costs one row however many there are.
+"""
+
+from __future__ import annotations
+
+from typing import Any
+
+from sqlalchemy import (
+ BigInteger,
+ ColumnElement,
+ SQLColumnExpression,
+ cast,
+ func,
+ literal,
+)
+from sqlalchemy.dialects.postgresql import BIT
+
+# A row count and the sum of the rows' hashes.
+Fingerprint = tuple[int, int]
+
+_FIELD_SEPARATOR = "\x1f"
+
+
+def row_hash(*fields: SQLColumnExpression[Any]) -> ColumnElement[int]:
+ """The first 15 hex digits of the md5 of `fields`, read as a 60-bit
+ number. A NULL field hashes as the empty string; pass `coalesce` where
+ NULL and empty must differ."""
+ joined = func.concat_ws(_FIELD_SEPARATOR, *fields)
+ return cast(
+ cast(literal("x") + func.substr(func.md5(joined), 1, 15), BIT(60)),
+ BigInteger,
+ )
diff --git a/core/switch_core/db/stores/client_store.py b/core/switch_core/db/stores/client_store.py
index 774b99506..12e59db6f 100644
--- a/core/switch_core/db/stores/client_store.py
+++ b/core/switch_core/db/stores/client_store.py
@@ -1,25 +1,17 @@
from collections.abc import Collection
-from sqlalchemy import BigInteger, ColumnElement, cast, delete, func, literal, select
-from sqlalchemy.dialects.postgresql import BIT
+from sqlalchemy import ColumnElement, delete, func, select
from sqlalchemy.ext.asyncio import AsyncSession
+from switch_core.db.fingerprint import Fingerprint, row_hash
from switch_core.db.models import Agent, Client, ClientRoom, require_tenant_id
-# How many clients a tenant runs and what they are, as one pair of numbers:
-# the count, and the sum of a 60-bit hash of each client's id, type and name
-# and of its agent's id, name, display name and description. Equal pairs mean
-# no client was added, removed or renamed and no agent was added, removed or
-# re-described in between. Computed by the database, so reading it costs one
-# row however many clients the tenant has.
-Fingerprint = tuple[int, int]
-
-_FIELD_SEPARATOR = "\x1f"
-
def _row_hash() -> ColumnElement[int]:
- fields = func.concat_ws(
- _FIELD_SEPARATOR,
+ """A client's id, type and name, and its agent's id, name, display name
+ and description: what a process running the client, and a bridge holding
+ the agent's platform handle, act on (`ClientStore.fingerprint`)."""
+ return row_hash(
Client.id,
Client.type,
Client.display_name,
@@ -28,11 +20,6 @@ def _row_hash() -> ColumnElement[int]:
func.coalesce(Agent.display_name, ""),
func.coalesce(Agent.description, ""),
)
- # The first 15 hex digits of the md5, read as a 60-bit number.
- return cast(
- cast(literal("x") + func.substr(func.md5(fields), 1, 15), BIT(60)),
- BigInteger,
- )
class ClientStore: