multi-agent-observability 0.1.0__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.
Potentially problematic release.
This version of multi-agent-observability might be problematic. Click here for more details.
- ma_trace/__init__.py +204 -0
- ma_trace/_version.py +2 -0
- ma_trace/adapters/__init__.py +9 -0
- ma_trace/adapters/autogen.py +159 -0
- ma_trace/adapters/crewai.py +337 -0
- ma_trace/adapters/langchain.py +395 -0
- ma_trace/adapters/langgraph.py +100 -0
- ma_trace/cli.py +349 -0
- ma_trace/context.py +29 -0
- ma_trace/contracts.py +107 -0
- ma_trace/episode.py +208 -0
- ma_trace/events.py +291 -0
- ma_trace/exporters.py +154 -0
- ma_trace/graph.py +211 -0
- ma_trace/memory.py +174 -0
- ma_trace/metrics.py +340 -0
- ma_trace/py.typed +0 -0
- ma_trace/replay.py +398 -0
- ma_trace/sampling.py +117 -0
- ma_trace/semconv.py +140 -0
- ma_trace/store.py +185 -0
- ma_trace/tracer.py +779 -0
- multi_agent_observability-0.1.0.dist-info/METADATA +365 -0
- multi_agent_observability-0.1.0.dist-info/RECORD +27 -0
- multi_agent_observability-0.1.0.dist-info/WHEEL +4 -0
- multi_agent_observability-0.1.0.dist-info/entry_points.txt +2 -0
- multi_agent_observability-0.1.0.dist-info/licenses/LICENSE +201 -0
ma_trace/store.py
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Episode persistence: one JSON document per episode."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import tempfile
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from .episode import Episode
|
|
14
|
+
|
|
15
|
+
DEFAULT_STORE_DIR = ".ma-trace/episodes"
|
|
16
|
+
ENV_STORE = "MA_TRACE_STORE"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class EpisodeSummary:
|
|
21
|
+
id: str
|
|
22
|
+
name: str
|
|
23
|
+
started_at: float
|
|
24
|
+
duration_s: float
|
|
25
|
+
events: int
|
|
26
|
+
agents: int
|
|
27
|
+
sampled: bool
|
|
28
|
+
seed: int | None
|
|
29
|
+
replay_of: str | None
|
|
30
|
+
|
|
31
|
+
@classmethod
|
|
32
|
+
def of(cls, ep: Episode) -> EpisodeSummary:
|
|
33
|
+
return cls(
|
|
34
|
+
id=ep.id,
|
|
35
|
+
name=ep.name,
|
|
36
|
+
started_at=ep.started_at,
|
|
37
|
+
duration_s=ep.duration_s,
|
|
38
|
+
events=len(ep.events),
|
|
39
|
+
agents=len(ep.agents),
|
|
40
|
+
sampled=ep.sampled,
|
|
41
|
+
seed=ep.seed,
|
|
42
|
+
replay_of=ep.replay_of,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class EpisodeStore:
|
|
47
|
+
"""File-backed store: ``<root>/<episode-id>.json``.
|
|
48
|
+
|
|
49
|
+
``root`` defaults to ``$MA_TRACE_STORE`` or ``./.ma-trace/episodes``. References
|
|
50
|
+
accepted by :meth:`load`, :meth:`delete` and :meth:`resolve` are a full id, a
|
|
51
|
+
unique id prefix, or ``"latest"``.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(self, root: str | os.PathLike[str] | None = None) -> None:
|
|
55
|
+
self.root = Path(root or os.environ.get(ENV_STORE) or DEFAULT_STORE_DIR).expanduser()
|
|
56
|
+
|
|
57
|
+
def _path(self, episode_id: str) -> Path:
|
|
58
|
+
return self.root / f"{episode_id}.json"
|
|
59
|
+
|
|
60
|
+
def save(self, episode: Episode) -> Path:
|
|
61
|
+
self.root.mkdir(parents=True, exist_ok=True)
|
|
62
|
+
target = self._path(episode.id)
|
|
63
|
+
fd, tmp = tempfile.mkstemp(prefix=".tmp-", suffix=".json", dir=self.root)
|
|
64
|
+
try:
|
|
65
|
+
with os.fdopen(fd, "w", encoding="utf-8") as fh:
|
|
66
|
+
fh.write(episode.to_json())
|
|
67
|
+
os.replace(tmp, target)
|
|
68
|
+
finally:
|
|
69
|
+
if os.path.exists(tmp): # pragma: no cover - only on failure
|
|
70
|
+
os.unlink(tmp)
|
|
71
|
+
return target
|
|
72
|
+
|
|
73
|
+
def ids(self) -> list[str]:
|
|
74
|
+
if not self.root.exists():
|
|
75
|
+
return []
|
|
76
|
+
return sorted(p.stem for p in self.root.glob("ep-*.json"))
|
|
77
|
+
|
|
78
|
+
def _recency_key(self, episode_id: str) -> tuple[float, int, str]:
|
|
79
|
+
"""Sort key for ``latest``: the episode's recorded start time first, then the file
|
|
80
|
+
modification time and the id as tie-breakers (file times coincide on file systems with
|
|
81
|
+
coarse timestamps, so they are not reliable on their own)."""
|
|
82
|
+
path = self._path(episode_id)
|
|
83
|
+
try:
|
|
84
|
+
with path.open(encoding="utf-8") as fh:
|
|
85
|
+
started = float(json.load(fh).get("started_at", 0.0))
|
|
86
|
+
except (OSError, ValueError, TypeError):
|
|
87
|
+
started = 0.0
|
|
88
|
+
try:
|
|
89
|
+
mtime = path.stat().st_mtime_ns
|
|
90
|
+
except OSError:
|
|
91
|
+
mtime = 0
|
|
92
|
+
return (started, mtime, episode_id)
|
|
93
|
+
|
|
94
|
+
def resolve(self, ref: str) -> str:
|
|
95
|
+
ids = self.ids()
|
|
96
|
+
if ref == "latest":
|
|
97
|
+
if not ids:
|
|
98
|
+
raise KeyError("no episodes recorded yet")
|
|
99
|
+
return max(ids, key=self._recency_key)
|
|
100
|
+
if ref in ids:
|
|
101
|
+
return ref
|
|
102
|
+
matches = [i for i in ids if i.startswith(ref)]
|
|
103
|
+
if len(matches) == 1:
|
|
104
|
+
return matches[0]
|
|
105
|
+
if not matches:
|
|
106
|
+
raise KeyError(f"no episode matches {ref!r}")
|
|
107
|
+
raise KeyError(f"ambiguous episode reference {ref!r}: {', '.join(matches)}")
|
|
108
|
+
|
|
109
|
+
def load(self, ref: str) -> Episode:
|
|
110
|
+
path = self._path(self.resolve(ref))
|
|
111
|
+
with path.open(encoding="utf-8") as fh:
|
|
112
|
+
return Episode.from_dict(json.load(fh))
|
|
113
|
+
|
|
114
|
+
def list(self, limit: int | None = None) -> list[EpisodeSummary]:
|
|
115
|
+
summaries = [EpisodeSummary.of(self.load(i)) for i in self.ids()]
|
|
116
|
+
summaries.sort(key=lambda s: s.started_at, reverse=True)
|
|
117
|
+
return summaries[:limit] if limit else summaries
|
|
118
|
+
|
|
119
|
+
def delete(self, ref: str) -> None:
|
|
120
|
+
self._path(self.resolve(ref)).unlink()
|
|
121
|
+
|
|
122
|
+
def latest(self) -> Episode | None:
|
|
123
|
+
try:
|
|
124
|
+
return self.load("latest")
|
|
125
|
+
except KeyError:
|
|
126
|
+
return None
|
|
127
|
+
|
|
128
|
+
def __contains__(self, episode_id: object) -> bool:
|
|
129
|
+
return isinstance(episode_id, str) and self._path(episode_id).exists()
|
|
130
|
+
|
|
131
|
+
def __len__(self) -> int:
|
|
132
|
+
return len(self.ids())
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class InMemoryEpisodeStore:
|
|
136
|
+
"""Dictionary-backed store with the same interface as :class:`EpisodeStore` (tests, notebooks)."""
|
|
137
|
+
|
|
138
|
+
def __init__(self) -> None:
|
|
139
|
+
self._episodes: dict[str, Episode] = {}
|
|
140
|
+
|
|
141
|
+
def save(self, episode: Episode) -> Any:
|
|
142
|
+
self._episodes[episode.id] = Episode.from_dict(episode.to_dict())
|
|
143
|
+
return episode.id
|
|
144
|
+
|
|
145
|
+
def ids(self) -> list[str]:
|
|
146
|
+
return sorted(self._episodes)
|
|
147
|
+
|
|
148
|
+
def resolve(self, ref: str) -> str:
|
|
149
|
+
if ref == "latest":
|
|
150
|
+
if not self._episodes:
|
|
151
|
+
raise KeyError("no episodes recorded yet")
|
|
152
|
+
# most recently started; among equal start times the one saved last
|
|
153
|
+
return max(enumerate(self._episodes.values()), key=lambda ie: (ie[1].started_at, ie[0]))[1].id
|
|
154
|
+
if ref in self._episodes:
|
|
155
|
+
return ref
|
|
156
|
+
matches = [i for i in self._episodes if i.startswith(ref)]
|
|
157
|
+
if len(matches) == 1:
|
|
158
|
+
return matches[0]
|
|
159
|
+
if not matches:
|
|
160
|
+
raise KeyError(f"no episode matches {ref!r}")
|
|
161
|
+
raise KeyError(f"ambiguous episode reference {ref!r}")
|
|
162
|
+
|
|
163
|
+
def load(self, ref: str) -> Episode:
|
|
164
|
+
return Episode.from_dict(self._episodes[self.resolve(ref)].to_dict())
|
|
165
|
+
|
|
166
|
+
def list(self, limit: int | None = None) -> list[EpisodeSummary]:
|
|
167
|
+
summaries = sorted(
|
|
168
|
+
(EpisodeSummary.of(e) for e in self._episodes.values()), key=lambda s: s.started_at, reverse=True
|
|
169
|
+
)
|
|
170
|
+
return summaries[:limit] if limit else summaries
|
|
171
|
+
|
|
172
|
+
def delete(self, ref: str) -> None:
|
|
173
|
+
del self._episodes[self.resolve(ref)]
|
|
174
|
+
|
|
175
|
+
def latest(self) -> Episode | None:
|
|
176
|
+
try:
|
|
177
|
+
return self.load("latest")
|
|
178
|
+
except KeyError:
|
|
179
|
+
return None
|
|
180
|
+
|
|
181
|
+
def __contains__(self, episode_id: object) -> bool:
|
|
182
|
+
return episode_id in self._episodes
|
|
183
|
+
|
|
184
|
+
def __len__(self) -> int:
|
|
185
|
+
return len(self._episodes)
|