Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

fix(tracing): a refused trace grant runs the crew untraced instead of failing it (#7812)

João Mouraca1d55abb368
diff --git a/lib/crewai/src/crewai/execution.py b/lib/crewai/src/crewai/execution.py
index b0cde19de..3bf1835fa 100644
--- a/lib/crewai/src/crewai/execution.py
+++ b/lib/crewai/src/crewai/execution.py
@@ -18,6 +18,7 @@ from __future__ import annotations
from contextlib import ExitStack
import contextvars
from dataclasses import dataclass
+import logging
import os
import sys
from types import TracebackType
@@ -29,6 +30,9 @@ if TYPE_CHECKING:
from crewai.telemetry.tracing.session import TraceSession
+logger = logging.getLogger(__name__)
+
+
@dataclass
class ExecutionTrace:
"""Trace lifetime that can be rebound between deferred conversational turns."""
@@ -143,22 +147,38 @@ def _start_tracing(execution_uuid: str, tracing: bool | None) -> None:
from crewai.telemetry.tracing.grants import (
GrantSpanExporter,
TraceGrantClient,
- tracing_credential,
+ resolve_tracing_credential,
)
from crewai.telemetry.tracing.session import TraceSession
stack = ExitStack()
# First-run discovery is local even if CLI credentials happen to exist.
- amp_credential = tracing_credential() if enabled else None
- if amp_credential is None:
+ # The credential and its source are resolved ONCE: the pair that is sent is
+ # the pair a refusal names, whatever the environment says afterwards.
+ resolved = resolve_tracing_credential() if enabled else None
+ if resolved is None:
from crewai.telemetry.tracing.ephemeral import ephemeral_tracing
session = stack.enter_context(
ephemeral_tracing(execution_uuid, first_time=not enabled)
)
else:
- client = TraceGrantClient(amp_credential)
- grant = client.create(execution_uuid)
+ from crewai.telemetry.tracing.grants import TraceGrantError
+
+ credential_source, amp_credential = resolved
+ try:
+ # The constructor refuses a blank credential with the same error, so
+ # it sits inside the same boundary as the grant request.
+ client = TraceGrantClient(amp_credential)
+ grant = client.create(execution_uuid)
+ except TraceGrantError as error:
+ # A trace is a record of the run, not a condition of it: a login
+ # that expired or a token that was revoked must not take the run
+ # down with it. The run goes on untraced and says so — never falls
+ # back to an anonymous upload of a run whose owner is logged in.
+ logger.warning(_untraced_because(error, credential_source))
+ stack.close()
+ return
exporter = GrantSpanExporter(client, grant)
session = TraceSession(grant.execution_uuid, [exporter])
@@ -170,6 +190,38 @@ def _start_tracing(execution_uuid: str, tracing: bool | None) -> None:
_activate_tracing(ExecutionTrace(session, stack))
+# What each credential is called, and what fixes it when AMP refuses it: the one
+# that was sent, never a different one — refreshing a login does nothing for a
+# rejected CREWAI_USER_PAT.
+_CREDENTIAL_FIX = {
+ "pat": (
+ "the CREWAI_USER_PAT token",
+ "Replace it with a valid personal access token",
+ ),
+ "integration": (
+ "the platform integration token",
+ "Check the integration token this environment is given",
+ ),
+ "login": ("the saved login", "Run `crewai login` again"),
+}
+
+
+def _untraced_because(error: Exception, source: str | None) -> str:
+ """The warning for a run AMP would not grant a trace to, naming the
+ credential it refused and the fix for that one."""
+ status = getattr(error, "status_code", None)
+ if status in (401, 403):
+ name, fix = _CREDENTIAL_FIX.get(source or "", ("the credential", "Check it"))
+ return (
+ f"This run is not traced: CrewAI AMP refused {name} (HTTP {status}). "
+ f"{fix} to trace your runs."
+ )
+ return (
+ f"This run is not traced: CrewAI AMP could not grant a trace "
+ f"({f'HTTP {status}' if status else error}). The run itself is unaffected."
+ )
+
+
def _activate_tracing(tracing: ExecutionTrace) -> None:
activation = ExitStack()
activation.enter_context(tracing.session.activate())
diff --git a/lib/crewai/src/crewai/telemetry/tracing/grants.py b/lib/crewai/src/crewai/telemetry/tracing/grants.py
index df0ce2c34..3fd85312e 100644
--- a/lib/crewai/src/crewai/telemetry/tracing/grants.py
+++ b/lib/crewai/src/crewai/telemetry/tracing/grants.py
@@ -47,14 +47,36 @@ class TraceGrantError(Exception):
def tracing_credential() -> str | None:
"""Resolve an explicit PAT, integration credential, or saved CLI login."""
+ resolved = resolve_tracing_credential()
+ return resolved[1] if resolved else None
+
+
+def tracing_credential_source() -> str | None:
+ """Which credential ``tracing_credential`` sends: ``"pat"``
+ (``CREWAI_USER_PAT``), ``"integration"`` (the platform integration token)
+ or ``"login"`` (the saved ``crewai login``) — so a refusal can name the one
+ that failed instead of sending somebody to refresh another."""
+ resolved = resolve_tracing_credential()
+ return resolved[0] if resolved else None
+
+
+def resolve_tracing_credential() -> tuple[str, str] | None:
+ """The credential tracing sends and where it came from, as ``(source,
+ token)`` — read once, in the one order both functions above follow.
+
+ A caller that sends the token and may later explain a refusal keeps this
+ pair: resolving the source again after the request can name a credential
+ AMP never saw (the environment or the context may have changed meanwhile).
+ """
if token := os.getenv("CREWAI_USER_PAT"):
- return token
+ return "pat", token
if token := get_platform_integration_token():
- return token
+ return "integration", token
try:
- return get_auth_token()
+ token = get_auth_token()
except AuthError:
return None
+ return ("login", token) if token else None
@dataclass(frozen=True)
diff --git a/lib/crewai/tests/telemetry/test_session_trace_export.py b/lib/crewai/tests/telemetry/test_session_trace_export.py
index 55e5593e0..b16a2d9c1 100644
--- a/lib/crewai/tests/telemetry/test_session_trace_export.py
+++ b/lib/crewai/tests/telemetry/test_session_trace_export.py
@@ -711,9 +711,9 @@ def test_first_time_execution_uses_local_session_even_with_saved_credentials(
"crewai.events.listeners.tracing.utils.should_auto_collect_first_time_traces",
lambda: True,
)
- credential = Mock(return_value="saved-login")
+ credential = Mock(return_value=("login", "saved-login"))
monkeypatch.setattr(
- "crewai.telemetry.tracing.grants.tracing_credential", credential
+ "crewai.telemetry.tracing.grants.resolve_tracing_credential", credential
)
save = Mock()
monkeypatch.setattr(ephemeral, "update_user_data", save)
diff --git a/lib/crewai/tests/telemetry/test_trace_lifecycle.py b/lib/crewai/tests/telemetry/test_trace_lifecycle.py
index fce79e34b..799f61289 100644
--- a/lib/crewai/tests/telemetry/test_trace_lifecycle.py
+++ b/lib/crewai/tests/telemetry/test_trace_lifecycle.py
@@ -19,6 +19,9 @@ from crewai.telemetry.tracing.grants import (
GrantSpanExporter,
TraceGrantClient,
TraceGrantError,
+ resolve_tracing_credential,
+ tracing_credential,
+ tracing_credential_source,
)
from opentelemetry import trace
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
@@ -322,15 +325,115 @@ def test_explicit_disable_prevents_first_time_collection(monkeypatch, disabled):
@pytest.mark.parametrize("async_run", [False, True])
-def test_grant_failure_restores_execution_context(monkeypatch, async_run):
+ ("status", "says"),
+ [
+ (401, "refused the CREWAI_USER_PAT token (HTTP 401)"),
+ (403, "Replace it with a valid personal access token"),
+ (503, "unaffected"),
+ ],
+)
+def test_a_refused_grant_runs_untraced_and_says_why(
+ monkeypatch, caplog, async_run, status, says
+):
+ """A trace is a record of the run, not a condition of it: an expired login
+ must not fail the crew. The run completes, nothing is uploaded, the context
+ is restored, and the warning names the fix."""
monkeypatch.setenv("CREWAI_USER_PAT", "invalid")
+ create = Mock(side_effect=TraceGrantError("AMP rejected credential", status))
+ monkeypatch.setattr(TraceGrantClient, "create", create)
+ exporter = Mock(return_value=InMemorySpanExporter())
+ monkeypatch.setattr(GrantSpanExporter, "_exporter", staticmethod(exporter))
+
+ with caplog.at_level("WARNING", logger="crewai.execution"):
+ flow = ExampleFlow(tracing=True)
+ result = asyncio.run(flow.kickoff_async()) if async_run else flow.kickoff()
+
+ assert result == "hello world"
+ create.assert_called_once()
+ exporter.assert_not_called()
+ assert get_trace_session() is None and get_execution_uuid() is None
+ assert any(says in record.getMessage() for record in caplog.records)
+
+
+ ("source", "says", "never"),
+ [
+ ("pat", "refused the CREWAI_USER_PAT token", "crewai login"),
+ ("integration", "refused the platform integration token", "crewai login"),
+ ("login", "refused the saved login (HTTP 401). Run `crewai login` again", "CREWAI_USER_PAT"),
+ ],
+)
+def test_the_warning_names_the_credential_that_was_refused(
+ monkeypatch, caplog, source, says, never
+):
+ """The fix named is for the credential that was sent: refreshing a login does
+ nothing for a rejected CREWAI_USER_PAT or an integration token."""
+ grants = "crewai.telemetry.tracing.grants"
+ monkeypatch.setattr(
+ f"{grants}.resolve_tracing_credential", lambda: (source, "rejected")
+ )
monkeypatch.setattr(
TraceGrantClient,
"create",
Mock(side_effect=TraceGrantError("AMP rejected credential", 401)),
)
- with pytest.raises(TraceGrantError) as error:
- flow = ExampleFlow(tracing=True)
- asyncio.run(flow.kickoff_async()) if async_run else flow.kickoff()
- assert error.value.status_code == 401
+
+ with caplog.at_level("WARNING", logger="crewai.execution"):
+ assert ExampleFlow(tracing=True).kickoff() == "hello world"
+
+ warning = next(r.getMessage() for r in caplog.records if "not traced" in r.getMessage())
+ assert says in warning and never not in warning
+
+
+def test_the_credential_source_follows_the_credential_order(monkeypatch):
+ grants = "crewai.telemetry.tracing.grants"
+ monkeypatch.setenv("CREWAI_USER_PAT", "pat")
+ assert tracing_credential_source() == "pat"
+ monkeypatch.delenv("CREWAI_USER_PAT")
+ monkeypatch.setattr(f"{grants}.get_platform_integration_token", lambda: "integration")
+ assert tracing_credential_source() == "integration"
+ monkeypatch.setattr(f"{grants}.get_platform_integration_token", lambda: None)
+ monkeypatch.setattr(f"{grants}.get_auth_token", lambda: "login")
+ assert tracing_credential_source() == "login"
+ assert tracing_credential() == "login"
+ assert resolve_tracing_credential() == ("login", "login")
+
+
+def test_the_warning_names_the_credential_that_was_sent_not_the_one_left_after(
+ monkeypatch, caplog
+):
+ """The credential and its source are read once: a PAT that disappears while
+ the grant request is in flight is still the one the warning names, never
+ the integration token the environment falls back to afterwards."""
+ grants = "crewai.telemetry.tracing.grants"
+ monkeypatch.setenv("CREWAI_USER_PAT", "rejected-pat")
+ monkeypatch.setattr(f"{grants}.get_platform_integration_token", lambda: "integration")
+ sent = []
+
+ def refuse(client, execution_uuid):
+ sent.append(client._api.api_key)
+ monkeypatch.delenv("CREWAI_USER_PAT")
+ raise TraceGrantError("AMP rejected credential", 401)
+
+ monkeypatch.setattr(TraceGrantClient, "create", refuse)
+ with caplog.at_level("WARNING", logger="crewai.execution"):
+ assert ExampleFlow(tracing=True).kickoff() == "hello world"
+
+ warning = next(r.getMessage() for r in caplog.records if "not traced" in r.getMessage())
+ assert sent == ["rejected-pat"]
+ assert "CREWAI_USER_PAT" in warning and "integration" not in warning
+
+
+def test_a_blank_credential_runs_untraced_too(monkeypatch, caplog):
+ """The grant client refuses a blank credential in its constructor, with the
+ same error a refused grant raises — so it must not fail the run either."""
+ monkeypatch.setattr(
+ "crewai.telemetry.tracing.grants.resolve_tracing_credential",
+ lambda: ("pat", " "),
+ )
+ with caplog.at_level("WARNING", logger="crewai.execution"):
+ assert ExampleFlow(tracing=True).kickoff() == "hello world"
assert get_trace_session() is None and get_execution_uuid() is None
+ assert any("not traced" in record.getMessage() for record in caplog.records)
+
diff --git a/lib/crewai/tests/tracing/test_tracing.py b/lib/crewai/tests/tracing/test_tracing.py
index 6d4eaac35..a1009ea40 100644
--- a/lib/crewai/tests/tracing/test_tracing.py
+++ b/lib/crewai/tests/tracing/test_tracing.py
@@ -86,8 +86,8 @@ class TestTraceListenerSetup:
transport = SimpleNamespace(grants=[], exporter=InMemorySpanExporter())
monkeypatch.setenv("OTEL_SDK_DISABLED", "false")
monkeypatch.setattr(
- "crewai.telemetry.tracing.grants.tracing_credential",
- lambda: "synthetic-login",
+ "crewai.telemetry.tracing.grants.resolve_tracing_credential",
+ lambda: ("login", "synthetic-login"),
)
def grant(client, execution_uuid):
@@ -441,7 +441,7 @@ class TestTraceListenerSetup:
def test_trace_listener_ephemeral_batch(self, trace_transport, monkeypatch):
"""Unauthenticated kickoff uploads buffered spans only after consent."""
monkeypatch.setattr(
- "crewai.telemetry.tracing.grants.tracing_credential", lambda: None
+ "crewai.telemetry.tracing.grants.resolve_tracing_credential", lambda: None
)
monkeypatch.setenv("CREWAI_TRACING_ENABLED", "true")