Skip to content

Commit 3df6c09

Browse files
ampagentIsaac Miller
andcommitted
Reject colliding PythonInterpreter mounts
Amp-Thread-ID: https://ampcode.com/threads/T-019fcce1-bc8b-753e-886f-9b77c0ccb376 Co-authored-by: Isaac Miller <isaac@cmpnd.ai>
1 parent 575ed59 commit 3df6c09

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎dspy/primitives/python_interpreter.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,13 @@ def __init__(
267267

268268
self.enable_read_paths = enable_read_paths or []
269269
self.enable_write_paths = enable_write_paths or []
270+
mounts = {
271+
_canonicalize_path(path): os.path.basename(os.fspath(path))
272+
for path in [*self.enable_read_paths, *self.enable_write_paths]
273+
if path
274+
}
275+
if len(mounts) != len(set(mounts.values())):
276+
raise CodeInterpreterError("Mounted files must have unique basenames inside the sandbox.")
270277
self.enable_env_vars = enable_env_vars or []
271278
self.enable_network_access = enable_network_access or []
272279
self.sync_files = sync_files

‎tests/primitives/test_python_interpreter.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,22 @@ def test_custom_deno_command_is_unchanged():
413413
assert interpreter.deno_command is not command
414414

415415

416+
def test_rejects_mounts_with_the_same_guest_basename(tmp_path):
417+
first = tmp_path / "first" / "shared.txt"
418+
second = tmp_path / "second" / "shared.txt"
419+
first.parent.mkdir()
420+
second.parent.mkdir()
421+
422+
with pytest.raises(CodeInterpreterError, match="unique basenames"):
423+
PythonInterpreter(deno_command=["deno"], enable_read_paths=[first], enable_write_paths=[second])
424+
425+
426+
def test_allows_same_canonical_file_as_read_and_write_mount(tmp_path):
427+
path = tmp_path / "shared.txt"
428+
429+
PythonInterpreter(deno_command=["deno"], enable_read_paths=[path], enable_write_paths=[path])
430+
431+
416432
def test_custom_deno_command_preserves_environment(monkeypatch):
417433
monkeypatch.setenv("DENO_NO_PACKAGE_JSON", "0")
418434
captured = {}

0 commit comments

Comments
 (0)