File size: 14,309 Bytes
f7359ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import unittest
import json
import uuid
from unittest.mock import patch
from fastapi.testclient import TestClient
from app import app, compact_tool_definitions

client = TestClient(app)

def generate_agent_tools():
    """Generates a realistic suite of 55 tools matching Pi Coding Agent + OpenViking."""
    base_tools = [
        {
            "type": "function",
            "function": {
                "name": "todo",
                "description": "Manage a task list for tracking multi-step progress. Actions: create, update, list, get, delete, clear.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "action": {"type": "string", "enum": ["create", "update", "list", "get", "delete", "clear"]},
                        "id": {"type": "integer", "description": "Task ID"},
                        "subject": {"type": "string", "description": "Short task title"},
                        "status": {"type": "string", "enum": ["pending", "in_progress", "completed", "deleted"]},
                        "activeForm": {"type": "string", "description": "Present-continuous spinner label"},
                        "tasks": {"type": "array", "description": "Batch create tasks"}
                    },
                    "required": ["action"]
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "bash",
                "description": "Execute bash commands in the current working directory.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "command": {"type": "string", "description": "Shell command to execute"},
                        "timeout": {"type": "number", "description": "Timeout in seconds"}
                    },
                    "required": ["command"]
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "read",
                "description": "Read file contents with optional offset and limit.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "path": {"type": "string", "description": "Path to the file to read"},
                        "offset": {"type": "integer", "description": "Line number to start reading from"},
                        "limit": {"type": "integer", "description": "Maximum number of lines to read"}
                    },
                    "required": ["path"]
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "edit",
                "description": "Edit a single file using exact text replacement.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "path": {"type": "string", "description": "Path to file"},
                        "edits": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "oldText": {"type": "string"},
                                    "newText": {"type": "string"}
                                },
                                "required": ["oldText", "newText"]
                            }
                        }
                    },
                    "required": ["path", "edits"]
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "lens_diagnostics",
                "description": "Query pi-lens diagnostics from LSP probe.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "paths": {"type": "array", "items": {"type": "string"}},
                        "scope": {"type": "string", "enum": ["paths", "workspace"]},
                        "source": {"type": "string", "enum": ["lsp", "session"]}
                    }
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "openviking_search",
                "description": "Retrieval with optional session context from OpenViking long term memory.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "query": {"type": "string", "description": "Semantic query"},
                        "limit": {"type": "integer"}
                    },
                    "required": ["query"]
                }
            }
        }
    ]

    # Fill up to 55 realistic tools to simulate full Pi environment
    for i in range(1, 50):
        base_tools.append({
            "type": "function",
            "function": {
                "name": f"extra_tool_{i}",
                "description": f"Extended utility tool {i} for specific subsystem operations.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "target": {"type": "string"},
                        "options": {"type": "object"}
                    },
                    "required": ["target"]
                }
            }
        })

    return base_tools


class TestFullAgentWorkflow(unittest.TestCase):
    def setUp(self):
        self.tools = generate_agent_tools()

    def test_schema_compaction_on_full_suite(self):
        """Verifies that 55 tools are compacted without error and remain under budget."""
        raw_defs = [t["function"] for t in self.tools]
        compacted = compact_tool_definitions(raw_defs)
        self.assertLessEqual(len(compacted), 50)
        json_str = json.dumps(compacted, ensure_ascii=False, separators=(',', ':'))
        self.assertLess(len(json_str), 30000)

    @patch("app.engine.chat_events")
    def test_multi_turn_agent_loop_streaming(self, mock_chat_events):
        """
        Simulates a complete 5-turn Agent Loop using streaming (stream=True):
        Round 1: Calling tool `todo` with arguments: {...} (Natural format)
        Round 2: Markdown block ````json {"name": "read", ...} ````
        Round 3: Concurrent tools [bash, lens_diagnostics]
        Round 4: Bare json {"name": "edit", ...}
        Round 5: Final text answer (finish_reason='stop')
        """
        responses_pool = [
            # Round 1: Natural calling syntax (user-reported case)
            [
                ("reasoning", "I should create a task first.\n"),
                ("text", "✅ 准备就绪,开始创建任务。\nCalling tool `todo` with arguments: {\"action\": \"create\", \"tasks\": [{\"subject\": \"Check system\"}]}")
            ],
            # Round 2: Standard markdown code block
            [
                ("reasoning", "Now let me inspect app.py.\n"),
                ("text", "Task created. Now reading the file.\n```json\n{\"name\": \"read\", \"arguments\": {\"path\": \"app.py\", \"limit\": 50}}\n```")
            ],
            # Round 3: Multi-tool concurrent calling
            [
                ("reasoning", "Running git check and diagnostics concurrently.\n"),
                ("text", "```json\n[\n  {\"name\": \"bash\", \"arguments\": {\"command\": \"git status\"}},\n  {\"name\": \"lens_diagnostics\", \"arguments\": {\"paths\": [\"app.py\"]}}\n]\n```")
            ],
            # Round 4: Bare JSON object with conversational lead
            [
                ("reasoning", "Applying the final edit.\n"),
                ("text", "I will now edit the file to fix the issue: {\"name\": \"edit\", \"arguments\": {\"path\": \"app.py\", \"edits\": [{\"oldText\": \"A\", \"newText\": \"B\"}]}}")
            ],
            # Round 5: Final completion (no tools)
            [
                ("reasoning", "All actions complete.\n"),
                ("text", "All 4 tasks have been successfully executed! The repository is clean and passing all tests.")
            ]
        ]

        turn = 0
        def get_turn_events(*args, **kwargs):
            nonlocal turn
            events = responses_pool[turn]
            turn += 1
            async def gen():
                for k, v in events:
                    yield (k, v)
            return gen()

        mock_chat_events.side_effect = get_turn_events

        # Initial Agent Message
        messages = [
            {"role": "system", "content": "You are Pi, an autonomous coding agent harness."},
            {"role": "user", "content": "Please inspect app.py, check diagnostics, and apply the fix."}
        ]

        executed_tools = []

        # Run 5 turns of the Agent Loop
        for round_idx in range(5):
            req_body = {
                "model": "mercury-3",
                "messages": messages,
                "tools": self.tools,
                "tool_choice": "auto",
                "stream": True
            }

            resp = client.post("/v1/chat/completions", json=req_body)
            self.assertEqual(resp.status_code, 200)

            # Consume SSE stream
            chunks = []
            for line in resp.iter_lines():
                if line.startswith("data: ") and line != "data: [DONE]":
                    chunks.append(json.loads(line[6:]))

            # Extract tool calls and finish reason from chunks
            tool_calls = []
            finish_reason = None
            assistant_content = []

            for c in chunks:
                choice = c["choices"][0]
                delta = choice.get("delta", {})
                if "content" in delta and delta["content"]:
                    assistant_content.append(delta["content"])
                if "tool_calls" in delta and delta["tool_calls"]:
                    tool_calls.extend(delta["tool_calls"])
                if choice.get("finish_reason"):
                    finish_reason = choice["finish_reason"]

            if finish_reason == "tool_calls":
                self.assertTrue(len(tool_calls) > 0)
                # Append assistant message with tool calls
                formatted_tc = []
                for tc in tool_calls:
                    fname = tc["function"]["name"]
                    fargs = tc["function"]["arguments"]
                    call_id = tc.get("id") or ("call_" + uuid.uuid4().hex[:12])
                    formatted_tc.append({
                        "id": call_id,
                        "type": "function",
                        "function": {"name": fname, "arguments": fargs}
                    })
                    executed_tools.append(fname)

                messages.append({
                    "role": "assistant",
                    "content": "".join(assistant_content),
                    "tool_calls": formatted_tc
                })

                # Client executes each tool and appends role="tool"
                for tc in formatted_tc:
                    mock_tool_result = f'{{"status": "ok", "tool": "{tc["function"]["name"]}", "executed": true}}'
                    messages.append({
                        "role": "tool",
                        "tool_call_id": tc["id"],
                        "name": tc["function"]["name"],
                        "content": mock_tool_result
                    })
            else:
                self.assertEqual(finish_reason, "stop")
                self.assertIn("All 4 tasks have been successfully executed", "".join(assistant_content))
                break

        # Verify all tools across all turns were captured and executed in order
        self.assertEqual(executed_tools, ["todo", "read", "bash", "lens_diagnostics", "edit"])

    @patch("app.engine.chat_events")
    def test_multi_turn_agent_loop_non_streaming(self, mock_chat_events):
        """Verifies the same 5-turn cycle under non-streaming mode (stream=False)."""
        responses_pool = [
            [("text", "Calling tool `bash` with arguments: {\"command\": \"pwd\"}")],
            [("text", "```json\n{\"name\": \"todo\", \"arguments\": {\"action\": \"list\"}}\n```")],
            [("text", "Done! Finished everything.")]
        ]

        turn = 0
        def get_turn_events(*args, **kwargs):
            nonlocal turn
            events = responses_pool[turn]
            turn += 1
            async def gen():
                for k, v in events:
                    yield (k, v)
            return gen()

        mock_chat_events.side_effect = get_turn_events

        messages = [{"role": "user", "content": "Run commands."}]

        # Turn 1: bash
        r1 = client.post("/v1/chat/completions", json={
            "model": "mercury-3",
            "messages": messages,
            "tools": self.tools,
            "stream": False
        })
        d1 = r1.json()["choices"][0]
        self.assertEqual(d1["finish_reason"], "tool_calls")
        self.assertEqual(d1["message"]["tool_calls"][0]["function"]["name"], "bash")

        messages.append(d1["message"])
        messages.append({"role": "tool", "tool_call_id": d1["message"]["tool_calls"][0]["id"], "content": "/home/app"})

        # Turn 2: todo
        r2 = client.post("/v1/chat/completions", json={
            "model": "mercury-3",
            "messages": messages,
            "tools": self.tools,
            "stream": False
        })
        d2 = r2.json()["choices"][0]
        self.assertEqual(d2["finish_reason"], "tool_calls")
        self.assertEqual(d2["message"]["tool_calls"][0]["function"]["name"], "todo")

        messages.append(d2["message"])
        messages.append({"role": "tool", "tool_call_id": d2["message"]["tool_calls"][0]["id"], "content": "[]"})

        # Turn 3: finish
        r3 = client.post("/v1/chat/completions", json={
            "model": "mercury-3",
            "messages": messages,
            "tools": self.tools,
            "stream": False
        })
        d3 = r3.json()["choices"][0]
        self.assertEqual(d3["finish_reason"], "stop")
        self.assertIn("Done! Finished everything.", d3["message"]["content"])


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