Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
feat(cli): record what the run app's Deploy met before an attempt (#7964)
João Moura24a1ddf23f88
diff --git a/lib/cli/src/crewai_cli/run_crew.py b/lib/cli/src/crewai_cli/run_crew.pyindex 9597b75a4..288488938 100644--- a/lib/cli/src/crewai_cli/run_crew.py+++ b/lib/cli/src/crewai_cli/run_crew.py@@ -6,7 +6,7 @@ from pathlib import Pathimport reimport subprocessimport sys-from typing import TYPE_CHECKING, Any+from typing import TYPE_CHECKING, Any, Literalimport clickfrom crewai_core.constants import CREWAI_TRAINED_AGENTS_FILE_ENV@@ -493,6 +493,30 @@ def _run_json_crew_in_project_env(return None+DeployButtonEvent = Literal[+ "deploy_login_prompted",+ "deploy_stopped:login_still_required",+ "deploy_stopped:login_abandoned",+ "deploy_stopped:login_failed",+]+++def _record_deploy_button(event: DeployButtonEvent) -> None:+ """Count what the run app's Deploy met before a deployment attempt.++ `cli_usage:deploy` counts the press and `Create Crew Deployment` the+ attempt; a press that never became an attempt said nothing about why.+ """+ try:+ from crewai_core.telemetry import Telemetry++ telemetry = Telemetry()+ telemetry.set_tracer()+ telemetry.feature_usage_span(f"cli_usage:{event}")+ except Exception: # noqa: S110 - telemetry must never break a command+ pass++def _chain_deploy() -> None:from rich.console import Console@@ -516,11 +540,20 @@ def _chain_deploy() -> None:except AuthenticationRequiredError:from crewai_cli.authentication.main import AuthenticationCommand+ _record_deploy_button("deploy_login_prompted")console.print()- AuthenticationCommand().login()+ try:+ AuthenticationCommand().login()+ except KeyboardInterrupt:+ _record_deploy_button("deploy_stopped:login_abandoned")+ raise+ except Exception:+ _record_deploy_button("deploy_stopped:login_failed")+ raisetry:DeployCommand().create_crew(confirm=True, skip_validate=True, source="tui")except AuthenticationRequiredError:+ _record_deploy_button("deploy_stopped:login_still_required")console.print("\nDeploy failed: authentication is still required.\n",style="bold red",diff --git a/lib/cli/tests/test_crew_run_tui.py b/lib/cli/tests/test_crew_run_tui.pyindex 611a41e3d..c4a4b4514 100644--- a/lib/cli/tests/test_crew_run_tui.py+++ b/lib/cli/tests/test_crew_run_tui.py@@ -43,6 +43,7 @@ from crewai_cli.crew_run_tui import (_format_json_in_text,_try_parse_structured,)+import httpximport pytestfrom rich.text import Text@@ -2231,3 +2232,123 @@ def test_format_json_in_text_still_pretty_prints_valid_json() -> None:assert _format_json_in_text('data: {"a": 1} and [...]') == ("data: " + '{\n "a": 1\n}' + " and [...]")+++# --- what the run app's Deploy met before a deployment attempt ---------------+++def deploy_usage(monkeypatch) -> list[str]:+ features: list[str] = []++ class FakeTelemetry:+ def set_tracer(self) -> None:+ pass++ def feature_usage_span(self, feature, attributes=None) -> None:+ features.append(feature)++ monkeypatch.setattr("crewai_core.telemetry.Telemetry", FakeTelemetry)+ return features+++def _deploy_doubles(monkeypatch, *, creates, login):+ calls: list[str] = []++ class FakeDeployCommand:+ def create_crew(self, **kwargs) -> None:+ calls.append("create")+ outcome = creates.pop(0)+ if outcome is not None:+ raise outcome++ class FakeAuthenticationCommand:+ def login(self) -> None:+ calls.append("login")+ if login is not None:+ raise login++ monkeypatch.setattr("crewai_cli.deploy.main.DeployCommand", FakeDeployCommand)+ monkeypatch.setattr(+ "crewai_cli.authentication.main.AuthenticationCommand",+ FakeAuthenticationCommand,+ )+ return calls+++def test_a_logged_in_deploy_counts_no_hurdle(monkeypatch, deploy_usage) -> None:+ calls = _deploy_doubles(monkeypatch, creates=[None], login=None)++ run_crew._chain_deploy()++ assert calls == ["create"]+ assert deploy_usage == []+++def test_a_login_prompt_that_succeeds_counts_the_prompt_only(monkeypatch, deploy_usage) -> None:+ calls = _deploy_doubles(+ monkeypatch, creates=[AuthenticationRequiredError(), None], login=None+ )++ run_crew._chain_deploy()++ assert calls == ["create", "login", "create"]+ assert deploy_usage == ["cli_usage:deploy_login_prompted"]+++def test_still_not_logged_in_after_the_prompt_counts_why_and_says_so(+ monkeypatch, capsys, deploy_usage+) -> None:+ _deploy_doubles(+ monkeypatch,+ creates=[AuthenticationRequiredError(), AuthenticationRequiredError()],+ login=None,+ )++ run_crew._chain_deploy()++ assert deploy_usage == [+ "cli_usage:deploy_login_prompted",+ "cli_usage:deploy_stopped:login_still_required",+ ]+ assert "Deploy failed: authentication is still required." in capsys.readouterr().out+++ ("raised", "reason"),+ [(KeyboardInterrupt(), "login_abandoned"), (httpx.HTTPError("access_denied"), "login_failed")],+)+def test_a_login_that_ends_badly_is_counted_and_still_raised(+ monkeypatch, deploy_usage, raised, reason+) -> None:+ """Counting must not change what the user sees: the error still escapes."""+ calls = _deploy_doubles(+ monkeypatch, creates=[AuthenticationRequiredError()], login=raised+ )++ with pytest.raises(type(raised)):+ run_crew._chain_deploy()++ assert calls == ["create", "login"]+ assert deploy_usage == [+ "cli_usage:deploy_login_prompted",+ f"cli_usage:deploy_stopped:{reason}",+ ]+++def test_a_failing_usage_count_never_changes_the_deploy(monkeypatch, capsys) -> None:+ class BrokenTelemetry:+ def set_tracer(self) -> None:+ raise RuntimeError("exporter is down")++ monkeypatch.setattr("crewai_core.telemetry.Telemetry", BrokenTelemetry)+ calls = _deploy_doubles(+ monkeypatch,+ creates=[AuthenticationRequiredError(), AuthenticationRequiredError()],+ login=None,+ )++ run_crew._chain_deploy()++ assert calls == ["create", "login", "create"]+ assert "Deploy failed: authentication is still required." in capsys.readouterr().out