diff --git a/backend/src/backend/scripts.py b/backend/src/backend/scripts.py index da047f0..c7545b4 100644 --- a/backend/src/backend/scripts.py +++ b/backend/src/backend/scripts.py @@ -424,14 +424,21 @@ async def create_script_record( workspace_id = context.workspace.workspace_id content_hash = hashlib.sha256(content).hexdigest() size_bytes = len(content) - if parent_ulid: - try: - await runtime_client.ensure_directory(workspace_id, f"{user_id}/{parent_ulid}") - except RuntimeClientError as exc: - raise HTTPException( - status_code=exc.status_code, - detail=exc.detail, - ) from exc + # Jupyter Contents API PUT does not create intermediate directories. + # Make sure the user's own subfolder exists at the Jupyter level before + # we try to write into it; on nested creates, also ensure the parent + # directory under it. ensure_directory is idempotent (GET-first pattern). + try: + await runtime_client.ensure_directory(workspace_id, user_id) + if parent_ulid: + await runtime_client.ensure_directory( + workspace_id, f"{user_id}/{parent_ulid}" + ) + except RuntimeClientError as exc: + raise HTTPException( + status_code=exc.status_code, + detail=exc.detail, + ) from exc try: if script_type == "notebook": @@ -826,14 +833,21 @@ async def create_workspace_directory( runtime_client = request.app.state.runtime_client workspace_id = context.workspace.workspace_id - if parent_ulid: - try: - await runtime_client.ensure_directory(workspace_id, f"{user_id}/{parent_ulid}") - except RuntimeClientError as exc: - raise HTTPException( - status_code=exc.status_code, - detail=exc.detail, - ) from exc + # Jupyter Contents API PUT does not create intermediate directories. + # Make sure the user's own subfolder exists at the Jupyter level before + # we try to write into it; on nested creates, also ensure the parent + # directory under it. ensure_directory is idempotent (GET-first pattern). + try: + await runtime_client.ensure_directory(workspace_id, user_id) + if parent_ulid: + await runtime_client.ensure_directory( + workspace_id, f"{user_id}/{parent_ulid}" + ) + except RuntimeClientError as exc: + raise HTTPException( + status_code=exc.status_code, + detail=exc.detail, + ) from exc try: await runtime_client.create_directory(workspace_id, name=jupyter_path)