Skip to content

Commit 8049c24

Browse files
committed
fix(sandbox): use a fixed-length staging basename
The staging name was derived from the destination, so it was always longer than the destination itself. A filename that fits the filesystem's component limit, and that the ordinary write path accepts, could then fail to stage: a 254 character name raised WorkspaceArchiveWriteError where a plain write succeeded before this branch. The staging basename is now constant at 52 characters regardless of the destination. Reported by fscfede-beep in #4930.
1 parent 9820c3f commit 8049c24

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ async def write_new_file(
10341034
requested = Path(path)
10351035
parent_path = self.normalize_path(requested.parent, for_write=True)
10361036
workspace_path = parent_path / requested.name
1037-
staging_path = parent_path / f".{requested.name}.create-{uuid.uuid4().hex}"
1037+
staging_path = parent_path / f".apply-patch-create-{uuid.uuid4().hex}"
10381038
try:
10391039
# Only the link may report a collision. A parent that is a regular file also
10401040
# raises FileExistsError from mkdir, and reporting that as "the target already

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,10 @@ async def write_new_file(
993993
parent_path = await self._validate_path_access(requested.parent, for_write=True)
994994
workspace_path = parent_path / requested.name
995995
path_arg = sandbox_path_str(workspace_path)
996-
staging_path = parent_path / f".{requested.name}.create-{uuid.uuid4().hex}"
996+
# A fixed-length staging basename. Deriving it from the destination made the
997+
# staging name longer than the destination, so a name that fits the filesystem's
998+
# component limit could still fail to stage.
999+
staging_path = parent_path / f".apply-patch-create-{uuid.uuid4().hex}"
9971000
staging_arg = sandbox_path_str(staging_path)
9981001

9991002
try:

‎tests/sandbox/test_unix_local.py‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ async def test_write_new_file_with_a_bound_user_reports_an_existing_name(tmp_pat
731731
# The payload only ever reached a staging name, and that staging entry is cleaned up,
732732
# so a rejected create leaves nothing behind at the requested name.
733733
assert [path.name for path in session.writes] != ["notes.txt"]
734-
assert all(path.name.startswith(".notes.txt.create-") for path in session.writes)
734+
assert all(path.name.startswith(".apply-patch-create-") for path in session.writes)
735735
assert session.removed == session.writes
736736
assert session.made_dirs != []
737737

@@ -748,7 +748,7 @@ async def test_write_new_file_with_a_bound_user_links_the_completed_payload(
748748
)
749749

750750
staged = session.writes[0]
751-
assert staged.name.startswith(".notes.txt.create-")
751+
assert staged.name.startswith(".apply-patch-create-")
752752
dispatched = [part for cmd in session.exec_commands for part in cmd]
753753
assert any("ln " in part for part in dispatched)
754754
assert any(part.endswith("notes.txt") for part in dispatched)
@@ -903,3 +903,27 @@ async def test_apply_patch_create_through_the_session_reports_a_file_parent_as_a
903903
await session.apply_patch(
904904
ApplyPatchOperation(type="create_file", path="parent/child.txt", diff="+hi\n")
905905
)
906+
907+
908+
@pytest.mark.asyncio
909+
async def test_apply_patch_create_accepts_a_destination_at_the_component_limit(
910+
tmp_path: Path,
911+
) -> None:
912+
"""Staging must not push a valid destination name past the filesystem's limit.
913+
914+
Deriving the staging basename from the destination made it longer than the
915+
destination itself, so a name the ordinary write path accepts failed to create.
916+
"""
917+
session = _exclusive_write_session(tmp_path)
918+
long_name = "a" * 250 + ".txt"
919+
# Confirm the platform really does accept this name, so the test fails for the
920+
# right reason rather than because the limit is lower here.
921+
probe = tmp_path / long_name
922+
probe.write_text("probe")
923+
probe.unlink()
924+
925+
await session.apply_patch(
926+
ApplyPatchOperation(type="create_file", path=long_name, diff="+hello\n")
927+
)
928+
929+
assert (tmp_path / long_name).read_text() == "hello"

0 commit comments

Comments
 (0)