Skip to content

Commit 72ede53

Browse files
committed
fix(sandbox): link a completed payload into place for create_file
Three problems with the previous commit. SandboxSession did not forward write_new_file, so a session built by SandboxClient fell back to the shared implementation and never reached the UnixLocal os.open override. Forward it. The shared implementation created an empty file and then wrote the payload in a separate step, so a concurrent writer could be overwritten and a failed upload left an empty file holding the name. Write the payload under a staging name first, then claim the target with ln, which fails when the name is taken. The content is complete before the name exists, and a failed create leaves only the staging entry, which is removed. The script used ':' for the noclobber redirection. ':' is a POSIX special builtin, so on dash a redirection failure ended the shell before the exit mapping ran and a collision surfaced as a generic write error instead of FileExistsError. ln is a regular command, and a test now runs the script through sh, dash and bash so this cannot regress silently. Also create local files with 0o666 so the process umask decides the final mode, matching Path.open("wb") on the ordinary write path.
1 parent 6ecc682 commit 72ede53

4 files changed

Lines changed: 135 additions & 32 deletions

File tree

‎src/agents/sandbox/sandboxes/unix_local.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,9 @@ async def write_new_file(
10411041
flags |= os.O_NOFOLLOW
10421042
try:
10431043
parent_path.mkdir(parents=True, exist_ok=True)
1044-
descriptor = os.open(workspace_path, flags, 0o644)
1044+
# 0o666 lets the process umask decide the final mode, matching what
1045+
# Path.open("wb") does on the ordinary write path.
1046+
descriptor = os.open(workspace_path, flags, 0o666)
10451047
except FileExistsError:
10461048
raise
10471049
except OSError as e:

‎src/agents/sandbox/session/base_sandbox_session.py‎

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import asyncio
33
import io
44
import shlex
5+
import uuid
56
from collections.abc import Awaitable, Callable, Mapping, Sequence
7+
from contextlib import suppress
68
from pathlib import Path, PurePath
79
from typing import Literal, NoReturn, TypeVar
810

@@ -150,12 +152,20 @@
150152
done
151153
""".strip()
152154
_EXCLUSIVE_CREATE_EXISTS_CODE = 13
153-
# ``set -C`` makes the redirection use O_EXCL, so it fails when the target name already
154-
# exists, including a dangling symlink, and the existing content is left untouched. The
155-
# distinct exit codes keep "already exists" separable from any other failure without
156-
# parsing shell-specific stderr text.
155+
# ``ln`` is the only step that claims the target name, and it fails when that name is
156+
# already taken, including by a dangling symlink. Linking a fully written staging file
157+
# means the content is complete before the name exists, so a failed or cancelled upload
158+
# cannot leave an empty file behind that would block a retry. The trailing test only
159+
# classifies a failure, so "already exists" stays separable from any other error without
160+
# parsing shell-specific stderr text. ``ln`` is a regular command, unlike ``:``, so a
161+
# failure still reaches the explicit exit mapping on shells where ``:`` is special.
157162
_EXCLUSIVE_CREATE_SCRIPT = (
158-
'target="$1"\nmkdir -p "$(dirname "$target")" || exit 12\nset -C\n: > "$target" || exit 13\n'
163+
'target="$1"\n'
164+
'source="$2"\n'
165+
'mkdir -p "$(dirname "$target")" || exit 12\n'
166+
'ln "$source" "$target" 2>/dev/null && exit 0\n'
167+
'if [ -e "$target" ] || [ -L "$target" ]; then exit 13; fi\n'
168+
"exit 14\n"
159169
)
160170

161171
_WRITE_ACCESS_CHECK_SCRIPT = (
@@ -963,8 +973,9 @@ async def write_new_file(
963973
) -> None:
964974
"""Write a file that must not already exist.
965975
966-
The target name is claimed atomically before the payload is written, so a
967-
concurrent creator either loses the race or keeps its content.
976+
The target name is claimed in a single atomic step once the payload is complete,
977+
so a concurrent creator either loses the race or keeps its own content, and a
978+
failed write does not leave a partial file holding the name.
968979
969980
:param path: Absolute path in the container or path relative to the
970981
workspace root.
@@ -980,27 +991,37 @@ async def write_new_file(
980991
parent_path = await self._validate_path_access(requested.parent, for_write=True)
981992
workspace_path = parent_path / requested.name
982993
path_arg = sandbox_path_str(workspace_path)
983-
result = await self.exec(
984-
"sh",
985-
"-lc",
986-
_EXCLUSIVE_CREATE_SCRIPT,
987-
"sh",
988-
path_arg,
989-
shell=False,
990-
user=user,
991-
)
992-
if result.exit_code == _EXCLUSIVE_CREATE_EXISTS_CODE:
993-
raise FileExistsError(path_arg)
994-
if not result.ok():
995-
raise WorkspaceArchiveWriteError(
996-
path=workspace_path,
997-
context={
998-
"command": ["sh", "-lc", "<exclusive_create>", path_arg],
999-
"stdout": result.stdout.decode("utf-8", errors="replace"),
1000-
"stderr": result.stderr.decode("utf-8", errors="replace"),
1001-
},
994+
staging_path = parent_path / f".{requested.name}.create-{uuid.uuid4().hex}"
995+
staging_arg = sandbox_path_str(staging_path)
996+
997+
await self.write(staging_path, data, user=user)
998+
try:
999+
result = await self.exec(
1000+
"sh",
1001+
"-lc",
1002+
_EXCLUSIVE_CREATE_SCRIPT,
1003+
"sh",
1004+
path_arg,
1005+
staging_arg,
1006+
shell=False,
1007+
user=user,
10021008
)
1003-
await self.write(workspace_path, data, user=user)
1009+
if result.exit_code == _EXCLUSIVE_CREATE_EXISTS_CODE:
1010+
raise FileExistsError(path_arg)
1011+
if not result.ok():
1012+
raise WorkspaceArchiveWriteError(
1013+
path=workspace_path,
1014+
context={
1015+
"command": ["sh", "-lc", "<exclusive_create>", path_arg, staging_arg],
1016+
"stdout": result.stdout.decode("utf-8", errors="replace"),
1017+
"stderr": result.stderr.decode("utf-8", errors="replace"),
1018+
},
1019+
)
1020+
finally:
1021+
# The staging entry is an implementation detail, and removing it must not
1022+
# replace the outcome of the create.
1023+
with suppress(Exception):
1024+
await self.rm(staging_path, user=user)
10041025

10051026
async def _check_read_with_exec(
10061027
self, path: Path | str, *, user: str | User | None = None

‎src/agents/sandbox/session/sandbox_session.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,19 @@ async def write(
677677
) -> None:
678678
await self._inner.write(path, data, user=user)
679679

680+
@instrumented_op("write", data=_write_start_data)
681+
async def write_new_file(
682+
self,
683+
path: Path,
684+
data: io.IOBase,
685+
*,
686+
user: str | User | None = None,
687+
) -> None:
688+
# Forwarded so a backend with a native exclusive-create primitive is actually
689+
# used. Without this the wrapper would fall back to the shared implementation and
690+
# bypass the inner session's override.
691+
await self._inner.write_new_file(path, data, user=user)
692+
680693
@instrumented_op(
681694
"running",
682695
finish_data=_running_finish_data,

‎tests/sandbox/test_unix_local.py‎

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import asyncio
44
import io
5+
import shutil
56
import signal
7+
import subprocess
68
import tarfile
79
import threading
810
import time
@@ -22,6 +24,10 @@
2224
UnixLocalSandboxSessionState,
2325
_UnixPtyProcessEntry,
2426
)
27+
from agents.sandbox.session.base_sandbox_session import (
28+
_EXCLUSIVE_CREATE_EXISTS_CODE,
29+
_EXCLUSIVE_CREATE_SCRIPT,
30+
)
2531
from agents.sandbox.snapshot import NoopSnapshot
2632
from agents.sandbox.types import ExecResult, User
2733

@@ -670,6 +676,7 @@ def __init__(self, root: Path, exit_code: int) -> None:
670676
self._exit_code = exit_code
671677
self.exec_commands: list[tuple[str, ...]] = []
672678
self.writes: list[Path] = []
679+
self.removed: list[Path] = []
673680

674681
async def _exec_internal(
675682
self,
@@ -684,6 +691,16 @@ async def write(self, path: Path, data: io.IOBase, *, user: object = None) -> No
684691
_ = (data, user)
685692
self.writes.append(path)
686693

694+
async def rm(
695+
self,
696+
path: Path | str,
697+
*,
698+
recursive: bool = False,
699+
user: object = None,
700+
) -> None:
701+
_ = (recursive, user)
702+
self.removed.append(Path(path))
703+
687704

688705
@pytest.mark.asyncio
689706
async def test_write_new_file_with_a_bound_user_reports_an_existing_name(tmp_path: Path) -> None:
@@ -695,21 +712,31 @@ async def test_write_new_file_with_a_bound_user_reports_an_existing_name(tmp_pat
695712
Path("notes.txt"), io.BytesIO(b"payload"), user=User(name="sandbox-user")
696713
)
697714

698-
assert session.writes == []
715+
# The payload only ever reached a staging name, and that staging entry is cleaned up,
716+
# so a rejected create leaves nothing behind at the requested name.
717+
assert [path.name for path in session.writes] != ["notes.txt"]
718+
assert all(path.name.startswith(".notes.txt.create-") for path in session.writes)
719+
assert session.removed == session.writes
699720

700721

701722
@pytest.mark.asyncio
702-
async def test_write_new_file_with_a_bound_user_writes_after_claiming_the_name(
723+
async def test_write_new_file_with_a_bound_user_links_the_completed_payload(
703724
tmp_path: Path,
704725
) -> None:
726+
"""The payload is written first, then the target name is claimed by linking it."""
705727
session = _ExitCodeUnixLocalSession(tmp_path, exit_code=0)
706728

707729
await session.write_new_file(
708730
Path("notes.txt"), io.BytesIO(b"payload"), user=User(name="sandbox-user")
709731
)
710732

711-
assert [path.name for path in session.writes] == ["notes.txt"]
712-
assert any("set -C" in part for cmd in session.exec_commands for part in cmd)
733+
staged = session.writes[0]
734+
assert staged.name.startswith(".notes.txt.create-")
735+
dispatched = [part for cmd in session.exec_commands for part in cmd]
736+
assert any("ln " in part for part in dispatched)
737+
assert any(part.endswith("notes.txt") for part in dispatched)
738+
assert str(staged) in dispatched
739+
assert session.removed == [staged]
713740

714741

715742
@pytest.mark.asyncio
@@ -728,3 +755,43 @@ async def test_write_new_file_with_a_bound_user_keeps_a_symlink_name_unresolved(
728755
dispatched = [part for cmd in session.exec_commands for part in cmd]
729756
assert any(part.endswith("link.txt") for part in dispatched)
730757
assert not any(part.endswith("missing.txt") for part in dispatched)
758+
759+
760+
@pytest.mark.parametrize("shell", ["sh", "dash", "bash"])
761+
def test_exclusive_create_script_reports_a_taken_name_on_each_shell(
762+
shell: str, tmp_path: Path
763+
) -> None:
764+
"""Run the shipped script through real shells.
765+
766+
The script is dispatched as ``sh -lc``, so whichever shell provides ``/bin/sh``
767+
decides how a failing command is handled. An earlier version used ``:``, which is a
768+
POSIX special builtin, so a redirection failure terminated dash before the explicit
769+
exit mapping ran and the collision surfaced as a generic write error. This lives with
770+
the Unix-local tests because tests/conftest.py already skips them on Windows.
771+
"""
772+
executable = shutil.which(shell)
773+
if executable is None:
774+
pytest.skip(f"{shell} is not available")
775+
776+
staging = tmp_path / "staging"
777+
staging.write_bytes(b"payload")
778+
taken = tmp_path / "taken.txt"
779+
taken.write_bytes(b"existing\n")
780+
dangling = tmp_path / "dangling.txt"
781+
dangling.symlink_to(tmp_path / "missing.txt")
782+
783+
def run(target: Path) -> int:
784+
return subprocess.run(
785+
[executable, "-c", _EXCLUSIVE_CREATE_SCRIPT, shell, str(target), str(staging)],
786+
capture_output=True,
787+
).returncode
788+
789+
assert run(taken) == _EXCLUSIVE_CREATE_EXISTS_CODE
790+
assert taken.read_bytes() == b"existing\n"
791+
792+
assert run(dangling) == _EXCLUSIVE_CREATE_EXISTS_CODE
793+
assert not (tmp_path / "missing.txt").exists()
794+
795+
fresh = tmp_path / "nested" / "fresh.txt"
796+
assert run(fresh) == 0
797+
assert fresh.read_bytes() == b"payload"

0 commit comments

Comments
 (0)