Skip to content

Commit 23afcf8

Browse files
Fix ScenarioRun Duration Tracking in SwarmAdapter (#63)
* Add basic evaluation example script * Fix typos and improve clarity in docstrings across core modules * Add Google-style docstrings to BaseAdapter methods * Format base adapter using ruff * docs: add instructions for running CI checks locally * Remove example file unrelated to CI documentation * Add py.typed marker for type checker support * Add test for markdown emoji encoding * Fix test_reporting: correct class usage, fields, and Windows-safe to_markdown * All tests passing: fixed dependencies and formatting * Update dependencies / poetry config * Fix emoji markdown test and align ScenarioRun signature * Fix reporting tests and update dependencies * Fix missing required dependencies (jsonschema, scipy) * Update all files * Add CSV export support for SuiteResult * Fix SIM118 linter issue in SuiteResult.to_csv * Fix Ruff formatting issues in SuiteResult.to_csv * Fix CSV export: iterate over dict keys correctly and pass Ruff lint * Fix: SwarmAdapter imports and end_session duration tracking, fully linted * Format files and remove lint error * Fix the issue of csv file --------- Signed-off-by: Jagriti-student <jagriti7989@gmail.com>
1 parent 8f9b43d commit 23afcf8

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

src/agentunit/adapters/swarm_adapter.py

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
from datetime import datetime
1414
from typing import TYPE_CHECKING, Any
1515

16+
17+
try:
18+
from swarm import Agent, Swarm
19+
20+
HAS_SWARM = True
21+
except ImportError:
22+
HAS_SWARM = False
23+
logging.getLogger(__name__).warning(
24+
"OpenAI Swarm not installed. SwarmAdapter will have limited functionality."
25+
)
26+
1627
from agentunit.multiagent import (
1728
AgentInteraction,
1829
AgentMetadata,
@@ -23,23 +34,14 @@
2334
OrchestrationPattern,
2435
SessionID,
2536
)
26-
from agentunit.reporting.results import ScenarioResult
2737

2838

2939
if TYPE_CHECKING:
3040
from agentunit.core import Scenario
41+
from agentunit.reporting.results import ScenarioResult
3142

32-
# Configure logging
3343
logger = logging.getLogger(__name__)
3444

35-
try:
36-
from swarm import Agent, Swarm
37-
38-
HAS_SWARM = True
39-
except ImportError:
40-
HAS_SWARM = False
41-
logger.warning("OpenAI Swarm not installed. SwarmAdapter will have limited functionality.")
42-
4345

4446
class SwarmAdapter(MultiAgentAdapter):
4547
"""OpenAI Swarm integration adapter for lightweight multi-agent testing."""
@@ -149,7 +151,11 @@ def wrapped_function(*args, **kwargs):
149151
return wrapped_function
150152

151153
def initiate_conversation(
152-
self, scenario: Scenario, initial_message: str, participants: list[str], **kwargs
154+
self,
155+
scenario: Scenario,
156+
initial_message: str,
157+
participants: list[str],
158+
**kwargs,
153159
) -> SessionID:
154160
"""Initiate a Swarm conversation."""
155161
session_id = f"session_{uuid.uuid4().hex[:8]}"
@@ -323,34 +329,52 @@ def end_session(self, session_id: SessionID) -> ScenarioResult:
323329
session["status"] = "completed"
324330
session["end_time"] = datetime.now()
325331

332+
# Calculate duration in milliseconds
333+
start_time = session.get("start_time")
334+
if start_time:
335+
duration_seconds = (session["end_time"] - start_time).total_seconds()
336+
duration_ms = float(duration_seconds * 1000)
337+
else:
338+
duration_ms = 0.0
339+
326340
# Collect metrics
327341
metrics = self._calculate_swarm_metrics(session_id)
328342

329343
# Create trace log
330344
from agentunit.core.trace import TraceLog
331345

332346
trace = TraceLog()
333-
trace.record("session_complete", session_id=session_id, metrics=metrics)
347+
trace.record(
348+
"session_complete",
349+
session_id=session_id,
350+
metrics=metrics,
351+
)
334352

335353
# Create scenario run
336354
from agentunit.reporting.results import ScenarioRun
337355

338356
scenario_run = ScenarioRun(
339-
scenario_name=session["scenario"].name
340-
if hasattr(session["scenario"], "name")
341-
else "swarm_scenario",
357+
scenario_name=(
358+
session["scenario"].name
359+
if hasattr(session["scenario"], "name")
360+
else "swarm_scenario"
361+
),
342362
case_id=session_id,
343363
success=True,
344364
metrics=metrics,
345-
duration_ms=0.0, # TODO: Track actual duration
365+
duration_ms=duration_ms,
346366
trace=trace,
347367
)
348368

349369
# Create result
370+
from agentunit.reporting.results import ScenarioResult
371+
350372
result = ScenarioResult(
351-
name=session["scenario"].name
352-
if hasattr(session["scenario"], "name")
353-
else "swarm_scenario"
373+
name=(
374+
session["scenario"].name
375+
if hasattr(session["scenario"], "name")
376+
else "swarm_scenario"
377+
)
354378
)
355379
result.add_run(scenario_run)
356380

@@ -414,7 +438,7 @@ def _analyze_handoff_patterns(self, handoffs: list[dict[str, Any]]) -> dict[str,
414438
return {
415439
"handoff_counts": patterns,
416440
"unique_patterns": len(patterns),
417-
"most_common": max(patterns.items(), key=lambda x: x[1]) if patterns else None,
441+
"most_common": (max(patterns.items(), key=lambda x: x[1]) if patterns else None),
418442
}
419443

420444
def _calculate_agent_utilization(self, session_id: SessionID) -> dict[str, float]:

src/agentunit/reporting/results.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ def to_csv(self, path: str | Path) -> Path:
200200

201201
rows.append(row)
202202

203-
if not rows:
204-
return target
205-
206203
fieldnames = sorted({key for row in rows for key in row})
207204

208205
with target.open("w", newline="", encoding="utf-8") as f:

0 commit comments

Comments
 (0)