Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(cli): derive version from pyproject, drop hardcoded fallback (#156)

Haozhe Wudf70288deeda
diff --git a/agent/cli/_version.py b/agent/cli/_version.py
index a5bfb25ff..9acb7cc2f 100644
--- a/agent/cli/_version.py
+++ b/agent/cli/_version.py
@@ -1,26 +1,46 @@
"""Single source of truth for the CLI version string.
Reads ``vibe-trading-ai``'s installed package metadata when available
-(``pip install -e .`` is enough). Falls back to the static constant
-below for un-installed checkouts (e.g. running straight from a clone
-with ``PYTHONPATH=agent``).
-
-Keep the fallback in sync with ``pyproject.toml`` ``[project] version``.
+(``pip install -e .`` is enough). For an un-installed checkout (e.g. running
+straight from a clone with ``PYTHONPATH=agent``) it falls back to reading the
+version straight out of ``pyproject.toml`` — so ``pyproject.toml`` is the one
+and only place the version is ever written. There is deliberately no hardcoded
+version constant to drift out of sync on release (issue #156).
"""
from __future__ import annotations
from typing import Final
+
+def _version_from_pyproject() -> str:
+ """Read ``[project] version`` from the repo's ``pyproject.toml``.
+
+ Returns:
+ The declared version, or ``"unknown"`` if the file cannot be located
+ or parsed (only reachable for an un-installed checkout whose tree has
+ been moved away from its ``pyproject.toml``).
+ """
+ import tomllib
+ from pathlib import Path
+
+ # agent/cli/_version.py -> parents[2] is the repo root holding pyproject.toml.
+ pyproject = Path(__file__).resolve().parents[2] / "pyproject.toml"
+ try:
+ return tomllib.loads(pyproject.read_text(encoding="utf-8"))["project"]["version"]
+ except (OSError, KeyError, tomllib.TOMLDecodeError):
+ return "unknown"
+
+
try:
from importlib.metadata import PackageNotFoundError, version as _pkg_version
try:
__version__: Final[str] = _pkg_version("vibe-trading-ai")
except PackageNotFoundError:
- __version__ = "0.1.8"
+ __version__ = _version_from_pyproject()
except ImportError: # pragma: no cover — importlib.metadata is stdlib on 3.8+
- __version__ = "0.1.8"
+ __version__ = _version_from_pyproject()
__all__ = ["__version__"]
diff --git a/agent/tests/test_cli_version.py b/agent/tests/test_cli_version.py
new file mode 100644
index 000000000..dff2c0e06
--- /dev/null
+++ b/agent/tests/test_cli_version.py
@@ -0,0 +1,42 @@
+"""Regression tests for issue #156 — CLI version must track pyproject.toml.
+
+The shipped 0.1.8 wheel reported ``0.1.7`` because a hardcoded constant was
+not bumped on release. ``cli/_version.py`` now derives the version from package
+metadata, falling back to reading ``pyproject.toml`` directly, so there is no
+constant left to drift. These tests pin both invariants.
+"""
+
+from __future__ import annotations
+
+import tomllib
+from pathlib import Path
+
+from cli import _version
+
+_PYPROJECT = Path(__file__).resolve().parents[2] / "pyproject.toml"
+
+
+def _declared_version() -> str:
+ return tomllib.loads(_PYPROJECT.read_text(encoding="utf-8"))["project"]["version"]
+
+
+def test_pyproject_fallback_matches_declared_version() -> None:
+ # The fallback is the single source of truth for an un-installed checkout;
+ # it must reproduce pyproject.toml exactly (never a stale hardcoded value).
+ assert _version._version_from_pyproject() == _declared_version()
+
+
+def test_exposed_version_is_resolved_not_unknown() -> None:
+ assert _version.__version__
+ assert _version.__version__ != "unknown"
+
+
+def test_no_hardcoded_version_constant_in_source() -> None:
+ # Guards against reintroducing a literal version string that can drift
+ # (the root cause of #156). The only version literal allowed is the
+ # "unknown" sentinel for a moved/un-installed tree.
+ import re
+
+ source = Path(_version.__file__).read_text(encoding="utf-8")
+ literals = re.findall(r"\"\d+\.\d+\.\d+\"", source)
+ assert not literals, f"hardcoded version literal(s) found in _version.py: {literals}"