From efa7f20e98e6b5f99dd3bb78755c0028d2e7dd52 Mon Sep 17 00:00:00 2001 From: Dan Fuller Date: Tue, 1 Dec 2026 16:31:18 -0800 Subject: [PATCH] feat: Show the coding agent hint when no DSN is set The hint only printed when a DSN was set. Agents rarely have one while they set up Sentry: the user adds it to production later, so the app and its tests run without it, and the agent finishes before the hint can print. It now prints without a DSN too, and starts by saying that no DSN is set and that this run sends nothing to Sentry. Every other condition for the hint is unchanged. --- sentry_sdk/_agent_hint.py | 11 +++++++---- tests/test_agent_hint.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/_agent_hint.py b/sentry_sdk/_agent_hint.py index 19c5f2e0f1..baa3e84386 100644 --- a/sentry_sdk/_agent_hint.py +++ b/sentry_sdk/_agent_hint.py @@ -33,10 +33,12 @@ GIT_TIMEOUT_SECONDS = 2 +NO_DSN_NOTE = "No DSN is set, so this run sends nothing to Sentry. When one is set:" + _exit_hook_registered = False # What _write_coding_agent_hint can print at exit: one entry for each file that called # init() with tracing or logs off. -_hints_at_exit: "List[Tuple[str, bool, bool]]" = [] +_hints_at_exit: "List[Tuple[str, bool, bool, bool]]" = [] def _running_under_coding_agent() -> bool: @@ -163,7 +165,7 @@ def register_coding_agent_hint_exit_hook() -> None: def _write_coding_agent_hint() -> None: with capture_internal_exceptions(): # One hint, for the first init() from a file that this user changed. - for init_file, tracing_on, logs_on in _hints_at_exit: + for init_file, tracing_on, logs_on, has_dsn in _hints_at_exit: if _setup_in_progress(init_file): break else: @@ -218,6 +220,8 @@ def _write_coding_agent_hint() -> None: "checklist, included in the sentry-sdk package you installed: " f"{SETUP_CHECKLIST_PATH}" ) + if not has_dsn: + hint = f"{NO_DSN_NOTE} {hint}" sys.stdout.flush() sys.stderr.write(f"[sentry] {hint} (hide with SENTRY_AGENT_HINTS=0)\n") sys.stderr.flush() @@ -232,7 +236,6 @@ def show_coding_agent_hint(client: "BaseClient", caller: "FrameType") -> None: not _exit_hook_registered or sys.version_info < (3, 9) or sys.platform not in ("darwin", "linux") - or not options["dsn"] or options["_experiments"].get("agent_setup_complete") or (tracing_on and logs_on) ): @@ -243,4 +246,4 @@ def show_coding_agent_hint(client: "BaseClient", caller: "FrameType") -> None: return # Agents often cut a command's output with `| tail`, so the hint goes last. if all(init_file != seen[0] for seen in _hints_at_exit): - _hints_at_exit.append((init_file, tracing_on, logs_on)) + _hints_at_exit.append((init_file, tracing_on, logs_on, bool(options["dsn"]))) diff --git a/tests/test_agent_hint.py b/tests/test_agent_hint.py index d81b1c163b..e44754b66b 100644 --- a/tests/test_agent_hint.py +++ b/tests/test_agent_hint.py @@ -23,10 +23,10 @@ def run_app(tmp_path, monkeypatch, capsys): monkeypatch.setattr(sentry_sdk._agent_hint.atexit, "register", at_exit.append) app = tmp_path / "app.py" - def run(env, init_args="", before_run=None, inits=1): + def run(env, init_args="", before_run=None, inits=1, dsn=AGENT_HINT_DSN): for name, value in env.items(): monkeypatch.setenv(name, value) - init = f"sentry_sdk.init(dsn={AGENT_HINT_DSN!r}, {init_args})\n" + init = f"sentry_sdk.init(dsn={dsn!r}, {init_args})\n" imports = ( "import sentry_sdk\n" "from sentry_sdk.integrations.logging import LoggingIntegration\n" @@ -125,6 +125,31 @@ def test_coding_agent_hint_shown_once(run_app): assert printed.count(SETUP_CHECKLIST_PATH) == 1 +@pytest.mark.skipif( + sys.version_info < (3, 9), reason="Python < 3.9 hits an early return" +) +@pytest.mark.parametrize( + "env, hint", + [ + ( + {"CLAUDECODE": "1"}, + "No DSN is set, so this run sends nothing to Sentry. When one is set: Sentry will get errors from this app but not the request trace around them, and none of its logs.", + ), + ( + {"CODEX_THREAD_ID": "1"}, + "No DSN is set, so this run sends nothing to Sentry. When one is set: Tracing and logs are off, so Sentry only gets errors.", + ), + ], +) +def test_coding_agent_hint_without_a_dsn(run_app, monkeypatch, env, hint): + monkeypatch.delenv("SENTRY_DSN", raising=False) + + printed = run_app(env, dsn=None) + + assert f"[sentry] {hint}" in printed + assert SETUP_CHECKLIST_PATH in printed + + def _commit(app, date=None): env = { **os.environ,