Skip to content

changelog

Fetches and parses mujoco-mojo's CHANGELOG.md into structured version entries, for Dojo's "What's Changed" footer button and for scripts/verify_changelog_by_tag.py's pre-push check - one parser, so the two can't drift apart.

The changelog is read live from GitHub. fetch_and_parse_changelog raises when that fails, and load_changelog is the caller that never leaves Dojo empty-handed: it falls back to the last copy it fetched successfully (saved under ~/.mujoco-mojo), then to the changelog that ships inside the package, and says which one it used.

LAST_SEEN_VERSION_FILE module-attribute

Python
LAST_SEEN_VERSION_FILE = (
    MUJOCO_MOJO_DIR / "last_seen_changelog_version.txt"
)

The latest changelog version the user has explicitly acknowledged (by closing the What's Changed modal on it). Read fresh on every GET /changelog/data request, not cached in memory, so a mark-seen write takes effect immediately - both for a later page load in the same Dojo process and across a full restart of it.

CHANGELOG_CACHE_FILE module-attribute

Python
CHANGELOG_CACHE_FILE = (
    MUJOCO_MOJO_DIR / "changelog_cache.md"
)

The last changelog fetched successfully from GitHub, kept as the raw text so a later parser change still reads it. load_changelog falls back to it, marked as possibly out of date, when GitHub can't be reached.

BUNDLED_CHANGELOG_FILE module-attribute

Python
BUNDLED_CHANGELOG_FILE = (
    DOCS_SOURCE_DIR / "docs" / "changelog.md"
)

The changelog that ships inside the package, as of the installed version. The last resort for a host that has never reached GitHub, so it can't know about any newer release.

ChangelogSource module-attribute

Python
ChangelogSource = Literal['remote', 'cache', 'bundled']

Where load_changelog's entries came from: GitHub just now, the copy saved from an earlier fetch, or the changelog that ships with the installed package.

ChangelogEntry

Bases: BaseModel

One ## Version X.Y.Z (YYYY-MM-DD) section of the changelog.

bullets instance-attribute

Python
bullets: list[str]

Each top-level - bullet, its marker included, with any of its indented sub-bullets appended as-is - a small, self-contained (possibly nested) markdown list per entry.

ChangelogUnavailable

Bases: RuntimeError

Raised by fetch_and_parse_changelog, without a network request, while a recent failure is still being waited out. The message is that failure's own.

version_tuple

Python
version_tuple(v: str) -> tuple[int, int, int]

The leading X.Y.Z as an int triple, for > comparison. A dev-build suffix (e.g. 2.6.11.dev3+g1234abc) is ignored past the prefix; a string with no such prefix degrades to (0, 0, 0) rather than raising, since this never needs to be exact, only ordered.

Source code in src/mujoco_mojo/utils/changelog.py
Python
def version_tuple(v: str) -> tuple[int, int, int]:
    """The leading `X.Y.Z` as an int triple, for `>` comparison. A dev-build suffix (e.g. `2.6.11.dev3+g1234abc`) is ignored past the prefix; a string with no such prefix degrades to `(0, 0, 0)` rather than raising, since this never needs to be exact, only ordered."""
    m = _VERSION_PREFIX_RE.match(v)
    if not m:
        return (0, 0, 0)
    return (int(m.group(1)), int(m.group(2)), int(m.group(3)))

parse_changelog

Python
parse_changelog(text: str) -> list[ChangelogEntry]

Splits text into one ChangelogEntry per ## Version X.Y.Z (date) heading, newest first (the file's own order). A malformed date (this repo's own CHANGELOG.md has one, a typo'd 5-digit year) does not fail the entry - it becomes date=None, with the version and bullets unaffected.

Source code in src/mujoco_mojo/utils/changelog.py
Python
def parse_changelog(text: str) -> list[ChangelogEntry]:
    """Splits `text` into one `ChangelogEntry` per `## Version X.Y.Z (date)` heading, newest first (the file's own order). A malformed date (this repo's own `CHANGELOG.md` has one, a typo'd 5-digit year) does not fail the entry - it becomes `date=None`, with the version and bullets unaffected."""
    headings = list(_HEADING_RE.finditer(text))
    entries: list[ChangelogEntry] = []
    for i, m in enumerate(headings):
        end = headings[i + 1].start() if i + 1 < len(headings) else len(text)
        raw_date = m.group("date").strip()
        entries.append(
            ChangelogEntry(
                version=m.group("version"),
                date=raw_date if _DATE_RE.match(raw_date) else None,
                bullets=_parse_bullets(text[m.end() : end]),
            )
        )
    return entries

fetch_and_parse_changelog

Python
fetch_and_parse_changelog(
    *, force_refresh: bool = False
) -> list[ChangelogEntry]

Fetches CHANGELOG_URL and parses it, reusing a successful result for _CACHE_TTL_SECONDS.

Synchronous by design, so it and everything it calls stay plain-function testable with no async test infra (this repo has none); call it from an async endpoint via starlette.concurrency.run_in_threadpool.

Raises on any failure - a network error, a timeout, a non-200 response, or a 200 that parses to zero entries (a captive-portal page would otherwise look like an empty changelog instead of a failure). After a failure the next calls raise ChangelogUnavailable for _RETRY_AFTER_SECONDS without touching the network, unless force_refresh is set. A success also saves the text to CHANGELOG_CACHE_FILE for load_changelog's fallback.

Source code in src/mujoco_mojo/utils/changelog.py
Python
def fetch_and_parse_changelog(*, force_refresh: bool = False) -> list[ChangelogEntry]:
    """
    Fetches `CHANGELOG_URL` and parses it, reusing a successful result for `_CACHE_TTL_SECONDS`.

    Synchronous by design, so it and everything it calls stay plain-function testable with no async test infra (this repo has none); call it from an async endpoint via `starlette.concurrency.run_in_threadpool`.

    Raises on any failure - a network error, a timeout, a non-200 response, or a 200 that parses to zero entries (a captive-portal page would otherwise look like an empty changelog instead of a failure). After a failure the next calls raise `ChangelogUnavailable` for `_RETRY_AFTER_SECONDS` without touching the network, unless `force_refresh` is set. A success also saves the text to `CHANGELOG_CACHE_FILE` for `load_changelog`'s fallback.
    """
    global _cache, _failure
    now = time.monotonic()
    with _cache_lock:
        if (
            not force_refresh
            and _cache is not None
            and now - _cache[0] < _CACHE_TTL_SECONDS
        ):
            return _cache[1]
        if (
            not force_refresh
            and _failure is not None
            and now - _failure[0] < _RETRY_AFTER_SECONDS
        ):
            raise ChangelogUnavailable(_failure[1])
        try:
            text = _fetch_remote_text()
            entries = parse_changelog(text)
            if not entries:
                raise ValueError(
                    "Remote changelog fetched successfully but parsed to zero entries"
                )
        except Exception as exc:
            _failure = (time.monotonic(), _describe(exc))
            raise
        _failure = None
        _cache = (now, entries)
        _save_cache_text(text)
        return entries

load_changelog

Python
load_changelog() -> LoadedChangelog | None

The changelog from the best place available, newest entry first: GitHub (fetch_and_parse_changelog), else the copy saved by the last successful fetch, else the changelog that ships in the package. source says which, so a caller can tell the user a copy may be out of date. Returns None only if none of the three has an entry.

A network failure is one warning line, not a traceback, and a failure still being waited out is not logged again. Any other failure (a bad status, a page with no versions) keeps its traceback.

Source code in src/mujoco_mojo/utils/changelog.py
Python
def load_changelog() -> LoadedChangelog | None:
    """
    The changelog from the best place available, newest entry first: GitHub (`fetch_and_parse_changelog`), else the copy saved by the last successful fetch, else the changelog that ships in the package. `source` says which, so a caller can tell the user a copy may be out of date. Returns `None` only if none of the three has an entry.

    A network failure is one warning line, not a traceback, and a failure still being waited out is not logged again. Any other failure (a bad status, a page with no versions) keeps its traceback.
    """
    try:
        return LoadedChangelog(entries=fetch_and_parse_changelog(), source="remote")
    except ChangelogUnavailable:
        pass
    except OSError as exc:
        logger.warning(
            "Couldn't fetch the remote changelog (%s), using a saved copy",
            _describe(exc),
        )
    except Exception:
        logger.warning(
            "Failed to fetch/parse the remote changelog, using a saved copy",
            exc_info=True,
        )
    entries = _parse_file(CHANGELOG_CACHE_FILE)
    if entries:
        return LoadedChangelog(entries=entries, source="cache")
    entries = _parse_file(BUNDLED_CHANGELOG_FILE)
    if entries:
        return LoadedChangelog(entries=entries, source="bundled")
    return None

read_last_seen_version

Python
read_last_seen_version() -> str | None

The version last written by write_last_seen_version, or None if the user has never acknowledged one (a first-ever launch of this feature, or a fresh ~/.mujoco-mojo).

Source code in src/mujoco_mojo/utils/changelog.py
Python
def read_last_seen_version() -> str | None:
    """The version last written by `write_last_seen_version`, or `None` if the user has never acknowledged one (a first-ever launch of this feature, or a fresh `~/.mujoco-mojo`)."""
    try:
        text = LAST_SEEN_VERSION_FILE.read_text(encoding="utf-8").strip()
    except FileNotFoundError:
        return None
    return text or None

write_last_seen_version

Python
write_last_seen_version(version: str) -> None

Persists version as acknowledged, so it (and everything older) stops being flagged is_new and stops triggering the modal's auto-open, on this and every future launch, until a genuinely newer version appears.

Source code in src/mujoco_mojo/utils/changelog.py
Python
def write_last_seen_version(version: str) -> None:
    """Persists `version` as acknowledged, so it (and everything older) stops being flagged `is_new` and stops triggering the modal's auto-open, on this and every future launch, until a genuinely newer version appears."""
    LAST_SEEN_VERSION_FILE.parent.mkdir(parents=True, exist_ok=True)
    LAST_SEEN_VERSION_FILE.write_text(version.strip() + "\n", encoding="utf-8")