Repository navigation
Description
I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.
Is there an existing issue for the same bug?
- I have searched existing issues and this is not a duplicate.
Bug Description
POST /api/file/upload (and the conversation-scoped upload, which forwards to it) answers a wrong destination path with a 500 instead of a client error. Uploading onto a path that is an existing directory, or to a path whose parent is a regular file, returns 500 {"detail": "Internal Server Error", "exception": "500: Failed to upload file: [Errno 21] Is a directory: ..."} (or [Errno 17] File exists). The caller cannot tell a bad path from a server fault, and the same collision on POST /api/file/create_directory is already a 400 Path exists and is not a directory.
Expected Behavior
Both cases are a 4xx (400, consistent with create_directory) with a clear detail, and nothing already on disk is touched:
- target is an existing directory:
400, e.g.Path is a directory; - a parent component is a file:
400, e.g.A parent of the path is not a directory.
Actual Behavior
path=<dir>(existing directory):500 ... Failed to upload file: [Errno 21] Is a directory: '<dir>'.path=<file>/child(parent is a file):500 ... Failed to upload file: [Errno 17] File exists: '<file>'; a deeper path such as<file>/a/bgives[Errno 20] Not a directory.
Steps to Reproduce
- In a checkout of main:
uv sync, thenSESSION_API_KEY=dev-key uv run python -m openhands.agent_server --host 127.0.0.1 --port 8000. - Prepare paths:
W=$(mktemp -d); mkdir "$W/is-a-dir"; printf 'x\n' > "$W/a-file.txt"curl -s -H 'X-Session-API-Key: dev-key' -F file=@"$W/a-file.txt" "http://127.0.0.1:8000/api/file/upload?path=$W/is-a-dir"returns 500[Errno 21] Is a directory.curl -s -H 'X-Session-API-Key: dev-key' -F file=@"$W/a-file.txt" "http://127.0.0.1:8000/api/file/upload?path=$W/a-file.txt/child"returns 500[Errno 17] File exists.- Control:
curl -s -X POST -H 'X-Session-API-Key: dev-key' "http://127.0.0.1:8000/api/file/create_directory?path=$W/a-file.txt/child"returns 400Path exists and is not a directory.
With the verify-agent-server skill: control-agent-server map run --file F14 --only F14.upload-onto-directory --fresh reports the known-bug bullet as an expected failure while the bug lasts.
Acceptance Criteria
- Uploading onto an existing directory returns 400 with a clear
detail, and the directory is unchanged. - Uploading to a path whose parent (direct or further up) is a regular file returns 400 with a clear
detail, and the file is unchanged. - Other upload failures (I/O errors, etc.) still return 500.
-
tests/agent_server/test_file_router.pycovers both cases. - The map bullet
F14.upload-onto-directorypasses with its# bugmarker removed.
Installation Method
uv sync in a checkout of main
Version
main at 6e7a853
Additional Context
Root cause, in openhands-agent-server/openhands/agent_server/file_router.py:
_upload_file(line 85) runstarget_path.parent.mkdir(parents=True, exist_ok=True)(line 98) andopen(target_path, "wb")(line 104);mkdirraisesFileExistsError/NotADirectoryErrorwhen a parent is a file, andopenraisesIsADirectoryErrorwhen the target is a directory.- The handler's
except Exception(lines 113-118) turns every non-HTTPExceptioninto a 500. create_directory(lines 731-740) already mapsFileExistsError/NotADirectoryErrorto 400Path exists and is not a directory.
Related: the same _upload_file is touched by the open upload fixes for the response file_size (F14.upload-size) and atomic overwrites (F14.upload-atomic).
Found by the first full agentic run of verify-agent-server; tracked in #5653.
OpenHands AI triage
The following comments and acceptance criteria were added by the OpenHands AI agent.
Triage
Confirmed bug with a clear, reproducible cause. _upload_file in openhands-agent-server/openhands/agent_server/file_router.py runs target_path.parent.mkdir(parents=True, exist_ok=True) and open(target_path, "wb"), then its except Exception maps every non-HTTPException to a 500. A target that is an existing directory raises IsADirectoryError, and a parent component that is a regular file raises FileExistsError/NotADirectoryError; both are caller path errors. The sibling handler create_directory in the same file already maps FileExistsError/NotADirectoryError to 400 Path exists and is not a directory, so the fix should bring upload to the same 4xx behavior. The global POST /api/file/upload route and the conversation-scoped POST /api/conversations/{runtime_conversation_id}/file/upload route share this handler (the same file_router is mounted under the runtime prefix in runtime_router.py), so one fix covers both.
Scope: classify the two destination-path collisions in _upload_file as 400 with a detail that names the cause, leave genuine I/O failures at 500, and cover both cases in tests/agent_server/test_file_router.py.
Non-goals: the response file_size (F14.upload-size) and atomic-overwrite (F14.upload-atomic) fixes are separate and out of scope; no new endpoint; no change to the global route accepting any absolute path the server user can reach; no change to successful upload/overwrite behavior or to parent-directory creation.
Acceptance Criteria
- Uploading to a
paththat is an existing directory returns HTTP 400, and the directory and its contents are unchanged. - Uploading to a
pathwhose immediate parent is a regular file returns HTTP 400, and the existing file is unchanged. - Uploading to a deeper path where a non-immediate ancestor is a regular file (e.g.
<file>/a/b) returns HTTP 400, and the existing file is unchanged. - In all three cases the response
detailis non-empty and identifies the cause: a directory target, or a parent component that is not a directory. - Upload failures that are not caller path errors (e.g. an I/O error such as a full disk) still return HTTP 500.
- The conversation-scoped route
POST /api/conversations/{conversation_id}/file/uploadreturns the same 400 for both collisions, since it forwards to the same handler. - Existing success behavior is unchanged: uploading to a new path creates missing parents and returns 200
{"success": true}, and uploading over an existing regular file overwrites it and returns 200. -
tests/agent_server/test_file_router.pycovers both collisions (target is a directory; parent is a file), asserting 400 and that nothing on disk changed. - The feature-map bullet
F14.upload-onto-directorypasses with its# bugmarker removed (both recipes return 4xx).
Activity
test(agent-server): cover upload collisions on the conversation-scope…
enyst commentedon Oct 14, 2026
I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.
Re the triage: no open questions remain, and #5931 implements the scope as written.
create_directory. 409 Conflict was the other option for a directory target. It would make upload andcreate_directorydisagree, so I did not use it. A maintainer can still ask for 409. Neither client (PythonRemoteWorkspace.file_upload, TypeScriptfileUpload) needs a change either way, because both treat any non-2xx as a failed upload.Path is a directory(the target is a directory) andA parent of the path is not a directory(the direct parent or any ancestor is a file, i.e.FileExistsError/NotADirectoryErrorfrommkdir(parents=True)). Every other exception still maps to 500.tests/agent_server/test_file_router.py. The conversation-scoped route is covered intests/agent_server/test_runtime_router.py. All tests assert that nothing on disk changed. The existing success and overwrite tests are unchanged and still pass.F14.upload-onto-directoryreturns xfail x2 on main and xpass x2 with the fix. With the# bugmarkers removed it passes, and it now asserts the exact 400 details.Generated by Claude Code