|
| 1 | +"""Opt-in commit-SHA stamping. |
| 2 | +
|
| 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. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from agentex.lib.core.tracing import code_revision |
| 12 | + |
| 13 | +SHA = "b362b171a9c4e1f09d8e7a6b5c4d3e2f1a0b9c8d" |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(autouse=True) |
| 17 | +def _reset(): |
| 18 | + """State is process-wide (like the lineage registry), so isolate each test.""" |
| 19 | + code_revision.disable() |
| 20 | + yield |
| 21 | + code_revision.disable() |
| 22 | + |
| 23 | + |
| 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) |
| 28 | + monkeypatch.setenv("AGENT_VERSION", SHA) |
| 29 | + assert code_revision.commit_sha() is None |
| 30 | + assert code_revision.is_enabled() is False |
| 31 | + |
| 32 | + def test_enable_reads_agent_commit_sha(self, monkeypatch): |
| 33 | + monkeypatch.setenv("AGENT_COMMIT_SHA", SHA) |
| 34 | + code_revision.enable() |
| 35 | + assert code_revision.commit_sha() == SHA |
| 36 | + assert code_revision.is_enabled() is True |
| 37 | + |
| 38 | + def test_explicit_argument_wins(self, monkeypatch): |
| 39 | + monkeypatch.setenv("AGENT_COMMIT_SHA", SHA) |
| 40 | + code_revision.enable("7f3a91c2") |
| 41 | + assert code_revision.commit_sha() == "7f3a91c2" |
| 42 | + |
| 43 | + def test_disable_turns_it_back_off(self, monkeypatch): |
| 44 | + monkeypatch.setenv("AGENT_COMMIT_SHA", SHA) |
| 45 | + code_revision.enable() |
| 46 | + code_revision.disable() |
| 47 | + assert code_revision.commit_sha() is None |
| 48 | + |
| 49 | + |
| 50 | +class TestValueIsAlwaysACommit: |
| 51 | + """A field named for a commit must never hold an image tag.""" |
| 52 | + |
| 53 | + @pytest.mark.parametrize( |
| 54 | + "value", |
| 55 | + [ |
| 56 | + "latest", |
| 57 | + "v1.2.3", |
| 58 | + "0.2.4-v4", |
| 59 | + "rocket_mock_agent-b362b171a9c4e1f09d8e7a6b5c4d3e2f1a0b9c8d", # AWS ECR composite |
| 60 | + "abc", # shorter than git's 7-char minimum |
| 61 | + "z" * 40, # right length, not hex |
| 62 | + ], |
| 63 | + ) |
| 64 | + def test_non_sha_is_refused(self, monkeypatch, value): |
| 65 | + monkeypatch.setenv("AGENT_COMMIT_SHA", value) |
| 66 | + code_revision.enable() |
| 67 | + assert code_revision.commit_sha() is None |
| 68 | + |
| 69 | + @pytest.mark.parametrize("value", [SHA, SHA.upper(), "b362b17", "a" * 64]) |
| 70 | + def test_git_object_names_are_accepted(self, monkeypatch, value): |
| 71 | + monkeypatch.setenv("AGENT_COMMIT_SHA", value) |
| 72 | + code_revision.enable() |
| 73 | + assert code_revision.commit_sha() == value |
| 74 | + |
| 75 | + def test_whitespace_only_is_refused(self, monkeypatch): |
| 76 | + monkeypatch.setenv("AGENT_COMMIT_SHA", " ") |
| 77 | + code_revision.enable() |
| 78 | + assert code_revision.commit_sha() is None |
| 79 | + |
| 80 | + def test_enable_with_nothing_available_is_a_no_op(self, monkeypatch): |
| 81 | + monkeypatch.delenv("AGENT_COMMIT_SHA", raising=False) |
| 82 | + monkeypatch.delenv("AGENT_VERSION", raising=False) |
| 83 | + code_revision.enable() |
| 84 | + assert code_revision.commit_sha() is None |
| 85 | + |
| 86 | + |
| 87 | +class TestAgentVersionFallback: |
| 88 | + def test_falls_back_to_agent_version_when_sha_shaped(self, monkeypatch): |
| 89 | + """A platform deploy already sets AGENT_VERSION; on GCP/Azure it is a |
| 90 | + bare SHA, so an opting-in agent needs no extra plumbing.""" |
| 91 | + monkeypatch.delenv("AGENT_COMMIT_SHA", raising=False) |
| 92 | + monkeypatch.setenv("AGENT_VERSION", SHA) |
| 93 | + code_revision.enable() |
| 94 | + assert code_revision.commit_sha() == SHA |
| 95 | + |
| 96 | + def test_does_not_fall_back_to_a_non_sha_agent_version(self, monkeypatch): |
| 97 | + """AGENT_VERSION is 'latest' or an AWS composite much of the time.""" |
| 98 | + monkeypatch.delenv("AGENT_COMMIT_SHA", raising=False) |
| 99 | + monkeypatch.setenv("AGENT_VERSION", "latest") |
| 100 | + code_revision.enable() |
| 101 | + assert code_revision.commit_sha() is None |
| 102 | + |
| 103 | + def test_bad_explicit_value_does_not_fall_through(self, monkeypatch): |
| 104 | + """An explicit AGENT_COMMIT_SHA is a statement of intent: if it is wrong, |
| 105 | + say so rather than silently substituting the image tag.""" |
| 106 | + monkeypatch.setenv("AGENT_COMMIT_SHA", "not-a-sha") |
| 107 | + monkeypatch.setenv("AGENT_VERSION", SHA) |
| 108 | + code_revision.enable() |
| 109 | + assert code_revision.commit_sha() is None |
0 commit comments