Skip to content

Commit 08d8454

Browse files
Copilotankitbko
andauthored
agentserver: snapshot is_hosted in AgentConfig.__init__ instead of reading os.environ dynamically
Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-python/sessions/263ad34e-e39a-4de1-aa0d-e0de484300b5 Co-authored-by: ankitbko <3169316+ankitbko@users.noreply.github.com>
1 parent e0f6ad5 commit 08d8454

2 files changed

Lines changed: 44 additions & 12 deletions

File tree

sdk/agentserver/azure-ai-agentserver-core/azure/ai/agentserver/core/_config.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class AgentConfig:
5454
:param agent_name: Agent name from ``FOUNDRY_AGENT_NAME``.
5555
:param agent_version: Agent version from ``FOUNDRY_AGENT_VERSION``.
5656
:param agent_id: Combined identifier (``"name:version"`` or ``"name"`` or ``""``).
57+
:param is_hosted: Whether the agent is running in a Foundry-hosted container environment,
58+
derived from ``FOUNDRY_HOSTING_ENVIRONMENT``.
5759
:param project_endpoint: Foundry project endpoint from ``FOUNDRY_PROJECT_ENDPOINT``.
5860
:param project_id: Foundry project ARM resource ID from ``FOUNDRY_PROJECT_ARM_ID``.
5961
:param session_id: Default session ID from ``FOUNDRY_AGENT_SESSION_ID``.
@@ -69,6 +71,7 @@ def __init__(
6971
agent_name: str,
7072
agent_version: str,
7173
agent_id: str,
74+
is_hosted: bool,
7275
project_endpoint: str,
7376
project_id: str,
7477
session_id: str,
@@ -80,6 +83,7 @@ def __init__(
8083
self.agent_name = agent_name
8184
self.agent_version = agent_version
8285
self.agent_id = agent_id
86+
self.is_hosted = is_hosted
8387
self.project_endpoint = project_endpoint
8488
self.project_id = project_id
8589
self.session_id = session_id
@@ -88,18 +92,6 @@ def __init__(
8892
self.otlp_endpoint = otlp_endpoint
8993
self.sse_keepalive_interval = sse_keepalive_interval
9094

91-
@property
92-
def is_hosted(self) -> bool:
93-
"""Whether the agent is running in a Foundry-hosted container environment.
94-
95-
Returns ``True`` when the platform-injected ``FOUNDRY_HOSTING_ENVIRONMENT``
96-
environment variable exists and is non-empty. This variable is set
97-
exclusively by the Foundry platform at container startup.
98-
99-
:rtype: bool
100-
"""
101-
return bool(os.environ.get(_ENV_FOUNDRY_HOSTING_ENVIRONMENT))
102-
10395
@classmethod
10496
def from_env(cls) -> Self:
10597
"""Create an ``AgentConfig`` by reading all platform environment variables.
@@ -121,6 +113,7 @@ def from_env(cls) -> Self:
121113
agent_name=agent_name,
122114
agent_version=agent_version,
123115
agent_id=agent_id,
116+
is_hosted=bool(os.environ.get(_ENV_FOUNDRY_HOSTING_ENVIRONMENT)),
124117
project_endpoint=os.environ.get(_ENV_FOUNDRY_PROJECT_ENDPOINT, ""),
125118
project_id=os.environ.get(_ENV_FOUNDRY_PROJECT_ARM_ID, ""),
126119
session_id=os.environ.get(_ENV_FOUNDRY_AGENT_SESSION_ID, ""),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
"""Unit tests for AgentConfig."""
5+
import pytest
6+
7+
from azure.ai.agentserver.core._config import AgentConfig
8+
9+
10+
class TestAgentConfigIsHosted:
11+
"""Tests for AgentConfig.is_hosted snapshotting behavior."""
12+
13+
def test_is_hosted_false_when_env_var_absent(self, monkeypatch: pytest.MonkeyPatch) -> None:
14+
"""is_hosted is False when FOUNDRY_HOSTING_ENVIRONMENT is not set."""
15+
monkeypatch.delenv("FOUNDRY_HOSTING_ENVIRONMENT", raising=False)
16+
config = AgentConfig.from_env()
17+
assert config.is_hosted is False
18+
19+
def test_is_hosted_false_when_env_var_empty(self, monkeypatch: pytest.MonkeyPatch) -> None:
20+
"""is_hosted is False when FOUNDRY_HOSTING_ENVIRONMENT is set to an empty string."""
21+
monkeypatch.setenv("FOUNDRY_HOSTING_ENVIRONMENT", "")
22+
config = AgentConfig.from_env()
23+
assert config.is_hosted is False
24+
25+
def test_is_hosted_true_when_env_var_set(self, monkeypatch: pytest.MonkeyPatch) -> None:
26+
"""is_hosted is True when FOUNDRY_HOSTING_ENVIRONMENT is set to a non-empty value."""
27+
monkeypatch.setenv("FOUNDRY_HOSTING_ENVIRONMENT", "production")
28+
config = AgentConfig.from_env()
29+
assert config.is_hosted is True
30+
31+
def test_is_hosted_snapshotted_at_creation(self, monkeypatch: pytest.MonkeyPatch) -> None:
32+
"""is_hosted reflects the env var value at creation time, not at access time."""
33+
monkeypatch.setenv("FOUNDRY_HOSTING_ENVIRONMENT", "production")
34+
config = AgentConfig.from_env()
35+
assert config.is_hosted is True
36+
37+
# Changing the env var after creation must not affect the already-created config.
38+
monkeypatch.delenv("FOUNDRY_HOSTING_ENVIRONMENT")
39+
assert config.is_hosted is True

0 commit comments

Comments
 (0)