Skip to content

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

feat(core): let switch-core report its own release version (CHOO-1865)

Louis Amaudruz11acd012f558
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index 95f87daab..a2542a5b3 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -86,6 +86,7 @@ from switch_core.matrix_admin import (
wait_for_homeserver,
)
from switch_core.room_service import RoomService
+from switch_core.version import switch_core_version
logger = logging.getLogger(__name__)
@@ -539,6 +540,9 @@ def main() -> None:
logging.getLogger().setLevel(log_level)
logging.getLogger("switch_core").setLevel(switch_log_level)
+ running_version = switch_core_version()
+ logger.info("Starting switch-core %s", running_version or "(version unknown)")
+
logger.info("Database migrations applied")
asyncio.run(run())
diff --git a/core/switch_core/version.py b/core/switch_core/version.py
new file mode 100644
index 000000000..9650a55dc
--- /dev/null
+++ b/core/switch_core/version.py
@@ -0,0 +1,47 @@
+"""switch-core's own release version (CHOO-1865).
+
+The version is declared once, in `core/pyproject.toml`, and reaches the running
+process through installed distribution metadata. There is deliberately no
+second copy: a `__version__` constant beside pyproject would be one more thing
+to keep in step by hand, which is the drift this ticket exists to end.
+
+An artifact's semver says *where it is* — which release you are running. It
+says nothing about whether it can talk to anything; that is what the contract
+revisions in `switch_core.contracts` are for. The two move independently and
+must never be derived from one another.
+
+When the version cannot be read it is reported as `None`, meaning *unknown*,
+and a warning is logged. It is never replaced with a placeholder string:
+"0.0.0" or "unknown" would read downstream as a version somebody chose, and
+the whole point of this work is that unknown must never render as fine.
+"""
+
+import logging
+from functools import lru_cache
+from importlib.metadata import PackageNotFoundError
+from importlib.metadata import version as distribution_version
+
+logger = logging.getLogger(__name__)
+
+DISTRIBUTION_NAME = "switch-core"
+
+
+@lru_cache(maxsize=1)
+def switch_core_version() -> str | None:
+ """Return the running switch-core release, or None when unknown.
+
+ None means the distribution metadata is absent — switch-core is running
+ from a source tree that was never installed. Callers must surface that as
+ unknown rather than substituting a value.
+ """
+ try:
+ return distribution_version(DISTRIBUTION_NAME)
+ except PackageNotFoundError:
+ logger.warning(
+ "No installed distribution metadata for %r, so switch-core cannot "
+ "report its own version. Clients will see it as unknown. This "
+ "usually means the package was added to the path rather than "
+ "installed.",
+ DISTRIBUTION_NAME,
+ )
+ return None
diff --git a/core/tests/switch_core/test_version.py b/core/tests/switch_core/test_version.py
new file mode 100644
index 000000000..5bcf6837f
--- /dev/null
+++ b/core/tests/switch_core/test_version.py
@@ -0,0 +1,63 @@
+"""Tests for switch-core reporting its own version (CHOO-1865)."""
+
+from __future__ import annotations
+
+import logging
+import re
+import tomllib
+from importlib.metadata import PackageNotFoundError
+from pathlib import Path
+
+import pytest
+
+from switch_core import version as version_module
+from switch_core.version import switch_core_version
+
+PYPROJECT = Path(__file__).resolve().parents[2] / "pyproject.toml"
+
+
[email protected](autouse=True)
+def _clear_cache() -> None:
+ switch_core_version.cache_clear()
+
+
+def _raise_not_found(name: str) -> str:
+ raise PackageNotFoundError(name)
+
+
+def test_matches_the_version_declared_in_pyproject() -> None:
+ """The running version is the one pyproject declares, with no second copy.
+
+ A `__version__` constant beside pyproject would be one more thing to keep
+ in step by hand — the drift CHOO-1865 exists to end.
+ """
+ declared = tomllib.loads(PYPROJECT.read_text())["project"]["version"]
+ assert switch_core_version() == declared
+
+
+def test_the_declared_version_is_three_part_semver() -> None:
+ """Every Switch artifact carries MAJOR.MINOR.PATCH, switch-core included."""
+ declared = tomllib.loads(PYPROJECT.read_text())["project"]["version"]
+ assert re.fullmatch(r"\d+\.\d+\.\d+", declared), declared
+
+
+def test_unknown_when_distribution_metadata_is_absent(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """An unreadable version is None, never a placeholder.
+
+ "0.0.0" or "unknown" would read downstream as a version somebody chose.
+ Unknown has to stay distinguishable from known.
+ """
+ monkeypatch.setattr(version_module, "distribution_version", _raise_not_found)
+ assert switch_core_version() is None
+
+
+def test_warns_when_the_version_cannot_be_read(
+ monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
+) -> None:
+ """Degraded, but disclosed — a silent None would hide a broken install."""
+ monkeypatch.setattr(version_module, "distribution_version", _raise_not_found)
+ with caplog.at_level(logging.WARNING, logger=version_module.__name__):
+ switch_core_version()
+ assert any(record.levelno == logging.WARNING for record in caplog.records)