Skip to content

Commit dbface8

Browse files
feat(tracing): stamp __commit_sha__ automatically when AGENT_COMMIT_SHA is set
The SGP cloud deploy now sets AGENT_COMMIT_SHA on agent pods from the build record's attested source_commit (scaleapi#160083). With stamping gated on an explicit adk.code_revision.enable() call, that env var was a silent no-op for every agent that never opted in. Resolve it once at import instead: when the process starts with AGENT_COMMIT_SHA set, __commit_sha__ is stamped; otherwise nothing changes. enable() remains for agents that pass a value or want the AGENT_VERSION fallback, and the SHA-shape guard still applies to both paths. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent f394ce7 commit dbface8

4 files changed

Lines changed: 54 additions & 23 deletions

File tree

src/agentex/lib/core/tracing/code_revision.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
"""Opt-in stamping of the agent's source commit onto its spans.
1+
"""Stamping of the agent's source commit onto its spans.
22
3-
Nothing is stamped until the agent calls :func:`enable`, mirroring the
4-
``lineage`` registry next door: a process-wide switch the agent sets once at
5-
import, rather than automatic behaviour every agent inherits. When enabled the
6-
resolved commit lands in span data under ``__commit_sha__`` and is searchable in
7-
the SGP Traces UI as ``__commit_sha__:<sha>``.
3+
Stamping turns on when the process starts with ``AGENT_COMMIT_SHA`` set, which
4+
the SGP cloud deploy does from the build record's attested commit, or when the
5+
agent calls :func:`enable` itself. Nothing is stamped otherwise: upgrading the
6+
SDK alone never starts emitting the field. When on, the resolved commit lands in
7+
span data under ``__commit_sha__`` and is searchable in the SGP Traces UI as
8+
``__commit_sha__:<sha>``.
89
910
This is deliberately separate from ``__agent_version__``, which is automatic and
1011
carries the deployed image tag verbatim ("image tag or git sha"). That tag is a
@@ -42,13 +43,16 @@
4243

4344

4445
def enable(commit_sha: str | None = None) -> None:
45-
"""Opt this process in to stamping ``__commit_sha__`` onto every span.
46+
"""Turn on stamping ``__commit_sha__`` onto every span from this process.
4647
4748
Value precedence: the explicit ``commit_sha`` argument, else
4849
``AGENT_COMMIT_SHA``, else ``AGENT_VERSION`` when the deployment happened to
4950
set it to a bare commit SHA. A value that is not a git object name is
5051
refused with a warning and leaves stamping off -- better an absent field
5152
than one named for a commit that holds an image tag.
53+
54+
Called once at import when ``AGENT_COMMIT_SHA`` is set, so a deployment that
55+
supplies the commit needs no code change in the agent.
5256
"""
5357
global _commit_sha
5458

@@ -103,3 +107,12 @@ def is_enabled() -> bool:
103107
def commit_sha() -> str | None:
104108
"""The resolved commit SHA, or ``None`` when stamping is not enabled."""
105109
return _commit_sha
110+
111+
112+
def _enable_from_environment() -> None:
113+
"""Auto-enable on ``AGENT_COMMIT_SHA`` only; ``AGENT_VERSION`` stays an explicit fallback."""
114+
if os.environ.get(_COMMIT_SHA_ENV, "").strip():
115+
enable()
116+
117+
118+
_enable_from_environment()

src/agentex/lib/environment_variables.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,8 @@ class EnvironmentVariables(BaseModel):
6868
AGENT_ID: str | None = None
6969
# Build/version discriminator (image tag or git sha), set by the deployment
7070
AGENT_VERSION: str | None = None
71-
# The agent's source commit, baked into the image or set by the deployment.
72-
# Unlike AGENT_VERSION this is expected to be a git SHA and nothing else, and
73-
# it is OPT-IN: nothing is stamped unless the agent calls
74-
# `adk.code_revision.enable()`, which also refuses a value that is not a git
75-
# object name. See agentex.lib.core.tracing.code_revision.
71+
# The agent's source commit, set by the deployment or baked into the image; a git
72+
# SHA and nothing else. Stamped as __commit_sha__ when set (see tracing.code_revision).
7673
AGENT_COMMIT_SHA: str | None = None
7774
AGENT_API_KEY: str | None = None
7875
ACP_TYPE: str | None = "async"

tests/lib/core/tracing/processors/test_sgp_tracing_processor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ def test_agent_identity_and_version_stamped_into_span_data(self):
5656

5757
SHA = "b362b171a9c4e1f09d8e7a6b5c4d3e2f1a0b9c8d"
5858

59-
def test_commit_sha_is_not_stamped_without_opt_in(self, monkeypatch):
60-
"""Upgrading the SDK must not start emitting __commit_sha__ on its own,
61-
even when the environment carries a perfectly good SHA."""
59+
def test_commit_sha_is_not_stamped_when_env_absent(self, monkeypatch):
60+
"""Upgrading the SDK must not start emitting __commit_sha__ on its own;
61+
only AGENT_COMMIT_SHA or an enable() call turns it on."""
6262
from agentex.lib.core.tracing import code_revision
6363
from agentex.lib.core.tracing.processors.sgp_tracing_processor import _sgp_metadata
6464

65-
monkeypatch.setenv("AGENT_COMMIT_SHA", self.SHA)
65+
monkeypatch.delenv("AGENT_COMMIT_SHA", raising=False)
6666
code_revision.disable()
6767

6868
span = _make_span(); span.data = {}

tests/lib/core/tracing/test_code_revision.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
"""Opt-in commit-SHA stamping.
1+
"""Commit-SHA stamping.
22
3-
The contract that matters: an agent that does not call ``enable()`` gets nothing,
4-
so upgrading the SDK never starts emitting this field on its own.
3+
The contract that matters: with ``AGENT_COMMIT_SHA`` absent and no ``enable()``
4+
call, nothing is stamped, so upgrading the SDK never starts emitting this field
5+
on its own. A deployment that sets the env var turns it on without agent code.
56
"""
67

78
from __future__ import annotations
89

10+
import importlib
11+
912
import pytest
1013

1114
from agentex.lib.core.tracing import code_revision
@@ -21,14 +24,32 @@ def _reset():
2124
code_revision.disable()
2225

2326

24-
class TestOptIn:
25-
def test_disabled_by_default(self, monkeypatch):
26-
"""Even with the env fully populated, nothing resolves until enable()."""
27-
monkeypatch.setenv("AGENT_COMMIT_SHA", SHA)
27+
class TestEnablement:
28+
def test_off_when_env_absent(self, monkeypatch):
29+
monkeypatch.delenv("AGENT_COMMIT_SHA", raising=False)
2830
monkeypatch.setenv("AGENT_VERSION", SHA)
31+
importlib.reload(code_revision)
2932
assert code_revision.commit_sha() is None
3033
assert code_revision.is_enabled() is False
3134

35+
def test_env_set_at_startup_enables_without_a_call(self, monkeypatch):
36+
"""The cloud deploy sets AGENT_COMMIT_SHA from the build record; the agent
37+
should not need to know."""
38+
monkeypatch.setenv("AGENT_COMMIT_SHA", SHA)
39+
importlib.reload(code_revision)
40+
assert code_revision.commit_sha() == SHA
41+
42+
def test_env_set_after_import_needs_enable(self, monkeypatch):
43+
monkeypatch.setenv("AGENT_COMMIT_SHA", SHA)
44+
assert code_revision.commit_sha() is None
45+
code_revision.enable()
46+
assert code_revision.commit_sha() == SHA
47+
48+
def test_bad_env_at_startup_leaves_it_off(self, monkeypatch):
49+
monkeypatch.setenv("AGENT_COMMIT_SHA", "latest")
50+
importlib.reload(code_revision)
51+
assert code_revision.commit_sha() is None
52+
3253
def test_enable_reads_agent_commit_sha(self, monkeypatch):
3354
monkeypatch.setenv("AGENT_COMMIT_SHA", SHA)
3455
code_revision.enable()

0 commit comments

Comments
 (0)