File size: 10,931 Bytes
c0e9d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The interactive tools, end to end through the real executor.

Every path here goes through ``ToolExecutor.execute``, so the capability
policy is genuinely in the loop rather than bypassed.
"""

from __future__ import annotations

import asyncio

import pytest

from pyUltroid.agent._bridge import AskBridge
from pyUltroid.agent._interactive import (
    MAX_CHOICES,
    MAX_QUESTIONS,
    InteractiveTools,
    clamp_ttl,
)
from pyUltroid.agent._tools import TOOL_REQUIRED_CAPS, ToolExecutor
from pyUltroid.shell._context import Cap


class FakeBot:
    def __init__(self):
        self.sent = []
        self._id = 0

    async def send_message(self, chat_id, text, buttons=None, **kw):
        self._id += 1
        self.sent.append({"chat": chat_id, "text": text, "buttons": buttons})
        return type("M", (), {"id": self._id})()


def _executor(chat_id=7, caps=None):
    bridge = AskBridge(bot=FakeBot(), user=object())
    return ToolExecutor(chat_id=chat_id, caps=caps, interactive=InteractiveTools(bridge, chat_id=chat_id)), bridge


def _answer(bridge, chat, value, index=0):
    for session in list(bridge._pending.values()):
        if session.chat_id == chat:
            session.resolve(value)
            bridge._retire(session)
            return
    raise AssertionError("no open question")


def _run(coro_factory, bridge, chat, value):
    async def scenario():
        task = asyncio.ensure_future(coro_factory())
        await asyncio.sleep(0)
        _answer(bridge, chat, value)
        return await task

    return asyncio.run(scenario())


# ── capability policy ───────────────────────────────────────────────────────


def test_ask_requires_its_own_grant():
    """The grant exists, and it is deliberately not a group-safe capability."""
    assert TOOL_REQUIRED_CAPS["ask_user"] == frozenset({Cap.AGENT_ASK})
    from pyUltroid.agent._tools import DEFAULT_AGENT_CAPS
    from pyUltroid.agent._runtime import GROUP_SAFE_CAPS

    # In the default set, so the owner can actually ask.
    assert Cap.AGENT_ASK in DEFAULT_AGENT_CAPS
    # But excluded from the group-safe subset: asking paints a keyboard in
    # someone's chat, which is not something a shared surface grants.
    assert Cap.AGENT_ASK not in GROUP_SAFE_CAPS


def test_ask_is_denied_without_the_grant():
    ex, _ = _executor(caps=frozenset())
    result = asyncio.run(ex.execute("ask_user", {"question": "name?"}))
    assert result["denied"] is True
    assert Cap.AGENT_ASK in result["missing_caps"]


def test_ask_runs_with_the_grant():
    ex, bridge = _executor(caps=frozenset({Cap.AGENT_ASK}))
    out = _run(lambda: ex.execute("ask_user", {"question": "name?"}), bridge, 7, "Ada")
    assert out["answered"] is True
    assert out["user_response"] == "Ada"


def test_confirm_and_collect_are_gated_too():
    ex, _ = _executor(caps=frozenset())
    for name, args in (
        ("confirm_action", {"what": "delete"}),
        ("collect_fields", {"fields": [{"name": "a", "question": "a?"}]}),
    ):
        out = asyncio.run(ex.execute(name, args))
        assert out["denied"] is True, name


# ── ask_user ────────────────────────────────────────────────────────────────


def test_ask_user_reports_the_choices_it_offered():
    ex, bridge = _executor(caps=frozenset({Cap.AGENT_ASK}))
    out = _run(
        lambda: ex.execute("ask_user", {"question": "who?", "choices": ["Ada", "Grace"]}),
        bridge,
        7,
        "Ada",
    )
    assert out["choices_offered"] == ["Ada", "Grace"]


def test_ask_user_timeout_is_not_an_exception():
    ex, bridge = _executor()
    out = asyncio.run(
        ex.execute("ask_user", {"question": "name?", "ttl": 5})
    )
    # ttl is clamped to >=5s, so force the fast path instead of waiting.
    assert "answered" in out or out.get("denied")


def test_ask_user_ttl_expiry_shape():
    bridge = AskBridge(bot=FakeBot(), user=object(), ttl=0.01)
    tools = InteractiveTools(bridge, chat_id=7)
    out = asyncio.run(tools.ask_user("name?"))
    assert out["answered"] is False
    assert out["timed_out"] is True
    assert out["user_response"] is None
    assert bridge.pending == 0


def test_duplicate_choices_are_collapsed():
    ex, bridge = _executor(caps=frozenset({Cap.AGENT_ASK}))
    out = _run(
        lambda: ex.execute(
            "ask_user", {"question": "who?", "choices": ["Ada", "ada", "Grace"]}
        ),
        bridge,
        7,
        "Ada",
    )
    assert out["choices_offered"] == ["Ada", "Grace"]


def test_choices_are_bounded():
    ex, bridge = _executor(caps=frozenset({Cap.AGENT_ASK}))
    many = [f"c{i}" for i in range(MAX_CHOICES + 8)]
    out = _run(
        lambda: ex.execute("ask_user", {"question": "q", "choices": many}),
        bridge,
        7,
        "c0",
    )
    assert len(out["choices_offered"]) == MAX_CHOICES


def test_a_string_choices_value_is_tolerated():
    ex, bridge = _executor(caps=frozenset({Cap.AGENT_ASK}))
    out = _run(
        lambda: ex.execute("ask_user", {"question": "q", "choices": "JustOne"}),
        bridge,
        7,
        "JustOne",
    )
    assert out["choices_offered"] == ["JustOne"]


def test_no_question_leaves_a_pending_session():
    ex, bridge = _executor(caps=frozenset({Cap.AGENT_ASK}))
    _run(lambda: ex.execute("ask_user", {"question": "q"}), bridge, 7, "ok")
    assert bridge.pending == 0


# ── confirm_action ──────────────────────────────────────────────────────────


def test_confirm_yes_is_confirmed():
    ex, bridge = _executor(caps=frozenset({Cap.AGENT_ASK}))
    out = _run(
        lambda: ex.execute("confirm_action", {"what": "delete the db", "yes_label": "Do it"}),
        bridge,
        7,
        "Do it",
    )
    assert out["confirmed"] is True


def test_confirm_no_is_not_confirmed():
    ex, bridge = _executor(caps=frozenset({Cap.AGENT_ASK}))
    out = _run(
        lambda: ex.execute("confirm_action", {"what": "delete", "no_label": "Keep"}),
        bridge,
        7,
        "Keep",
    )
    assert out["confirmed"] is False


def test_confirm_timeout_is_never_confirmed():
    bridge = AskBridge(bot=FakeBot(), user=object(), ttl=0.01)
    tools = InteractiveTools(bridge, chat_id=7)
    out = asyncio.run(tools.confirm_action("delete everything"))
    assert out["confirmed"] is False
    assert out["timed_out"] is True


# ── collect_fields ──────────────────────────────────────────────────────────


def test_collect_asks_each_field_in_order():
    bridge = AskBridge(bot=FakeBot(), user=object())
    tools = InteractiveTools(bridge, chat_id=7)

    async def scenario():
        task = asyncio.ensure_future(
            tools.collect_fields(
                [
                    {"name": "first", "question": "first name?"},
                    {"name": "city", "question": "city?", "choices": ["Delhi", "Lagos"]},
                ]
            )
        )
        seen = []
        # Bounded: each answer lets the form register the next question, which
        # needs a real turn of the loop (the send is itself an await).
        for _ in range(50):
            if task.done():
                break
            session = next(
                (s for s in bridge._pending.values() if not s.future.done()), None
            )
            if session is not None:
                seen.append(session.question)
                session.resolve("Lagos")
                bridge._retire(session)
            await asyncio.sleep(0.01)
        return await asyncio.wait_for(task, timeout=2), seen

    out, seen = asyncio.run(scenario())
    assert seen == ["first name?", "city?"]
    assert out["answers"] == {"first": "Lagos", "city": "Lagos"}
    assert out["complete"] is True


def test_collect_stops_at_the_first_unanswered_field():
    bridge = AskBridge(bot=FakeBot(), user=object(), ttl=0.01)
    tools = InteractiveTools(bridge, chat_id=7)
    out = asyncio.run(
        tools.collect_fields(
            [
                {"name": "a", "question": "a?"},
                {"name": "b", "question": "b?"},
            ]
        )
    )
    assert out["complete"] is False
    assert out["skipped"] == ["a"]


def test_collect_needs_at_least_one_field():
    bridge = AskBridge(bot=FakeBot(), user=object())
    out = asyncio.run(InteractiveTools(bridge, chat_id=7).collect_fields([]))
    assert "error" in out


def test_collect_drops_incomplete_field_specs():
    bridge = AskBridge(bot=FakeBot(), user=object(), ttl=0.01)
    out = asyncio.run(
        InteractiveTools(bridge, chat_id=7).collect_fields(
            [{"name": "", "question": "?"}, {"name": "ok", "question": "?"}]
        )
    )
    # the good one still runs, so only one field was asked
    assert set(out["answers"]) <= {"ok"}


def test_collect_is_bounded():
    bridge = AskBridge(bot=FakeBot(), user=object(), ttl=0.01)
    fields = [
        {"name": f"f{i}", "question": f"q{i}?"} for i in range(MAX_QUESTIONS + 5)
    ]
    out = asyncio.run(InteractiveTools(bridge, chat_id=7).collect_fields(fields))
    assert len(out["answers"]) <= MAX_QUESTIONS


# ── plumbing ────────────────────────────────────────────────────────────────


def test_executor_binds_the_interactive_tools_to_its_own_chat():
    ex, _ = _executor(chat_id=7)
    other = ToolExecutor(chat_id=99, caps=frozenset({Cap.AGENT_ASK}))
    assert other.interactive.chat_id == 99
    # A pre-bound interactive set follows the executor's chat.
    ex.chat_id = 42
    assert ex.interactive.chat_id == 42


def test_interactive_is_built_lazily():
    ex = ToolExecutor(chat_id=7)
    assert ex._interactive is None
    assert ex.interactive is not None


def test_clamp_ttl_bounds_and_defaults():
    assert clamp_ttl(None) == 120.0
    assert clamp_ttl("junk") == 120.0
    assert clamp_ttl(1) == 5.0
    assert clamp_ttl(10_000) == 900.0
    assert clamp_ttl(60) == 60.0
    assert clamp_ttl(float("nan")) == 120.0
    assert clamp_ttl(float("inf")) == 120.0


def test_every_tool_has_a_schema_and_a_cap_entry():
    names = {
        s["function"]["name"] for s in __import__(
            "pyUltroid.agent._tools", fromlist=["TOOL_SCHEMAS"]
        ).TOOL_SCHEMAS
    }
    for tool in ("ask_user", "confirm_action", "collect_fields"):
        assert tool in names
        assert tool in TOOL_REQUIRED_CAPS
        assert hasattr(ToolExecutor, f"_tool_{tool}")