File size: 30,173 Bytes
881f9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
"""Team-local Agent-as-Tool runner for Experiment 1 Cells A, B, and C.

A thin wrapper over the OpenAI Agents SDK's Runner.run() using AOB's
system prompt verbatim. Cell A feeds it direct Python callables from
mcp_servers/direct_adapter; Cell B feeds it MCPServerStdio connections
to the team's hardened Smart Grid MCP servers; Cell C does the same as
Cell B with two optimizations enabled:

  - parallel_tool_calls=True (set via AAT_PARALLEL_TOOL_CALLS=true in
    configs/aat_mcp_optimized.env) lets the model emit multiple tool
    calls per turn, reducing MCP round-trips.
  - MCP connection reuse: when invoked with --scenarios-glob, all
    scenario×trial runs share the same four MCP subprocesses, eliminating
    per-trial subprocess startup/teardown overhead.

Single-scenario invocation (Cells A and B, and Cell C single-trial):
    aat_runner.py --prompt TEXT --output PATH --model-id ID --mcp-mode MODE

Multi-scenario batch invocation (Cell C connection reuse):
    aat_runner.py --scenarios-glob GLOB --trials N --output-dir DIR
                  --run-basename NAME --model-id ID --mcp-mode optimized

The runner code is otherwise identical across cells — only the tool
surface and invocation mode differ. That is the fairness contract for
latency comparisons between cells.

Spec: docs/specs/2026-04-24-aat-runner-design.md
Issue: #31 (Cell C batch mode), #104 (runner design) → unblocks #25 (Experiment 1).
"""

from __future__ import annotations

import argparse
import asyncio
import datetime as _dt
import glob
import json
import logging
import os
import shlex
import sys
import time
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any

# Ensure repo root is on sys.path so `from scripts.*` imports resolve when
# this file is invoked as `python scripts/aat_runner.py` (which puts
# scripts/, not the repo root, on sys.path by default).
_REPO_ROOT = Path(__file__).resolve().parent.parent
if str(_REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(_REPO_ROOT))

_LOG = logging.getLogger("aat_runner")


def _json_default(obj: Any) -> Any:
    """JSON encoder for trial output payloads. Type-specific, fail-closed.

    Replaces the prior `default=str` which silently stringified anything —
    fine for `pandas.Timestamp`/`datetime` (roundtrippable ISO-8601) but
    lossy for `numpy.ndarray` (str() truncates above ~1000 elements per
    `numpy.set_printoptions`) and outright wrong for `pandas.DataFrame`
    (str() is the human-readable table). #131 / PR #130 review Medium 5.

    Accept: stdlib datetime + date, plus pandas.Timestamp and
    numpy.datetime64 if those packages are importable. Anything else
    raises TypeError so the caller sees the failure instead of a
    silently-corrupted artifact downstream.
    """
    if isinstance(obj, (_dt.datetime, _dt.date)):
        return obj.isoformat()

    # pandas / numpy are optional dependencies — import lazily and fall
    # through to TypeError if absent so we don't pretend to handle them.
    try:
        import pandas as _pd  # type: ignore

        if isinstance(obj, _pd.Timestamp):
            # pandas.Timestamp.isoformat() is roundtrippable.
            return obj.isoformat()
    except ImportError:
        pass
    try:
        import numpy as _np  # type: ignore

        if isinstance(obj, _np.datetime64):
            # `_np.datetime64` doesn't have isoformat(); cast through pandas
            # if available for an ISO string, else emit the str() form which
            # IS roundtrippable for datetime64 specifically (e.g.
            # '2026-04-28T17:00:00.000000000').
            try:
                import pandas as _pd  # type: ignore

                return _pd.Timestamp(obj).isoformat()
            except ImportError:
                return str(obj)
    except ImportError:
        pass

    raise TypeError(
        f"unserializable type {type(obj).__module__}.{type(obj).__name__!r}: "
        f"{_json_default.__module__} only handles datetime / date / "
        f"pandas.Timestamp / numpy.datetime64. If a tool is returning a "
        f"numpy array or pandas DataFrame, convert it inside the tool to a "
        f"plain list/dict before returning, since str() on those types is "
        f"silently lossy."
    )


# Importable without the SDK installed, so unit tests can patch before import.
# Real imports happen lazily inside _main().


def _expand_scenario_glob(scenario_glob: str, repo_root: Path) -> list[Path]:
    """Expand one glob pattern or a shell-style list of scenario paths.

    `run_experiment.sh` has long accepted `SCENARIOS_GLOB` values containing
    either one glob or a whitespace-separated list of explicit paths. Cell C
    passes that value through to this batch runner, so mirror the shell-level
    behavior here instead of treating the whole string as one literal glob.
    """
    if not scenario_glob.strip():
        return []

    tokens = shlex.split(scenario_glob)
    if not tokens:
        return []

    paths: list[Path] = []
    seen: set[Path] = set()
    for token in tokens:
        pattern = Path(token)
        if not pattern.is_absolute():
            pattern = repo_root / pattern
        for raw_match in sorted(glob.glob(str(pattern))):
            match = Path(raw_match)
            key = match.resolve()
            if key in seen:
                continue
            paths.append(match)
            seen.add(key)
    return paths


def build_parser() -> argparse.ArgumentParser:
    p = argparse.ArgumentParser(
        prog="aat_runner",
        description="Team-local Agent-as-Tool runner for Cells A, B, and C of Experiment 1.",
    )
    p.add_argument(
        "--prompt",
        default=None,
        help="Scenario text — required in single-scenario mode (omit when using --scenarios-glob).",
    )
    p.add_argument(
        "--output",
        default=None,
        help="Trial JSON output path — required in single-scenario mode.",
    )
    p.add_argument(
        "--scenarios-glob",
        default=None,
        dest="scenarios_glob",
        help=(
            "Multi-scenario batch mode: glob pattern relative to repo root "
            "(e.g. 'data/scenarios/multi_*.json'). MCP servers are reused "
            "across all scenario×trial runs. Replaces --prompt/--output."
        ),
    )
    p.add_argument(
        "--trials",
        type=int,
        default=1,
        help="Trials per scenario in multi-scenario batch mode (default 1).",
    )
    p.add_argument(
        "--output-dir",
        default=None,
        dest="output_dir",
        help="Output directory for batch mode (required when --scenarios-glob is set).",
    )
    p.add_argument(
        "--run-basename",
        default="batch",
        dest="run_basename",
        help=(
            "Filename prefix for batch-mode trial outputs. "
            "run_experiment.sh passes RUN_BASENAME here so naming matches single-trial convention."
        ),
    )
    p.add_argument(
        "--model-id",
        required=True,
        help="LiteLLM-style model string, e.g. openai/Llama-3.1-8B-Instruct",
    )
    p.add_argument(
        "--mcp-mode",
        required=True,
        choices=("direct", "baseline", "optimized"),
        help=(
            "direct = Cell A in-process callables; "
            "baseline = Cell B MCP stdio; "
            "optimized = Cell C MCP stdio (parallel tool calls configured separately)"
        ),
    )
    p.add_argument(
        "--max-turns",
        type=int,
        default=30,
        help="Agent turn budget (default 30 matches upstream openai-agent)",
    )
    p.add_argument(
        "--parallel-tool-calls",
        type=_parse_parallel_tool_calls,
        # argparse captures env defaults when the parser is constructed; tests
        # that exercise env-driven defaults must set AAT_PARALLEL_TOOL_CALLS
        # before calling build_parser().
        default=_parse_parallel_tool_calls(os.environ.get("AAT_PARALLEL_TOOL_CALLS")),
        help=(
            "Whether the model may emit multiple tool calls in one turn. "
            "Use false for local vLLM Llama 3 tool-calling compatibility "
            "(default: false; set AAT_PARALLEL_TOOL_CALLS=auto for SDK default)."
        ),
    )
    p.add_argument("--verbose", action="store_true")
    return p


def _setup_logging(verbose: bool) -> None:
    logging.basicConfig(
        level=logging.DEBUG if verbose else logging.INFO,
        format="%(asctime)s %(levelname)s %(name)s: %(message)s",
    )


def _parse_parallel_tool_calls(value: str | None) -> bool | None:
    """Parse CLI/env tri-state for Agents SDK parallel tool calls."""
    raw = (value if value is not None else "false").strip().lower()
    if raw in {"", "false", "0", "no", "off"}:
        return False
    if raw in {"true", "1", "yes", "on"}:
        return True
    if raw in {"auto", "default", "none"}:
        return None
    raise argparse.ArgumentTypeError(
        "parallel tool calls must be true, false, or auto; " f"got {value!r}"
    )


def _is_watsonx_model(model_id: str) -> bool:
    return model_id.strip().lower().startswith("watsonx/")


def _configure_litellm_provider_compat(model_id: str) -> None:
    """Apply provider-specific LiteLLM compatibility knobs."""
    if not _is_watsonx_model(model_id):
        return
    # Bridge documented WATSONX_* env vars to the WX_* names litellm's newer
    # WatsonX provider expects (litellm 1.81.x rejects with
    # "Watsonx project_id and space_id not set" otherwise). Shared helper
    # at scripts/watsonx_env.py covers every Python call site that drives
    # a watsonx/* model. (#177)
    from scripts.watsonx_env import propagate_watsonx_env

    propagate_watsonx_env()
    try:
        import litellm  # type: ignore
    except ImportError:
        return
    # WatsonX rejects OpenAI-only request fields such as parallel_tool_calls
    # even when their value is false. Ask LiteLLM to drop unsupported params
    # instead of turning hosted 70B AaT spot checks into artifact failures.
    litellm.drop_params = True


def _serialize_run_result(
    args: argparse.Namespace,
    prompt: str,
    result: Any,
    duration_seconds: float,
    scenario_file: str | None = None,
) -> dict[str, Any]:
    """Translate an Agents SDK RunResult into our output JSON schema.

    Walks ``result.new_items`` with ``item_type`` discrimination, matching
    AOB's _build_trajectory in
    ../AssetOpsBench/src/agent/openai_agent/runner.py:121. A turn boundary
    is each ``message_output_item``; tool calls and their outputs that
    precede the next message belong to the current turn.
    """
    from scripts.aat_system_prompt import AOB_PROMPT_SHA

    items = list(getattr(result, "new_items", []) or [])
    history: list[dict[str, Any]] = []
    tool_call_count = 0
    turn = 0
    pending_tool_calls: list[dict[str, Any]] = []
    failed_tools: list[dict[str, str]] = []

    # Flushing semantics match AOB's _build_trajectory
    # (../AssetOpsBench/src/agent/openai_agent/runner.py:121). A "turn" is
    # terminated by the next message_output_item; any tool calls that
    # appeared before that message are attributed to the turn being CLOSED,
    # not to the new message's turn.
    current_text_parts: list[str] = []

    def _close_turn() -> None:
        """If there's pending text or tool calls, emit a turn record for them.

        Empty-content turns with non-empty tool_calls are valid tool-execution
        turns, not parser anomalies. Some SDK item orderings surface a tool
        call before the assistant text that explains it.
        """
        nonlocal turn
        if not current_text_parts and not pending_tool_calls:
            return
        turn += 1
        history.append(
            {
                "turn": turn,
                "role": "assistant",
                "content": "".join(current_text_parts),
                "tool_calls": list(pending_tool_calls),
            }
        )
        current_text_parts.clear()
        pending_tool_calls.clear()

    for item in items:
        item_type = getattr(item, "type", "")
        if item_type == "message_output_item":
            # New assistant message arrived — close out the previous turn.
            _close_turn()
            raw = getattr(item, "raw_item", None)
            if raw is not None:
                for part in getattr(raw, "content", []) or []:
                    t = getattr(part, "text", None)
                    if t:
                        current_text_parts.append(t)
        elif item_type == "tool_call_item":
            raw = getattr(item, "raw_item", None)
            if raw is None:
                continue
            name = getattr(raw, "name", "") or ""
            call_id = getattr(raw, "call_id", "") or getattr(raw, "id", "") or ""
            args_raw = getattr(raw, "arguments", "{}") or "{}"
            try:
                arguments = (
                    json.loads(args_raw) if isinstance(args_raw, str) else args_raw
                )
            except (json.JSONDecodeError, TypeError):
                arguments = {"raw": args_raw}
            pending_tool_calls.append(
                {
                    "name": name,
                    "arguments": arguments,
                    "call_id": call_id,
                }
            )
            tool_call_count += 1
        elif item_type == "tool_call_output_item":
            output = getattr(item, "output", "")
            # Attach to the most recent pending call that has no output yet.
            attached = False
            for call in reversed(pending_tool_calls):
                if "output" not in call:
                    call["output"] = output
                    attached = True
                    break
            if not attached:
                _LOG.warning(
                    "dropping tool_call_output_item without a pending tool call"
                )
            # Defensive: the real Agents SDK today surfaces tool errors as
            # stringified output content, not a separate .error attribute.
            # Kept as a forward-compat hook for future SDK versions that may.
            err = getattr(item, "error", None)
            if err:
                last_name = pending_tool_calls[-1]["name"] if pending_tool_calls else ""
                failed_tools.append({"tool": last_name, "error": str(err)})

    # Close the final turn (text or tool calls still buffered).
    _close_turn()

    final_output = getattr(result, "final_output", None)
    if final_output:
        answer = final_output
    else:
        # Find the last turn with non-empty assistant content.
        answer = ""
        for entry in reversed(history):
            if entry.get("role") == "assistant" and entry.get("content"):
                answer = entry["content"]
                break

    max_turns_reached = bool(getattr(result, "max_turns_reached", False))
    success = (not max_turns_reached) and bool(answer)

    try:
        import agents as _agents

        sdk_version = getattr(_agents, "__version__", "unknown")
    except Exception:
        sdk_version = "unknown"

    # Token usage — pulled from the OpenAI Agents SDK's
    # RunContextWrapper.usage. The SDK accumulates per-LLM-call counters
    # across the run loop, so this single read covers every turn the
    # agent took. Missing on stubbed test results / older SDKs; fall
    # through to None values so summary aggregation can detect missing
    # data instead of treating it as zero. (#133 / PR #130 review Low 9)
    usage_block: dict[str, Any] = {
        "input_tokens": None,
        "output_tokens": None,
        "total_tokens": None,
        "requests": None,
    }
    ctx_wrapper = getattr(result, "context_wrapper", None)
    sdk_usage = getattr(ctx_wrapper, "usage", None) if ctx_wrapper else None
    if sdk_usage is not None:
        for field_name in ("input_tokens", "output_tokens", "total_tokens", "requests"):
            value = getattr(sdk_usage, field_name, None)
            if isinstance(value, int) and not isinstance(value, bool) and value >= 0:
                usage_block[field_name] = value

    return {
        "question": prompt,
        "answer": answer,
        "success": success,
        "failed_tools": failed_tools,
        "max_turns_exhausted": max_turns_reached,
        "turn_count": turn,
        "tool_call_count": tool_call_count,
        "usage": usage_block,
        "history": history,
        "runner_meta": {
            "model_id": args.model_id,
            "mcp_mode": args.mcp_mode,
            "aob_prompt_sha": AOB_PROMPT_SHA,
            "max_turns": args.max_turns,
            "parallel_tool_calls": args.parallel_tool_calls,
            "sdk_version": f"openai-agents=={sdk_version}",
            "duration_seconds": duration_seconds,
            "scenario_file": scenario_file,
        },
    }


def _write_output(path: Path, payload: dict[str, Any]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(
        json.dumps(payload, indent=2, default=_json_default) + "\n",
        encoding="utf-8",
    )


@dataclass
class AaTRunner:
    """Minimal wrapper around Agents SDK. Constructor-injected tool surface."""

    model_id: str
    mcp_mode: str
    max_turns: int = 30
    tools: list = field(default_factory=list)
    mcp_servers: list = field(default_factory=list)
    litellm_base_url: str | None = None
    litellm_api_key: str | None = None
    parallel_tool_calls: bool | None = False

    async def run(self, prompt: str):
        from agents import Agent, ModelSettings, Runner
        from agents.extensions.models.litellm_model import LitellmModel
        from scripts.aat_system_prompt import AOB_SYSTEM_PROMPT

        if _is_watsonx_model(self.model_id) and self.parallel_tool_calls is True:
            raise ValueError(
                "WatsonX does not support parallel_tool_calls; set "
                "AAT_PARALLEL_TOOL_CALLS=false or auto for hosted WatsonX runs."
            )
        _configure_litellm_provider_compat(self.model_id)

        base_url = self.litellm_base_url or os.environ.get("LITELLM_BASE_URL")
        api_key = self.litellm_api_key or os.environ.get("LITELLM_API_KEY")
        effective_parallel_tool_calls = self.parallel_tool_calls
        if _is_watsonx_model(self.model_id) and effective_parallel_tool_calls is False:
            effective_parallel_tool_calls = None
        agent_kwargs: dict[str, Any] = {
            "name": "smartgrid_aat",
            "instructions": AOB_SYSTEM_PROMPT,
            "tools": self.tools,
            "mcp_servers": self.mcp_servers,
            "model": LitellmModel(
                model=self.model_id,
                base_url=base_url,
                api_key=api_key,
            ),
        }
        if effective_parallel_tool_calls is not None:
            agent_kwargs["model_settings"] = ModelSettings(
                parallel_tool_calls=self.parallel_tool_calls,
            )
        agent = Agent(**agent_kwargs)
        return await Runner.run(agent, prompt, max_turns=self.max_turns)


async def _main_multi(args: argparse.Namespace, repo_root: Path) -> int:
    """Multi-scenario batch mode: keeps MCP servers alive across all runs.

    Globs scenario files, runs each scenario for args.trials trials, writes
    per-trial JSON to args.output_dir, and appends a _batch_latencies.jsonl
    with per-trial latency records in the same format as latencies.jsonl.
    """
    if args.mcp_mode != "optimized":
        _LOG.error(
            "--scenarios-glob is only supported with --mcp-mode optimized; got %r",
            args.mcp_mode,
        )
        return 2

    if args.trials < 1:
        _LOG.error("--trials must be >= 1; got %d", args.trials)
        return 2

    from scripts.aat_tools_mcp import build_mcp_servers

    output_dir = Path(args.output_dir)
    if not output_dir.is_absolute():
        output_dir = repo_root / output_dir
    output_dir.mkdir(parents=True, exist_ok=True)

    scenario_files = _expand_scenario_glob(args.scenarios_glob, repo_root)
    if not scenario_files:
        _LOG.error("no scenario files matched --scenarios-glob %r", args.scenarios_glob)
        return 2

    _LOG.info(
        "batch mode: %d scenario(s) × %d trial(s), output → %s",
        len(scenario_files),
        args.trials,
        output_dir,
    )

    mcp_servers: list = []
    latency_records: list[dict[str, Any]] = []
    any_failed = False

    try:
        mcp_setup_start = time.time()
        mcp_servers = await build_mcp_servers(repo_root)
        mcp_setup_seconds = time.time() - mcp_setup_start
        _LOG.info("MCP servers ready (setup=%.1fs)", mcp_setup_seconds)

        runner = AaTRunner(
            model_id=args.model_id,
            mcp_mode=args.mcp_mode,
            max_turns=args.max_turns,
            mcp_servers=mcp_servers,
            parallel_tool_calls=args.parallel_tool_calls,
        )

        for sf in scenario_files:
            try:
                scenario_payload = json.loads(sf.read_text(encoding="utf-8"))
                if not isinstance(scenario_payload, dict):
                    raise TypeError(
                        f"scenario payload must be a JSON object, got {type(scenario_payload).__name__}"
                    )
                prompt = scenario_payload["text"]
            except (json.JSONDecodeError, KeyError, TypeError) as exc:
                _LOG.error("skipping %s — failed to read prompt: %s", sf, exc)
                any_failed = True
                continue

            sf_rel = sf.relative_to(repo_root).as_posix()

            for trial in range(1, args.trials + 1):
                out_name = f"{args.run_basename}_{sf.stem}_run{trial:02d}.json"
                out_path = output_dir / out_name

                start = time.time()
                # False is the correct safe default: exception path leaves it False.
                trial_ok = False
                try:
                    result = await runner.run(prompt)
                    # run_only_duration measures only runner.run(prompt).
                    # It excludes file write and later shared-resource cleanup,
                    # so batch runner_meta.duration_seconds is run-only time.
                    run_only_duration = time.time() - start
                    output = _serialize_run_result(
                        args, prompt, result, run_only_duration, scenario_file=sf_rel
                    )
                    output["scenario"] = scenario_payload
                    _write_output(out_path, output)
                    trial_ok = output["success"]
                    if not trial_ok:
                        any_failed = True
                    if output["max_turns_exhausted"]:
                        _LOG.warning(
                            "max_turns=%d exhausted (%s trial %d)",
                            args.max_turns,
                            sf.name,
                            trial,
                        )
                except Exception as exc:
                    _LOG.exception(
                        "trial failed (%s trial %d): %s", sf.name, trial, exc
                    )
                    from scripts.aat_system_prompt import AOB_PROMPT_SHA

                    error_payload = {
                        "question": prompt,
                        "answer": "",
                        "success": False,
                        "error": f"{type(exc).__name__}: {exc}",
                        "failed_tools": [],
                        "max_turns_exhausted": False,
                        "turn_count": 0,
                        "tool_call_count": 0,
                        "history": [],
                        "scenario": scenario_payload,
                        "runner_meta": {
                            "model_id": args.model_id,
                            "mcp_mode": args.mcp_mode,
                            "aob_prompt_sha": AOB_PROMPT_SHA,
                            "max_turns": args.max_turns,
                            "parallel_tool_calls": args.parallel_tool_calls,
                            "sdk_version": "unknown",
                            "duration_seconds": time.time() - start,
                            "scenario_file": sf_rel,
                        },
                    }
                    _write_output(out_path, error_payload)
                    any_failed = True

                # wall_clock_duration: ends after the JSON write to match
                # single-trial path semantics (run_experiment.sh measures
                # END_EPOCH after the runner process exits, which includes
                # the file write).
                wall_clock_duration = time.time() - start

                try:
                    _rel_path = out_path.relative_to(repo_root).as_posix()
                except ValueError:
                    _rel_path = out_path.as_posix()
                latency_records.append(
                    {
                        "scenario_file": sf_rel,
                        "trial_index": trial,
                        "latency_seconds": wall_clock_duration,
                        "output_path": _rel_path,
                        # One-time MCP setup cost: notebooks can compute total
                        # batch cost as sum(latency_seconds) + mcp_setup_seconds.
                        # Per-trial latency_seconds remains comparable to the
                        # single-trial path (where each trial pays MCP startup).
                        "mcp_setup_seconds": mcp_setup_seconds,
                    }
                )
                _LOG.info(
                    "wrote %s (%.1fs, success=%s)",
                    out_path.name,
                    wall_clock_duration,
                    trial_ok,
                )

    finally:
        for srv in mcp_servers:
            try:
                await srv.cleanup()
            except (asyncio.CancelledError, Exception) as cleanup_exc:
                _LOG.warning("cleanup failed for %s: %s", srv, cleanup_exc)

    latency_path = output_dir / "_batch_latencies.jsonl"
    latency_path.write_text(
        "\n".join(json.dumps(r) for r in latency_records) + "\n",
        encoding="utf-8",
    )
    _LOG.info("wrote %d latency records → %s", len(latency_records), latency_path)

    return 1 if any_failed else 0


async def _main(args: argparse.Namespace) -> int:
    repo_root = Path(__file__).resolve().parent.parent

    # Batch mode: multi-scenario with MCP connection reuse (Cell C optimized).
    if args.scenarios_glob:
        if not args.output_dir:
            _LOG.error("--output-dir is required when --scenarios-glob is set")
            return 2
        return await _main_multi(args, repo_root)

    # Single-scenario mode: validate that --prompt and --output are present.
    if not args.prompt or not args.output:
        _LOG.error("--prompt and --output are required in single-scenario mode")
        return 2

    tools: list = []
    mcp_servers: list = []

    if args.mcp_mode == "direct":
        from scripts.aat_tools_direct import build_direct_tools

        tools = build_direct_tools()
    elif args.mcp_mode == "baseline":
        from scripts.aat_tools_mcp import build_mcp_servers

        mcp_servers = await build_mcp_servers(repo_root)
    elif args.mcp_mode == "optimized":
        from scripts.aat_tools_mcp import build_mcp_servers

        mcp_servers = await build_mcp_servers(repo_root)
    else:
        _LOG.error("unknown --mcp-mode %r", args.mcp_mode)
        return 2

    runner = AaTRunner(
        model_id=args.model_id,
        mcp_mode=args.mcp_mode,
        max_turns=args.max_turns,
        tools=tools,
        mcp_servers=mcp_servers,
        parallel_tool_calls=args.parallel_tool_calls,
    )

    start = time.time()
    error_payload: dict[str, Any] | None = None
    try:
        result = await runner.run(args.prompt)
    except Exception as exc:
        _LOG.exception("runner failed: %s", exc)
        from scripts.aat_system_prompt import AOB_PROMPT_SHA

        error_payload = {
            "question": args.prompt,
            "answer": "",
            "success": False,
            "error": f"{type(exc).__name__}: {exc}",
            "failed_tools": [],
            "max_turns_exhausted": False,
            "turn_count": 0,
            "tool_call_count": 0,
            "history": [],
            "runner_meta": {
                "model_id": args.model_id,
                "mcp_mode": args.mcp_mode,
                "aob_prompt_sha": AOB_PROMPT_SHA,
                "max_turns": args.max_turns,
                "parallel_tool_calls": args.parallel_tool_calls,
                "sdk_version": "unknown",
                "duration_seconds": time.time() - start,
                "scenario_file": None,
            },
        }
    finally:
        for srv in mcp_servers:
            try:
                await srv.cleanup()
            except (asyncio.CancelledError, Exception) as cleanup_exc:
                # CancelledError is a BaseException in Py3.8+, and the
                # openai-agents MCP stdio teardown can surface it from anyio
                # subprocess waits. Cleanup failures should not mask the
                # original runner error.
                _LOG.warning("cleanup failed for %s: %s", srv, cleanup_exc)

    if error_payload is not None:
        _write_output(Path(args.output), error_payload)
        return 1

    duration = time.time() - start
    output = _serialize_run_result(
        args, args.prompt, result, duration, scenario_file=None
    )
    _write_output(Path(args.output), output)

    if output["max_turns_exhausted"]:
        _LOG.warning(
            "max_turns=%d exhausted without a final answer; trial marked unsuccessful",
            args.max_turns,
        )

    return 0 if output["success"] else 1


def main() -> None:
    args = build_parser().parse_args()
    _setup_logging(args.verbose)
    rc = asyncio.run(_main(args))
    sys.exit(rc)


if __name__ == "__main__":
    main()