-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_session_fs_e2e.py
More file actions
672 lines (554 loc) · 24.2 KB
/
test_session_fs_e2e.py
File metadata and controls
672 lines (554 loc) · 24.2 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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
"""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
import tempfile
from pathlib import Path
import pytest
import pytest_asyncio
from copilot import (
CopilotClient,
RuntimeConnection,
SessionFsConfig,
define_tool,
)
from copilot.generated.rpc import (
SessionFSReaddirWithTypesEntry,
SessionFSReaddirWithTypesEntryType,
)
from copilot.generated.session_events import SessionCompactionCompleteData, SessionEvent
from copilot.session import PermissionHandler
from copilot.session_fs_provider import SessionFsFileInfo, SessionFsProvider
from .testharness import DEFAULT_GITHUB_TOKEN, E2ETestContext
pytestmark = pytest.mark.asyncio(loop_scope="module")
SESSION_STATE_PATH = (
"/session-state"
if os.name == "nt"
else (Path(tempfile.mkdtemp(prefix="copilot-sessionfs-state-")) / "session-state")
.resolve()
.as_posix()
)
SESSION_FS_CONFIG: SessionFsConfig = {
"initial_working_directory": "/",
"session_state_path": SESSION_STATE_PATH,
"conventions": "posix",
}
@pytest_asyncio.fixture(scope="module", loop_scope="module")
async def session_fs_client(ctx: E2ETestContext):
client = CopilotClient(
connection=RuntimeConnection.for_stdio(path=ctx.cli_path),
working_directory=ctx.work_dir,
env=ctx.get_env(),
github_token=DEFAULT_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, f"{SESSION_STATE_PATH}/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, f"{SESSION_STATE_PATH}/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):
client1 = CopilotClient(
connection=RuntimeConnection.for_tcp(path=ctx.cli_path),
working_directory=ctx.work_dir,
env=ctx.get_env(),
github_token=DEFAULT_GITHUB_TOKEN,
)
session = None
client2 = None
try:
session = await client1.create_session(
on_permission_request=PermissionHandler.approve_all,
)
actual_port = client1.runtime_port
assert actual_port is not None
client2 = CopilotClient(
connection=RuntimeConnection.for_uri(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_events()
tool_result = find_tool_call_result(messages, "get_big_string")
assert tool_result is not None
assert f"{SESSION_STATE_PATH}/temp/" in tool_result
match = re.search(rf"({re.escape(SESSION_STATE_PATH)}/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
match event.data:
case SessionCompactionCompleteData() as data:
compaction_success = 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, f"{SESSION_STATE_PATH}/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")
async def test_should_write_workspace_metadata_via_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),
)
msg = await session.send_and_wait("What is 7 * 8?")
assert msg is not None
assert msg.data.content is not None
assert "56" in msg.data.content
# WorkspaceManager should have created workspace.yaml via sessionFs
workspace_yaml_path = provider_path(
provider_root, session.session_id, f"{SESSION_STATE_PATH}/workspace.yaml"
)
await wait_for_path(workspace_yaml_path)
yaml_content = workspace_yaml_path.read_text(encoding="utf-8")
assert "id:" in yaml_content
# Checkpoint index should also exist
index_path = provider_path(
provider_root, session.session_id, f"{SESSION_STATE_PATH}/checkpoints/index.md"
)
await wait_for_path(index_path)
await session.disconnect()
async def test_should_persist_plan_md_via_sessionfs(
self, ctx: E2ETestContext, session_fs_client: CopilotClient
):
from copilot.generated.rpc import PlanUpdateRequest
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),
)
# Write a plan via the session RPC
await session.send_and_wait("What is 2 + 3?")
await session.rpc.plan.update(PlanUpdateRequest(content="# Test Plan\n\nThis is a test."))
plan_path = provider_path(
provider_root, session.session_id, f"{SESSION_STATE_PATH}/plan.md"
)
await wait_for_path(plan_path)
content = plan_path.read_text(encoding="utf-8")
assert "# Test Plan" in content
await session.disconnect()
async def test_should_map_all_sessionfs_handler_operations(self, ctx: E2ETestContext):
from copilot.generated.rpc import (
SessionFSAppendFileRequest,
SessionFSExistsRequest,
SessionFSMkdirRequest,
SessionFSReaddirRequest,
SessionFSReaddirWithTypesRequest,
SessionFSReadFileRequest,
SessionFSRenameRequest,
SessionFSRmRequest,
SessionFSSqliteExistsRequest,
SessionFSSqliteQueryRequest,
SessionFSSqliteQueryType,
SessionFSStatRequest,
SessionFSWriteFileRequest,
)
from copilot.session_fs_provider import create_session_fs_adapter
provider_root = Path(ctx.work_dir) / "handler-provider"
provider_root.mkdir(parents=True, exist_ok=True)
session_id = "handler-session"
provider = _TestSessionFsProvider(provider_root, session_id)
handler = create_session_fs_adapter(provider)
try:
mkdir_error = await handler.mkdir(
SessionFSMkdirRequest(
session_id=session_id, path="/workspace/nested", recursive=True
)
)
assert mkdir_error is None
write_error = await handler.write_file(
SessionFSWriteFileRequest(
session_id=session_id,
path="/workspace/nested/file.txt",
content="hello",
)
)
assert write_error is None
append_error = await handler.append_file(
SessionFSAppendFileRequest(
session_id=session_id,
path="/workspace/nested/file.txt",
content=" world",
)
)
assert append_error is None
exists = await handler.exists(
SessionFSExistsRequest(session_id=session_id, path="/workspace/nested/file.txt")
)
assert exists.exists is True
stat = await handler.stat(
SessionFSStatRequest(session_id=session_id, path="/workspace/nested/file.txt")
)
assert stat.is_file is True
assert stat.is_directory is False
assert stat.size == len("hello world")
assert stat.error is None
content = await handler.read_file(
SessionFSReadFileRequest(session_id=session_id, path="/workspace/nested/file.txt")
)
assert content.content == "hello world"
assert content.error is None
entries = await handler.readdir(
SessionFSReaddirRequest(session_id=session_id, path="/workspace/nested")
)
assert "file.txt" in entries.entries
assert entries.error is None
typed_entries = await handler.readdir_with_types(
SessionFSReaddirWithTypesRequest(session_id=session_id, path="/workspace/nested")
)
assert any(
e.name == "file.txt" and e.type == SessionFSReaddirWithTypesEntryType.FILE
for e in typed_entries.entries
)
assert typed_entries.error is None
rename_error = await handler.rename(
SessionFSRenameRequest(
session_id=session_id,
src="/workspace/nested/file.txt",
dest="/workspace/nested/renamed.txt",
)
)
assert rename_error is None
old_path = await handler.exists(
SessionFSExistsRequest(session_id=session_id, path="/workspace/nested/file.txt")
)
assert old_path.exists is False
renamed_content = await handler.read_file(
SessionFSReadFileRequest(
session_id=session_id, path="/workspace/nested/renamed.txt"
)
)
assert renamed_content.content == "hello world"
rm_error = await handler.rm(
SessionFSRmRequest(session_id=session_id, path="/workspace/nested/renamed.txt")
)
assert rm_error is None
removed = await handler.exists(
SessionFSExistsRequest(session_id=session_id, path="/workspace/nested/renamed.txt")
)
assert removed.exists is False
missing = await handler.stat(
SessionFSStatRequest(session_id=session_id, path="/workspace/nested/missing.txt")
)
assert missing.error is not None
from copilot.generated.rpc import SessionFSErrorCode
assert missing.error.code == SessionFSErrorCode.ENOENT
# SQLite methods are not on the non-sqlite provider, so the adapter
# should return unsupported/not-found results.
sqlite_query = await handler.sqlite_query(
SessionFSSqliteQueryRequest(
session_id=session_id,
query="select 1",
query_type=SessionFSSqliteQueryType.QUERY,
)
)
assert sqlite_query.error is not None
assert sqlite_query.error.code == SessionFSErrorCode.UNKNOWN
sqlite_exists = await handler.sqlite_exists(
SessionFSSqliteExistsRequest(session_id=session_id)
)
assert sqlite_exists.exists is False
finally:
try:
import shutil
shutil.rmtree(provider_root, ignore_errors=True)
except Exception:
pass
async def test_sessionfsprovider_converts_exceptions_to_rpc_errors(self):
from copilot.generated.rpc import (
SessionFSAppendFileRequest,
SessionFSErrorCode,
SessionFSExistsRequest,
SessionFSMkdirRequest,
SessionFSReaddirRequest,
SessionFSReaddirWithTypesRequest,
SessionFSReadFileRequest,
SessionFSRenameRequest,
SessionFSRmRequest,
SessionFSSqliteExistsRequest,
SessionFSSqliteQueryRequest,
SessionFSSqliteQueryType,
SessionFSStatRequest,
SessionFSWriteFileRequest,
)
from copilot.session_fs_provider import create_session_fs_adapter
class _ThrowingProvider(SessionFsProvider):
def __init__(self, exc: Exception) -> None:
self._exc = exc
async def read_file(self, path: str) -> str:
raise self._exc
async def write_file(self, path, content, mode=None):
raise self._exc
async def append_file(self, path, content, mode=None):
raise self._exc
async def exists(self, path):
raise self._exc
async def stat(self, path):
raise self._exc
async def mkdir(self, path, recursive, mode=None):
raise self._exc
async def readdir(self, path):
raise self._exc
async def readdir_with_types(self, path):
raise self._exc
async def rm(self, path, recursive, force):
raise self._exc
async def rename(self, src, dest):
raise self._exc
def assert_fs_error(error) -> None:
assert error is not None
assert error.code == SessionFSErrorCode.ENOENT
assert "missing" in error.message.lower()
sid = "throwing-session"
handler = create_session_fs_adapter(_ThrowingProvider(FileNotFoundError("missing")))
assert_fs_error(
(
await handler.read_file(
SessionFSReadFileRequest(session_id=sid, path="missing.txt")
)
).error
)
assert_fs_error(
await handler.write_file(
SessionFSWriteFileRequest(session_id=sid, path="missing.txt", content="content")
)
)
assert_fs_error(
await handler.append_file(
SessionFSAppendFileRequest(session_id=sid, path="missing.txt", content="content")
)
)
# exists swallows exceptions and reports False
exists_result = await handler.exists(
SessionFSExistsRequest(session_id=sid, path="missing.txt")
)
assert exists_result.exists is False
assert_fs_error(
(await handler.stat(SessionFSStatRequest(session_id=sid, path="missing.txt"))).error
)
assert_fs_error(
await handler.mkdir(SessionFSMkdirRequest(session_id=sid, path="missing-dir"))
)
assert_fs_error(
(
await handler.readdir(SessionFSReaddirRequest(session_id=sid, path="missing-dir"))
).error
)
assert_fs_error(
(
await handler.readdir_with_types(
SessionFSReaddirWithTypesRequest(session_id=sid, path="missing-dir")
)
).error
)
assert_fs_error(await handler.rm(SessionFSRmRequest(session_id=sid, path="missing.txt")))
assert_fs_error(
await handler.rename(
SessionFSRenameRequest(session_id=sid, src="missing.txt", dest="dest.txt")
)
)
# _ThrowingProvider does not implement SessionFsSqliteProvider, so the
# adapter returns "not supported" results rather than propagating throws.
sqlite_query = await handler.sqlite_query(
SessionFSSqliteQueryRequest(
session_id=sid, query="select 1", query_type=SessionFSSqliteQueryType.QUERY
)
)
assert sqlite_query.error is not None
assert sqlite_query.error.code == SessionFSErrorCode.UNKNOWN
assert sqlite_query.columns == []
assert sqlite_query.rows == []
assert sqlite_query.rows_affected == 0
sqlite_exists = await handler.sqlite_exists(SessionFSSqliteExistsRequest(session_id=sid))
assert sqlite_exists.exists is False
unknown_handler = create_session_fs_adapter(_ThrowingProvider(RuntimeError("bad path")))
unknown_error = await unknown_handler.write_file(
SessionFSWriteFileRequest(session_id=sid, path="bad.txt", content="content")
)
assert unknown_error is not None
assert unknown_error.code == SessionFSErrorCode.UNKNOWN
class _TestSessionFsProvider(SessionFsProvider):
def __init__(self, provider_root: Path, session_id: str):
self._provider_root = provider_root
self._session_id = session_id
def _path(self, path: str) -> Path:
return provider_path(self._provider_root, self._session_id, path)
async def read_file(self, path: str) -> str:
return self._path(path).read_text(encoding="utf-8")
async def write_file(self, path: str, content: str, mode: int | None = None) -> None:
p = self._path(path)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(content, encoding="utf-8")
async def append_file(self, path: str, content: str, mode: int | None = None) -> None:
p = self._path(path)
p.parent.mkdir(parents=True, exist_ok=True)
with p.open("a", encoding="utf-8") as handle:
handle.write(content)
async def exists(self, path: str) -> bool:
return self._path(path).exists()
async def stat(self, path: str) -> SessionFsFileInfo:
p = self._path(path)
info = p.stat()
timestamp = dt.datetime.fromtimestamp(info.st_mtime, tz=dt.UTC)
return SessionFsFileInfo(
is_file=not p.is_dir(),
is_directory=p.is_dir(),
size=info.st_size,
mtime=timestamp,
birthtime=timestamp,
)
async def mkdir(self, path: str, recursive: bool, mode: int | None = None) -> None:
p = self._path(path)
if recursive:
p.mkdir(parents=True, exist_ok=True)
else:
p.mkdir()
async def readdir(self, path: str) -> list[str]:
return sorted(entry.name for entry in self._path(path).iterdir())
async def readdir_with_types(self, path: str) -> list[SessionFSReaddirWithTypesEntry]:
entries = []
for entry in sorted(self._path(path).iterdir(), key=lambda item: item.name):
entries.append(
SessionFSReaddirWithTypesEntry(
name=entry.name,
type=SessionFSReaddirWithTypesEntryType.DIRECTORY
if entry.is_dir()
else SessionFSReaddirWithTypesEntryType.FILE,
)
)
return entries
async def rm(self, path: str, recursive: bool, force: bool) -> None:
self._path(path).unlink()
async def rename(self, src: str, dest: str) -> None:
d = self._path(dest)
d.parent.mkdir(parents=True, exist_ok=True)
self._path(src).rename(d)
def create_test_session_fs_handler(provider_root: Path):
def create_handler(session):
return _TestSessionFsProvider(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")