fix(traces): align is_tracing_enabled with runtime gate and rewrite enable/disable panels

This commit is contained in:
Greyson Lalonde
2026-05-06 05:26:02 +08:00
parent d90a1edcf6
commit e01b096817
3 changed files with 25 additions and 16 deletions

View File

@@ -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),
)

View File

@@ -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

View File

@@ -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: