Skip to content

Fix duplicate gateway upload filenames#2789

Open
ZenCGL wants to merge 2 commits intobytedance:mainfrom
ZenCGL:main
Open

Fix duplicate gateway upload filenames#2789
ZenCGL wants to merge 2 commits intobytedance:mainfrom
ZenCGL:main

Conversation

@ZenCGL
Copy link
Copy Markdown

@ZenCGL ZenCGL commented May 8, 2026

Motivation

  • Multipart uploads to the Gateway could silently overwrite earlier parts when multiple form fields used the same basename, losing data for the overwritten file.

Description

  • Use the shared claim_unique_filename helper to auto-rename duplicate filenames within a single POST /api/threads/{thread_id}/uploads request by adding _N suffixes and track them with a seen_filenames set so later parts do not truncate earlier ones in the same request (app/gateway/routers/uploads.py).
  • Include original_filename in the upload response when a part was renamed so callers/agents can see the original basename (app/gateway/routers/uploads.py).
  • Add a regression test test_upload_files_auto_renames_duplicate_form_filenames proving two form parts named data.txt are persisted as data.txt and data_1.txt with preserved contents (tests/test_uploads_router.py).
  • Update backend docs to mention the new auto-rename behavior in uploads (backend/README.md, backend/CLAUDE.md).

Testing

  • Ran the uploads router unit tests: cd backend && PYTHONPATH=. uv run pytest tests/test_uploads_router.py -q, all tests passed (29 passed).
  • Ran the linter/formatter check: cd backend && uv run ruff check app/gateway/routers/uploads.py tests/test_uploads_router.py and fixed import ordering; final check passed.

Fixes #2795 -

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 8, 2026

CLA assistant check
All committers have signed the CLA.

@WillemJiang
Copy link
Copy Markdown
Collaborator

@ZenCGL, thanks for your contribution. Please create a GitHub issue for the bug you found.
Please click the CLA button to sign the CLA before we start reviewing the PR.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Prevents silent overwrites when a single multipart upload request contains multiple parts with the same filename by ensuring per-request filename uniqueness and exposing the original name when auto-renaming occurs.

Changes:

  • Deduplicate filenames within POST /api/threads/{thread_id}/uploads using claim_unique_filename(...) and track seen names per request.
  • Include original_filename in the upload response when a file is renamed due to duplication.
  • Add a regression test and update backend docs to describe the new auto-rename behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
backend/app/gateway/routers/uploads.py Tracks per-request seen filenames and auto-renames duplicates; adds original_filename in response when renamed.
backend/tests/test_uploads_router.py Adds regression test ensuring duplicate form filenames persist as distinct files with preserved contents.
backend/README.md Documents auto-renaming behavior for duplicate filenames in a single upload request.
backend/CLAUDE.md Notes the duplicate-filename auto-rename behavior in the file upload feature description.

Comment on lines +71 to +75
with (
patch.object(uploads, "get_uploads_dir", return_value=thread_uploads_dir),
patch.object(uploads, "ensure_uploads_dir", return_value=thread_uploads_dir),
patch.object(uploads, "get_sandbox_provider", return_value=provider),
):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

多文件上传时同一请求内重复文件名会互相覆盖的问题

4 participants