|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | +"""DurabilityContext — recovery-awareness state exposed to response handlers. |
| 4 | +
|
| 5 | +Per spec 015 FR-040 / FR-005, the handler-facing metadata wrapper rejects |
| 6 | +any key (or named-namespace name) starting with ``_`` so that response |
| 7 | +handlers cannot accidentally collide with framework-reserved namespaces |
| 8 | +(e.g. ``_responses``). The framework layer reaches those namespaces via |
| 9 | +the underlying :class:`~azure.ai.agentserver.core.durable.TaskContext` |
| 10 | +directly — the primitive itself does not enforce the convention. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +from collections.abc import Iterator, MutableMapping |
| 16 | +from typing import Any, Literal, Optional |
| 17 | + |
| 18 | +DurabilityEntryMode = Literal["fresh", "recovered"] |
| 19 | + |
| 20 | + |
| 21 | +class _DeveloperMetadataFacade(MutableMapping[str, Any]): |
| 22 | + """Handler-facing wrapper over a ``TaskMetadata``-like backing store. |
| 23 | +
|
| 24 | + Provides the same dict-like + callable shape as |
| 25 | + :class:`~azure.ai.agentserver.core.durable.TaskMetadata` but rejects |
| 26 | + any key (or namespace name) starting with ``_``. Framework layers |
| 27 | + that need to write into reserved namespaces (e.g. ``_responses``) |
| 28 | + must use the underlying ``TaskContext.metadata`` directly — they do |
| 29 | + NOT go through this wrapper. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self, raw: Any, _namespaces: Optional[dict[str, Any]] = None) -> None: |
| 33 | + self._raw = raw |
| 34 | + # For plain-dict backing stores (used in unit tests where the |
| 35 | + # backing object isn't a real TaskMetadata), maintain a private |
| 36 | + # per-namespace dict registry so ``facade(name)`` returns a |
| 37 | + # genuinely isolated store. For real TaskMetadata stores (callable), |
| 38 | + # the underlying primitive owns the registry. |
| 39 | + self._namespaces: dict[str, Any] = _namespaces if _namespaces is not None else {} |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def _check_key(key: Any) -> None: |
| 43 | + if isinstance(key, str) and key.startswith("_"): |
| 44 | + raise ValueError( |
| 45 | + f"metadata keys starting with '_' are reserved for " |
| 46 | + f"framework-internal namespaces (got {key!r}). Pick a " |
| 47 | + f"non-underscore-prefixed name." |
| 48 | + ) |
| 49 | + |
| 50 | + def __getitem__(self, key: str) -> Any: |
| 51 | + self._check_key(key) |
| 52 | + return self._raw[key] |
| 53 | + |
| 54 | + def __setitem__(self, key: str, value: Any) -> None: |
| 55 | + self._check_key(key) |
| 56 | + self._raw[key] = value |
| 57 | + |
| 58 | + def __delitem__(self, key: str) -> None: |
| 59 | + self._check_key(key) |
| 60 | + del self._raw[key] |
| 61 | + |
| 62 | + def __iter__(self) -> Iterator[str]: |
| 63 | + return iter(k for k in self._raw if not (isinstance(k, str) and k.startswith("_"))) |
| 64 | + |
| 65 | + def __len__(self) -> int: |
| 66 | + return sum(1 for k in self._raw if not (isinstance(k, str) and k.startswith("_"))) |
| 67 | + |
| 68 | + def __contains__(self, key: object) -> bool: |
| 69 | + if isinstance(key, str) and key.startswith("_"): |
| 70 | + return False |
| 71 | + return key in self._raw |
| 72 | + |
| 73 | + def get(self, key: str, default: Any = None) -> Any: |
| 74 | + if isinstance(key, str) and key.startswith("_"): |
| 75 | + return default |
| 76 | + return self._raw.get(key, default) |
| 77 | + |
| 78 | + def __call__(self, name: Optional[str] = None) -> "_DeveloperMetadataFacade": |
| 79 | + """Return a sibling namespace facade. |
| 80 | +
|
| 81 | + ``ctx.metadata`` accesses the default (unnamed) namespace. |
| 82 | + ``ctx.metadata(name)`` accesses a named namespace. |
| 83 | +
|
| 84 | + :raises ValueError: If ``name`` starts with ``_`` (reserved). |
| 85 | + """ |
| 86 | + if name is None: |
| 87 | + return self |
| 88 | + if not isinstance(name, str): |
| 89 | + raise TypeError( |
| 90 | + f"namespace name must be a str, got {type(name).__name__}" |
| 91 | + ) |
| 92 | + if name.startswith("_"): |
| 93 | + raise ValueError( |
| 94 | + f"named namespace {name!r} starts with '_', which is " |
| 95 | + f"reserved for framework-internal layers (e.g. " |
| 96 | + f"'_responses'). Pick a non-underscore-prefixed name." |
| 97 | + ) |
| 98 | + raw = self._raw |
| 99 | + if callable(raw): |
| 100 | + sub = raw(name) |
| 101 | + return _DeveloperMetadataFacade(sub) |
| 102 | + # Plain-dict fallback: keep an isolated sub-dict per namespace |
| 103 | + sub = self._namespaces.setdefault(name, {}) |
| 104 | + return _DeveloperMetadataFacade(sub) |
| 105 | + |
| 106 | + async def flush(self) -> None: |
| 107 | + """Force-persist any pending metadata writes for this namespace. |
| 108 | +
|
| 109 | + Delegates to the underlying ``TaskMetadata.flush()`` when present. |
| 110 | + For non-durable / transient contexts (e.g. ``store=false`` responses |
| 111 | + or unit tests where the backing store is a plain ``dict``), this |
| 112 | + is a no-op. |
| 113 | + """ |
| 114 | + flush = getattr(self._raw, "flush", None) |
| 115 | + if callable(flush): |
| 116 | + import asyncio # local import to avoid top-level cycle # noqa: PLC0415 |
| 117 | + |
| 118 | + result = flush() |
| 119 | + if asyncio.iscoroutine(result): |
| 120 | + await result |
| 121 | + |
| 122 | + |
| 123 | +class DurabilityContext: |
| 124 | + """Recovery-awareness context exposed to response handlers. |
| 125 | +
|
| 126 | + All properties are read-only except :attr:`metadata`, which is a |
| 127 | + mutable mapping (also callable for named namespaces) for |
| 128 | + developer-controlled checkpointing. |
| 129 | +
|
| 130 | + :param entry_mode: How the handler was entered — ``"fresh"`` for |
| 131 | + normal invocation or ``"recovered"`` after a crash. |
| 132 | + :param retry_attempt: Retry attempt counter — durable across crash |
| 133 | + recovery. Resets to 0 on a successful invocation chain; increments |
| 134 | + only on retryable failures. |
| 135 | + :param was_steered: Whether this invocation resulted from steering. |
| 136 | + :param pending_inputs: Number of queued steering inputs after this one. |
| 137 | + :param metadata: Developer-accessible checkpoint store. Use |
| 138 | + ``ctx.metadata`` for the default namespace or |
| 139 | + ``ctx.metadata(name)`` for a named namespace. |
| 140 | + """ |
| 141 | + |
| 142 | + __slots__ = ( |
| 143 | + "_entry_mode", |
| 144 | + "_retry_attempt", |
| 145 | + "_was_steered", |
| 146 | + "_pending_inputs", |
| 147 | + "_metadata", |
| 148 | + ) |
| 149 | + |
| 150 | + def __init__( |
| 151 | + self, |
| 152 | + *, |
| 153 | + entry_mode: DurabilityEntryMode, |
| 154 | + retry_attempt: int, |
| 155 | + was_steered: bool, |
| 156 | + pending_inputs: int, |
| 157 | + metadata: Any, |
| 158 | + ) -> None: |
| 159 | + self._entry_mode = entry_mode |
| 160 | + self._retry_attempt = retry_attempt |
| 161 | + self._was_steered = was_steered |
| 162 | + self._pending_inputs = pending_inputs |
| 163 | + self._metadata = ( |
| 164 | + metadata |
| 165 | + if isinstance(metadata, _DeveloperMetadataFacade) |
| 166 | + else _DeveloperMetadataFacade(metadata) |
| 167 | + ) |
| 168 | + |
| 169 | + @property |
| 170 | + def entry_mode(self) -> DurabilityEntryMode: |
| 171 | + """How the handler was entered: ``'fresh'`` or ``'recovered'``.""" |
| 172 | + return self._entry_mode |
| 173 | + |
| 174 | + @property |
| 175 | + def is_recovery(self) -> bool: |
| 176 | + """Convenience: True when this is a recovered re-invocation after a crash. |
| 177 | +
|
| 178 | + Equivalent to ``entry_mode == "recovered"``. |
| 179 | + """ |
| 180 | + return self._entry_mode == "recovered" |
| 181 | + |
| 182 | + @property |
| 183 | + def retry_attempt(self) -> int: |
| 184 | + """Retry attempt counter — durable across crash recovery. |
| 185 | +
|
| 186 | + Resets to 0 on a successful invocation; increments only when the |
| 187 | + handler is re-invoked due to a retryable failure. The value is |
| 188 | + persisted to the task store at lifecycle boundaries, so it is |
| 189 | + stable across both in-process retries and post-crash recovery. |
| 190 | +
|
| 191 | + Per spec 015 FR-001/FR-002, this counter unifies the previous |
| 192 | + ``run_attempt`` (per-process) and the cross-lifetime intent: the |
| 193 | + framework now tracks a single durable retry count. |
| 194 | + """ |
| 195 | + return self._retry_attempt |
| 196 | + |
| 197 | + @property |
| 198 | + def was_steered(self) -> bool: |
| 199 | + """Whether this invocation was triggered by a steering input.""" |
| 200 | + return self._was_steered |
| 201 | + |
| 202 | + @property |
| 203 | + def pending_inputs(self) -> int: |
| 204 | + """Number of queued steering inputs remaining after this one.""" |
| 205 | + return self._pending_inputs |
| 206 | + |
| 207 | + @property |
| 208 | + def metadata(self) -> _DeveloperMetadataFacade: |
| 209 | + """Developer-accessible checkpoint store. |
| 210 | +
|
| 211 | + Use ``ctx.metadata["key"] = value`` for the default namespace, or |
| 212 | + ``ctx.metadata("my_namespace")["key"] = value`` for a named |
| 213 | + namespace. Keys (and namespace names) starting with ``_`` are |
| 214 | + rejected — those are reserved for framework-internal layers. |
| 215 | + """ |
| 216 | + return self._metadata |
0 commit comments