-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_session_fs.py
More file actions
349 lines (282 loc) · 12.4 KB
/
test_session_fs.py
File metadata and controls
349 lines (282 loc) · 12.4 KB
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
"""E2E SessionFs tests mirroring nodejs/test/e2e/session_fs.test.ts."""
from __future__ import annotations
import asyncio
import datetime as dt
import os
import re
from pathlib import Path
import pytest
import pytest_asyncio
from copilot import CopilotClient, SessionFsConfig, define_tool
from copilot.client import ExternalServerConfig, SubprocessConfig
from copilot.generated.rpc import (
SessionFSExistsResult,
SessionFSReaddirResult,
SessionFSReaddirWithTypesResult,
SessionFSReadFileResult,
SessionFSStatResult,
)
from copilot.generated.session_events import SessionEvent
from copilot.session import PermissionHandler
from .testharness import E2ETestContext
pytestmark = pytest.mark.asyncio(loop_scope="module")
SESSION_FS_CONFIG: SessionFsConfig = {
"initial_cwd": "/",
"session_state_path": "/session-state",
"conventions": "posix",
}
@pytest_asyncio.fixture(scope="module", loop_scope="module")
async def session_fs_client(ctx: E2ETestContext):
github_token = (
"fake-token-for-e2e-tests" if os.environ.get("GITHUB_ACTIONS") == "true" else None
)
client = CopilotClient(
SubprocessConfig(
cli_path=ctx.cli_path,
cwd=ctx.work_dir,
env=ctx.get_env(),
github_token=github_token,
session_fs=SESSION_FS_CONFIG,
)
)
yield client
try:
await client.stop()
except Exception:
await client.force_stop()
class TestSessionFs:
async def test_should_route_file_operations_through_the_session_fs_provider(
self, ctx: E2ETestContext, session_fs_client: CopilotClient
):
provider_root = Path(ctx.work_dir) / "provider"
session = await session_fs_client.create_session(
on_permission_request=PermissionHandler.approve_all,
create_session_fs_handler=create_test_session_fs_handler(provider_root),
)
msg = await session.send_and_wait("What is 100 + 200?")
assert msg is not None
assert msg.data.content is not None
assert "300" in msg.data.content
await session.disconnect()
events_path = provider_path(
provider_root, session.session_id, "/session-state/events.jsonl"
)
assert "300" in events_path.read_text(encoding="utf-8")
async def test_should_load_session_data_from_fs_provider_on_resume(
self, ctx: E2ETestContext, session_fs_client: CopilotClient
):
provider_root = Path(ctx.work_dir) / "provider"
create_session_fs_handler = create_test_session_fs_handler(provider_root)
session1 = await session_fs_client.create_session(
on_permission_request=PermissionHandler.approve_all,
create_session_fs_handler=create_session_fs_handler,
)
session_id = session1.session_id
msg = await session1.send_and_wait("What is 50 + 50?")
assert msg is not None
assert msg.data.content is not None
assert "100" in msg.data.content
await session1.disconnect()
assert provider_path(provider_root, session_id, "/session-state/events.jsonl").exists()
session2 = await session_fs_client.resume_session(
session_id,
on_permission_request=PermissionHandler.approve_all,
create_session_fs_handler=create_session_fs_handler,
)
msg2 = await session2.send_and_wait("What is that times 3?")
assert msg2 is not None
assert msg2.data.content is not None
assert "300" in msg2.data.content
await session2.disconnect()
async def test_should_reject_setprovider_when_sessions_already_exist(self, ctx: E2ETestContext):
github_token = (
"fake-token-for-e2e-tests" if os.environ.get("GITHUB_ACTIONS") == "true" else None
)
client1 = CopilotClient(
SubprocessConfig(
cli_path=ctx.cli_path,
cwd=ctx.work_dir,
env=ctx.get_env(),
use_stdio=False,
github_token=github_token,
)
)
session = None
client2 = None
try:
session = await client1.create_session(
on_permission_request=PermissionHandler.approve_all,
)
actual_port = client1.actual_port
assert actual_port is not None
client2 = CopilotClient(
ExternalServerConfig(
url=f"localhost:{actual_port}",
session_fs=SESSION_FS_CONFIG,
)
)
with pytest.raises(Exception):
await client2.start()
finally:
if session is not None:
await session.disconnect()
if client2 is not None:
await client2.force_stop()
await client1.force_stop()
async def test_should_map_large_output_handling_into_sessionfs(
self, ctx: E2ETestContext, session_fs_client: CopilotClient
):
provider_root = Path(ctx.work_dir) / "provider"
supplied_file_content = "x" * 100_000
@define_tool("get_big_string", description="Returns a large string")
def get_big_string() -> str:
return supplied_file_content
session = await session_fs_client.create_session(
on_permission_request=PermissionHandler.approve_all,
create_session_fs_handler=create_test_session_fs_handler(provider_root),
tools=[get_big_string],
)
await session.send_and_wait(
"Call the get_big_string tool and reply with the word DONE only."
)
messages = await session.get_messages()
tool_result = find_tool_call_result(messages, "get_big_string")
assert tool_result is not None
assert "/session-state/temp/" in tool_result
match = re.search(r"(/session-state/temp/[^\s]+)", tool_result)
assert match is not None
temp_file = provider_path(provider_root, session.session_id, match.group(1))
assert temp_file.read_text(encoding="utf-8") == supplied_file_content
async def test_should_succeed_with_compaction_while_using_sessionfs(
self, ctx: E2ETestContext, session_fs_client: CopilotClient
):
provider_root = Path(ctx.work_dir) / "provider"
session = await session_fs_client.create_session(
on_permission_request=PermissionHandler.approve_all,
create_session_fs_handler=create_test_session_fs_handler(provider_root),
)
compaction_event = asyncio.Event()
compaction_success: bool | None = None
def on_event(event: SessionEvent):
nonlocal compaction_success
if event.type.value == "session.compaction_complete":
compaction_success = event.data.success
compaction_event.set()
session.on(on_event)
await session.send_and_wait("What is 2+2?")
events_path = provider_path(
provider_root, session.session_id, "/session-state/events.jsonl"
)
await wait_for_path(events_path)
assert "checkpointNumber" not in events_path.read_text(encoding="utf-8")
result = await session.rpc.history.compact()
await asyncio.wait_for(compaction_event.wait(), timeout=5.0)
assert result.success is True
assert compaction_success is True
await wait_for_content(events_path, "checkpointNumber")
class _SessionFsHandler:
def __init__(self, provider_root: Path, session_id: str):
self._provider_root = provider_root
self._session_id = session_id
async def read_file(self, params) -> SessionFSReadFileResult:
content = provider_path(self._provider_root, self._session_id, params.path).read_text(
encoding="utf-8"
)
return SessionFSReadFileResult.from_dict({"content": content})
async def write_file(self, params) -> None:
path = provider_path(self._provider_root, self._session_id, params.path)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(params.content, encoding="utf-8")
async def append_file(self, params) -> None:
path = provider_path(self._provider_root, self._session_id, params.path)
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("a", encoding="utf-8") as handle:
handle.write(params.content)
async def exists(self, params) -> SessionFSExistsResult:
path = provider_path(self._provider_root, self._session_id, params.path)
return SessionFSExistsResult.from_dict({"exists": path.exists()})
async def stat(self, params) -> SessionFSStatResult:
path = provider_path(self._provider_root, self._session_id, params.path)
info = path.stat()
timestamp = dt.datetime.fromtimestamp(info.st_mtime, tz=dt.UTC).isoformat()
if timestamp.endswith("+00:00"):
timestamp = f"{timestamp[:-6]}Z"
return SessionFSStatResult.from_dict(
{
"isFile": not path.is_dir(),
"isDirectory": path.is_dir(),
"size": info.st_size,
"mtime": timestamp,
"birthtime": timestamp,
}
)
async def mkdir(self, params) -> None:
path = provider_path(self._provider_root, self._session_id, params.path)
if params.recursive:
path.mkdir(parents=True, exist_ok=True)
else:
path.mkdir()
async def readdir(self, params) -> SessionFSReaddirResult:
entries = sorted(
entry.name
for entry in provider_path(self._provider_root, self._session_id, params.path).iterdir()
)
return SessionFSReaddirResult.from_dict({"entries": entries})
async def readdir_with_types(self, params) -> SessionFSReaddirWithTypesResult:
entries = []
for entry in sorted(
provider_path(self._provider_root, self._session_id, params.path).iterdir(),
key=lambda item: item.name,
):
entries.append(
{
"name": entry.name,
"type": "directory" if entry.is_dir() else "file",
}
)
return SessionFSReaddirWithTypesResult.from_dict({"entries": entries})
async def rm(self, params) -> None:
provider_path(self._provider_root, self._session_id, params.path).unlink()
async def rename(self, params) -> None:
src = provider_path(self._provider_root, self._session_id, params.src)
dest = provider_path(self._provider_root, self._session_id, params.dest)
dest.parent.mkdir(parents=True, exist_ok=True)
src.rename(dest)
def create_test_session_fs_handler(provider_root: Path):
def create_handler(session):
return _SessionFsHandler(provider_root, session.session_id)
return create_handler
def provider_path(provider_root: Path, session_id: str, path: str) -> Path:
return provider_root / session_id / path.lstrip("/")
def find_tool_call_result(messages: list[SessionEvent], tool_name: str) -> str | None:
for message in messages:
if (
message.type.value == "tool.execution_complete"
and message.data.tool_call_id is not None
):
if find_tool_name(messages, message.data.tool_call_id) == tool_name:
return message.data.result.content if message.data.result is not None else None
return None
def find_tool_name(messages: list[SessionEvent], tool_call_id: str) -> str | None:
for message in messages:
if (
message.type.value == "tool.execution_start"
and message.data.tool_call_id == tool_call_id
):
return message.data.tool_name
return None
async def wait_for_path(path: Path, timeout: float = 5.0) -> None:
async def predicate():
return path.exists()
await wait_for_predicate(predicate, timeout=timeout)
async def wait_for_content(path: Path, expected: str, timeout: float = 5.0) -> None:
async def predicate():
return path.exists() and expected in path.read_text(encoding="utf-8")
await wait_for_predicate(predicate, timeout=timeout)
async def wait_for_predicate(predicate, timeout: float = 5.0) -> None:
deadline = asyncio.get_running_loop().time() + timeout
while asyncio.get_running_loop().time() < deadline:
if await predicate():
return
await asyncio.sleep(0.1)
raise TimeoutError("timed out waiting for condition")