Fixing Heisenbridge on FreeBSD After Upgrading to Python 3.15
Upgrading to a newer Python release can occasionally break older dependencies before upstream packages ship full compatibility fixes.
After moving to Python 3.15 on FreeBSD, running Heisenbridge (the Matrix-to-IRC bridge) can hit issues with NamedTuple definitions inside mautrix (PaginatedMessages, HelpSection, and HelpCacheKey).
To keep recovery quick and repeatable—whether rebuilding the virtual environment from scratch or re-applying the patch after a package upgrade—I put together a single POSIX shell script at /usr/local/bin/fix-heisenbridge.sh.
What the Script Does:
- Optional Virtual Environment Rebuild: When called with
--recreate-venv(or when the target Python binary is missing), it removes/usr/home/irc/hb-env, creates a freshpython3.15virtual environment, upgradespip, and installsheisenbridge. - Idempotent
mautrixPatching: It detects the active Pythonmajor.minorversion inside the virtual environment, locatesmautrixinsite-packages, and converts the keyword-styleNamedTuple(...)definitions intypes/misc.pyandbridge/commands/handler.pyto explicit(field_name, field_type)sequence pairs. Each patch is idempotent and raises a clear error if an unexpected definition is encountered. - Smoke Test, Ownership, and Restart: It runs
python -m heisenbridge --helpto verify imports, restores ownership toconduit:conduit, and restarts theheisenbridgeservice.
The Recovery Script (/usr/local/bin/fix-heisenbridge.sh):
#!/bin/sh
set -eu
VENV_DIR="/usr/home/irc/hb-env"
PYTHON_BIN="/usr/local/bin/python3.15"
OWNER="conduit:conduit"
SERVICE_NAME="heisenbridge"
RECREATE_VENV=0
if [ "${1:-}" = "--recreate-venv" ]; then
RECREATE_VENV=1
fi
if [ "$RECREATE_VENV" -eq 1 ] || [ ! -x "$VENV_DIR/bin/python" ]; then
rm -rf "$VENV_DIR"
"$PYTHON_BIN" -m venv "$VENV_DIR"
"$VENV_DIR/bin/pip" install --upgrade pip heisenbridge
fi
"$VENV_DIR/bin/python" - <<'EOF'
import sys
from pathlib import Path
ver = f"{sys.version_info.major}.{sys.version_info.minor}"
site_pkg = Path(f"/usr/home/irc/hb-env/lib/python{ver}/site-packages/mautrix")
misc_path = site_pkg / "types/misc.py"
misc_src = misc_path.read_text()
misc_old = '''PaginatedMessages = NamedTuple(
"PaginatedMessages", start=SyncToken, end=SyncToken, events=List[Event]
)'''
misc_new = '''PaginatedMessages = NamedTuple(
"PaginatedMessages", [("start", SyncToken), ("end", SyncToken), ("events", List[Event])]
)'''
if misc_old in misc_src:
misc_path.write_text(misc_src.replace(misc_old, misc_new, 1))
print(f"Patched {misc_path}")
elif misc_new in misc_src:
print(f"Already patched {misc_path}")
else:
raise RuntimeError(f"Unexpected PaginatedMessages definition in {misc_path}")
handler_path = site_pkg / "bridge/commands/handler.py"
handler_src = handler_path.read_text()
handler_old = '''HelpSection = NamedTuple("HelpSection", name=str, order=int, description=str)
HelpCacheKey = NamedTuple(
"HelpCacheKey", is_management=bool, is_portal=bool, is_admin=bool, is_logged_in=bool
)'''
handler_new = '''HelpSection = NamedTuple("HelpSection", [("name", str), ("order", int), ("description", str)])
HelpCacheKey = NamedTuple(
"HelpCacheKey",
[
("is_management", bool),
("is_portal", bool),
("is_admin", bool),
("is_logged_in", bool),
],
)'''
if handler_old in handler_src:
handler_path.write_text(handler_src.replace(handler_old, handler_new, 1))
print(f"Patched {handler_path}")
elif handler_new in handler_src:
print(f"Already patched {handler_path}")
else:
raise RuntimeError(f"Unexpected HelpSection/HelpCacheKey definition in {handler_path}")
EOF
"$VENV_DIR/bin/python" -m heisenbridge --help >/dev/null
chown -R "$OWNER" "$VENV_DIR"
service "$SERVICE_NAME" restart
Usage
Re-patch and restart only (useful after running pip install --upgrade on mautrix or heisenbridge):
sh /usr/local/bin/fix-heisenbridge.shRecreate the virtual environment from scratch with Python 3.15, apply the patches, fix permissions, and restart the service:
sh /usr/local/bin/fix-heisenbridge.sh --recreate-venvOnce upstream releases include native compatibility for Python 3.15, you can drop the patch block and keep just the virtual environment and service management steps.