trodo-python 3.0.6__tar.gz → 3.0.7__tar.gz
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.
- {trodo_python-3.0.6 → trodo_python-3.0.7}/PKG-INFO +1 -1
- {trodo_python-3.0.6 → trodo_python-3.0.7}/pyproject.toml +1 -1
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_prompt_trace.py +79 -6
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/__init__.py +6 -0
- trodo_python-3.0.7/trodo/_version.py +1 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/managers/prompt_manager.py +9 -4
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/otel/auto_instrument.py +2 -1
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/otel/prompt_trace.py +86 -4
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/otel/wrap_agent.py +2 -1
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/prompts/template.py +8 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/prompts/types.py +38 -25
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo_python.egg-info/PKG-INFO +1 -1
- trodo_python-3.0.6/trodo/_version.py +0 -1
- {trodo_python-3.0.6 → trodo_python-3.0.7}/README.md +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/setup.cfg +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_anon_distinct_id.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_auto_instrument_fixes.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_cross_process_session.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_datasets.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_end_run.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_end_run_order.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_error_enrichment.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_framework_deference.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_get_prompt_version_types.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_instrumentor_resolution.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_llm_usage_cost.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_long_runs.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_multi_agent_shape.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_processor_methods.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_prompt_fingerprint.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_prompts.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_register_otel.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_sender.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_start_run.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_user_traits.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/tests/test_wrap_agent_unchanged.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/api/__init__.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/api/_retry.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/api/async_client.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/api/endpoints.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/api/http_client.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/api/management.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/auto/__init__.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/auto/auto_event_manager.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/client.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/delivery/__init__.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/delivery/sender.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/managers/__init__.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/managers/dataset_manager.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/managers/group_manager.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/managers/people_manager.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/managers/user_manager.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/otel/__init__.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/otel/context.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/otel/helpers.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/otel/processor.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/otel/register.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/otel/transport.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/prompts/__init__.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/prompts/compile.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/queue/__init__.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/queue/batch_flusher.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/queue/event_queue.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/session/__init__.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/session/server_session.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/session/session_manager.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/types.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/user_context.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/util/__init__.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo/util/lru.py +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo_python.egg-info/SOURCES.txt +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo_python.egg-info/dependency_links.txt +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo_python.egg-info/requires.txt +0 -0
- {trodo_python-3.0.6 → trodo_python-3.0.7}/trodo_python.egg-info/top_level.txt +0 -0
|
@@ -90,7 +90,7 @@ def test_no_op_outside_a_run(processor, http):
|
|
|
90
90
|
assert len(http.run_ingest) == 0
|
|
91
91
|
|
|
92
92
|
|
|
93
|
-
def
|
|
93
|
+
def test_a_fallback_is_traced_by_name_marked_with_no_hash(processor, http):
|
|
94
94
|
mgr = PromptManager(PromptHttp(fail=True))
|
|
95
95
|
with wrap_agent(processor=processor, team_site_id="site-x", agent_name="chat"):
|
|
96
96
|
p = mgr.get(
|
|
@@ -108,9 +108,80 @@ def test_fallback_prompt_is_not_traced(processor, http):
|
|
|
108
108
|
|
|
109
109
|
spans = http.run_ingest[0]["spans"]
|
|
110
110
|
attrs = next(s for s in spans if s["name"] == "llm").get("attributes") or {}
|
|
111
|
-
assert "trodo.prompt.name"
|
|
112
|
-
|
|
113
|
-
assert "
|
|
111
|
+
assert attrs["trodo.prompt.name"] == "refund-agent"
|
|
112
|
+
assert attrs["trodo.prompt.fallback"] == "true"
|
|
113
|
+
assert "trodo.prompt.version_hash" not in attrs
|
|
114
|
+
assert "trodo.prompt.version" not in attrs
|
|
115
|
+
assert http.run_ingest[0]["run"]["attributes"]["trodo.prompts"] == [
|
|
116
|
+
{"name": "refund-agent", "label": "production", "fallback": True}
|
|
117
|
+
]
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def test_stamps_the_version_number_beside_the_hash(processor, http):
|
|
121
|
+
mgr = PromptManager(PromptHttp(prompt=PROMPT_WIRE))
|
|
122
|
+
with wrap_agent(processor=processor, team_site_id="site-x", agent_name="chat"):
|
|
123
|
+
p = mgr.get("refund-agent")
|
|
124
|
+
with span("llm"):
|
|
125
|
+
p.compile(q="x")
|
|
126
|
+
attrs = next(s for s in http.run_ingest[0]["spans"] if s["name"] == "llm").get("attributes") or {}
|
|
127
|
+
assert attrs["trodo.prompt.version_hash"] == "a3f9c2e8d1b04756"
|
|
128
|
+
assert attrs["trodo.prompt.version"] == "3"
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class TwoPromptHttp(PromptHttp):
|
|
132
|
+
def get_prompt(self, name, label=None, version=None):
|
|
133
|
+
if name == "refund-agent":
|
|
134
|
+
return {"prompt": dict(PROMPT_WIRE)}
|
|
135
|
+
return {"prompt": {**PROMPT_WIRE, "name": name, "version": 7, "version_hash": "b1b2b3b4b5b6b7b8", "content_hash": "other"}}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def test_a_span_built_from_several_prompts_lists_them_all(processor, http):
|
|
139
|
+
import json
|
|
140
|
+
|
|
141
|
+
mgr = PromptManager(TwoPromptHttp())
|
|
142
|
+
with wrap_agent(processor=processor, team_site_id="site-x", agent_name="chat"):
|
|
143
|
+
system = mgr.get("refund-agent")
|
|
144
|
+
skill = mgr.get("refund-skill")
|
|
145
|
+
with span("llm"):
|
|
146
|
+
system.compile(q="system part")
|
|
147
|
+
skill.compile(q="skill part")
|
|
148
|
+
attrs = next(s for s in http.run_ingest[0]["spans"] if s["name"] == "llm").get("attributes") or {}
|
|
149
|
+
assert attrs["trodo.prompt.name"] == "refund-skill"
|
|
150
|
+
assert json.loads(attrs["trodo.prompts"]) == [
|
|
151
|
+
{"name": "refund-agent", "version": 3, "version_hash": "a3f9c2e8d1b04756"},
|
|
152
|
+
{"name": "refund-skill", "version": 7, "version_hash": "b1b2b3b4b5b6b7b8"},
|
|
153
|
+
]
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def test_one_prompt_is_not_a_list(processor, http):
|
|
157
|
+
mgr = PromptManager(PromptHttp(prompt=PROMPT_WIRE))
|
|
158
|
+
with wrap_agent(processor=processor, team_site_id="site-x", agent_name="chat"):
|
|
159
|
+
p = mgr.get("refund-agent")
|
|
160
|
+
with span("llm"):
|
|
161
|
+
p.compile(q="a")
|
|
162
|
+
p.compile(q="b")
|
|
163
|
+
attrs = next(s for s in http.run_ingest[0]["spans"] if s["name"] == "llm").get("attributes") or {}
|
|
164
|
+
assert "trodo.prompts" not in attrs
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def test_records_compiled_values_only_when_asked(processor, http):
|
|
168
|
+
import json
|
|
169
|
+
|
|
170
|
+
mgr = PromptManager(PromptHttp(prompt=PROMPT_WIRE))
|
|
171
|
+
with wrap_agent(processor=processor, team_site_id="site-x", agent_name="chat"):
|
|
172
|
+
quiet = mgr.get("refund-agent", cache_ttl_seconds=0)
|
|
173
|
+
with span("quiet"):
|
|
174
|
+
quiet.compile(q="secret")
|
|
175
|
+
loud = mgr.get("refund-agent", cache_ttl_seconds=0, record_variables=True)
|
|
176
|
+
with span("loud"):
|
|
177
|
+
loud.compile(q="shown")
|
|
178
|
+
spans = http.run_ingest[0]["spans"]
|
|
179
|
+
quiet_attrs = next(s for s in spans if s["name"] == "quiet").get("attributes") or {}
|
|
180
|
+
loud_attrs = next(s for s in spans if s["name"] == "loud").get("attributes") or {}
|
|
181
|
+
assert "trodo.prompt.variables" not in quiet_attrs
|
|
182
|
+
assert json.loads(loud_attrs["trodo.prompt.variables"]) == {"q": "shown"}
|
|
183
|
+
# Values stay on the span; the run's list says which prompts, not what was in them.
|
|
184
|
+
assert all("variables" not in r for r in http.run_ingest[0]["run"]["attributes"]["trodo.prompts"])
|
|
114
185
|
|
|
115
186
|
|
|
116
187
|
# ── compiled OUTSIDE the run ────────────────────────────────────────────────
|
|
@@ -206,7 +277,7 @@ def test_hand_written_messages_are_not_carriers(processor, http):
|
|
|
206
277
|
assert "trodo.prompt.name" not in attrs
|
|
207
278
|
|
|
208
279
|
|
|
209
|
-
def
|
|
280
|
+
def test_a_fallback_via_the_input_route_is_marked_as_one(processor, http):
|
|
210
281
|
mgr = PromptManager(PromptHttp(fail=True))
|
|
211
282
|
p = mgr.get(
|
|
212
283
|
"refund-agent",
|
|
@@ -222,7 +293,9 @@ def test_a_fallback_is_never_tagged_via_the_input_route(processor, http):
|
|
|
222
293
|
pass
|
|
223
294
|
|
|
224
295
|
attrs = next(s for s in http.run_ingest[0]["spans"] if s["name"] == "llm").get("attributes") or {}
|
|
225
|
-
assert "trodo.prompt.name"
|
|
296
|
+
assert attrs["trodo.prompt.name"] == "refund-agent"
|
|
297
|
+
assert attrs["trodo.prompt.fallback"] == "true"
|
|
298
|
+
assert "trodo.prompt.version_hash" not in attrs
|
|
226
299
|
|
|
227
300
|
|
|
228
301
|
def test_carrier_table_is_bounded(processor, http):
|
|
@@ -339,6 +339,7 @@ def get_prompt(
|
|
|
339
339
|
cache_ttl_seconds: Optional[float] = None,
|
|
340
340
|
fallback: Optional[Dict[str, Any]] = None,
|
|
341
341
|
max_retries: int = 2,
|
|
342
|
+
record_variables: bool = False,
|
|
342
343
|
) -> ManagedPrompt:
|
|
343
344
|
"""Fetch a managed prompt by name.
|
|
344
345
|
|
|
@@ -357,6 +358,10 @@ def get_prompt(
|
|
|
357
358
|
compiled = p.compile(q="where is my order")
|
|
358
359
|
# compiled.messages / compiled.model / compiled.tools
|
|
359
360
|
|
|
361
|
+
``record_variables=True`` records the compiled values on the span
|
|
362
|
+
(``trodo.prompt.variables``) so the Playground can replay the exact call.
|
|
363
|
+
Off by default: values are often user data.
|
|
364
|
+
|
|
360
365
|
:raises LookupError: if the prompt cannot be fetched and nothing is cached
|
|
361
366
|
and no ``fallback`` was given
|
|
362
367
|
"""
|
|
@@ -367,6 +372,7 @@ def get_prompt(
|
|
|
367
372
|
cache_ttl_seconds=cache_ttl_seconds,
|
|
368
373
|
fallback=fallback,
|
|
369
374
|
max_retries=max_retries,
|
|
375
|
+
record_variables=record_variables,
|
|
370
376
|
)
|
|
371
377
|
|
|
372
378
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
SDK_VERSION = "3.0.7"
|
|
@@ -78,6 +78,7 @@ def _to_prompt(
|
|
|
78
78
|
raw: Dict[str, Any],
|
|
79
79
|
is_fallback: bool = False,
|
|
80
80
|
trace_label: Optional[str] = None,
|
|
81
|
+
record_variables: bool = False,
|
|
81
82
|
) -> ManagedPrompt:
|
|
82
83
|
return ManagedPrompt(
|
|
83
84
|
name=str(raw.get("name") or ""),
|
|
@@ -96,6 +97,7 @@ def _to_prompt(
|
|
|
96
97
|
updated_at=raw.get("updated_at") or raw.get("updatedAt"),
|
|
97
98
|
is_fallback=is_fallback,
|
|
98
99
|
trace_label=trace_label,
|
|
100
|
+
record_variables=record_variables,
|
|
99
101
|
)
|
|
100
102
|
|
|
101
103
|
|
|
@@ -191,6 +193,7 @@ class PromptManager:
|
|
|
191
193
|
cache_ttl_seconds: Optional[float] = None,
|
|
192
194
|
fallback: Optional[Dict[str, Any]] = None,
|
|
193
195
|
max_retries: int = 2,
|
|
196
|
+
record_variables: bool = False,
|
|
194
197
|
) -> ManagedPrompt:
|
|
195
198
|
"""Fetch a managed prompt by name.
|
|
196
199
|
|
|
@@ -262,20 +265,20 @@ class PromptManager:
|
|
|
262
265
|
if ttl > 0:
|
|
263
266
|
fresh = self._cache.get_fresh(key)
|
|
264
267
|
if fresh is not None:
|
|
265
|
-
return _to_prompt(fresh, trace_label=trace_label)
|
|
268
|
+
return _to_prompt(fresh, trace_label=trace_label, record_variables=record_variables)
|
|
266
269
|
|
|
267
270
|
stale = self._cache.get_stale(key)
|
|
268
271
|
if stale is not None:
|
|
269
272
|
# Serve immediately, refresh behind the caller's back. A slow or
|
|
270
273
|
# dead API costs latency on nobody's request.
|
|
271
274
|
self._cache.revalidate(key, fetcher, ttl)
|
|
272
|
-
return _to_prompt(stale, trace_label=trace_label)
|
|
275
|
+
return _to_prompt(stale, trace_label=trace_label, record_variables=record_variables)
|
|
273
276
|
|
|
274
277
|
try:
|
|
275
278
|
raw = fetcher()
|
|
276
279
|
if ttl > 0:
|
|
277
280
|
self._cache.set(key, raw, ttl)
|
|
278
|
-
return _to_prompt(raw, trace_label=trace_label)
|
|
281
|
+
return _to_prompt(raw, trace_label=trace_label, record_variables=record_variables)
|
|
279
282
|
except Exception as e:
|
|
280
283
|
# A config error is not an outage: a typo'd label silently serving
|
|
281
284
|
# the fallback forever would hide the mistake for as long as it
|
|
@@ -284,7 +287,7 @@ class PromptManager:
|
|
|
284
287
|
raise
|
|
285
288
|
stale = self._cache.get_stale(key)
|
|
286
289
|
if stale is not None:
|
|
287
|
-
return _to_prompt(stale, trace_label=trace_label)
|
|
290
|
+
return _to_prompt(stale, trace_label=trace_label, record_variables=record_variables)
|
|
288
291
|
if fallback:
|
|
289
292
|
return _to_prompt(
|
|
290
293
|
{
|
|
@@ -299,6 +302,8 @@ class PromptManager:
|
|
|
299
302
|
"variables": fallback.get("variables") or [],
|
|
300
303
|
},
|
|
301
304
|
is_fallback=True,
|
|
305
|
+
trace_label=trace_label,
|
|
306
|
+
record_variables=record_variables,
|
|
302
307
|
)
|
|
303
308
|
raise
|
|
304
309
|
|
|
@@ -16,6 +16,7 @@ from typing import Any, Callable, Dict, Iterable, List, Optional
|
|
|
16
16
|
from .context import get_active_context
|
|
17
17
|
from .prompt_trace import (
|
|
18
18
|
prompt_attributes,
|
|
19
|
+
prompt_list_attributes,
|
|
19
20
|
prompt_ref_from_fingerprint,
|
|
20
21
|
remember_prompt,
|
|
21
22
|
)
|
|
@@ -407,7 +408,7 @@ class _OtelAdapter(_SpanProcessorBase): # type: ignore[valid-type,misc]
|
|
|
407
408
|
# rewrites what a past span ran.
|
|
408
409
|
state = getattr(ctx, "prompt_state", None)
|
|
409
410
|
current = state.get("current") if isinstance(state, dict) else None
|
|
410
|
-
for k, v in prompt_attributes(current).items():
|
|
411
|
+
for k, v in {**prompt_attributes(current), **prompt_list_attributes(state, current)}.items():
|
|
411
412
|
set_attr(k, v)
|
|
412
413
|
if isinstance(state, dict):
|
|
413
414
|
if len(_span_prompt_state) >= _SPAN_STATE_MAX:
|
|
@@ -31,13 +31,18 @@ from typing import Any, Dict, List, Optional, Tuple
|
|
|
31
31
|
|
|
32
32
|
from .context import get_active_context
|
|
33
33
|
|
|
34
|
-
# A PromptRef is a plain dict: {name, version_hash?, content_hash?, label
|
|
34
|
+
# A PromptRef is a plain dict: {name, version_hash?, content_hash?, label?,
|
|
35
|
+
# version?, fallback?, variables?}. ``fallback`` marks the caller's own text,
|
|
36
|
+
# served because the prompt could not be fetched (no hash: not a version).
|
|
37
|
+
# ``variables`` are the compiled values, only when the caller asked.
|
|
35
38
|
PromptRef = Dict[str, Any]
|
|
36
39
|
|
|
37
40
|
|
|
38
41
|
def new_prompt_state() -> Dict[str, Any]:
|
|
39
42
|
"""Per-run accumulator, shared by reference across the run's span tree."""
|
|
40
|
-
|
|
43
|
+
# by_span: the prompts compiled in each span, in order (deduped). One model
|
|
44
|
+
# call is often built from several -- a system prompt, a skill, a context.
|
|
45
|
+
return {"current": None, "current_span_id": None, "all": [], "by_span": {}}
|
|
41
46
|
|
|
42
47
|
|
|
43
48
|
# Compiled prompt (or its messages list) -> the version it came from.
|
|
@@ -90,7 +95,7 @@ def remember_prompt(state: Any, ref: PromptRef) -> None:
|
|
|
90
95
|
|
|
91
96
|
|
|
92
97
|
def _ref_key(ref: PromptRef) -> str:
|
|
93
|
-
return ref.get("version_hash") or ref.get("name") or ""
|
|
98
|
+
return ref.get("version_hash") or ((ref.get("name") or "") + ("#fallback" if ref.get("fallback") else ""))
|
|
94
99
|
|
|
95
100
|
|
|
96
101
|
def record_compiled_prompt(ref: PromptRef) -> None:
|
|
@@ -110,9 +115,48 @@ def record_compiled_prompt(ref: PromptRef) -> None:
|
|
|
110
115
|
used: List[PromptRef] = state["all"]
|
|
111
116
|
if not any(_ref_key(r) == key for r in used):
|
|
112
117
|
used.append(ref)
|
|
118
|
+
here = state.setdefault("by_span", {}).setdefault(state["current_span_id"], [])
|
|
119
|
+
if not any(_ref_key(r) == key for r in here):
|
|
120
|
+
here.append(ref)
|
|
113
121
|
_stamp_active_otel_span(ref)
|
|
114
122
|
|
|
115
123
|
|
|
124
|
+
#: Longest recorded variable value, and the most a span carries in all. Keep in sync with the Node SDK.
|
|
125
|
+
VARIABLE_VALUE_MAX_CHARS = 2000
|
|
126
|
+
VARIABLES_MAX_CHARS = 16000
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def recordable_variables(values: Optional[Dict[str, Any]]) -> Optional[Dict[str, str]]:
|
|
130
|
+
"""The values a prompt was compiled with, as strings, clipped.
|
|
131
|
+
|
|
132
|
+
Over the total cap nothing is recorded: a partial set would replay as a
|
|
133
|
+
different prompt. Strings follow JS rules (as the template engine does).
|
|
134
|
+
"""
|
|
135
|
+
if not isinstance(values, dict):
|
|
136
|
+
return None
|
|
137
|
+
out: Dict[str, str] = {}
|
|
138
|
+
total = 0
|
|
139
|
+
for k, v in values.items():
|
|
140
|
+
if v is None:
|
|
141
|
+
s = ""
|
|
142
|
+
elif isinstance(v, str):
|
|
143
|
+
s = v
|
|
144
|
+
elif isinstance(v, bool):
|
|
145
|
+
s = "true" if v else "false"
|
|
146
|
+
else:
|
|
147
|
+
try:
|
|
148
|
+
s = json.dumps(v, separators=(",", ":"), ensure_ascii=False)
|
|
149
|
+
except Exception: # noqa: BLE001
|
|
150
|
+
s = str(v)
|
|
151
|
+
if len(s) > VARIABLE_VALUE_MAX_CHARS:
|
|
152
|
+
s = s[:VARIABLE_VALUE_MAX_CHARS] + "\u2026"
|
|
153
|
+
total += len(str(k)) + len(s)
|
|
154
|
+
if total > VARIABLES_MAX_CHARS:
|
|
155
|
+
return None
|
|
156
|
+
out[str(k)] = s
|
|
157
|
+
return out or None
|
|
158
|
+
|
|
159
|
+
|
|
116
160
|
def prompt_attributes(ref: Optional[PromptRef]) -> Dict[str, str]:
|
|
117
161
|
"""Flatten a PromptRef to primitive span attributes (None values omitted)."""
|
|
118
162
|
out: Dict[str, str] = {}
|
|
@@ -121,13 +165,47 @@ def prompt_attributes(ref: Optional[PromptRef]) -> Dict[str, str]:
|
|
|
121
165
|
out["trodo.prompt.name"] = ref["name"]
|
|
122
166
|
if ref.get("version_hash"):
|
|
123
167
|
out["trodo.prompt.version_hash"] = ref["version_hash"]
|
|
168
|
+
if ref.get("version"):
|
|
169
|
+
out["trodo.prompt.version"] = str(ref["version"])
|
|
124
170
|
if ref.get("label"):
|
|
125
171
|
out["trodo.prompt.label"] = ref["label"]
|
|
126
172
|
if ref.get("content_hash"):
|
|
127
173
|
out["trodo.prompt.content_hash"] = ref["content_hash"]
|
|
174
|
+
if ref.get("fallback"):
|
|
175
|
+
out["trodo.prompt.fallback"] = "true"
|
|
176
|
+
if ref.get("variables"):
|
|
177
|
+
out["trodo.prompt.variables"] = json.dumps(ref["variables"], separators=(",", ":"), ensure_ascii=False)
|
|
128
178
|
return out
|
|
129
179
|
|
|
130
180
|
|
|
181
|
+
def prompt_list_attributes(state: Any, main: Optional[PromptRef]) -> Dict[str, str]:
|
|
182
|
+
"""``trodo.prompts`` for one span: every prompt compiled with ``main`` in the
|
|
183
|
+
same scope, when there is more than one. A JSON string (OTLP attributes are
|
|
184
|
+
primitives); ``trodo.prompt.*`` keeps naming the main one."""
|
|
185
|
+
if not isinstance(state, dict) or not main:
|
|
186
|
+
return {}
|
|
187
|
+
key = _ref_key(main)
|
|
188
|
+
for refs in (state.get("by_span") or {}).values():
|
|
189
|
+
if len(refs) > 1 and any(_ref_key(r) == key for r in refs):
|
|
190
|
+
items = []
|
|
191
|
+
for r in refs:
|
|
192
|
+
item: Dict[str, Any] = {"name": r.get("name")}
|
|
193
|
+
if r.get("version"):
|
|
194
|
+
item["version"] = r["version"]
|
|
195
|
+
if r.get("version_hash"):
|
|
196
|
+
item["version_hash"] = r["version_hash"]
|
|
197
|
+
if r.get("fallback"):
|
|
198
|
+
item["fallback"] = True
|
|
199
|
+
items.append(item)
|
|
200
|
+
return {"trodo.prompts": json.dumps(items, separators=(",", ":"), ensure_ascii=False)}
|
|
201
|
+
return {}
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def run_prompt_list(refs: List[PromptRef]) -> List[PromptRef]:
|
|
205
|
+
"""The run's prompt list: which prompts and versions -- values stay on the span."""
|
|
206
|
+
return [{k: v for k, v in r.items() if k != "variables"} for r in refs]
|
|
207
|
+
|
|
208
|
+
|
|
131
209
|
def merge_prompt_attrs(
|
|
132
210
|
active: Any,
|
|
133
211
|
attrs: Optional[Dict[str, Any]],
|
|
@@ -150,6 +228,7 @@ def merge_prompt_attrs(
|
|
|
150
228
|
if from_input is not None:
|
|
151
229
|
remember_prompt(state, from_input)
|
|
152
230
|
merged.update(prompt_attributes(from_input))
|
|
231
|
+
merged.update(prompt_list_attributes(state, from_input))
|
|
153
232
|
elif isinstance(state, dict):
|
|
154
233
|
# The run's "most recently compiled" prompt is a FALLBACK, used only
|
|
155
234
|
# where it is defensible. Applying it to every span meant compiling a
|
|
@@ -169,6 +248,7 @@ def merge_prompt_attrs(
|
|
|
169
248
|
)
|
|
170
249
|
if kind == "llm" or compiled_here:
|
|
171
250
|
merged.update(prompt_attributes(state.get("current")))
|
|
251
|
+
merged.update(prompt_list_attributes(state, state.get("current")))
|
|
172
252
|
return merged or None
|
|
173
253
|
|
|
174
254
|
|
|
@@ -184,7 +264,9 @@ def _stamp_active_otel_span(ref: PromptRef) -> None:
|
|
|
184
264
|
span = trace.get_current_span()
|
|
185
265
|
if span is None:
|
|
186
266
|
return
|
|
187
|
-
|
|
267
|
+
active = get_active_context()
|
|
268
|
+
state = getattr(active, "prompt_state", None) if active is not None else None
|
|
269
|
+
for k, v in {**prompt_attributes(ref), **prompt_list_attributes(state, ref)}.items():
|
|
188
270
|
span.set_attribute(k, v)
|
|
189
271
|
except Exception: # noqa: BLE001
|
|
190
272
|
pass # not in OTLP mode / opentelemetry not installed — no-op
|
|
@@ -37,6 +37,7 @@ from .prompt_trace import (
|
|
|
37
37
|
new_prompt_state,
|
|
38
38
|
prompt_ref_from_carrier,
|
|
39
39
|
remember_prompt,
|
|
40
|
+
run_prompt_list,
|
|
40
41
|
)
|
|
41
42
|
from .processor import TrodoSpanProcessor, TrodoRun, TrodoSpan
|
|
42
43
|
from .transport import get_transport_mode, get_otel_tracer, get_otel_helpers
|
|
@@ -693,7 +694,7 @@ class wrap_agent:
|
|
|
693
694
|
# exactly when you still want to know who the user was.
|
|
694
695
|
user=self._user,
|
|
695
696
|
attributes=(
|
|
696
|
-
{"trodo.prompts": self._ctx.prompt_state["all"]}
|
|
697
|
+
{"trodo.prompts": run_prompt_list(self._ctx.prompt_state["all"])}
|
|
697
698
|
if self._ctx is not None
|
|
698
699
|
and self._ctx.prompt_state
|
|
699
700
|
and self._ctx.prompt_state["all"]
|
|
@@ -70,6 +70,14 @@ def parse(template: Optional[str]) -> List[Token]:
|
|
|
70
70
|
if i < len(src):
|
|
71
71
|
push({"t": "text", "v": src[i:]})
|
|
72
72
|
break
|
|
73
|
+
# ESCAPE: \{{...}} is literal text -- the backslash goes, the tag stays
|
|
74
|
+
# as written and is neither a variable nor an error.
|
|
75
|
+
if open_at > 0 and src[open_at - 1] == "\\":
|
|
76
|
+
end = src.find(_CLOSE, open_at + len(_OPEN))
|
|
77
|
+
stop = len(src) if end == -1 else end + len(_CLOSE)
|
|
78
|
+
push({"t": "text", "v": src[i : open_at - 1] + src[open_at:stop]})
|
|
79
|
+
i = stop
|
|
80
|
+
continue
|
|
73
81
|
if open_at > i:
|
|
74
82
|
push({"t": "text", "v": src[i:open_at]})
|
|
75
83
|
|
|
@@ -81,6 +81,9 @@ class ManagedPrompt:
|
|
|
81
81
|
#: PromptManager.get(); not part of the wire contract. ``repr=False`` /
|
|
82
82
|
#: ``compare=False`` so it never affects equality or reprs.
|
|
83
83
|
trace_label: Optional[str] = field(default=None, repr=False, compare=False)
|
|
84
|
+
#: Record compiled values on the span (``trodo.prompt.variables``). Set by
|
|
85
|
+
#: PromptManager.get(record_variables=True); off by default (user data).
|
|
86
|
+
record_variables: bool = field(default=False, repr=False, compare=False)
|
|
84
87
|
|
|
85
88
|
def compile(self, variables: Optional[Dict[str, Any]] = None, **kwargs: Any) -> CompiledPrompt:
|
|
86
89
|
"""Compile with variable values, as a dict or as keyword arguments."""
|
|
@@ -89,38 +92,48 @@ class ManagedPrompt:
|
|
|
89
92
|
values = dict(variables or {})
|
|
90
93
|
values.update(kwargs)
|
|
91
94
|
compiled = compile_prompt(self, values)
|
|
92
|
-
# Prompt traceability.
|
|
93
|
-
#
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
95
|
+
# Prompt traceability. A fallback is recorded too, marked as one and
|
|
96
|
+
# with no hash: a span that ran the caller's own text says so.
|
|
97
|
+
try:
|
|
98
|
+
from ..otel.prompt_trace import (
|
|
99
|
+
record_compiled_prompt,
|
|
100
|
+
register_compiled_messages,
|
|
101
|
+
tag_carrier_with_prompt,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
from ..otel.prompt_trace import recordable_variables
|
|
105
|
+
|
|
106
|
+
if self.is_fallback:
|
|
107
|
+
ref: Dict[str, Any] = {"name": self.name, "label": self.trace_label, "fallback": True}
|
|
108
|
+
else:
|
|
102
109
|
ref = {
|
|
103
110
|
"name": self.name,
|
|
104
111
|
"version_hash": self.version_hash,
|
|
105
112
|
"content_hash": self.content_hash,
|
|
106
113
|
"label": self.trace_label,
|
|
114
|
+
"version": self.version or None,
|
|
107
115
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
116
|
+
if self.record_variables:
|
|
117
|
+
ref["variables"] = recordable_variables(values)
|
|
118
|
+
# (1) the surrounding run scope, if any — the only route that
|
|
119
|
+
# reaches auto-instrumented spans, whose input we never see.
|
|
120
|
+
record_compiled_prompt(ref)
|
|
121
|
+
# (2) the compiled value itself, so a span handed these messages
|
|
122
|
+
# as ``input`` recovers the version even when compile() ran
|
|
123
|
+
# outside the run. Both the wrapper and the list are tagged,
|
|
124
|
+
# since callers pass one or the other.
|
|
125
|
+
tag_carrier_with_prompt(compiled, ref)
|
|
126
|
+
tag_carrier_with_prompt(getattr(compiled, "messages", None), ref)
|
|
127
|
+
# (3) a content fingerprint, so an auto-instrumented provider
|
|
128
|
+
# span handed exactly these messages (compiled before the
|
|
129
|
+
# run; no carrier on the serialised input) links by exact
|
|
130
|
+
# match. Not for a fallback: its text may equal a real
|
|
131
|
+
# version's, and an exact match must never swap a real
|
|
132
|
+
# version for "fallback".
|
|
133
|
+
if not self.is_fallback:
|
|
121
134
|
register_compiled_messages(getattr(compiled, "messages", None), ref)
|
|
122
|
-
|
|
123
|
-
|
|
135
|
+
except Exception: # noqa: BLE001
|
|
136
|
+
pass # traceability must never break compile
|
|
124
137
|
return compiled
|
|
125
138
|
|
|
126
139
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
SDK_VERSION = "3.0.6"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|