digitalkin 0.3.0rc2__py3-none-any.whl → 0.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. digitalkin/__version__.py +1 -1
  2. digitalkin/core/common/__init__.py +9 -0
  3. digitalkin/core/common/factories.py +156 -0
  4. digitalkin/core/job_manager/base_job_manager.py +128 -28
  5. digitalkin/core/job_manager/single_job_manager.py +80 -25
  6. digitalkin/core/job_manager/taskiq_broker.py +114 -19
  7. digitalkin/core/job_manager/taskiq_job_manager.py +291 -39
  8. digitalkin/core/task_manager/base_task_manager.py +539 -0
  9. digitalkin/core/task_manager/local_task_manager.py +108 -0
  10. digitalkin/core/task_manager/remote_task_manager.py +87 -0
  11. digitalkin/core/task_manager/surrealdb_repository.py +43 -4
  12. digitalkin/core/task_manager/task_executor.py +249 -0
  13. digitalkin/core/task_manager/task_session.py +95 -17
  14. digitalkin/grpc_servers/module_server.py +2 -2
  15. digitalkin/grpc_servers/module_servicer.py +21 -12
  16. digitalkin/grpc_servers/registry_server.py +1 -1
  17. digitalkin/grpc_servers/registry_servicer.py +4 -4
  18. digitalkin/grpc_servers/utils/grpc_error_handler.py +53 -0
  19. digitalkin/models/core/task_monitor.py +17 -0
  20. digitalkin/models/module/module_context.py +5 -0
  21. digitalkin/models/module/module_types.py +299 -15
  22. digitalkin/modules/_base_module.py +66 -28
  23. digitalkin/services/cost/grpc_cost.py +8 -41
  24. digitalkin/services/filesystem/grpc_filesystem.py +9 -38
  25. digitalkin/services/services_config.py +11 -0
  26. digitalkin/services/services_models.py +3 -1
  27. digitalkin/services/setup/default_setup.py +5 -6
  28. digitalkin/services/setup/grpc_setup.py +51 -14
  29. digitalkin/services/storage/grpc_storage.py +2 -2
  30. digitalkin/services/user_profile/__init__.py +12 -0
  31. digitalkin/services/user_profile/default_user_profile.py +55 -0
  32. digitalkin/services/user_profile/grpc_user_profile.py +69 -0
  33. digitalkin/services/user_profile/user_profile_strategy.py +40 -0
  34. digitalkin/utils/__init__.py +28 -0
  35. digitalkin/utils/dynamic_schema.py +483 -0
  36. {digitalkin-0.3.0rc2.dist-info → digitalkin-0.3.1.dist-info}/METADATA +8 -8
  37. {digitalkin-0.3.0rc2.dist-info → digitalkin-0.3.1.dist-info}/RECORD +41 -29
  38. modules/dynamic_setup_module.py +362 -0
  39. digitalkin/core/task_manager/task_manager.py +0 -442
  40. {digitalkin-0.3.0rc2.dist-info → digitalkin-0.3.1.dist-info}/WHEEL +0 -0
  41. {digitalkin-0.3.0rc2.dist-info → digitalkin-0.3.1.dist-info}/licenses/LICENSE +0 -0
  42. {digitalkin-0.3.0rc2.dist-info → digitalkin-0.3.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,55 @@
1
+ """Default user profile implementation."""
2
+
3
+ from typing import Any
4
+
5
+ from digitalkin.logger import logger
6
+ from digitalkin.services.user_profile.user_profile_strategy import (
7
+ UserProfileServiceError,
8
+ UserProfileStrategy,
9
+ )
10
+
11
+
12
+ class DefaultUserProfile(UserProfileStrategy):
13
+ """Default user profile strategy with in-memory storage."""
14
+
15
+ def __init__(
16
+ self,
17
+ mission_id: str,
18
+ setup_id: str,
19
+ setup_version_id: str,
20
+ ) -> None:
21
+ """Initialize the strategy.
22
+
23
+ Args:
24
+ mission_id: The ID of the mission this strategy is associated with
25
+ setup_id: The ID of the setup
26
+ setup_version_id: The ID of the setup version
27
+ """
28
+ super().__init__(mission_id=mission_id, setup_id=setup_id, setup_version_id=setup_version_id)
29
+ self.db: dict[str, dict[str, Any]] = {}
30
+
31
+ def get_user_profile(self) -> dict[str, Any]:
32
+ """Get user profile from in-memory storage.
33
+
34
+ Returns:
35
+ dict[str, Any]: User profile data
36
+
37
+ Raises:
38
+ UserProfileServiceError: If the user profile is not found
39
+ """
40
+ if self.mission_id not in self.db:
41
+ msg = f"User profile for mission {self.mission_id} not found in the database."
42
+ logger.warning(msg)
43
+ raise UserProfileServiceError(msg)
44
+
45
+ logger.debug(f"Retrieved user profile for mission_id: {self.mission_id}")
46
+ return self.db[self.mission_id]
47
+
48
+ def add_user_profile(self, user_profile_data: dict[str, Any]) -> None:
49
+ """Add a user profile to the in-memory database (helper for testing).
50
+
51
+ Args:
52
+ user_profile_data: Dictionary containing user profile data
53
+ """
54
+ self.db[self.mission_id] = user_profile_data
55
+ logger.debug(f"Added user profile for mission_id: {self.mission_id}")
@@ -0,0 +1,69 @@
1
+ """Digital Kin UserProfile Service gRPC Client."""
2
+
3
+ from typing import Any
4
+
5
+ from digitalkin_proto.agentic_mesh_protocol.user_profile.v1 import (
6
+ user_profile_pb2,
7
+ user_profile_service_pb2_grpc,
8
+ )
9
+ from google.protobuf import json_format
10
+
11
+ from digitalkin.grpc_servers.utils.grpc_client_wrapper import GrpcClientWrapper
12
+ from digitalkin.grpc_servers.utils.grpc_error_handler import GrpcErrorHandlerMixin
13
+ from digitalkin.logger import logger
14
+ from digitalkin.models.grpc_servers.models import ClientConfig
15
+ from digitalkin.services.user_profile.user_profile_strategy import UserProfileServiceError, UserProfileStrategy
16
+
17
+
18
+ class GrpcUserProfile(UserProfileStrategy, GrpcClientWrapper, GrpcErrorHandlerMixin):
19
+ """This class implements the gRPC user profile service."""
20
+
21
+ def __init__(
22
+ self,
23
+ mission_id: str,
24
+ setup_id: str,
25
+ setup_version_id: str,
26
+ client_config: ClientConfig,
27
+ ) -> None:
28
+ """Initialize the user profile service.
29
+
30
+ Args:
31
+ mission_id: The ID of the mission this strategy is associated with
32
+ setup_id: The ID of the setup
33
+ setup_version_id: The ID of the setup version
34
+ client_config: Client configuration for gRPC connection
35
+ """
36
+ super().__init__(mission_id=mission_id, setup_id=setup_id, setup_version_id=setup_version_id)
37
+ channel = self._init_channel(client_config)
38
+ self.stub = user_profile_service_pb2_grpc.UserProfileServiceStub(channel)
39
+ logger.debug("Channel client 'UserProfile' initialized successfully")
40
+
41
+ def get_user_profile(self) -> dict[str, Any]:
42
+ """Get user profile by mission_id (which maps to user_id).
43
+
44
+ Returns:
45
+ dict[str, Any]: User profile data
46
+
47
+ Raises:
48
+ UserProfileServiceError: If the user profile cannot be retrieved
49
+ ServerError: If gRPC operation fails
50
+ """
51
+ with self.handle_grpc_errors("GetUserProfile", UserProfileServiceError):
52
+ # mission_id maps to the user context in the proto request
53
+ request = user_profile_pb2.GetUserProfileRequest(mission_id=self.mission_id)
54
+ response = self.exec_grpc_query("GetUserProfile", request)
55
+
56
+ if not response.success:
57
+ msg = f"Failed to get user profile for mission_id: {self.mission_id}"
58
+ logger.error(msg)
59
+ raise UserProfileServiceError(msg)
60
+
61
+ # Convert proto to dict
62
+ user_profile_dict = json_format.MessageToDict(
63
+ response.user_profile,
64
+ preserving_proto_field_name=True,
65
+ always_print_fields_with_no_presence=True,
66
+ )
67
+
68
+ logger.debug(f"Retrieved user profile for mission_id: {self.mission_id}")
69
+ return user_profile_dict
@@ -0,0 +1,40 @@
1
+ """This module contains the abstract base class for UserProfile strategies."""
2
+
3
+ from abc import ABC, abstractmethod
4
+ from typing import Any
5
+
6
+ from digitalkin.services.base_strategy import BaseStrategy
7
+
8
+
9
+ class UserProfileServiceError(Exception):
10
+ """Base exception for UserProfile service errors."""
11
+
12
+
13
+ class UserProfileStrategy(BaseStrategy, ABC):
14
+ """Abstract base class for UserProfile strategies."""
15
+
16
+ def __init__(
17
+ self,
18
+ mission_id: str,
19
+ setup_id: str,
20
+ setup_version_id: str,
21
+ ) -> None:
22
+ """Initialize the strategy.
23
+
24
+ Args:
25
+ mission_id: The ID of the mission this strategy is associated with
26
+ setup_id: The ID of the setup
27
+ setup_version_id: The ID of the setup version this strategy is associated with
28
+ """
29
+ super().__init__(mission_id, setup_id, setup_version_id)
30
+
31
+ @abstractmethod
32
+ def get_user_profile(self) -> dict[str, Any]:
33
+ """Get user profile data.
34
+
35
+ Returns:
36
+ dict[str, Any]: User profile data
37
+
38
+ Raises:
39
+ UserProfileServiceError: If the user profile cannot be retrieved
40
+ """
@@ -1 +1,29 @@
1
1
  """General utils folder."""
2
+
3
+ from digitalkin.utils.dynamic_schema import (
4
+ DEFAULT_TIMEOUT,
5
+ DynamicField,
6
+ Fetcher,
7
+ ResolveResult,
8
+ get_dynamic_metadata,
9
+ get_fetchers,
10
+ has_dynamic,
11
+ resolve,
12
+ resolve_safe,
13
+ )
14
+
15
+ # Alias for cleaner API: `Dynamic` is shorter than `DynamicField`
16
+ Dynamic = DynamicField
17
+
18
+ __all__ = [
19
+ "DEFAULT_TIMEOUT",
20
+ "Dynamic",
21
+ "DynamicField",
22
+ "Fetcher",
23
+ "ResolveResult",
24
+ "get_dynamic_metadata",
25
+ "get_fetchers",
26
+ "has_dynamic",
27
+ "resolve",
28
+ "resolve_safe",
29
+ ]
@@ -0,0 +1,483 @@
1
+ """Dynamic schema utilities for runtime value refresh in Pydantic models.
2
+
3
+ This module provides a clean way to mark fields as dynamic using Annotated metadata,
4
+ allowing their schema values to be refreshed at runtime via sync or async fetchers.
5
+
6
+ Example:
7
+ from typing import Annotated
8
+ from digitalkin.utils import DynamicField
9
+
10
+ class AgentSetup(SetupModel):
11
+ model_name: Annotated[str, DynamicField(enum=fetch_models)] = Field(default="gpt-4")
12
+
13
+ See Also:
14
+ - Documentation: docs/api/dynamic_schema.md
15
+ - Tests: tests/utils/test_dynamic_schema.py
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import asyncio
21
+ import time
22
+ import traceback
23
+ from collections.abc import Awaitable, Callable
24
+ from dataclasses import dataclass, field
25
+ from itertools import starmap
26
+ from typing import TYPE_CHECKING, Any, TypeVar
27
+
28
+ from digitalkin.logger import logger
29
+
30
+ if TYPE_CHECKING:
31
+ from pydantic.fields import FieldInfo
32
+
33
+ T = TypeVar("T")
34
+
35
+ # Fetcher callable type: sync or async function with no arguments
36
+ Fetcher = Callable[[], T | Awaitable[T]]
37
+
38
+ # Default timeout for fetcher resolution (None = no timeout)
39
+ DEFAULT_TIMEOUT: float | None = None
40
+
41
+
42
+ @dataclass
43
+ class ResolveResult:
44
+ """Result of resolving dynamic fetchers.
45
+
46
+ Provides structured access to resolved values and any errors that occurred.
47
+ This allows callers to handle partial failures gracefully.
48
+
49
+ Attributes:
50
+ values: Dict mapping key names to successfully resolved values.
51
+ errors: Dict mapping key names to exceptions that occurred during resolution.
52
+ """
53
+
54
+ values: dict[str, Any] = field(default_factory=dict)
55
+ errors: dict[str, Exception] = field(default_factory=dict)
56
+
57
+ @property
58
+ def success(self) -> bool:
59
+ """Check if all fetchers resolved successfully.
60
+
61
+ Returns:
62
+ True if no errors occurred, False otherwise.
63
+ """
64
+ return len(self.errors) == 0
65
+
66
+ @property
67
+ def partial(self) -> bool:
68
+ """Check if some but not all fetchers succeeded.
69
+
70
+ Returns:
71
+ True if there are both values and errors, False otherwise.
72
+ """
73
+ return len(self.values) > 0 and len(self.errors) > 0
74
+
75
+ def get(self, key: str, default: T | None = None) -> T | None:
76
+ """Get a resolved value by key.
77
+
78
+ Args:
79
+ key: The fetcher key name.
80
+ default: Default value if key not found or errored.
81
+
82
+ Returns:
83
+ The resolved value or default.
84
+ """
85
+ return self.values.get(key, default) # type: ignore[return-value]
86
+
87
+
88
+ class DynamicField:
89
+ """Metadata class for Annotated fields with dynamic fetchers.
90
+
91
+ Use with typing.Annotated to mark fields that need runtime value resolution.
92
+ Fetchers are callables (sync or async) that return values at runtime.
93
+
94
+ Args:
95
+ **fetchers: Mapping of key names to fetcher callables.
96
+ Each fetcher is a function (sync or async) that takes no arguments
97
+ and returns the value for that key (e.g., enum values, defaults).
98
+
99
+ Example:
100
+ from typing import Annotated
101
+
102
+ async def fetch_models() -> list[str]:
103
+ return await api.get_models()
104
+
105
+ class Setup(SetupModel):
106
+ model: Annotated[str, DynamicField(enum=fetch_models)] = Field(default="gpt-4")
107
+ """
108
+
109
+ __slots__ = ("fetchers",)
110
+
111
+ def __init__(self, **fetchers: Fetcher[Any]) -> None:
112
+ """Initialize with fetcher callables."""
113
+ self.fetchers: dict[str, Fetcher[Any]] = fetchers
114
+
115
+ def __repr__(self) -> str:
116
+ """Return string representation."""
117
+ keys = ", ".join(self.fetchers.keys())
118
+ return f"DynamicField({keys})"
119
+
120
+ def __eq__(self, other: object) -> bool:
121
+ """Check equality based on fetchers.
122
+
123
+ Returns:
124
+ True if fetchers are equal, NotImplemented for non-DynamicField types.
125
+ """
126
+ if not isinstance(other, DynamicField):
127
+ return NotImplemented
128
+ return self.fetchers == other.fetchers
129
+
130
+ def __hash__(self) -> int:
131
+ """Hash based on fetcher keys (fetchers themselves aren't hashable).
132
+
133
+ Returns:
134
+ Hash value based on sorted fetcher keys.
135
+ """
136
+ return hash(tuple(sorted(self.fetchers.keys())))
137
+
138
+
139
+ def get_dynamic_metadata(field_info: FieldInfo) -> DynamicField | None:
140
+ """Extract DynamicField metadata from a FieldInfo's metadata list.
141
+
142
+ Args:
143
+ field_info: The Pydantic FieldInfo object to inspect.
144
+
145
+ Returns:
146
+ The DynamicField metadata instance if found, None otherwise.
147
+ """
148
+ for meta in field_info.metadata:
149
+ if isinstance(meta, DynamicField):
150
+ return meta
151
+ return None
152
+
153
+
154
+ def has_dynamic(field_info: FieldInfo) -> bool:
155
+ """Check if a field has DynamicField metadata.
156
+
157
+ Args:
158
+ field_info: The Pydantic FieldInfo object to check.
159
+
160
+ Returns:
161
+ True if the field has DynamicField metadata, False otherwise.
162
+ """
163
+ return get_dynamic_metadata(field_info) is not None
164
+
165
+
166
+ def get_fetchers(field_info: FieldInfo) -> dict[str, Fetcher[Any]]:
167
+ """Extract fetchers from a field's DynamicField metadata.
168
+
169
+ Args:
170
+ field_info: The Pydantic FieldInfo object to extract from.
171
+
172
+ Returns:
173
+ Dict mapping key names to fetcher callables, empty if no DynamicField metadata.
174
+ """
175
+ meta = get_dynamic_metadata(field_info)
176
+ if meta is None:
177
+ return {}
178
+ return meta.fetchers
179
+
180
+
181
+ def _get_fetcher_info(fetcher: Fetcher[Any]) -> str:
182
+ """Get descriptive info about a fetcher for logging.
183
+
184
+ Args:
185
+ fetcher: The fetcher callable.
186
+
187
+ Returns:
188
+ A string describing the fetcher (module.name or repr).
189
+ """
190
+ if hasattr(fetcher, "__module__") and hasattr(fetcher, "__qualname__"):
191
+ return f"{fetcher.__module__}.{fetcher.__qualname__}"
192
+ if hasattr(fetcher, "__name__"):
193
+ return fetcher.__name__
194
+ return repr(fetcher)
195
+
196
+
197
+ async def _resolve_one(key: str, fetcher: Fetcher[Any]) -> tuple[str, Any]:
198
+ """Resolve a single fetcher.
199
+
200
+ Args:
201
+ key: The fetcher key name.
202
+ fetcher: The fetcher callable.
203
+
204
+ Returns:
205
+ Tuple of (key, resolved_value).
206
+
207
+ Raises:
208
+ Exception: If the fetcher raises an exception.
209
+ """
210
+ fetcher_info = _get_fetcher_info(fetcher)
211
+ logger.debug(
212
+ "Resolving fetcher '%s' using %s",
213
+ key,
214
+ fetcher_info,
215
+ extra={"fetcher_key": key, "fetcher": fetcher_info},
216
+ )
217
+
218
+ start_time = time.perf_counter()
219
+
220
+ try:
221
+ result = fetcher()
222
+ is_async = asyncio.iscoroutine(result)
223
+
224
+ if is_async:
225
+ logger.debug(
226
+ "Fetcher '%s' returned coroutine, awaiting...",
227
+ key,
228
+ extra={"fetcher_key": key, "is_async": True},
229
+ )
230
+ result = await result
231
+
232
+ except Exception as e:
233
+ elapsed_ms = (time.perf_counter() - start_time) * 1000
234
+ logger.error(
235
+ "Fetcher '%s' (%s) failed after %.2fms: %s: %s",
236
+ key,
237
+ fetcher_info,
238
+ elapsed_ms,
239
+ type(e).__name__,
240
+ str(e) or "(no message)",
241
+ extra={
242
+ "fetcher_key": key,
243
+ "fetcher": fetcher_info,
244
+ "elapsed_ms": elapsed_ms,
245
+ "error_type": type(e).__name__,
246
+ "error_message": str(e),
247
+ "traceback": traceback.format_exc(),
248
+ },
249
+ )
250
+ raise
251
+
252
+ elapsed_ms = (time.perf_counter() - start_time) * 1000
253
+
254
+ logger.debug(
255
+ "Fetcher '%s' resolved successfully in %.2fms, result type: %s",
256
+ key,
257
+ elapsed_ms,
258
+ type(result).__name__,
259
+ extra={
260
+ "fetcher_key": key,
261
+ "elapsed_ms": elapsed_ms,
262
+ "result_type": type(result).__name__,
263
+ },
264
+ )
265
+
266
+ return key, result
267
+
268
+
269
+ async def resolve(
270
+ fetchers: dict[str, Fetcher[Any]],
271
+ *,
272
+ timeout: float | None = DEFAULT_TIMEOUT,
273
+ ) -> dict[str, Any]:
274
+ """Resolve all dynamic fetchers to their actual values in parallel.
275
+
276
+ Fetchers are executed concurrently using asyncio.gather() for better
277
+ performance when multiple async fetchers are involved.
278
+
279
+ Args:
280
+ fetchers: Dict mapping key names to fetcher callables.
281
+ timeout: Optional timeout in seconds for all fetchers combined.
282
+ If None (default), no timeout is applied.
283
+
284
+ Returns:
285
+ Dict mapping key names to resolved values.
286
+
287
+ Raises:
288
+ asyncio.TimeoutError: If timeout is exceeded.
289
+ Exception: If any fetcher raises an exception, it is propagated.
290
+
291
+ Example:
292
+ fetchers = {"enum": fetch_models, "default": get_default}
293
+ resolved = await resolve(fetchers, timeout=5.0)
294
+ # resolved = {"enum": ["gpt-4", "gpt-3.5"], "default": "gpt-4"}
295
+ """
296
+ if not fetchers:
297
+ logger.debug("resolve() called with empty fetchers, returning {}")
298
+ return {}
299
+
300
+ fetcher_keys = list(fetchers.keys())
301
+ fetcher_infos = {k: _get_fetcher_info(f) for k, f in fetchers.items()}
302
+
303
+ logger.info(
304
+ "resolve() starting parallel resolution of %d fetcher(s): %s",
305
+ len(fetchers),
306
+ fetcher_keys,
307
+ extra={
308
+ "fetcher_count": len(fetchers),
309
+ "fetcher_keys": fetcher_keys,
310
+ "fetcher_infos": fetcher_infos,
311
+ "timeout": timeout,
312
+ },
313
+ )
314
+
315
+ start_time = time.perf_counter()
316
+
317
+ # Create tasks for parallel execution
318
+ tasks = list(starmap(_resolve_one, fetchers.items()))
319
+
320
+ # Execute with optional timeout
321
+ try:
322
+ if timeout is not None:
323
+ results = await asyncio.wait_for(asyncio.gather(*tasks), timeout=timeout)
324
+ else:
325
+ results = await asyncio.gather(*tasks)
326
+ except asyncio.TimeoutError:
327
+ elapsed_ms = (time.perf_counter() - start_time) * 1000
328
+ logger.error(
329
+ "resolve() timed out after %.2fms (timeout=%.2fs)",
330
+ elapsed_ms,
331
+ timeout,
332
+ extra={"elapsed_ms": elapsed_ms, "timeout": timeout},
333
+ )
334
+ raise
335
+
336
+ elapsed_ms = (time.perf_counter() - start_time) * 1000
337
+ logger.info(
338
+ "resolve() completed successfully in %.2fms, resolved %d fetcher(s)",
339
+ elapsed_ms,
340
+ len(results),
341
+ extra={"elapsed_ms": elapsed_ms, "resolved_count": len(results)},
342
+ )
343
+
344
+ return dict(results)
345
+
346
+
347
+ async def resolve_safe(
348
+ fetchers: dict[str, Fetcher[Any]],
349
+ *,
350
+ timeout: float | None = DEFAULT_TIMEOUT,
351
+ ) -> ResolveResult:
352
+ """Resolve fetchers with structured error handling.
353
+
354
+ Unlike `resolve()`, this function catches individual fetcher errors
355
+ and returns them in a structured result, allowing partial success.
356
+
357
+ Args:
358
+ fetchers: Dict mapping key names to fetcher callables.
359
+ timeout: Optional timeout in seconds for all fetchers combined.
360
+ If None (default), no timeout is applied. Note: timeout applies
361
+ to the entire operation, not individual fetchers.
362
+
363
+ Returns:
364
+ ResolveResult with values and any errors that occurred.
365
+
366
+ Example:
367
+ result = await resolve_safe(fetchers, timeout=5.0)
368
+ if result.success:
369
+ print("All resolved:", result.values)
370
+ elif result.partial:
371
+ print("Partial success:", result.values)
372
+ print("Errors:", result.errors)
373
+ else:
374
+ print("All failed:", result.errors)
375
+ """
376
+ if not fetchers:
377
+ logger.debug("resolve_safe() called with empty fetchers, returning empty ResolveResult")
378
+ return ResolveResult()
379
+
380
+ fetcher_keys = list(fetchers.keys())
381
+ fetcher_infos = {k: _get_fetcher_info(f) for k, f in fetchers.items()}
382
+
383
+ logger.info(
384
+ "resolve_safe() starting parallel resolution of %d fetcher(s): %s",
385
+ len(fetchers),
386
+ fetcher_keys,
387
+ extra={
388
+ "fetcher_count": len(fetchers),
389
+ "fetcher_keys": fetcher_keys,
390
+ "fetcher_infos": fetcher_infos,
391
+ "timeout": timeout,
392
+ },
393
+ )
394
+
395
+ start_time = time.perf_counter()
396
+ result = ResolveResult()
397
+
398
+ async def safe_resolve_one(key: str, fetcher: Fetcher[Any]) -> None:
399
+ """Resolve one fetcher, capturing errors."""
400
+ try:
401
+ _, value = await _resolve_one(key, fetcher)
402
+ result.values[key] = value
403
+ except Exception as e:
404
+ # Error already logged in _resolve_one, just capture it
405
+ result.errors[key] = e
406
+
407
+ # Create tasks for parallel execution
408
+ tasks = list(starmap(safe_resolve_one, fetchers.items()))
409
+
410
+ try:
411
+ if timeout is not None:
412
+ await asyncio.wait_for(asyncio.gather(*tasks), timeout=timeout)
413
+ else:
414
+ await asyncio.gather(*tasks)
415
+ except asyncio.TimeoutError as e:
416
+ elapsed_ms = (time.perf_counter() - start_time) * 1000
417
+ # Add timeout error for any keys that didn't complete
418
+ resolved_keys = set(result.values.keys()) | set(result.errors.keys())
419
+ timed_out_keys = [key for key in fetchers if key not in resolved_keys]
420
+ for key in timed_out_keys:
421
+ result.errors[key] = e
422
+
423
+ logger.error(
424
+ "resolve_safe() timed out after %.2fms (timeout=%.2fs), %d succeeded, %d failed, %d timed out",
425
+ elapsed_ms,
426
+ timeout,
427
+ len(result.values),
428
+ len(result.errors) - len(timed_out_keys),
429
+ len(timed_out_keys),
430
+ extra={
431
+ "elapsed_ms": elapsed_ms,
432
+ "timeout": timeout,
433
+ "succeeded_keys": list(result.values.keys()),
434
+ "failed_keys": [k for k in result.errors if k not in timed_out_keys],
435
+ "timed_out_keys": timed_out_keys,
436
+ },
437
+ )
438
+
439
+ elapsed_ms = (time.perf_counter() - start_time) * 1000
440
+
441
+ # Log summary
442
+ if result.success:
443
+ logger.info(
444
+ "resolve_safe() completed successfully in %.2fms, all %d fetcher(s) resolved",
445
+ elapsed_ms,
446
+ len(result.values),
447
+ extra={
448
+ "elapsed_ms": elapsed_ms,
449
+ "success": True,
450
+ "resolved_count": len(result.values),
451
+ },
452
+ )
453
+ elif result.partial:
454
+ logger.warning(
455
+ "resolve_safe() completed with partial success in %.2fms: %d succeeded, %d failed",
456
+ elapsed_ms,
457
+ len(result.values),
458
+ len(result.errors),
459
+ extra={
460
+ "elapsed_ms": elapsed_ms,
461
+ "success": False,
462
+ "partial": True,
463
+ "resolved_count": len(result.values),
464
+ "error_count": len(result.errors),
465
+ "succeeded_keys": list(result.values.keys()),
466
+ "failed_keys": list(result.errors.keys()),
467
+ },
468
+ )
469
+ else:
470
+ logger.error(
471
+ "resolve_safe() completed with all failures in %.2fms: %d failed",
472
+ elapsed_ms,
473
+ len(result.errors),
474
+ extra={
475
+ "elapsed_ms": elapsed_ms,
476
+ "success": False,
477
+ "partial": False,
478
+ "error_count": len(result.errors),
479
+ "failed_keys": list(result.errors.keys()),
480
+ },
481
+ )
482
+
483
+ return result