[PR #415] [MERGED] Enforce runtime-only directory creation for startup modules #720

Closed
opened 2026-03-13 21:05:37 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/AJaySi/ALwrity/pull/415
Author: @AJaySi
Created: 3/12/2026
Status: Merged
Merged: 3/12/2026
Merged by: @AJaySi

Base: mainHead: codex/apply-no-filesystem-writes-policy


📝 Commits (1)

  • 3ebe884 Enforce runtime-only workspace directory creation policy

📊 Changes

12 files changed (+241 additions, -105 deletions)

View changed files

📝 backend/alwrity_utils/environment_setup.py (+14 -17)
📝 backend/api/youtube/handlers/audio.py (+2 -5)
📝 backend/api/youtube/handlers/avatar.py (+5 -7)
📝 backend/api/youtube/handlers/images.py (+3 -9)
backend/api/youtube/paths.py (+21 -0)
📝 backend/api/youtube/router.py (+6 -11)
📝 backend/middleware/logging_middleware.py (+11 -4)
📝 backend/routers/seo_tools.py (+9 -1)
📝 backend/services/user_workspace_manager.py (+12 -48)
backend/services/workspace_dirs.py (+80 -0)
backend/tests/test_no_import_time_mkdir.py (+72 -0)
📝 backend/utils/media_utils.py (+6 -3)

📄 Description

Motivation

  • Prevent modules that load at application startup from performing filesystem writes during import, which can cause side-effects, flakiness, or platform failures.
  • Centralize and delay tenant/content directory creation so directories are created on demand with a user context (e.g., user_id and capabilities).
  • Keep truly global operational directories (logs/temp) available while avoiding pre-creating per-tenant content/media folders at startup.
  • Add a static guard so future changes don't reintroduce import-time mkdir/os.makedirs calls in startup-loaded modules.

Description

  • Added a centralized helper module backend/services/workspace_dirs.py providing ensure_user_workspace_dirs(user_id, capabilities) and ensure_global_operational_dirs(...) to create only required directories at runtime.
  • Updated environment/startup logic in backend/alwrity_utils/environment_setup.py to only create operational directories (logs, temp) and to use ensure_global_operational_dirs.
  • Eliminated import-time directory creation from startup-loaded modules and moved creation to runtime helpers: refactored backend/middleware/logging_middleware.py, backend/routers/seo_tools.py, backend/utils/media_utils.py to create directories lazily.
  • Centralized YouTube media path logic in backend/api/youtube/paths.py and updated backend/api/youtube/router.py and handlers (avatar.py, images.py, audio.py) to call ensure_youtube_media_dirs(user_id) after resolving the user.
  • Reworked backend/services/user_workspace_manager.py to use ensure_user_workspace_dirs for capability-driven per-user folder creation (AI services, integrations, content, media, assets).
  • Added a static guard test backend/tests/test_no_import_time_mkdir.py that parses startup modules and fails when top-level mkdir/os.makedirs calls are present, excluding calls inside functions/classes (runtime-safe).

Testing

  • Ran python -m py_compile against the modified backend modules to ensure they are syntactically valid (succeeded).
  • Ran the new guard test with pytest -q backend/tests/test_no_import_time_mkdir.py, which passed and will fail if a top-level filesystem write is reintroduced (succeeded).

Codex Task


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/AJaySi/ALwrity/pull/415 **Author:** [@AJaySi](https://github.com/AJaySi) **Created:** 3/12/2026 **Status:** ✅ Merged **Merged:** 3/12/2026 **Merged by:** [@AJaySi](https://github.com/AJaySi) **Base:** `main` ← **Head:** `codex/apply-no-filesystem-writes-policy` --- ### 📝 Commits (1) - [`3ebe884`](https://github.com/AJaySi/ALwrity/commit/3ebe884a37e7dc5d9d71fb3b7139b2bc4975c374) Enforce runtime-only workspace directory creation policy ### 📊 Changes **12 files changed** (+241 additions, -105 deletions) <details> <summary>View changed files</summary> 📝 `backend/alwrity_utils/environment_setup.py` (+14 -17) 📝 `backend/api/youtube/handlers/audio.py` (+2 -5) 📝 `backend/api/youtube/handlers/avatar.py` (+5 -7) 📝 `backend/api/youtube/handlers/images.py` (+3 -9) ➕ `backend/api/youtube/paths.py` (+21 -0) 📝 `backend/api/youtube/router.py` (+6 -11) 📝 `backend/middleware/logging_middleware.py` (+11 -4) 📝 `backend/routers/seo_tools.py` (+9 -1) 📝 `backend/services/user_workspace_manager.py` (+12 -48) ➕ `backend/services/workspace_dirs.py` (+80 -0) ➕ `backend/tests/test_no_import_time_mkdir.py` (+72 -0) 📝 `backend/utils/media_utils.py` (+6 -3) </details> ### 📄 Description ### Motivation - Prevent modules that load at application startup from performing filesystem writes during import, which can cause side-effects, flakiness, or platform failures. - Centralize and delay tenant/content directory creation so directories are created on demand with a user context (e.g., `user_id` and capabilities). - Keep truly global operational directories (logs/temp) available while avoiding pre-creating per-tenant content/media folders at startup. - Add a static guard so future changes don't reintroduce import-time mkdir/os.makedirs calls in startup-loaded modules. ### Description - Added a centralized helper module `backend/services/workspace_dirs.py` providing `ensure_user_workspace_dirs(user_id, capabilities)` and `ensure_global_operational_dirs(...)` to create only required directories at runtime. - Updated environment/startup logic in `backend/alwrity_utils/environment_setup.py` to only create operational directories (`logs`, `temp`) and to use `ensure_global_operational_dirs`. - Eliminated import-time directory creation from startup-loaded modules and moved creation to runtime helpers: refactored `backend/middleware/logging_middleware.py`, `backend/routers/seo_tools.py`, `backend/utils/media_utils.py` to create directories lazily. - Centralized YouTube media path logic in `backend/api/youtube/paths.py` and updated `backend/api/youtube/router.py` and handlers (`avatar.py`, `images.py`, `audio.py`) to call `ensure_youtube_media_dirs(user_id)` after resolving the user. - Reworked `backend/services/user_workspace_manager.py` to use `ensure_user_workspace_dirs` for capability-driven per-user folder creation (AI services, integrations, content, media, assets). - Added a static guard test `backend/tests/test_no_import_time_mkdir.py` that parses startup modules and fails when top-level `mkdir`/`os.makedirs` calls are present, excluding calls inside functions/classes (runtime-safe). ### Testing - Ran `python -m py_compile` against the modified backend modules to ensure they are syntactically valid (succeeded). - Ran the new guard test with `pytest -q backend/tests/test_no_import_time_mkdir.py`, which passed and will fail if a top-level filesystem write is reintroduced (succeeded). ------ [Codex Task](https://chatgpt.com/codex/tasks/task_e_69afb0f7e818832889ba42f5446a228a) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-13 21:05:37 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ALwrity#720
No description provided.