langwatch-scenario 0.7.2__py3-none-any.whl → 0.7.7__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.
- {langwatch_scenario-0.7.2.dist-info → langwatch_scenario-0.7.7.dist-info}/METADATA +56 -12
- {langwatch_scenario-0.7.2.dist-info → langwatch_scenario-0.7.7.dist-info}/RECORD +21 -17
- scenario/__init__.py +1 -1
- scenario/_error_messages.py +2 -2
- scenario/_events/event_alert_message_logger.py +95 -0
- scenario/_events/event_bus.py +90 -30
- scenario/_events/event_reporter.py +43 -28
- scenario/_generated/langwatch_api_client/README.md +27 -17
- scenario/_utils/__init__.py +16 -3
- scenario/_utils/ids.py +76 -38
- scenario/config/__init__.py +43 -0
- scenario/config/langwatch.py +51 -0
- scenario/config/model.py +39 -0
- scenario/{config.py → config/scenario.py} +5 -34
- scenario/judge_agent.py +2 -2
- scenario/scenario_executor.py +16 -4
- scenario/scenario_state.py +2 -1
- scenario/user_simulator_agent.py +6 -6
- {langwatch_scenario-0.7.2.dist-info → langwatch_scenario-0.7.7.dist-info}/WHEEL +0 -0
- {langwatch_scenario-0.7.2.dist-info → langwatch_scenario-0.7.7.dist-info}/entry_points.txt +0 -0
- {langwatch_scenario-0.7.2.dist-info → langwatch_scenario-0.7.7.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: langwatch-scenario
|
3
|
-
Version: 0.7.
|
3
|
+
Version: 0.7.7
|
4
4
|
Summary: The end-to-end agent testing library
|
5
5
|
Author-email: LangWatch Team <support@langwatch.ai>
|
6
6
|
License: MIT
|
@@ -30,12 +30,12 @@ Requires-Dist: pksuid>=1.1.2
|
|
30
30
|
Requires-Dist: httpx>=0.27.0
|
31
31
|
Requires-Dist: rx>=3.2.0
|
32
32
|
Requires-Dist: python-dateutil>=2.9.0.post0
|
33
|
+
Requires-Dist: pydantic-settings>=2.9.1
|
33
34
|
Provides-Extra: dev
|
34
35
|
Requires-Dist: black; extra == "dev"
|
35
36
|
Requires-Dist: isort; extra == "dev"
|
36
37
|
Requires-Dist: pytest-cov; extra == "dev"
|
37
38
|
Requires-Dist: pre-commit; extra == "dev"
|
38
|
-
Requires-Dist: commitizen; extra == "dev"
|
39
39
|
Requires-Dist: pyright; extra == "dev"
|
40
40
|
Requires-Dist: pydantic-ai; extra == "dev"
|
41
41
|
Requires-Dist: function-schema; extra == "dev"
|
@@ -88,7 +88,7 @@ result = await scenario.run(
|
|
88
88
|
# Define the agents that will play this simulation
|
89
89
|
agents=[
|
90
90
|
WeatherAgent(),
|
91
|
-
scenario.UserSimulatorAgent(model="openai/gpt-4.1
|
91
|
+
scenario.UserSimulatorAgent(model="openai/gpt-4.1"),
|
92
92
|
],
|
93
93
|
|
94
94
|
# (Optional) Control the simulation
|
@@ -159,7 +159,7 @@ import pytest
|
|
159
159
|
import scenario
|
160
160
|
import litellm
|
161
161
|
|
162
|
-
scenario.configure(default_model="openai/gpt-4.1
|
162
|
+
scenario.configure(default_model="openai/gpt-4.1")
|
163
163
|
|
164
164
|
|
165
165
|
@pytest.mark.agent_test
|
@@ -189,6 +189,7 @@ async def test_vegetarian_recipe_agent():
|
|
189
189
|
]
|
190
190
|
),
|
191
191
|
],
|
192
|
+
set_id="python-examples",
|
192
193
|
)
|
193
194
|
|
194
195
|
# Assert for pytest to know whether the test passed
|
@@ -202,7 +203,7 @@ import litellm
|
|
202
203
|
@scenario.cache()
|
203
204
|
def vegetarian_recipe_agent(messages) -> scenario.AgentReturnTypes:
|
204
205
|
response = litellm.completion(
|
205
|
-
model="openai/gpt-4.1
|
206
|
+
model="openai/gpt-4.1",
|
206
207
|
messages=[
|
207
208
|
{
|
208
209
|
"role": "system",
|
@@ -227,17 +228,17 @@ def vegetarian_recipe_agent(messages) -> scenario.AgentReturnTypes:
|
|
227
228
|
Save it as `tests/vegetarian-recipe-agent.test.ts`:
|
228
229
|
|
229
230
|
```typescript
|
231
|
+
import scenario, { type AgentAdapter, AgentRole } from "@langwatch/scenario";
|
230
232
|
import { openai } from "@ai-sdk/openai";
|
231
|
-
import * as scenario from "@langwatch/scenario";
|
232
233
|
import { generateText } from "ai";
|
233
234
|
import { describe, it, expect } from "vitest";
|
234
235
|
|
235
236
|
describe("Vegetarian Recipe Agent", () => {
|
236
|
-
const agent:
|
237
|
-
role:
|
237
|
+
const agent: AgentAdapter = {
|
238
|
+
role: AgentRole.AGENT,
|
238
239
|
call: async (input) => {
|
239
240
|
const response = await generateText({
|
240
|
-
model: openai("gpt-4.1
|
241
|
+
model: openai("gpt-4.1"),
|
241
242
|
messages: [
|
242
243
|
{
|
243
244
|
role: "system",
|
@@ -258,7 +259,7 @@ describe("Vegetarian Recipe Agent", () => {
|
|
258
259
|
agent,
|
259
260
|
scenario.userSimulatorAgent(),
|
260
261
|
scenario.judgeAgent({
|
261
|
-
model: openai("gpt-4.1
|
262
|
+
model: openai("gpt-4.1"),
|
262
263
|
criteria: [
|
263
264
|
"Agent should not ask more than two follow-up questions",
|
264
265
|
"Agent should generate a recipe",
|
@@ -268,6 +269,7 @@ describe("Vegetarian Recipe Agent", () => {
|
|
268
269
|
],
|
269
270
|
}),
|
270
271
|
],
|
272
|
+
setId: "javascript-examples",
|
271
273
|
});
|
272
274
|
expect(result.success).toBe(true);
|
273
275
|
});
|
@@ -417,7 +419,7 @@ You can enable debug mode by setting the `debug` field to `True` in the `Scenari
|
|
417
419
|
Debug mode allows you to see the messages in slow motion step by step, and intervene with your own inputs to debug your agent from the middle of the conversation.
|
418
420
|
|
419
421
|
```python
|
420
|
-
scenario.configure(default_model="openai/gpt-4.1
|
422
|
+
scenario.configure(default_model="openai/gpt-4.1", debug=True)
|
421
423
|
```
|
422
424
|
|
423
425
|
or
|
@@ -431,7 +433,7 @@ pytest -s tests/test_vegetarian_recipe_agent.py --debug
|
|
431
433
|
Each time the scenario runs, the testing agent might chose a different input to start, this is good to make sure it covers the variance of real users as well, however we understand that the non-deterministic nature of it might make it less repeatable, costly and harder to debug. To solve for it, you can use the `cache_key` field in the `Scenario.configure` method or in the specific scenario you are running, this will make the testing agent give the same input for given the same scenario:
|
432
434
|
|
433
435
|
```python
|
434
|
-
scenario.configure(default_model="openai/gpt-4.1
|
436
|
+
scenario.configure(default_model="openai/gpt-4.1", cache_key="42")
|
435
437
|
```
|
436
438
|
|
437
439
|
To bust the cache, you can simply pass a different `cache_key`, disable it, or delete the cache files located at `~/.scenario/cache`.
|
@@ -450,6 +452,48 @@ class MyAgent:
|
|
450
452
|
|
451
453
|
This will cache any function call you decorate when running the tests and make them repeatable, hashed by the function arguments, the scenario being executed, and the `cache_key` you provided. You can exclude arguments that should not be hashed for the cache key by naming them in the `ignore` argument.
|
452
454
|
|
455
|
+
## Grouping Your Sets and Batches
|
456
|
+
|
457
|
+
While optional, we strongly recommend setting stable identifiers for your scenarios, sets, and batches for better organization and tracking in LangWatch.
|
458
|
+
|
459
|
+
- **set_id**: Groups related scenarios into a test suite. This corresponds to the "Simulation Set" in the UI.
|
460
|
+
- **batch_run_id**: Groups all scenarios that were run together in a single execution (e.g., a single CI job). This is automatically generated but can be overridden.
|
461
|
+
|
462
|
+
```python
|
463
|
+
import os
|
464
|
+
|
465
|
+
result = await scenario.run(
|
466
|
+
name="my first scenario",
|
467
|
+
description="A simple test to see if the agent responds.",
|
468
|
+
set_id="my-test-suite",
|
469
|
+
agents=[
|
470
|
+
scenario.Agent(my_agent),
|
471
|
+
scenario.UserSimulatorAgent(),
|
472
|
+
]
|
473
|
+
)
|
474
|
+
```
|
475
|
+
|
476
|
+
You can also set the `batch_run_id` using environment variables for CI/CD integration:
|
477
|
+
|
478
|
+
```python
|
479
|
+
import os
|
480
|
+
|
481
|
+
# Set batch ID for CI/CD integration
|
482
|
+
os.environ["SCENARIO_BATCH_RUN_ID"] = os.environ.get("GITHUB_RUN_ID", "local-run")
|
483
|
+
|
484
|
+
result = await scenario.run(
|
485
|
+
name="my first scenario",
|
486
|
+
description="A simple test to see if the agent responds.",
|
487
|
+
set_id="my-test-suite",
|
488
|
+
agents=[
|
489
|
+
scenario.Agent(my_agent),
|
490
|
+
scenario.UserSimulatorAgent(),
|
491
|
+
]
|
492
|
+
)
|
493
|
+
```
|
494
|
+
|
495
|
+
The `batch_run_id` is automatically generated for each test run, but you can also set it globally using the `SCENARIO_BATCH_RUN_ID` environment variable.
|
496
|
+
|
453
497
|
## Disable Output
|
454
498
|
|
455
499
|
You can remove the `-s` flag from pytest to hide the output during test, which will only show up if the test fails. Alternatively, you can set `verbose=False` in the `Scenario.configure` method or in the specific scenario you are running.
|
@@ -1,22 +1,22 @@
|
|
1
|
-
scenario/__init__.py,sha256=
|
2
|
-
scenario/_error_messages.py,sha256=
|
1
|
+
scenario/__init__.py,sha256=4WO8TjY8Lc0NhYL7b9LvaB1xCBqwUkLuI0uIA6PQP6c,4223
|
2
|
+
scenario/_error_messages.py,sha256=QVFSbhzsVNGz2GOBOaoQFW6w6AOyZCWLTt0ySWPfnGw,3882
|
3
3
|
scenario/agent_adapter.py,sha256=PoY2KQqYuqzIIb3-nhIU-MPXwHJc1vmwdweMy7ut-hk,4255
|
4
4
|
scenario/cache.py,sha256=J6s6Sia_Ce6TrnsInlhfxm6SF8tygo3sH-_cQCRX1WA,6213
|
5
|
-
scenario/
|
6
|
-
scenario/judge_agent.py,sha256=d8vORsqpUPIA4yhlBTv5Yi4I2MdcfXselYBTFvfZx-4,16221
|
5
|
+
scenario/judge_agent.py,sha256=7NsgeMu6wRMjU_HYTCFqkLma6H2AJuEkw9hJkt11190,16211
|
7
6
|
scenario/pytest_plugin.py,sha256=DGrpgB6e71eq8QXWWxwLjAKNhiyYyzfzZ0L5Ax8iEmo,11317
|
8
|
-
scenario/scenario_executor.py,sha256=
|
9
|
-
scenario/scenario_state.py,sha256=
|
7
|
+
scenario/scenario_executor.py,sha256=2ZPy2cywwEMIbUfBP1jHN__Ffjf5WGB144MX2SNr5IM,33101
|
8
|
+
scenario/scenario_state.py,sha256=LWGqEQN-Yz0DIiC-TyMRHd-9rEiuBVUHKllMmKv-qGg,7029
|
10
9
|
scenario/script.py,sha256=A0N5pP0l4FFn1xdKc78U_wkwWhEWH3EFeU_LRDtNyEI,12241
|
11
10
|
scenario/types.py,sha256=qH5KFzJBDG1fEJB_qFRVtL3EZulxq3G1mztYczIzIAY,9613
|
12
|
-
scenario/user_simulator_agent.py,sha256=
|
11
|
+
scenario/user_simulator_agent.py,sha256=UJ75xhqHwoi8-3JkR1AsHDzpHM2Lx-aDSTJ1gnq_SXc,9101
|
13
12
|
scenario/_events/__init__.py,sha256=4cj6H9zuXzvWhT2P2JNdjWzeF1PUepTjqIDw85Vid9s,1500
|
14
|
-
scenario/_events/
|
15
|
-
scenario/_events/
|
13
|
+
scenario/_events/event_alert_message_logger.py,sha256=K0Pu76Gd36lGEEYh8e8r7NMt7J-OQhbw0cZmiwutCOE,3591
|
14
|
+
scenario/_events/event_bus.py,sha256=KFN0OxAQIQXIk_tVrorDoN_YLKVK9dos5SXFALstHgE,9809
|
15
|
+
scenario/_events/event_reporter.py,sha256=4uND_kdPBXe-aUWCdSj4BLrMA33TDnbZzokAEOU3_08,3771
|
16
16
|
scenario/_events/events.py,sha256=UtEGY-_1B0LrwpgsNKgrvJBZhRtxuj3K_i6ZBfF7E4Q,6387
|
17
17
|
scenario/_events/messages.py,sha256=quwP2OkeaGasNOoaV8GUeosZVKc5XDsde08T0xx_YQo,2297
|
18
18
|
scenario/_events/utils.py,sha256=SproqiwjhLWAW7p82EirCgawpxAo0ksW1pBB4mKkcEs,3436
|
19
|
-
scenario/_generated/langwatch_api_client/README.md,sha256=
|
19
|
+
scenario/_generated/langwatch_api_client/README.md,sha256=Az5f2L4ChOnG_ZtrdBagzRVgeTCtBkbD_S5cIeAry2o,5424
|
20
20
|
scenario/_generated/langwatch_api_client/pyproject.toml,sha256=Z8wxuGp4H9BJYVVJB8diW7rRU9XYxtPfw9mU4_wq4cA,560
|
21
21
|
scenario/_generated/langwatch_api_client/lang_watch_api_client/__init__.py,sha256=vVrn17y-3l3fOqeJk8aN3GlStRm2fo0f313l_0LtJNs,368
|
22
22
|
scenario/_generated/langwatch_api_client/lang_watch_api_client/client.py,sha256=o_mdLqyBCQstu5tS1WZFwqIEbGwkvWQ7eQjuCJw_5VY,12419
|
@@ -226,12 +226,16 @@ scenario/_generated/langwatch_api_client/lang_watch_api_client/models/search_req
|
|
226
226
|
scenario/_generated/langwatch_api_client/lang_watch_api_client/models/search_response.py,sha256=zDYmJ8bFBSJyF9D3cEn_ffrey-ITIfwr-_7eu72zLyk,2832
|
227
227
|
scenario/_generated/langwatch_api_client/lang_watch_api_client/models/timestamps.py,sha256=-nRKUPZTAJQNxiKz128xF7DKgZNbFo4G3mr5xNXrkaw,2173
|
228
228
|
scenario/_generated/langwatch_api_client/lang_watch_api_client/models/trace.py,sha256=K9Lc_EQOrJ2dqMXx9EpiUXReT1_uYF7WRfYyhlfbi3I,7537
|
229
|
-
scenario/_utils/__init__.py,sha256=
|
230
|
-
scenario/_utils/ids.py,sha256=
|
229
|
+
scenario/_utils/__init__.py,sha256=ptNVzmjhypznnozdNIiuBDHZ0NLqtp7xhio9kEDovWQ,1311
|
230
|
+
scenario/_utils/ids.py,sha256=v3JS8J7vrFuubK5bXJviU-BVZoLGWINCN1hUyAO9NZw,2074
|
231
231
|
scenario/_utils/message_conversion.py,sha256=AWHn31E7J0mz9sBXWruVVAgtsrJz1R_xEf-dGbX6jjs,3636
|
232
232
|
scenario/_utils/utils.py,sha256=msQgUWaLh3U9jIIHmxkEbOaklga63AF0KJzsaKa_mZc,14008
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
langwatch_scenario-0.7.
|
233
|
+
scenario/config/__init__.py,sha256=b2X_bqkIrd7jZY9dRrXk2wOqoPe87Nl_SRGuZhlolxA,1123
|
234
|
+
scenario/config/langwatch.py,sha256=ijWchFbUsLbQooAZmwyTw4rxfRLQseZ1GoVSiPPbzpw,1677
|
235
|
+
scenario/config/model.py,sha256=Ve49S2FyzUifXJ-SAyKPiNtVqs8BfsYbODu_M5y0c8Y,1155
|
236
|
+
scenario/config/scenario.py,sha256=tVVnsUgG6Z0hYZiTDX-GGZz8l8co1HhyTqJUJNPinBk,5184
|
237
|
+
langwatch_scenario-0.7.7.dist-info/METADATA,sha256=L7h0kgOaIij6MYVCac0EqPu8ODkZNKxDeIrHCSJg2l4,20003
|
238
|
+
langwatch_scenario-0.7.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
239
|
+
langwatch_scenario-0.7.7.dist-info/entry_points.txt,sha256=WlEnJ_gku0i18bIa3DSuGqXRX-QDQLe_s0YmRzK45TI,45
|
240
|
+
langwatch_scenario-0.7.7.dist-info/top_level.txt,sha256=45Mn28aedJsetnBMB5xSmrJ-yo701QLH89Zlz4r1clE,9
|
241
|
+
langwatch_scenario-0.7.7.dist-info/RECORD,,
|
scenario/__init__.py
CHANGED
scenario/_error_messages.py
CHANGED
@@ -8,12 +8,12 @@ def agent_not_configured_error_message(class_name: str):
|
|
8
8
|
|
9
9
|
{termcolor.colored("->", "cyan")} {class_name} was initialized without a model, please set the model when defining the testing agent, for example:
|
10
10
|
|
11
|
-
{class_name}(model="openai/gpt-4.1
|
11
|
+
{class_name}(model="openai/gpt-4.1")
|
12
12
|
{termcolor.colored("^" * (29 + len(class_name)), "green")}
|
13
13
|
|
14
14
|
{termcolor.colored("->", "cyan")} Alternatively, you can set the default model globally, for example:
|
15
15
|
|
16
|
-
scenario.configure(default_model="openai/gpt-4.1
|
16
|
+
scenario.configure(default_model="openai/gpt-4.1")
|
17
17
|
{termcolor.colored("^" * 55, "green")}
|
18
18
|
"""
|
19
19
|
|
@@ -0,0 +1,95 @@
|
|
1
|
+
import os
|
2
|
+
from typing import Set
|
3
|
+
from .._utils.ids import get_batch_run_id
|
4
|
+
|
5
|
+
|
6
|
+
class EventAlertMessageLogger:
|
7
|
+
"""
|
8
|
+
Handles console output of alert messages for scenario events.
|
9
|
+
|
10
|
+
Single responsibility: Display user-friendly messages about event reporting status
|
11
|
+
and simulation watching instructions.
|
12
|
+
"""
|
13
|
+
|
14
|
+
_shown_batch_ids: Set[str] = set()
|
15
|
+
|
16
|
+
def handle_greeting(self) -> None:
|
17
|
+
"""
|
18
|
+
Shows a fancy greeting message about simulation reporting status.
|
19
|
+
Only shows once per batch run to avoid spam.
|
20
|
+
"""
|
21
|
+
if self._is_greeting_disabled():
|
22
|
+
return
|
23
|
+
|
24
|
+
batch_run_id = get_batch_run_id()
|
25
|
+
|
26
|
+
if batch_run_id in EventAlertMessageLogger._shown_batch_ids:
|
27
|
+
return
|
28
|
+
|
29
|
+
EventAlertMessageLogger._shown_batch_ids.add(batch_run_id)
|
30
|
+
self._display_greeting(batch_run_id)
|
31
|
+
|
32
|
+
def handle_watch_message(self, set_url: str) -> None:
|
33
|
+
"""
|
34
|
+
Shows a fancy message about how to watch the simulation.
|
35
|
+
Called when a run started event is received with a session ID.
|
36
|
+
"""
|
37
|
+
if self._is_greeting_disabled():
|
38
|
+
return
|
39
|
+
|
40
|
+
self._display_watch_message(set_url)
|
41
|
+
|
42
|
+
def _is_greeting_disabled(self) -> bool:
|
43
|
+
"""Check if greeting messages are disabled via environment variable."""
|
44
|
+
return bool(os.getenv("SCENARIO_DISABLE_SIMULATION_REPORT_INFO"))
|
45
|
+
|
46
|
+
def _display_greeting(self, batch_run_id: str) -> None:
|
47
|
+
"""Display the greeting message with simulation reporting status."""
|
48
|
+
separator = "─" * 60
|
49
|
+
|
50
|
+
if not os.getenv("LANGWATCH_API_KEY"):
|
51
|
+
print(f"\n{separator}")
|
52
|
+
print("🚀 LangWatch Simulation Reporting")
|
53
|
+
print(f"{separator}")
|
54
|
+
print("➡️ API key not configured")
|
55
|
+
print(" Simulations will only output final results")
|
56
|
+
print("")
|
57
|
+
print("💡 To visualize conversations in real time:")
|
58
|
+
print(" • Set LANGWATCH_API_KEY environment variable")
|
59
|
+
print(" • Or configure apiKey in scenario.config.js")
|
60
|
+
print("")
|
61
|
+
print(f"📦 Batch Run ID: {batch_run_id}")
|
62
|
+
print("")
|
63
|
+
print("🔇 To disable these messages:")
|
64
|
+
print(" • Set SCENARIO_DISABLE_SIMULATION_REPORT_INFO=true")
|
65
|
+
print(f"{separator}\n")
|
66
|
+
else:
|
67
|
+
endpoint = os.getenv("LANGWATCH_ENDPOINT", "https://app.langwatch.ai")
|
68
|
+
api_key = os.getenv("LANGWATCH_API_KEY", "")
|
69
|
+
|
70
|
+
print(f"\n{separator}")
|
71
|
+
print("🚀 LangWatch Simulation Reporting")
|
72
|
+
print(f"{separator}")
|
73
|
+
print("✅ Simulation reporting enabled")
|
74
|
+
print(f" Endpoint: {endpoint}")
|
75
|
+
print(f" API Key: {'Configured' if api_key else 'Not configured'}")
|
76
|
+
print("")
|
77
|
+
print(f"📦 Batch Run ID: {batch_run_id}")
|
78
|
+
print("")
|
79
|
+
print("🔇 To disable these messages:")
|
80
|
+
print(" • Set SCENARIO_DISABLE_SIMULATION_REPORT_INFO=true")
|
81
|
+
print(f"{separator}\n")
|
82
|
+
|
83
|
+
def _display_watch_message(self, set_url: str) -> None:
|
84
|
+
"""Display the watch message with URLs for viewing the simulation."""
|
85
|
+
separator = "─" * 60
|
86
|
+
batch_url = f"{set_url}/{get_batch_run_id()}"
|
87
|
+
|
88
|
+
print(f"\n{separator}")
|
89
|
+
print("👀 Watch Your Simulation Live")
|
90
|
+
print(f"{separator}")
|
91
|
+
print("🌐 Open in your browser:")
|
92
|
+
print(f" Scenario Set: {set_url}")
|
93
|
+
print(f" Batch Run: {batch_url}")
|
94
|
+
print("")
|
95
|
+
print(f"{separator}\n")
|
scenario/_events/event_bus.py
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
from rx.core.observable.observable import Observable
|
2
|
-
from typing import Optional, Any
|
2
|
+
from typing import Optional, Any, Dict
|
3
3
|
from .events import ScenarioEvent
|
4
4
|
from .event_reporter import EventReporter
|
5
|
+
from .event_alert_message_logger import EventAlertMessageLogger
|
5
6
|
|
6
7
|
import asyncio
|
7
8
|
import queue
|
8
9
|
import threading
|
9
10
|
import logging
|
10
11
|
|
12
|
+
|
11
13
|
class ScenarioEventBus:
|
12
14
|
"""
|
13
15
|
Subscribes to scenario event streams and handles HTTP posting using a dedicated worker thread.
|
@@ -24,6 +26,7 @@ class ScenarioEventBus:
|
|
24
26
|
|
25
27
|
Attributes:
|
26
28
|
_event_reporter: EventReporter instance for HTTP posting of events
|
29
|
+
_event_alert_message_logger: EventAlertMessageLogger for user-friendly console output
|
27
30
|
_max_retries: Maximum number of retry attempts for failed event processing
|
28
31
|
_event_queue: Thread-safe queue for passing events to worker thread
|
29
32
|
_completed: Whether the event stream has completed
|
@@ -44,11 +47,12 @@ class ScenarioEventBus:
|
|
44
47
|
Defaults to 3 attempts with exponential backoff.
|
45
48
|
"""
|
46
49
|
self._event_reporter: EventReporter = event_reporter or EventReporter()
|
50
|
+
self._event_alert_message_logger = EventAlertMessageLogger()
|
47
51
|
self._max_retries = max_retries
|
48
|
-
|
52
|
+
|
49
53
|
# Custom logger for this class
|
50
54
|
self.logger = logging.getLogger(__name__)
|
51
|
-
|
55
|
+
|
52
56
|
# Threading infrastructure
|
53
57
|
self._event_queue: queue.Queue[ScenarioEvent] = queue.Queue()
|
54
58
|
self._completed = False
|
@@ -61,9 +65,7 @@ class ScenarioEventBus:
|
|
61
65
|
if self._worker_thread is None or not self._worker_thread.is_alive():
|
62
66
|
self.logger.debug("Creating new worker thread")
|
63
67
|
self._worker_thread = threading.Thread(
|
64
|
-
target=self._worker_loop,
|
65
|
-
daemon=False,
|
66
|
-
name="ScenarioEventBus-Worker"
|
68
|
+
target=self._worker_loop, daemon=False, name="ScenarioEventBus-Worker"
|
67
69
|
)
|
68
70
|
self._worker_thread.start()
|
69
71
|
self.logger.debug("Worker thread started")
|
@@ -76,52 +78,108 @@ class ScenarioEventBus:
|
|
76
78
|
if self._shutdown_event.wait(timeout=0.1):
|
77
79
|
self.logger.debug("Worker thread received shutdown signal")
|
78
80
|
break
|
79
|
-
|
81
|
+
|
80
82
|
try:
|
81
83
|
event = self._event_queue.get(timeout=0.1)
|
82
|
-
self.logger.debug(
|
84
|
+
self.logger.debug(
|
85
|
+
f"Worker picked up event: {event.type_} ({event.scenario_run_id})"
|
86
|
+
)
|
83
87
|
self._process_event_sync(event)
|
84
88
|
self._event_queue.task_done()
|
85
89
|
except queue.Empty:
|
86
90
|
# Exit if stream completed and no more events
|
87
91
|
if self._completed:
|
88
|
-
self.logger.debug(
|
92
|
+
self.logger.debug(
|
93
|
+
"Stream completed and no more events, worker thread exiting"
|
94
|
+
)
|
89
95
|
break
|
90
96
|
continue
|
91
|
-
|
97
|
+
|
92
98
|
except Exception as e:
|
93
99
|
self.logger.error(f"Worker thread error: {e}")
|
94
|
-
|
100
|
+
|
95
101
|
self.logger.debug("Worker thread loop ended")
|
96
102
|
|
97
103
|
def _process_event_sync(self, event: ScenarioEvent) -> None:
|
98
104
|
"""
|
99
105
|
Process event synchronously in worker thread with retry logic.
|
100
106
|
"""
|
101
|
-
self.logger.debug(
|
102
|
-
|
107
|
+
self.logger.debug(
|
108
|
+
f"Processing HTTP post for {event.type_} ({event.scenario_run_id})"
|
109
|
+
)
|
110
|
+
|
103
111
|
try:
|
104
|
-
|
105
|
-
|
106
|
-
if not success:
|
107
|
-
self.logger.warning(f"Failed to process event {event.type_} after {self._max_retries} attempts")
|
108
|
-
else:
|
109
|
-
self.logger.debug(f"Successfully posted {event.type_} ({event.scenario_run_id})")
|
112
|
+
result = self._post_event_with_retry(event)
|
113
|
+
self._handle_event_result(event, result)
|
110
114
|
except Exception as e:
|
111
115
|
self.logger.error(f"Error processing event {event.type_}: {e}")
|
112
116
|
|
113
|
-
|
117
|
+
def _post_event_with_retry(self, event: ScenarioEvent) -> Optional[Dict[str, Any]]:
|
118
|
+
"""
|
119
|
+
Post event with retry logic, converting async to sync.
|
120
|
+
"""
|
121
|
+
return asyncio.run(self._process_event_with_retry(event))
|
122
|
+
|
123
|
+
def _handle_event_result(
|
124
|
+
self, event: ScenarioEvent, result: Optional[Dict[str, Any]]
|
125
|
+
) -> None:
|
126
|
+
"""
|
127
|
+
Handle the result of event processing, including logging and watch messages.
|
128
|
+
"""
|
129
|
+
if result is None:
|
130
|
+
self.logger.warning(
|
131
|
+
f"Failed to process event {event.type_} after {self._max_retries} attempts"
|
132
|
+
)
|
133
|
+
return
|
134
|
+
|
135
|
+
self.logger.debug(
|
136
|
+
f"Successfully posted {event.type_} ({event.scenario_run_id})"
|
137
|
+
)
|
138
|
+
|
139
|
+
# Handle watch message for run started events
|
140
|
+
if event.type_ == "SCENARIO_RUN_STARTED" and result.get("setUrl"):
|
141
|
+
self._handle_watch_message(event, result)
|
142
|
+
|
143
|
+
def _handle_watch_message(
|
144
|
+
self, event: ScenarioEvent, result: Dict[str, Any]
|
145
|
+
) -> None:
|
146
|
+
"""
|
147
|
+
Handle watch message for scenario run started events.
|
148
|
+
"""
|
149
|
+
self._event_alert_message_logger.handle_watch_message(
|
150
|
+
set_url=str(result["setUrl"]),
|
151
|
+
)
|
152
|
+
|
153
|
+
def _extract_scenario_set_id(self, event: ScenarioEvent) -> str:
|
154
|
+
"""
|
155
|
+
Extract scenario set ID from event, handling Unset types from generated models.
|
156
|
+
"""
|
157
|
+
scenario_set_id = getattr(event, "scenario_set_id", "default")
|
158
|
+
|
159
|
+
# Handle Unset type from generated models
|
160
|
+
if hasattr(scenario_set_id, "__class__") and "Unset" in str(
|
161
|
+
scenario_set_id.__class__
|
162
|
+
):
|
163
|
+
return "default"
|
164
|
+
|
165
|
+
return str(scenario_set_id)
|
166
|
+
|
167
|
+
async def _process_event_with_retry(
|
168
|
+
self, event: ScenarioEvent, attempt: int = 1
|
169
|
+
) -> Optional[Dict[str, Any]]:
|
114
170
|
"""
|
115
171
|
Process a single event with retry logic (now runs in worker thread context).
|
116
172
|
"""
|
117
173
|
try:
|
118
174
|
if self._event_reporter:
|
119
|
-
await self._event_reporter.post_event(event)
|
120
|
-
return
|
175
|
+
return await self._event_reporter.post_event(event)
|
176
|
+
return {}
|
121
177
|
except Exception as e:
|
122
178
|
if attempt >= self._max_retries:
|
123
|
-
return
|
124
|
-
print(
|
179
|
+
return None
|
180
|
+
print(
|
181
|
+
f"Error processing event (attempt {attempt}/{self._max_retries}): {e}"
|
182
|
+
)
|
125
183
|
await asyncio.sleep(0.1 * (2 ** (attempt - 1))) # Exponential backoff
|
126
184
|
return await self._process_event_with_retry(event, attempt + 1)
|
127
185
|
|
@@ -135,7 +193,9 @@ class ScenarioEventBus:
|
|
135
193
|
return
|
136
194
|
|
137
195
|
def handle_event(event: ScenarioEvent) -> None:
|
138
|
-
self.logger.debug(
|
196
|
+
self.logger.debug(
|
197
|
+
f"Event received, queuing: {event.type_} ({event.scenario_run_id})"
|
198
|
+
)
|
139
199
|
self._get_or_create_worker()
|
140
200
|
self._event_queue.put(event)
|
141
201
|
self.logger.debug(f"Event queued: {event.type_} ({event.scenario_run_id})")
|
@@ -144,7 +204,7 @@ class ScenarioEventBus:
|
|
144
204
|
self._subscription = event_stream.subscribe(
|
145
205
|
handle_event,
|
146
206
|
lambda e: self.logger.error(f"Error in event stream: {e}"),
|
147
|
-
lambda: self._set_completed()
|
207
|
+
lambda: self._set_completed(),
|
148
208
|
)
|
149
209
|
|
150
210
|
def _set_completed(self):
|
@@ -155,17 +215,17 @@ class ScenarioEventBus:
|
|
155
215
|
def drain(self) -> None:
|
156
216
|
"""
|
157
217
|
Waits for all queued events to complete processing.
|
158
|
-
|
218
|
+
|
159
219
|
This method blocks until all events in the queue have been processed.
|
160
220
|
Since _process_event_sync() uses asyncio.run(), HTTP requests complete
|
161
221
|
before task_done() is called, so join() ensures everything is finished.
|
162
222
|
"""
|
163
223
|
self.logger.debug("Drain started - waiting for queue to empty")
|
164
|
-
|
224
|
+
|
165
225
|
# Wait for all events to be processed - this is sufficient!
|
166
226
|
self._event_queue.join()
|
167
227
|
self.logger.debug("Event queue drained")
|
168
|
-
|
228
|
+
|
169
229
|
# Signal worker to shutdown and wait for it
|
170
230
|
self._shutdown_event.set()
|
171
231
|
if self._worker_thread and self._worker_thread.is_alive():
|
@@ -175,7 +235,7 @@ class ScenarioEventBus:
|
|
175
235
|
self.logger.warning("Worker thread did not shutdown within timeout")
|
176
236
|
else:
|
177
237
|
self.logger.debug("Worker thread shutdown complete")
|
178
|
-
|
238
|
+
|
179
239
|
self.logger.info("Drain completed")
|
180
240
|
|
181
241
|
def is_completed(self) -> bool:
|