From e01b096817f737a4019040afecbe74c59b3efc98 Mon Sep 17 00:00:00 2001 From: Greyson Lalonde Date: Wed, 6 May 2026 05:26:02 +0800 Subject: [PATCH] fix(traces): align is_tracing_enabled with runtime gate and rewrite enable/disable panels --- lib/cli/src/crewai_cli/cli.py | 21 ++++++++++---------- lib/crewai-core/src/crewai_core/user_data.py | 12 ++++++++--- lib/crewai-core/tests/test_smoke.py | 8 ++++++-- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/cli/src/crewai_cli/cli.py b/lib/cli/src/crewai_cli/cli.py index e9a5ce59c..9bd1ac396 100644 --- a/lib/cli/src/crewai_cli/cli.py +++ b/lib/cli/src/crewai_cli/cli.py @@ -797,11 +797,10 @@ def traces_enable() -> None: update_user_data({"trace_consent": True, "first_execution_done": True}) panel = Panel( - "✅ Trace consent recorded.\n\n" - "To activate trace collection, set [bold]CREWAI_TRACING_ENABLED=true[/bold] " - "in your environment or .env file.\n" - "Use 'crewai traces disable' to revoke consent.", - title="Trace Consent Recorded", + "✅ Trace collection enabled.\n\n" + "Your crew/flow executions will now send traces to CrewAI+.\n" + "Use 'crewai traces disable' to opt out.", + title="Traces Enabled", border_style="green", padding=(1, 2), ) @@ -819,12 +818,12 @@ def traces_disable() -> None: update_user_data({"trace_consent": False, "first_execution_done": True}) panel = Panel( - "❌ Trace consent revoked.\n\n" - "Your crew/flow executions will no longer send traces, even when " - "[bold]CREWAI_TRACING_ENABLED=true[/bold] is set.\n" - "Use 'crewai traces enable' to grant consent again " - "(activation also requires CREWAI_TRACING_ENABLED=true).", - title="Trace Consent Revoked", + "❌ Trace collection disabled.\n\n" + "Your crew/flow executions will no longer send traces " + "(unless [bold]CREWAI_TRACING_ENABLED=true[/bold] is set in the environment, " + "which overrides the opt-out).\n" + "Use 'crewai traces enable' to opt back in.", + title="Traces Disabled", border_style="red", padding=(1, 2), ) diff --git a/lib/crewai-core/src/crewai_core/user_data.py b/lib/crewai-core/src/crewai_core/user_data.py index 85ba6097c..91e4f35fe 100644 --- a/lib/crewai-core/src/crewai_core/user_data.py +++ b/lib/crewai-core/src/crewai_core/user_data.py @@ -77,9 +77,15 @@ def has_user_declined_tracing() -> bool: def is_tracing_enabled() -> bool: """Return True if tracing should currently be active. - Consent only *blocks* tracing; activation requires - ``CREWAI_TRACING_ENABLED=true`` in the environment. + Mirrors the runtime gate (``crewai.events.listeners.tracing.utils. + should_enable_tracing``): ``CREWAI_TRACING_ENABLED=true`` always activates; + otherwise recorded consent activates; otherwise off. Used by + ``crewai traces status`` so the displayed state matches what crews and + flows actually do. """ + if os.getenv("CREWAI_TRACING_ENABLED", "false").lower() in ("true", "1"): + return True if has_user_declined_tracing(): return False - return os.getenv("CREWAI_TRACING_ENABLED", "false").lower() == "true" + data = _load_user_data() + return data.get("trace_consent", False) is not False diff --git a/lib/crewai-core/tests/test_smoke.py b/lib/crewai-core/tests/test_smoke.py index d9f0692af..93b2e46f9 100644 --- a/lib/crewai-core/tests/test_smoke.py +++ b/lib/crewai-core/tests/test_smoke.py @@ -70,7 +70,9 @@ def test_user_data_round_trip(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) - monkeypatch.setenv("CREWAI_TRACING_ENABLED", "true") assert user_data.is_tracing_enabled() is True monkeypatch.delenv("CREWAI_TRACING_ENABLED", raising=False) - assert user_data.is_tracing_enabled() is False # consent without env var = off + assert ( + user_data.is_tracing_enabled() is True + ) # consent alone enables (matches runtime) def test_user_data_decline_blocks( @@ -81,10 +83,12 @@ def test_user_data_decline_blocks( "crewai_core.paths.appdirs.user_data_dir", lambda app, author: str(tmp_path / app), ) - monkeypatch.setenv("CREWAI_TRACING_ENABLED", "true") user_data.update_user_data({"trace_consent": False, "first_execution_done": True}) assert user_data.has_user_declined_tracing() is True + monkeypatch.delenv("CREWAI_TRACING_ENABLED", raising=False) assert user_data.is_tracing_enabled() is False + monkeypatch.setenv("CREWAI_TRACING_ENABLED", "true") + assert user_data.is_tracing_enabled() is True # env-var override (matches runtime) def test_unused_var_warning_silenced() -> None: