Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(storage): pass state paths to Alembic config as literals (#2182)

Max6e0b76148f5f
diff --git a/scripts/migration_release_guard.py b/scripts/migration_release_guard.py
index a69b4d0fb..a9594bd72 100644
--- a/scripts/migration_release_guard.py
+++ b/scripts/migration_release_guard.py
@@ -89,6 +89,7 @@ from storage.migrations import (
alembic_dir,
background_tables_ready,
run_migrations,
+ set_literal_main_option,
)
VERSIONS_PATH = "storage/alembic/versions"
@@ -686,14 +687,14 @@ def edited_released_bodies(baseline: str | None = None) -> list[str]:
def _alembic_config(db_path: Path, versions: Path | None = None) -> Config:
config = Config()
- config.set_main_option("script_location", str(alembic_dir()))
+ set_literal_main_option(config, "script_location", str(alembic_dir()))
if versions is not None:
# Without this Alembic splits version_locations on spaces and commas, so a
# temporary directory containing either would silently resolve to no location at
# all -- and an empty graph upgrades to head without applying anything.
- config.set_main_option("path_separator", "os")
- config.set_main_option("version_locations", str(versions))
- config.set_main_option("sqlalchemy.url", f"sqlite:///{db_path}")
+ set_literal_main_option(config, "path_separator", "os")
+ set_literal_main_option(config, "version_locations", str(versions))
+ set_literal_main_option(config, "sqlalchemy.url", f"sqlite:///{db_path}")
return config
diff --git a/storage/migrations.py b/storage/migrations.py
index e55cc5c3c..14e2bc6f7 100644
--- a/storage/migrations.py
+++ b/storage/migrations.py
@@ -223,10 +223,24 @@ def alembic_dir() -> Path:
return Path(__file__).resolve().parent / "alembic"
+def set_literal_main_option(cfg: Config, name: str, value: str) -> None:
+ """Store `value` in Alembic's config so it reads back unchanged.
+
+ Alembic keeps main options in a ConfigParser with `%` interpolation, so a raw
+ `%` is rejected on write, or, shaped like `%(here)s`, silently replaced on
+ read with a different path. Paths reach here verbatim: a directory can contain a
+ literal `%`, and SQLAlchemy 2.1 percent-encodes every non-ASCII character when
+ rendering a URL, so a state directory under a Chinese user name became
+ `%E9%...` and migrations failed before they started. Escaping is the
+ documented way to pass a literal through; interpolation undoes it on read.
+ """
+ cfg.set_main_option(name, value.replace("%", "%%"))
+
+
def alembic_config(db_path: Path | None = None) -> Config:
cfg = Config()
- cfg.set_main_option("script_location", str(alembic_dir()))
- cfg.set_main_option("sqlalchemy.url", sqlite_url(db_path or paths.get_sqlite_state_path()))
+ set_literal_main_option(cfg, "script_location", str(alembic_dir()))
+ set_literal_main_option(cfg, "sqlalchemy.url", sqlite_url(db_path or paths.get_sqlite_state_path()))
return cfg
diff --git a/tests/test_sqlite_state_migration.py b/tests/test_sqlite_state_migration.py
index ec3840931..d73d13849 100644
--- a/tests/test_sqlite_state_migration.py
+++ b/tests/test_sqlite_state_migration.py
@@ -18,6 +18,7 @@ from alembic.migration import MigrationContext
from alembic.operations import Operations
from alembic.script import ScriptDirectory
from sqlalchemy.dialects.sqlite import dialect as sqlite_dialect
+from sqlalchemy.engine import make_url
from sqlalchemy.exc import IntegrityError
from sqlalchemy.schema import CreateIndex
@@ -6894,3 +6895,23 @@ def test_legacy_sessions_import_still_requires_platform_for_an_unresolvable_lega
with pytest.raises(ValueError, match="primary_platform is required"):
ensure_sqlite_state(db_path=db_path, state_dir=state_dir)
+
+
+def test_state_under_any_directory_name_migrates_in_place(tmp_path: Path) -> None:
+ # One name that carries every character Alembic's ConfigParser treats specially
+ # or that SQLAlchemy 2.1 percent-encodes when it renders the URL: non-ASCII, a
+ # literal `%`, an interpolation-shaped `%(here)s`, and URL-escape-shaped
+ # `%20` / `%2F` that must stay literal rather than decode to a space or `/`.
+ state_dir = tmp_path / "麦 50%(here)s a%20b a%2Fb"
+ db_path = state_dir / "vibe.sqlite"
+
+ cfg = migrations.alembic_config(db_path)
+ assert make_url(cfg.get_main_option("sqlalchemy.url")).database == str(db_path.resolve())
+
+ run_migrations(db_path)
+
+ with closing(sqlite3.connect(db_path)) as conn:
+ assert conn.execute("select version_num from alembic_version").fetchall() == [(HEAD_REVISION,)]
+ assert sorted(p.relative_to(tmp_path) for p in tmp_path.rglob("*.sqlite")) == [
+ db_path.relative_to(tmp_path)
+ ]