Skip to content

Commit 1da24cc

Browse files
ammaaradammicrosoftCopilotankitbko
authored
agentserver: use FOUNDRY_HOSTING_ENVIRONMENT for hosted detection (#46288)
* agentserver: use FOUNDRY_HOSTING_ENVIRONMENT for hosted detection Add is_hosted property to AgentConfig that returns True when FOUNDRY_HOSTING_ENVIRONMENT exists and is non-empty. This variable is injected exclusively by the Foundry platform at container startup. Use config.is_hosted instead of config.project_endpoint in _routing.py for Foundry storage auto-activation, preventing false positives when developers set FOUNDRY_* vars locally for agent_framework features. * 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> * agentserver: add explicit default value to os.environ.get for FOUNDRY_HOSTING_ENVIRONMENT Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-python/sessions/8ffff4c5-be35-4d03-aea9-7e373860be86 Co-authored-by: ankitbko <3169316+ankitbko@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ankitbko <3169316+ankitbko@users.noreply.github.com>
1 parent f847855 commit 1da24cc

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
_ENV_FOUNDRY_AGENT_NAME = "FOUNDRY_AGENT_NAME"
2525
_ENV_FOUNDRY_AGENT_VERSION = "FOUNDRY_AGENT_VERSION"
26+
_ENV_FOUNDRY_HOSTING_ENVIRONMENT = "FOUNDRY_HOSTING_ENVIRONMENT"
2627
_ENV_FOUNDRY_PROJECT_ENDPOINT = "FOUNDRY_PROJECT_ENDPOINT"
2728
_ENV_FOUNDRY_PROJECT_ARM_ID = "FOUNDRY_PROJECT_ARM_ID"
2829
_ENV_FOUNDRY_AGENT_SESSION_ID = "FOUNDRY_AGENT_SESSION_ID"
@@ -53,6 +54,8 @@ class AgentConfig:
5354
:param agent_name: Agent name from ``FOUNDRY_AGENT_NAME``.
5455
:param agent_version: Agent version from ``FOUNDRY_AGENT_VERSION``.
5556
: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``.
5659
:param project_endpoint: Foundry project endpoint from ``FOUNDRY_PROJECT_ENDPOINT``.
5760
:param project_id: Foundry project ARM resource ID from ``FOUNDRY_PROJECT_ARM_ID``.
5861
:param session_id: Default session ID from ``FOUNDRY_AGENT_SESSION_ID``.
@@ -68,6 +71,7 @@ def __init__(
6871
agent_name: str,
6972
agent_version: str,
7073
agent_id: str,
74+
is_hosted: bool,
7175
project_endpoint: str,
7276
project_id: str,
7377
session_id: str,
@@ -79,6 +83,7 @@ def __init__(
7983
self.agent_name = agent_name
8084
self.agent_version = agent_version
8185
self.agent_id = agent_id
86+
self.is_hosted = is_hosted
8287
self.project_endpoint = project_endpoint
8388
self.project_id = project_id
8489
self.session_id = session_id
@@ -108,6 +113,7 @@ def from_env(cls) -> Self:
108113
agent_name=agent_name,
109114
agent_version=agent_version,
110115
agent_id=agent_id,
116+
is_hosted=bool(os.environ.get(_ENV_FOUNDRY_HOSTING_ENVIRONMENT, "")),
111117
project_endpoint=os.environ.get(_ENV_FOUNDRY_PROJECT_ENDPOINT, ""),
112118
project_id=os.environ.get(_ENV_FOUNDRY_PROJECT_ARM_ID, ""),
113119
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

sdk/agentserver/azure-ai-agentserver-responses/azure/ai/agentserver/responses/hosting/_routing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def __init__(
141141
}
142142

143143
if store is None:
144-
if config.project_endpoint:
144+
if config.is_hosted:
145145
from ..store._foundry_provider import FoundryStorageProvider
146146
from ..store._foundry_settings import FoundryStorageSettings
147147

0 commit comments

Comments
 (0)