Load cogs on a fresh instance's first start

Red loads no cogs at all, not even --load-cogs, when the Python version it
last saw differs from the running one, which is always the case on a new
instance. dogesetup therefore never loaded on the first start. The entry
point now records the running Python in Red's config first, but only while
Downloader's lib folder is empty, so Red still handles real Python upgrades.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GUebqhE8dK19vyn4Ct5g1L
This commit is contained in:
ziadarafatandClaude Opus 5.5 committed 2026-11-04 19:42:30 -07:00
1 parent ae08961a38
commit 01400dfc9d
3 files changed
+92 -1

No files matched your search

+58 -1
View File
@@ -10,12 +10,17 @@ Everything comes from the environment, so the container needs no interactive
and ``RED_PREFIX`` (default ``d!``, comma-separated for more than one).
"""
import asyncio
import json
import logging
import os
import shutil
import sys
from pathlib import Path
from redbot.core import data_manager
from redbot.core import Config, _drivers, data_manager
log = logging.getLogger("dogebot.start")
SETUP_COG = Path(__file__).resolve().parents[2] / "cogs/dogesetup"
@@ -63,6 +68,57 @@ def install_setup_cog(data_path: Path, source: Path = SETUP_COG) -> Path:
return target
def should_record_python(
recorded: list[int] | None, running: list[int], lib_path: Path
) -> bool:
"""Whether to record the running Python version before Red starts.
Red loads no cogs at all, not even ``--load-cogs``, when the Python
version it last saw differs from the running one. On a fresh instance
nothing is recorded yet, so dogesetup would never load. Recording the
version is only safe while Downloader's lib folder is empty, since
anything in it was built for the old Python and Red has to clear it.
"""
if recorded == running:
return False
return not lib_path.exists() or not any(lib_path.iterdir())
async def record_python_version(name: str) -> None:
"""Record the running Python in Red's config if it's safe to.
Uses the same calls Red makes at startup to open its storage.
"""
data_manager.load_basic_configuration(name)
driver = _drivers.get_driver_class()
await driver.initialize(**data_manager.storage_details())
try:
config = Config.get_core_conf(force_registration=False)
recorded = await config.get_raw(
"last_system_info", "python_version", default=None
)
running = list(sys.version_info[:2])
lib_path = (
data_manager.cog_data_path(raw_name="Downloader")
/ "lib"
)
if should_record_python(recorded, running, lib_path):
await config.set_raw(
"last_system_info",
"python_version",
value=running,
)
log.info("Recorded Python %s for Red", running)
elif recorded != running:
log.warning(
"Python changed from %s; Red starts without "
"cogs once. Run d!cog reinstallreqs, restart.",
recorded,
)
finally:
await driver.teardown()
def redbot_args(name: str, prefixes: list[str]) -> list[str]:
"""Return the ``redbot`` command line."""
args = ["redbot", name, "--no-prompt", "--load-cogs", "dogesetup"]
@@ -79,5 +135,6 @@ def main() -> None:
data_path.mkdir(parents=True, exist_ok=True)
write_instance_config(Path(data_manager.config_file), name, data_path)
install_setup_cog(data_path)
asyncio.run(record_python_version(name))
args = redbot_args(name, [p for p in prefixes if p])
os.execvp(args[0], args) # noqa: S606
+15
View File
@@ -5,8 +5,10 @@ after bumping red-discordbot, update cogs/dogesetup before deploying.
"""
import inspect
import sys
import pytest
import redbot.core.bot # noqa: F401 (inspected below)
from redbot.cogs.downloader.downloader import Downloader
from redbot.cogs.downloader.repo_manager import RepoManager
from redbot.core.core_commands import CoreLogic
@@ -52,3 +54,16 @@ def test_audio_config_identifier() -> None:
source = inspect.getsource(audio_core).replace(" ", "")
assert f"Config.get_conf(self,{AUDIO_IDENTIFIER}," in source
def test_startup_storage_calls_exist() -> None:
"""start.record_python_version uses these, as Red's own startup does."""
from redbot.core import Config, _drivers, data_manager # noqa: PLC0415
assert callable(data_manager.load_basic_configuration)
assert callable(data_manager.storage_details)
assert callable(_drivers.get_driver_class)
assert callable(Config.get_core_conf)
source = inspect.getsource(sys.modules["redbot.core.bot"])
assert "last_system_info__python_version" in source
assert 'if ver_info != last_system_info["python_version"]:' in source
+19
View File
@@ -41,3 +41,22 @@ def test_redbot_args() -> None:
assert args[:3] == ["redbot", "dogebot", "--no-prompt"]
assert args[args.index("--load-cogs") + 1] == "dogesetup"
assert args.count("--prefix") == 2
def test_record_python_on_fresh_instance(tmp_path: Path) -> None:
"""A fresh instance (nothing recorded, no lib) gets the version."""
assert start.should_record_python(None, [3, 11], tmp_path / "lib")
assert start.should_record_python([3, 7], [3, 11], tmp_path / "lib")
def test_record_python_skips_when_unchanged(tmp_path: Path) -> None:
"""Nothing to do when Red already saw this Python."""
assert not start.should_record_python([3, 11], [3, 11], tmp_path)
def test_record_python_leaves_real_upgrades_to_red(tmp_path: Path) -> None:
"""With requirements built for another Python, Red must clear them."""
lib = tmp_path / "lib"
lib.mkdir()
(lib / "somepackage").mkdir()
assert not start.should_record_python([3, 10], [3, 11], lib)