Skip to content

The agent that grows with you

fix(tui_gateway): restore atomic file.attach allocation lost in a prior main merge

Tekniumc1f44a2ef164
diff --git a/tests/tui_gateway/test_file_attachment_allocation.py b/tests/tui_gateway/test_file_attachment_allocation.py
index 225585d7e7a..b778b68fbb0 100644
--- a/tests/tui_gateway/test_file_attachment_allocation.py
+++ b/tests/tui_gateway/test_file_attachment_allocation.py
@@ -136,3 +136,19 @@ def test_cross_session_attached_images_allocate_unique_paths(sessions, tmp_path)
assert path_b.read_bytes() == img_bytes_b
assert path_a.exists() and path_b.exists()
+
+
+def test_upload_never_writes_through_a_planted_symlink(sessions, tmp_path):
+ """A repo-shipped dangling symlink at the candidate name must not redirect the upload."""
+ root = tmp_path / "profile" / "attachments"
+ root.mkdir(parents=True)
+ outside = tmp_path / "outside.txt"
+ (root / "notes.txt").symlink_to(outside)
+
+ response = upload("first", "notes.txt", b"user upload")
+
+ assert "error" not in response, response
+ stored = Path(response["result"]["path"])
+ assert not outside.exists()
+ assert stored.parent == root.resolve() and stored.name == "notes-2.txt"
+ assert stored.read_bytes() == b"user upload"
diff --git a/tui_gateway/prompt_attachments.py b/tui_gateway/prompt_attachments.py
index 01cf6cda398..9223b6a2ba6 100644
--- a/tui_gateway/prompt_attachments.py
+++ b/tui_gateway/prompt_attachments.py
@@ -242,6 +242,7 @@ def _stage_session_file_attachment(
stem = Path(filename).stem or "attachment"
suffix = Path(filename).suffix
counter = 2
+ # O_CREAT|O_EXCL: never follows a planted (dangling) symlink, never races a same-name upload.
while True:
try:
upload = target.open("xb")