DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(update): distinguish desktop scopes from systemd services (#1555)
Bingxi Zhao (Frank)bb146e72e3fd
diff --git a/deeptutor/services/app_update.py b/deeptutor/services/app_update.pyindex 0b2850a54..4351d0d9a 100644--- a/deeptutor/services/app_update.py+++ b/deeptutor/services/app_update.py@@ -181,13 +181,19 @@ def _running_from_source_checkout() -> bool:def _systemd_service_unit(cgroup_text: str) -> str | None:- """Find a service unit in either cgroup v1 or v2 membership text."""+ """Find the owning service unit, ignoring user managers and app scopes."""for line in cgroup_text.splitlines():fields = line.split(":", 2)if len(fields) != 3:continue- for component in fields[2].split("/"):+ # The nearest unit owns the process. A desktop app scope commonly sits+ # below [email protected], but is not itself that service's process.+ for component in reversed(fields[2].split("/")):+ if component.endswith(".scope"):+ breakif component.endswith(".service"):+ if component.startswith("user@"):+ breakreturn componentreturn None@@ -196,15 +202,18 @@ def running_under_systemd_service() -> bool:"""A detached process group does not escape a systemd service cgroup."""if sys.platform != "linux":return False- # systemd supplies this to service processes, including when the service- # uses a private cgroup namespace and /proc/self/cgroup reads as '/'.- if os.getenv("INVOCATION_ID"):- return Truetry:membership = Path("/proc/self/cgroup").read_text(encoding="utf-8")except OSError:- return False- return _systemd_service_unit(membership) is not None+ return bool(os.getenv("INVOCATION_ID"))+ if _systemd_service_unit(membership) is not None:+ return True+ # INVOCATION_ID can also be inherited by a desktop app scope. It is only+ # useful when a private cgroup namespace hides the actual unit path.+ visible_paths = [+ line.split(":", 2)[2] for line in membership.splitlines() if line.count(":") >= 2+ ]+ return bool(os.getenv("INVOCATION_ID")) and all(path in {"", "/"} for path in visible_paths)def detect_installation() -> Installation:diff --git a/tests/services/test_app_update.py b/tests/services/test_app_update.pyindex 494610b53..26b3ca520 100644--- a/tests/services/test_app_update.py+++ b/tests/services/test_app_update.py@@ -178,9 +178,20 @@ def test_systemd_service_cgroup_disables_in_app_update(app_update._systemd_service_unit("1:name=systemd:/user.slice/[email protected]/app.slice/deeptutor.service\n")- == "[email protected]"+ == "deeptutor.service")assert app_update._systemd_service_unit("0::/user.slice/session-2.scope\n") is None+ assert (+ app_update._systemd_service_unit(+ "0::/user.slice/[email protected]/app.slice/app-gnome-terminal-123.scope\n"+ )+ is None+ )+ assert app_update._systemd_service_unit("0::/user.slice/[email protected]/app.slice\n") is None+ assert (+ app_update._systemd_service_unit("0::/system.slice/deeptutor.service/worker\n")+ == "deeptutor.service"+ )monkeypatch.setattr(app_update, "_running_in_container", lambda: False)monkeypatch.setattr(app_update, "_running_from_source_checkout", lambda: False)@@ -195,6 +206,24 @@ def test_systemd_service_cgroup_disables_in_app_update(assert "systemctl" in installation.reason+def test_desktop_scope_is_not_mistaken_for_systemd_service(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr(app_update.sys, "platform", "linux")+ monkeypatch.setenv("INVOCATION_ID", "desktop-app-unit")+ monkeypatch.setattr(+ app_update.Path,+ "read_text",+ lambda self, **kwargs: (+ "0::/user.slice/[email protected]/app.slice/app-gnome-terminal-123.scope\n"+ ),+ )+ assert app_update.running_under_systemd_service() is False++ monkeypatch.setattr(app_update.Path, "read_text", lambda self, **kwargs: "0::/\n")+ assert app_update.running_under_systemd_service() is True++def test_detect_installation_prefers_source_checkout_when_metadata_is_shadowed(monkeypatch: pytest.MonkeyPatch,tmp_path: Path,