AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): turn off graphiti-core and mem0 vendor telemetry by default (#15747)
Krzysztof Czerwinski39c10f0c6cd5
diff --git a/autogpt_platform/backend/.env.default b/autogpt_platform/backend/.env.defaultindex 424e3c070e..d97d56ad78 100644--- a/autogpt_platform/backend/.env.default+++ b/autogpt_platform/backend/.env.default@@ -403,6 +403,13 @@ POSTHOG_FLAG_DEFINITION_CACHE=redisPOSTHOG_FLAG_DEFINITION_REFRESH_SECONDS=30POSTHOG_FLAG_DEFINITION_CACHE_TTL_SECONDS=600+# Usage telemetry that graphiti-core and mem0 send to their vendors' own+# analytics. The backend turns both off when these are unset. To opt in, set+# true in the process environment (compose env_file or an export); a value+# only in backend/.env is ignored for local `poetry run` starts.+GRAPHITI_TELEMETRY_ENABLED=false+MEM0_TELEMETRY=false+# Tally Form Integration (pre-populate business understanding on signup)TALLY_API_KEY=diff --git a/autogpt_platform/backend/backend/__init__.py b/autogpt_platform/backend/backend/__init__.pyindex e69de29bb2..c23838f47d 100644--- a/autogpt_platform/backend/backend/__init__.py+++ b/autogpt_platform/backend/backend/__init__.py@@ -0,0 +1,3 @@+from backend.util.vendor_telemetry import disable_vendor_telemetry++disable_vendor_telemetry()diff --git a/autogpt_platform/backend/backend/util/vendor_telemetry.py b/autogpt_platform/backend/backend/util/vendor_telemetry.pynew file mode 100644index 0000000000..0f25ff5d4e--- /dev/null+++ b/autogpt_platform/backend/backend/util/vendor_telemetry.py@@ -0,0 +1,23 @@+"""Turns off the usage telemetry graphiti-core and mem0 send to their vendors.++Both libraries report usage to their own PostHog projects unless told not to.+mem0 reads its switch once, when it is first imported, so this has to run+before anything imports mem0; ``backend/__init__.py`` calls it, which puts it+ahead of every import of either library. A variable already in the process+environment keeps its value. One only in ``backend/.env`` does not: the+``load_dotenv()`` calls run after this and never overwrite a variable that is+already set.+"""++import os+from collections.abc import MutableMapping++VENDOR_TELEMETRY_OFF = {+ "GRAPHITI_TELEMETRY_ENABLED": "false",+ "MEM0_TELEMETRY": "false",+}+++def disable_vendor_telemetry(environ: MutableMapping[str, str] = os.environ) -> None:+ for name, value in VENDOR_TELEMETRY_OFF.items():+ environ.setdefault(name, value)diff --git a/autogpt_platform/backend/backend/util/vendor_telemetry_test.py b/autogpt_platform/backend/backend/util/vendor_telemetry_test.pynew file mode 100644index 0000000000..7d8dd77085--- /dev/null+++ b/autogpt_platform/backend/backend/util/vendor_telemetry_test.py@@ -0,0 +1,70 @@+import os+import subprocess+import sys+from pathlib import Path++from graphiti_core.telemetry.telemetry import TELEMETRY_ENV_VAR++import backend+from backend.util.vendor_telemetry import VENDOR_TELEMETRY_OFF, disable_vendor_telemetry++BACKEND_ROOT = Path(backend.__file__).resolve().parents[1]+++def run_in_fresh_process(code: str, tmp_path: Path, **env: str) -> None:+ """Runs `code` in a new interpreter whose environment has neither switch+ unless passed in `env`, so the result doesn't depend on this process."""+ environ = {k: v for k, v in os.environ.items() if k not in VENDOR_TELEMETRY_OFF}+ # mem0 writes its anonymous id under MEM0_DIR on import.+ environ["MEM0_DIR"] = str(tmp_path)+ environ.update(env)+ subprocess.run(+ [sys.executable, "-c", code], cwd=BACKEND_ROOT, env=environ, check=True+ )+++def test_turns_vendor_telemetry_off_when_unset():+ environ: dict[str, str] = {}++ disable_vendor_telemetry(environ)++ assert environ == {+ "GRAPHITI_TELEMETRY_ENABLED": "false",+ "MEM0_TELEMETRY": "false",+ }+++def test_keeps_a_value_the_deployment_set():+ environ = {"MEM0_TELEMETRY": "true"}++ disable_vendor_telemetry(environ)++ assert environ["MEM0_TELEMETRY"] == "true"+ assert environ["GRAPHITI_TELEMETRY_ENABLED"] == "false"+++def test_graphiti_reads_the_variable_we_set():+ assert TELEMETRY_ENV_VAR in VENDOR_TELEMETRY_OFF+++def test_importing_backend_turns_mem0_telemetry_off(tmp_path: Path):+ run_in_fresh_process(+ "import os\n"+ "import backend\n"+ "from mem0.memory import telemetry\n"+ "assert os.environ['GRAPHITI_TELEMETRY_ENABLED'] == 'false'\n"+ "assert os.environ['MEM0_TELEMETRY'] == 'false'\n"+ "assert telemetry.MEM0_TELEMETRY is False\n"+ "assert telemetry.client_telemetry.posthog.disabled is True\n",+ tmp_path,+ )+++def test_mem0_telemetry_stays_on_when_the_environment_opts_in(tmp_path: Path):+ run_in_fresh_process(+ "import backend\n"+ "from mem0.memory import telemetry\n"+ "assert telemetry.MEM0_TELEMETRY is True\n",+ tmp_path,+ MEM0_TELEMETRY="true",+ )