nat.plugins.a365.turn_context#

Per-turn A365 identity propagated from front-end handlers to telemetry exporter.

The Microsoft Agents SDK exposes the active agent’s app id and tenant on the inbound Activity (get_agentic_instance_id() / get_agentic_tenant_id()). The A365 backend validates that the agent_id in the export URL matches the appid claim in the bearer token, so telemetry must be partitioned by the same identity that authenticated the turn — not by static config.

This module owns the contextvar that ties the two together. The front-end worker writes it for the lifetime of a turn; the telemetry exporter reads it when stamping span attributes and when looking up the cached token.

Attributes#

Classes#

A365TurnIdentity

Identity of the agent serving the current turn.

Functions#

get_turn_identity(→ A365TurnIdentity | None)

Return the identity of the current turn, or None if not in a turn.

set_turn_identity(→ collections.abc.Iterator[None])

Bind identity for the duration of the with-block, restoring on exit.

_call_if_callable(→ Any)

_get_attr(→ Any)

_lookup_mapping_or_attr(→ Any)

_as_nonempty_str(→ str | None)

_first_nonempty(→ str | None)

_normalize_channel_name(→ str | None)

_extract_client_address(→ str | None)

_is_agentic_via_role(→ bool)

extract_identity_from_activity(→ A365TurnIdentity | None)

Pull A365 identity from a Microsoft Agents Activity (or duck-typed shape).

Module Contents#

logger#
_AGENTIC_ROLES#
class A365TurnIdentity#

Identity of the agent serving the current turn.

agent_app_id: str#
tenant_id: str | None#
on_behalf_user_id: str | None#
conversation_id: str | None = None#
channel_name: str | None = None#
service_url: str | None = None#
user_id: str | None = None#
user_name: str | None = None#
user_email: str | None = None#
client_address: str | None = None#
_TURN_IDENTITY: contextvars.ContextVar[A365TurnIdentity | None]#
get_turn_identity() A365TurnIdentity | None#

Return the identity of the current turn, or None if not in a turn.

set_turn_identity(
identity: A365TurnIdentity | None,
) collections.abc.Iterator[None]#

Bind identity for the duration of the with-block, restoring on exit.

_call_if_callable(obj: Any, name: str) Any#
_get_attr(obj: Any, *names: str) Any#
_lookup_mapping_or_attr(obj: Any, *names: str) Any#
_as_nonempty_str(value: Any) str | None#
_first_nonempty(*values: Any) str | None#
_normalize_channel_name(channel_name: Any) str | None#
_extract_client_address(activity: Any) str | None#
_is_agentic_via_role(activity: Any) bool#
extract_identity_from_activity(
activity: Any,
) A365TurnIdentity | None#

Pull A365 identity from a Microsoft Agents Activity (or duck-typed shape).

Returns None when the activity is not an agentic request, or when no agent app id can be determined. Agent app id is required because the A365 export URL depends on it.