File size: 4,462 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
"""Tests for the upstream AOB OpenAIAgentRunner parity wrapper."""

from __future__ import annotations

import pathlib
import sys
from types import SimpleNamespace

import pytest


def test_upstream_patch_requires_mcp_factory() -> None:
    from scripts.aat_system_prompt import AOB_SOURCE_SHA
    from scripts.aat_upstream_openai_runner import _patch_aob_openai_runner

    module = SimpleNamespace(Agent=lambda *args, **kwargs: None)

    with pytest.raises(RuntimeError) as excinfo:
        _patch_aob_openai_runner(module, pathlib.Path.cwd())

    message = str(excinfo.value)
    assert "_build_mcp_servers" in message
    assert AOB_SOURCE_SHA in message


def test_upstream_patch_requires_agent_symbol() -> None:
    from scripts.aat_system_prompt import AOB_SOURCE_SHA
    from scripts.aat_upstream_openai_runner import _patch_aob_openai_runner

    module = SimpleNamespace(_build_mcp_servers=lambda *_args, **_kwargs: [])

    with pytest.raises(RuntimeError) as excinfo:
        _patch_aob_openai_runner(module, pathlib.Path.cwd())

    message = str(excinfo.value)
    assert "Agent" in message
    assert AOB_SOURCE_SHA in message


def test_upstream_patch_replaces_expected_symbols(monkeypatch) -> None:
    from scripts.aat_upstream_openai_runner import _patch_aob_openai_runner

    def original_build() -> list[object]:
        return []

    def original_agent(*_args: object, **_kwargs: object) -> None:
        return None

    module = SimpleNamespace(
        _build_mcp_servers=original_build,
        Agent=original_agent,
    )

    monkeypatch.setitem(
        sys.modules,
        "agents",
        SimpleNamespace(Agent=original_agent, ModelSettings=lambda **_kwargs: None),
    )
    monkeypatch.setitem(
        sys.modules,
        "agents.mcp",
        SimpleNamespace(MCPServerStdio=lambda **_kwargs: None),
    )

    patches = _patch_aob_openai_runner(module, pathlib.Path.cwd())

    assert module._build_mcp_servers is not original_build
    assert module.Agent is not original_agent
    assert "mcp_server_launch" in patches
    assert any(patch.startswith("parallel_tool_calls=") for patch in patches)


def test_upstream_patch_omits_watsonx_parallel_setting(monkeypatch) -> None:
    from scripts.aat_upstream_openai_runner import _patch_aob_openai_runner

    captured: dict[str, object] = {}

    def original_build() -> list[object]:
        return []

    def sdk_agent(*_args: object, **kwargs: object) -> None:
        captured["kwargs"] = kwargs
        return None

    class FakeModelSettings:
        def __init__(self, **kwargs):
            self.kwargs = kwargs

    fake_litellm = SimpleNamespace(drop_params=False)
    monkeypatch.setitem(sys.modules, "litellm", fake_litellm)
    monkeypatch.setitem(
        sys.modules,
        "agents",
        SimpleNamespace(Agent=sdk_agent, ModelSettings=FakeModelSettings),
    )
    monkeypatch.setitem(
        sys.modules,
        "agents.mcp",
        SimpleNamespace(MCPServerStdio=lambda **_kwargs: None),
    )

    module = SimpleNamespace(
        _build_mcp_servers=original_build,
        Agent=sdk_agent,
    )

    patches = _patch_aob_openai_runner(
        module,
        pathlib.Path.cwd(),
        model_id="watsonx/meta-llama/llama-3-3-70b-instruct",
    )
    module.Agent(name="x")

    assert "model_settings" not in captured["kwargs"]
    assert "watsonx_drop_unsupported_params" in patches
    assert fake_litellm.drop_params is True


def test_upstream_serialize_marks_max_turns_unsuccessful(tmp_path) -> None:
    from scripts.aat_upstream_openai_runner import _serialize_result

    result = SimpleNamespace(
        answer="partial answer",
        max_turns_reached=True,
        trajectory=SimpleNamespace(turns=[]),
    )
    args = SimpleNamespace(
        aob_path=str(tmp_path),
        model_id="litellm_proxy/test",
        max_turns=3,
    )

    payload = _serialize_result(
        args=args,
        prompt="question",
        result=result,
        duration_seconds=1.0,
        server_paths={},
        patches=[],
    )

    assert payload["answer"] == "partial answer"
    assert payload["success"] is False
    assert payload["max_turns_exhausted"] is True


def test_smartgrid_server_paths_fail_early(tmp_path) -> None:
    from scripts.aat_upstream_openai_runner import _smartgrid_server_paths

    with pytest.raises(FileNotFoundError, match="Smart Grid MCP server missing"):
        _smartgrid_server_paths(tmp_path)