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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Agent-expansion tests: parallel tool rounds, delegation, and the
Telegram bridge (tg tools, plugin_manager, raw MTProto allowlist).

Follows the established pattern in tests/test_agent.py: a stubbed
``pyUltroid`` namespace, fake provider SDKs, and plain ``asyncio.run``.
"""

from __future__ import annotations

import asyncio
import importlib
import logging
import sys
import time
import types

import pytest
from conftest import ROOT


def _install_stub_namespace():
    root = types.ModuleType("pyUltroid")
    root.__path__ = [str(ROOT / "pyUltroid")]
    root.LOGS = logging.getLogger("agent-expansion-tests")
    root.asst = None
    root.ultroid_bot = None
    root.udB = None
    sys.modules["pyUltroid"] = root

    startup = types.ModuleType("pyUltroid.startup")
    startup.__path__ = []
    logger = types.ModuleType("pyUltroid.startup.logger")
    logger.LOGS = root.LOGS
    sys.modules["pyUltroid.startup"] = startup
    sys.modules["pyUltroid.startup.logger"] = logger

    configs = types.ModuleType("pyUltroid.configs")
    from types import SimpleNamespace

    configs.Var = SimpleNamespace()
    sys.modules["pyUltroid.configs"] = configs
    return root


@pytest.fixture
def agent_modules(module_isolation):
    with module_isolation("pyUltroid"):
        root = _install_stub_namespace()
        importlib.import_module("pyUltroid.agent")
        yield SimpleNamespace2(
            root=root,
            pkg=sys.modules["pyUltroid.agent"],
            tools=sys.modules["pyUltroid.agent._tools"],
            agent_mod=sys.modules["pyUltroid.agent._agent"],
            bridge=sys.modules["pyUltroid.agent._tgbridge"],
            runtime=sys.modules["pyUltroid.agent._runtime"],
            ctx=sys.modules["pyUltroid.shell._context"],
        )


class SimpleNamespace2:
    def __init__(self, **kw):
        self.__dict__.update(kw)


class FakeMini:
    """Scripted MiniAI stand-in: pops one AIResponse per round."""

    def __init__(self, turns):
        self._turns = list(turns)

    async def mini_respond(self, **kwargs):
        return self._turns.pop(0)


def _response(mod, **kw):
    return mod.AIResponse(provider="fake", model="fake-model", **kw)


def _tool_call(name, args, call_id="c1"):
    import json

    return {"name": name, "arguments": json.dumps(args), "id": call_id}


# ── Schemas / caps ──────────────────────────────────────────────────────────


def test_new_tools_registered(agent_modules):
    names = {t["function"]["name"] for t in agent_modules.tools.TOOL_SCHEMAS}
    for expected in (
        "tg_route_send",
        "send_text_file",
        "tg_read",
        "plugin_manager",
        "raw_mtproto",
        "web_fetch",
        "think",
        "delegate",
    ):
        assert expected in names, f"{expected} missing from TOOL_SCHEMAS"
        assert expected in agent_modules.tools.TOOL_REQUIRED_CAPS
    route_send = next(
        schema["function"]
        for schema in agent_modules.tools.TOOL_SCHEMAS
        if schema["function"]["name"] == "tg_route_send"
    )
    assert route_send["parameters"]["properties"]["rich"]["type"] == "boolean"


def test_group_safe_excludes_escalations(agent_modules):
    caps = agent_modules.runtime.GROUP_SAFE_CAPS
    assert "agent.spawn" not in caps
    assert "tg.raw" not in caps
    assert agent_modules.tools.Cap.TG_SEND_FILE not in caps
    assert (
        agent_modules.tools.Cap.TG_SEND_FILE in agent_modules.tools.DEFAULT_AGENT_CAPS
    )
    # owner/sudo default keeps delegation and read access
    defaults = agent_modules.tools.DEFAULT_AGENT_CAPS
    assert "agent.spawn" in defaults
    assert "tg.read" in defaults
    assert "tg.raw" not in defaults


# ── Parallel tool execution ─────────────────────────────────────────────────


def test_tool_calls_in_one_round_run_concurrently(agent_modules):
    tools_mod = agent_modules.tools
    agent_mod = agent_modules.agent_mod

    async def slow_think(self, thought):
        await asyncio.sleep(0.08)
        return {"ok": True}

    original = tools_mod.ToolExecutor._tool_think
    tools_mod.ToolExecutor._tool_think = slow_think
    try:
        fake = FakeMini(
            [
                _response(
                    agent_modules.pkg,
                    content="",
                    tool_calls=[
                        _tool_call("think", {"thought": "a"}, "c1"),
                        _tool_call("think", {"thought": "b"}, "c2"),
                    ],
                ),
                _response(agent_modules.pkg, content="done"),
            ]
        )
        agent = agent_mod.UltroidAgent(mini_ai=fake)
        t0 = time.perf_counter()
        resp = asyncio.run(
            agent.respond("x", chat_id=1, use_history=False, caps=frozenset())
        )
        elapsed = time.perf_counter() - t0
        assert resp.ok
        assert resp.content == "done"
        assert resp.tool_rounds == 1
        assert sorted(resp.tools_used) == ["think", "think"]
        # Two 80 ms sleeps running concurrently finish well under 160 ms.
        assert elapsed < 0.15, f"tool calls appear sequential ({elapsed:.3f}s)"
    finally:
        tools_mod.ToolExecutor._tool_think = original


def test_identical_tool_calls_in_one_round_execute_once(agent_modules):
    tools_mod = agent_modules.tools
    calls = []

    async def counted_think(self, thought):
        calls.append(thought)
        return {"ok": True}

    original = tools_mod.ToolExecutor._tool_think
    tools_mod.ToolExecutor._tool_think = counted_think
    try:
        fake = FakeMini(
            [
                _response(
                    agent_modules.pkg,
                    content="",
                    tool_calls=[
                        _tool_call("think", {"thought": "same"}, "c1"),
                        _tool_call("think", {"thought": "same"}, "c2"),
                    ],
                ),
                _response(agent_modules.pkg, content="done"),
            ]
        )
        agent = agent_modules.agent_mod.UltroidAgent(mini_ai=fake)
        result = asyncio.run(
            agent.respond("x", chat_id=1, use_history=False, caps=frozenset())
        )
        assert result.ok
        assert result.content == "done"
        assert calls == ["same"]
    finally:
        tools_mod.ToolExecutor._tool_think = original


# ── Delegation depth ────────────────────────────────────────────────────────


def test_delegate_refused_at_depth_limit(agent_modules):
    executor = agent_modules.tools.ToolExecutor(chat_id=1, depth=2)
    result = asyncio.run(executor.execute("delegate", {"task": "x"}))
    assert result.get("denied")


def test_delegate_refused_without_spawn_cap(agent_modules):
    executor = agent_modules.tools.ToolExecutor(chat_id=1, caps=frozenset({"tg.read"}))
    result = asyncio.run(executor.execute("delegate", {"task": "x"}))
    assert result.get("denied")


def test_delegate_caps_strip_spawn(agent_modules, monkeypatch):
    """The sub-agent's executor must never carry agent.spawn."""
    captured = {}
    tools_mod = agent_modules.tools
    agent_mod = agent_modules.agent_mod

    parent = tools_mod.ToolExecutor(
        chat_id=1,
        caps=frozenset(tools_mod.DEFAULT_AGENT_CAPS),
        depth=0,
    )

    class FakeSubAgent:
        def __init__(self):
            pass

        async def respond(self, **kwargs):
            captured["caps"] = kwargs.get("caps")
            captured["depth"] = kwargs.get("_depth")
            return agent_modules.agent_mod.AgentResponse(
                content="sub done",
                provider="fake",
                model="fake-model",
                tools_used=[],
                tool_rounds=0,
            )

    monkeypatch.setattr(agent_mod, "UltroidAgent", FakeSubAgent)
    result = asyncio.run(parent.execute("delegate", {"task": "research"}))
    assert result["ok"]
    assert "agent.spawn" not in captured["caps"]
    assert captured["depth"] == 1


# ── plugin_manager capability escalation ────────────────────────────────────


def test_plugin_load_denied_by_default(agent_modules):
    executor = agent_modules.tools.ToolExecutor(
        chat_id=1, caps=frozenset(agent_modules.tools.DEFAULT_AGENT_CAPS)
    )
    result = asyncio.run(
        executor.execute("plugin_manager", {"action": "load", "plugin": "echo"})
    )
    assert result.get("denied")


def test_plugin_list_allowed_with_default_caps(agent_modules):
    executor = agent_modules.tools.ToolExecutor(
        chat_id=1, caps=frozenset(agent_modules.tools.DEFAULT_AGENT_CAPS)
    )
    result = asyncio.run(executor.execute("plugin_manager", {"action": "list"}))
    # list only needs PLUGIN_READ (granted); it may fail on missing runtime
    # state, but it must NOT be a capability denial.
    assert not result.get("denied")


# ── Telegram bridge ─────────────────────────────────────────────────────────


def test_parse_entity_ref(agent_modules):
    parse = agent_modules.bridge.parse_entity_ref
    assert parse("123456") == 123456
    assert parse("-100123") == -100123
    assert parse("@user") == "@user"
    assert parse("user") == "user"
    assert parse("https://t.me/somechannel") == "somechannel"
    assert parse("https://t.me/joinchat/AbCdEf") == "AbCdEf"
    assert parse("") is None
    assert parse(None) is None


def _install_fake_telethon(monkeypatch, flood_seconds=None, rpc_error=None):
    """Fake telethon.errors so run_tg_call failure paths are testable."""
    errors_mod = types.ModuleType("telethon.errors")

    class FloodWaitError(Exception):
        def __init__(self, seconds=30):
            self.seconds = seconds

    class RPCError(Exception):
        pass

    errors_mod.FloodWaitError = FloodWaitError
    errors_mod.RPCError = RPCError
    telethon = types.ModuleType("telethon")
    telethon.errors = errors_mod
    monkeypatch.setitem(sys.modules, "telethon", telethon)
    monkeypatch.setitem(sys.modules, "telethon.errors", errors_mod)
    return FloodWaitError, RPCError


def _install_fake_richmsg(monkeypatch):
    """Keep bridge tests independent of core package startup imports."""
    core = types.ModuleType("pyUltroid._core")
    core.__path__ = []
    richmsg = types.ModuleType("pyUltroid._core.richmsg")

    class RichMarkup:
        def __init__(self, text, format="html"):
            self.text = text
            self.format = format

    async def send_rich(client, entity, content, **kwargs):
        return await client.send_message(entity, content.text, **kwargs)

    def blocks_to_plain_text(content):
        return content.text

    async def client_can_send_rich_message(client):
        me = getattr(client, "me", None)
        return bool(getattr(client, "_bot", False) or getattr(me, "premium", False))

    richmsg.RichMarkup = RichMarkup
    richmsg.send_rich = send_rich
    richmsg.blocks_to_plain_text = blocks_to_plain_text
    richmsg.client_can_send_rich_message = client_can_send_rich_message
    monkeypatch.setitem(sys.modules, "pyUltroid._core", core)
    monkeypatch.setitem(sys.modules, "pyUltroid._core.richmsg", richmsg)


def test_run_tg_call_flood_wait(agent_modules, monkeypatch):
    FloodWaitError, _ = _install_fake_telethon(monkeypatch)

    async def raise_flood():
        raise FloodWaitError(42)

    result = asyncio.run(agent_modules.bridge.run_tg_call(raise_flood))
    assert result["ok"] is False
    assert result["error"] == "FloodWait"
    assert result["wait_seconds"] == 42


def test_send_via_master_routing(agent_modules, monkeypatch):
    _install_fake_telethon(monkeypatch)
    _install_fake_richmsg(monkeypatch)
    sent = {}

    async def fake_send(entity, text, **kwargs):
        sent["entity"] = entity
        sent["text"] = text
        sent["kwargs"] = kwargs
        from types import SimpleNamespace

        return SimpleNamespace(id=7, peer_id=SimpleNamespace(user_id=555))

    from types import SimpleNamespace

    user_client = SimpleNamespace(send_message=fake_send)
    master = SimpleNamespace(
        route=lambda: user_client,
        user=user_client,
        bot=None,
        business=None,
        available=lambda: {"user"},
    )
    monkeypatch.setattr(agent_modules.root, "master", master, raising=False)

    result = asyncio.run(
        agent_modules.bridge.send_via_master(
            "hello", "@someone", route="auto", reply_to=3, parse_mode="md"
        )
    )
    assert result["ok"] is True
    assert result["route"] == "auto"
    assert result["message_id"] == 7
    assert sent["kwargs"]["reply_to"] == 3
    assert sent["text"] == "hello"
    assert sent["kwargs"]["parse_mode"] is None


def test_send_via_master_rejects_rich_message_for_nonpremium_user(
    agent_modules, monkeypatch
):
    _install_fake_telethon(monkeypatch)
    _install_fake_richmsg(monkeypatch)
    from types import SimpleNamespace

    sent = []

    async def fake_send(*args, **kwargs):
        sent.append((args, kwargs))

    user_client = SimpleNamespace(
        _bot=False,
        me=SimpleNamespace(bot=False, premium=False),
        send_message=fake_send,
    )
    master = SimpleNamespace(
        route=lambda: user_client,
        user=user_client,
        bot=None,
        business=None,
        available=lambda: {"user"},
    )
    monkeypatch.setattr(agent_modules.root, "master", master, raising=False)

    result = asyncio.run(
        agent_modules.bridge.send_via_master(
            "**bold**", "@someone", route="user", rich=True
        )
    )

    assert result["ok"] is False
    assert "rich=false" in result["error"]
    assert sent == []


def test_send_via_master_missing_route(agent_modules, monkeypatch):
    _install_fake_telethon(monkeypatch)
    _install_fake_richmsg(monkeypatch)
    from types import SimpleNamespace

    master = SimpleNamespace(
        route=lambda: None.__class__,  # never called
        user=None,
        bot=None,
        business=None,
        available=lambda: set(),
    )
    monkeypatch.setattr(agent_modules.root, "master", master, raising=False)
    result = asyncio.run(agent_modules.bridge.send_via_master("hi", "@x", route="bot"))
    assert result["ok"] is False
    assert "no connected client" in result["error"]


# ── Raw MTProto allowlist ───────────────────────────────────────────────────


def _install_fake_telethon_functions(monkeypatch, build_captures):
    tl = types.ModuleType("telethon.tl")
    functions = types.ModuleType("telethon.tl.functions")
    messages = types.ModuleType("telethon.tl.functions.messages")
    channels = types.ModuleType("telethon.tl.functions.channels")

    class GetHistoryRequest:
        def __init__(self, **kwargs):
            build_captures["history"] = kwargs

    class SendMessageRequest:
        def __init__(self, **kwargs):
            build_captures["send"] = kwargs

    class GetChannelsRequest:
        def __init__(self, **kwargs):
            build_captures["channels"] = kwargs

    messages.GetHistoryRequest = GetHistoryRequest
    messages.SendMessageRequest = SendMessageRequest
    channels.GetChannelsRequest = GetChannelsRequest
    functions.messages = messages
    functions.channels = channels
    tl.functions = functions
    monkeypatch.setitem(sys.modules, "telethon.tl", tl)
    monkeypatch.setitem(sys.modules, "telethon.tl.functions", functions)
    monkeypatch.setitem(sys.modules, "telethon.tl.functions.messages", messages)
    monkeypatch.setitem(sys.modules, "telethon.tl.functions.channels", channels)


def test_raw_invoke_denies_write_request(agent_modules, monkeypatch):
    _install_fake_telethon_functions(monkeypatch, {})
    client = object()
    result = asyncio.run(
        agent_modules.bridge.invoke_raw(
            client, "messages.SendMessageRequest", {"message": "hi"}
        )
    )
    assert result["denied"]


def test_raw_invoke_runs_allowlisted_request(agent_modules, monkeypatch):
    captured = {}
    _install_fake_telethon_functions(monkeypatch, captured)
    _install_fake_telethon(monkeypatch)

    async def fake_client(request):
        return "raw-ok"

    result = asyncio.run(
        agent_modules.bridge.invoke_raw(
            fake_client,
            "messages.GetHistoryRequest",
            {"peer": "x", "limit": 5},
        )
    )
    assert result["ok"] is True
    assert result["result_repr"] == repr("raw-ok")
    assert captured["history"]["limit"] == 5


def test_raw_resolve_unknown_namespace(agent_modules, monkeypatch):
    resolve = agent_modules.bridge.resolve_raw_function
    assert resolve("messages.SendMessageRequest") is None
    assert resolve("not.on.the.list") is None
    assert resolve("bogus.Foo") is None


# ── web_fetch scheme gate ───────────────────────────────────────────────────


def test_web_fetch_denies_non_http(agent_modules):
    executor = agent_modules.tools.ToolExecutor(
        chat_id=1, caps=frozenset(agent_modules.tools.DEFAULT_AGENT_CAPS)
    )
    result = asyncio.run(executor.execute("web_fetch", {"url": "file:///etc/passwd"}))
    assert "error" in result