fix(B03): 직접 업로드 마지막 파일 404 + 실패 기록이 실패하던 문제 (E2E 결함 1·4)

결함 1 — 업로드 세션이 인증 세션을 가리고 있었다
- finalize_project_upload가 Depends(verify_session)로 받은 session을 같은 이름으로
  덮어써, str(session["role"])이 업로드 세션 행에서 role을 찾다 KeyError를 냈다.
  KeyError는 LookupError 하위라 404 {"message": "'role'"}로 나가고 로그도 안 남았다.
- 업로드 세션 변수를 upload_session으로 분리(청크 업로드·finalize·상태 조회 3곳).
- B03_FileInput_Router_Errors.lookup_error_response() 신설: 조회 실패만 404,
  KeyError·IndexError는 500 + 예외 로그. 두 라우터의 LookupError 처리 13곳에 적용.
  batch 단위 엔드포인트에는 batch_id를, 프로젝트 단위에는 project_id를 로그 필드로 준다.

결함 4 — fail_stage가 예외 문자열을 그대로 넣어 UPDATE가 죽었다
- project_workflow_stages.message는 varchar(255)인데 PermissionError 메시지는 300자를
  넘겨 DataError로 실패했고, 단계가 FAILED로 못 가 화면이 영영 "분석 중"이었다.
- 200자로 자르고 말줄임표를 붙인다. 원문은 호출부 로그에 남는다.

검증(실서버, 신규 프로젝트 f45243b3에 표본 5개 직접 청크 업로드):
  finalize 성공 11건 / "'role'" 오류 0건, 마지막 파일 complete_upload=true도 성공.
  이어서 WF1 자동 분석이 시작됨(stage 0 COMPLETE, stage 1 IN_PROGRESS) — 종전에는
  예외가 스케줄링 앞에서 터져 자동 분석이 아예 걸리지 않았다.
ruff format·check 통과.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-08 19:18:42 +09:00
co-authored by Claude Opus 5
parent 1e905c257f
commit 1f119f9845
4 changed files with 74 additions and 29 deletions
+20 -19
View File
@@ -36,6 +36,7 @@ from B03_FileInput.B03_FileInput_Repository import (
mark_upload_session_failed,
upsert_upload_chunk,
)
from B03_FileInput.B03_FileInput_Router_Errors import lookup_error_response
from B03_FileInput.B03_FileInput_Schema import (
ChunkSessionCreateRequest,
ChunkSessionCreateResponse,
@@ -348,7 +349,7 @@ async def upload_project_files(
)
return FileUploadResponse(project_id=str(project_id), files=results)
except LookupError as exc:
return JSONResponse(status_code=404, content={"status": "error", "message": str(exc)})
return lookup_error_response(exc, logger, context="B03 업로드", project_id=project_id)
except (OSError, ValueError) as exc:
for saved_path in saved_paths:
saved_path.unlink(missing_ok=True)
@@ -398,7 +399,7 @@ async def create_project_upload_session(
total_chunks=total_chunks,
)
except LookupError as exc:
return JSONResponse(status_code=404, content={"status": "error", "message": str(exc)})
return lookup_error_response(exc, logger, context="B03 업로드", project_id=project_id)
except (OSError, ValueError) as exc:
return JSONResponse(status_code=400, content={"status": "error", "message": str(exc)})
except Exception:
@@ -420,12 +421,12 @@ async def upload_project_chunk(
pool = get_db_pool()
try:
async with pool.acquire() as connection:
session = await get_upload_session(
upload_session = await get_upload_session(
connection,
project_id=project_id,
session_id=session_id,
)
if chunk_index < 0 or chunk_index >= int(session["total_chunks"]):
if chunk_index < 0 or chunk_index >= int(upload_session["total_chunks"]):
return JSONResponse(
status_code=400,
content={"status": "error", "message": "청크 인덱스가 범위를 벗어났습니다."},
@@ -437,7 +438,7 @@ async def upload_project_chunk(
chunk_data,
session_dir,
chunk_index,
expected_max_bytes=int(session["chunk_size_bytes"]),
expected_max_bytes=int(upload_session["chunk_size_bytes"]),
)
relative_chunk_path = chunk_path.relative_to(project_root).as_posix()
completed_chunks = await upsert_upload_chunk(
@@ -452,11 +453,11 @@ async def upload_project_chunk(
upload_session_id=session_id,
chunk_index=chunk_index,
completed_chunks=completed_chunks,
total_chunks=int(session["total_chunks"]),
total_chunks=int(upload_session["total_chunks"]),
chunk_hash=chunk_hash,
)
except LookupError as exc:
return JSONResponse(status_code=404, content={"status": "error", "message": str(exc)})
return lookup_error_response(exc, logger, context="B03 업로드", project_id=project_id)
except (OSError, ValueError) as exc:
return JSONResponse(status_code=400, content={"status": "error", "message": str(exc)})
except Exception:
@@ -485,12 +486,12 @@ async def finalize_project_upload(
point_cloud_input_id: int | None = None
try:
async with pool.acquire() as connection:
session = await get_upload_session(
upload_session = await get_upload_session(
connection,
project_id=project_id,
session_id=payload.session_id,
)
if int(session["total_chunks"]) != payload.total_chunks:
if int(upload_session["total_chunks"]) != payload.total_chunks:
return JSONResponse(
status_code=400,
content={"status": "error", "message": "세션 청크 개수가 일치하지 않습니다."},
@@ -509,8 +510,8 @@ async def finalize_project_upload(
stored_path = await get_project_storage_relative_path(connection, project_id)
project_root = Path(resolve_stored_project_path(stored_path))
descriptor = FileUploadDescriptor(
original_filename=str(session["original_filename"]),
size_bytes=int(session["file_size_bytes"]),
original_filename=str(upload_session["original_filename"]),
size_bytes=int(upload_session["file_size_bytes"]),
)
final_path = merge_upload_chunks(
project_root,
@@ -571,7 +572,7 @@ async def finalize_project_upload(
)
return FileUploadResponse(project_id=str(project_id), files=[result])
except LookupError as exc:
return JSONResponse(status_code=404, content={"status": "error", "message": str(exc)})
return lookup_error_response(exc, logger, context="B03 업로드", project_id=project_id)
except (OSError, ValueError) as exc:
if final_path is not None:
final_path.unlink(missing_ok=True)
@@ -595,7 +596,7 @@ async def get_project_upload_status(
pool = get_db_pool()
try:
async with pool.acquire() as connection:
session = await get_upload_session(
upload_session = await get_upload_session(
connection,
project_id=project_id,
session_id=session_id,
@@ -606,16 +607,16 @@ async def get_project_upload_status(
)
return UploadStatusResponse(
upload_session_id=session_id,
upload_status=str(session["status"]),
original_filename=str(session["original_filename"]),
file_size_bytes=int(session["file_size_bytes"]),
chunk_size_bytes=int(session["chunk_size_bytes"]),
total_chunks=int(session["total_chunks"]),
upload_status=str(upload_session["status"]),
original_filename=str(upload_session["original_filename"]),
file_size_bytes=int(upload_session["file_size_bytes"]),
chunk_size_bytes=int(upload_session["chunk_size_bytes"]),
total_chunks=int(upload_session["total_chunks"]),
completed_chunks=len(completed_indexes),
completed_chunk_indexes=completed_indexes,
)
except LookupError as exc:
return JSONResponse(status_code=404, content={"status": "error", "message": str(exc)})
return lookup_error_response(exc, logger, context="B03 업로드", project_id=project_id)
except (OSError, ValueError) as exc:
return JSONResponse(status_code=400, content={"status": "error", "message": str(exc)})
except Exception: