Skip to content

Self-improving AI Employees for everyone

fix(config): expand ~ once at Settings load — DATABASE_URL + DATA_DIR + Temporal pgserver dir

trohitgc042ffee7bab
diff --git a/.env.template b/.env.template
index a052e01b..72d810a2 100644
--- a/.env.template
+++ b/.env.template
@@ -30,7 +30,11 @@ JWT_COOKIE_SAMESITE=lax
CORS_ORIGINS=["http://localhost:3000","http://localhost:3001","http://localhost:3002","http://localhost:3003","http://localhost:3004","http://localhost:3005","http://localhost:3006","http://127.0.0.1:3000","http://127.0.0.1:3001","http://127.0.0.1:3002","http://127.0.0.1:3003","http://127.0.0.1:3004","http://127.0.0.1:3005","http://127.0.0.1:3006","http://localhost:5173"]
# Database Configuration
-DATABASE_URL=sqlite+aiosqlite:///./data/workflow.db
+# SQLite DB file. ``~`` expands cross-platform at Settings load time
+# (Path.expanduser → ``$HOME`` on POSIX, ``%USERPROFILE%`` on Windows).
+# Keep this in sync with DATA_DIR below — both should live under the
+# same root.
+DATABASE_URL=sqlite+aiosqlite:///~/.machina/workflow.db
DATABASE_ECHO=false
DATABASE_POOL_SIZE=5
DATABASE_MAX_OVERFLOW=10
diff --git a/server/core/config.py b/server/core/config.py
index 88766487..bc7ce0bf 100644
--- a/server/core/config.py
+++ b/server/core/config.py
@@ -264,14 +264,34 @@ class Settings(BaseSettings):
gunicorn_max_requests: int = Field(default=10000, env="GUNICORN_MAX_REQUESTS", ge=0)
gunicorn_max_requests_jitter: int = Field(default=1000, env="GUNICORN_MAX_REQUESTS_JITTER", ge=0)
- @field_validator("database_url")
+ # Filesystem-path fields that may use ``~`` in ``.env``. Expanded
+ # once at load time so every downstream consumer
+ # (``core.paths.machina_root``, ``Settings._resolve_under_data``,
+ # the Temporal pgserver data dir, WhatsApp runtime dir, ...) sees
+ # a real path. ``Path.expanduser()`` is a no-op when ``~`` isn't
+ # present, so we can run it unconditionally.
+ @field_validator("data_dir", "log_file", mode="after")
@classmethod
- def validate_database_url(cls, v):
- """Ensure database directory exists for SQLite."""
- if v and v.startswith("sqlite"):
- if ":///" in v:
- db_path = v.split("///")[1]
- Path(db_path).parent.mkdir(parents=True, exist_ok=True)
+ def _expanduser_path(cls, v: Optional[str]) -> Optional[str]:
+ return str(Path(v).expanduser()) if v else v
+
+ @field_validator("database_url", mode="after")
+ @classmethod
+ def validate_database_url(cls, v: str) -> str:
+ """Expand ``~`` in sqlite paths + ensure parent dir exists.
+
+ Parses the URL via SQLAlchemy's canonical parser instead of
+ string surgery on ``:///``.
+ """
+ if not v:
+ return v
+ from sqlalchemy.engine.url import make_url
+ url = make_url(v)
+ if url.drivername.startswith("sqlite") and url.database:
+ db_path = Path(url.database).expanduser()
+ db_path.parent.mkdir(parents=True, exist_ok=True)
+ if str(db_path) != url.database:
+ v = url.set(database=str(db_path)).render_as_string(hide_password=False)
return v
diff --git a/server/core/paths.py b/server/core/paths.py
index 01f7a14b..ee1d835f 100644
--- a/server/core/paths.py
+++ b/server/core/paths.py
@@ -66,27 +66,15 @@ def project_root() -> Path:
def machina_root() -> Path:
"""Absolute path of ``~/.machina/`` (or the configured DATA_DIR).
- Resolves :class:`core.config.Settings.data_dir` per the rules in
- the module docstring. Default expands to ``~/.machina/``, i.e.
- the user's home dir — same shape on Windows
- (``%USERPROFILE%/.machina``), macOS, and Linux.
-
- Settings is instantiated lazily so this module stays a leaf
- dependency. The Settings class itself reads ``../.env`` at
- construction; subsequent calls are cheap (Pydantic caches).
+ ``Path.expanduser()`` is a no-op when there's no ``~`` to expand,
+ so we can always call it unconditionally. After expansion: if
+ absolute, use verbatim; if relative, resolve under repo root
+ (back-compat with ``DATA_DIR=data`` etc.). Settings is
+ instantiated lazily — Pydantic caches the result.
"""
from core.config import Settings
- settings = Settings()
- raw = settings.data_dir
- p = Path(raw)
- # ``~/...`` shape: expand to the user's home dir cross-platform.
- if raw.startswith("~"):
- return p.expanduser().resolve()
- # Absolute (POSIX or drive-rooted on Windows): use verbatim.
- if p.is_absolute():
- return p.resolve()
- # Relative: keep the pre-cutover repo-local back-compat path.
- return (project_root() / p).resolve()
+ p = Path(Settings().data_dir).expanduser()
+ return p.resolve() if p.is_absolute() else (project_root() / p).resolve()
def claude_config_dir() -> Path: