File size: 4,224 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
import asyncio
import unittest

from pyUltroid.agent._runtime import AgentRequest, AgentRuntime, resolve_caps


class FakeAgent:
    def __init__(self):
        self.calls = []

    async def respond(self, **kwargs):
        self.calls.append(kwargs)
        return "ok"


class RuntimeTests(unittest.IsolatedAsyncioTestCase):
    def test_non_privileged_caps_exclude_shell(self):
        caps = resolve_caps(surface=".ai", user_id=999999, tools_enabled=True)
        self.assertNotIn("shell_exec", caps)
        self.assertNotIn("fs_write", caps)

    def test_disabled_tools_grant_no_caps(self):
        self.assertEqual(
            resolve_caps(surface=".ai", user_id=1, tools_enabled=False),
            frozenset(),
        )

    async def test_runtime_passes_normalized_request_to_agent(self):
        fake = FakeAgent()
        rt = AgentRuntime(fake)
        response = await rt.runtime_run(
            AgentRequest(
                prompt="hello",
                chat_id=42,
                origin=".ai",
                user_id=1,
                provider="openai",
                model="gpt-test",
                temperature=0.1,
                use_tools=False,
            )
        )
        self.assertEqual(response, "ok")
        call = fake.calls[0]
        self.assertEqual(call["user_message"], "hello")
        self.assertEqual(call["chat_id"], 42)
        self.assertEqual(call["provider_name"], "openai")
        self.assertEqual(call["model"], "gpt-test")
        self.assertEqual(call["temperature"], 0.1)
        self.assertFalse(call["use_tools"])
        self.assertEqual(call["scope"], ".ai")

    async def test_runtime_applies_surface_caps(self):
        fake = FakeAgent()
        rt = AgentRuntime(fake)
        await rt.runtime_run(
            AgentRequest(
                prompt="hello",
                chat_id=43,
                origin=".ai",
                user_id=999999,
                use_tools=True,
            )
        )
        caps = fake.calls[0]["caps"]
        self.assertNotIn("shell_exec", caps)

    async def test_runtime_timeout_cancels_hung_agent_and_returns_timeout(self):
        class HangingAgent:
            def __init__(self):
                self.started = asyncio.Event()
                self.cancelled = False

            async def respond(self, **kwargs):
                self.started.set()
                try:
                    await asyncio.Event().wait()
                except asyncio.CancelledError:
                    self.cancelled = True
                    raise

        fake = HangingAgent()
        rt = AgentRuntime(fake)
        response = await rt.runtime_run(
            AgentRequest(
                prompt="hang safely",
                chat_id=44,
                origin=".agent",
                use_tools=False,
                timeout_seconds=0.01,
            )
        )
        self.assertTrue(fake.cancelled)
        self.assertFalse(response.ok)
        self.assertEqual(response.provider, "runtime")
        self.assertEqual(response.model, "timeout")
        self.assertIn("time limit", response.content)

    async def test_external_cancellation_is_not_misreported_as_timeout(self):
        class HangingAgent:
            def __init__(self):
                self.started = asyncio.Event()
                self.cancelled = False

            async def respond(self, **kwargs):
                self.started.set()
                try:
                    await asyncio.Event().wait()
                except asyncio.CancelledError:
                    self.cancelled = True
                    raise

        fake = HangingAgent()
        rt = AgentRuntime(fake)
        task = asyncio.create_task(
            rt.runtime_run(
                AgentRequest(
                    prompt="cancel safely",
                    chat_id=45,
                    origin=".agent",
                    use_tools=False,
                    timeout_seconds=30,
                )
            )
        )
        await fake.started.wait()
        task.cancel()
        with self.assertRaises(asyncio.CancelledError):
            await task
        self.assertTrue(fake.cancelled)


if __name__ == "__main__":
    unittest.main()