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(state): honor custom sqlite state roots

cyhca22859b27ff
diff --git a/config/v2_sessions.py b/config/v2_sessions.py
index f2187cdb6..301e45628 100644
--- a/config/v2_sessions.py
+++ b/config/v2_sessions.py
@@ -229,7 +229,7 @@ class SessionsStore:
ensure_sqlite_state(
db_path=target_db,
state_dir=Path(self.sessions_path).parent,
- primary_platform=resolve_primary_platform_from_config(),
+ primary_platform=resolve_primary_platform_from_config(Path(self.sessions_path).parent),
)
self.db_path = target_db
self._service = SQLiteSessionsService(target_db)
diff --git a/config/v2_settings.py b/config/v2_settings.py
index 8be5a4c98..1256273b8 100644
--- a/config/v2_settings.py
+++ b/config/v2_settings.py
@@ -358,7 +358,7 @@ class SettingsStore:
ensure_sqlite_state(
db_path=self.db_path,
state_dir=self.settings_path.parent,
- primary_platform=resolve_primary_platform_from_config(),
+ primary_platform=resolve_primary_platform_from_config(self.settings_path.parent),
)
self._service = SQLiteSettingsService(self.db_path)
self._load()
diff --git a/storage/importer.py b/storage/importer.py
index 9629e4f44..a07e86a6b 100644
--- a/storage/importer.py
+++ b/storage/importer.py
@@ -59,9 +59,13 @@ def ensure_sqlite_state(
) -> MigrationImportReport:
"""Create/migrate the SQLite DB and import existing JSON state once."""
- paths.ensure_data_dirs()
target_db = (db_path or paths.get_sqlite_state_path()).expanduser().resolve()
target_state_dir = (state_dir or paths.get_state_dir()).expanduser().resolve()
+ _ensure_sqlite_target_dirs(
+ target_state_dir=target_state_dir,
+ target_db=target_db,
+ use_default_dirs=db_path is None and state_dir is None,
+ )
lock_path = target_state_dir / "migration.lock"
with MigrationFileLock(lock_path):
@@ -94,10 +98,38 @@ def ensure_sqlite_state(
engine.dispose()
-def resolve_primary_platform_from_config() -> str | None:
+def _ensure_sqlite_target_dirs(*, target_state_dir: Path, target_db: Path, use_default_dirs: bool) -> None:
+ if use_default_dirs:
+ paths.ensure_data_dirs()
+ return
+ target_state_dir.mkdir(parents=True, exist_ok=True)
+ target_db.parent.mkdir(parents=True, exist_ok=True)
+
+
+def resolve_primary_platform_from_config(state_dir: Path | None = None) -> str | None:
"""Best-effort primary platform lookup for store-level SQLite bootstrap."""
+ config_paths = _candidate_config_paths(state_dir)
+ for config_path in config_paths:
+ platform = _resolve_primary_platform_from_config_path(config_path)
+ if platform is not None:
+ return platform
+ return None
+
+
+def _candidate_config_paths(state_dir: Path | None) -> list[Path]:
+ if state_dir is None:
+ return [paths.get_config_path()]
+
+ state_path = Path(state_dir).expanduser().resolve()
+ candidates: list[Path] = []
+ if state_path.name == "state":
+ candidates.append(state_path.parent / "config" / "config.json")
+ candidates.append(state_path / "config.json")
+ return candidates
+
+
+def _resolve_primary_platform_from_config_path(config_path: Path) -> str | None:
try:
- config_path = paths.get_config_path()
if not config_path.exists():
return None
payload = json.loads(config_path.read_text(encoding="utf-8"))
diff --git a/tests/test_sqlite_sessions_store.py b/tests/test_sqlite_sessions_store.py
index ac8a5eaa2..feccbe1d5 100644
--- a/tests/test_sqlite_sessions_store.py
+++ b/tests/test_sqlite_sessions_store.py
@@ -131,6 +131,44 @@ def test_sessions_store_bootstrap_uses_config_primary_platform(tmp_path: Path, m
store.close()
+def test_sessions_store_custom_path_uses_sibling_config_primary_platform(tmp_path: Path) -> None:
+ root = tmp_path / "custom-home"
+ state_dir = root / "state"
+ config_dir = root / "config"
+ state_dir.mkdir(parents=True)
+ config_dir.mkdir(parents=True)
+ (config_dir / "config.json").write_text(
+ json.dumps({"platform": "lark", "platforms": {"enabled": ["lark"], "primary": "lark"}}),
+ encoding="utf-8",
+ )
+ sessions_path = state_dir / "sessions.json"
+ sessions_path.write_text(
+ json.dumps(
+ {
+ "session_mappings": {"chat-2": {"codex": {"1774074591.762089:/repo": "session-2"}}},
+ "active_polls": {
+ "oc-2": {
+ "opencode_session_id": "oc-2",
+ "base_session_id": "base-2",
+ "channel_id": "chat-2",
+ "thread_id": "1774074591.762089",
+ "settings_key": "chat-2",
+ "working_path": "/repo",
+ }
+ },
+ }
+ ),
+ encoding="utf-8",
+ )
+
+ store = SessionsStore(sessions_path)
+ try:
+ assert "lark::chat-2" in store.state.session_mappings
+ assert store.state.active_polls["oc-2"]["platform"] == "lark"
+ finally:
+ store.close()
+
+
def test_sessions_store_preserves_legacy_non_string_session_values(tmp_path: Path) -> None:
sessions_path = tmp_path / "sessions.json"
store = SessionsStore(sessions_path)
diff --git a/tests/test_sqlite_settings_store.py b/tests/test_sqlite_settings_store.py
index 3e33345ef..9989e81c3 100644
--- a/tests/test_sqlite_settings_store.py
+++ b/tests/test_sqlite_settings_store.py
@@ -99,3 +99,43 @@ def test_settings_store_bootstrap_uses_config_primary_platform(tmp_path: Path, m
finally:
sessions.close()
store.close()
+
+
+def test_settings_store_custom_path_uses_sibling_config_primary_platform(tmp_path: Path) -> None:
+ root = tmp_path / "custom-home"
+ state_dir = root / "state"
+ config_dir = root / "config"
+ state_dir.mkdir(parents=True)
+ config_dir.mkdir(parents=True)
+ (config_dir / "config.json").write_text(
+ json.dumps({"platform": "discord", "platforms": {"enabled": ["discord"], "primary": "discord"}}),
+ encoding="utf-8",
+ )
+ (state_dir / "sessions.json").write_text(
+ json.dumps(
+ {
+ "session_mappings": {"G456": {"codex": {"1774074591.762089:/repo": "session-1"}}},
+ "active_polls": {
+ "oc-2": {
+ "opencode_session_id": "oc-2",
+ "base_session_id": "base-2",
+ "channel_id": "G456",
+ "thread_id": "1774074591.762089",
+ "settings_key": "G456",
+ "working_path": "/repo",
+ }
+ },
+ }
+ ),
+ encoding="utf-8",
+ )
+
+ store = SettingsStore(state_dir / "settings.json")
+ sessions = SQLiteSessionsService(state_dir / "vibe.sqlite")
+ try:
+ state = sessions.load_state()
+ assert "discord::G456" in state.session_mappings
+ assert state.active_polls["oc-2"]["platform"] == "discord"
+ finally:
+ sessions.close()
+ store.close()
diff --git a/tests/test_sqlite_state_migration.py b/tests/test_sqlite_state_migration.py
index a579e1dd4..3e57c50b8 100644
--- a/tests/test_sqlite_state_migration.py
+++ b/tests/test_sqlite_state_migration.py
@@ -6,6 +6,7 @@ from pathlib import Path
import pytest
+from config import paths
from storage.db import SqliteInvalidationProbe, create_sqlite_engine
from storage.importer import ensure_sqlite_state
from storage.migrations import run_migrations
@@ -125,6 +126,20 @@ def test_ensure_sqlite_state_imports_json_once(tmp_path: Path) -> None:
assert second.counts == first.counts
+def test_custom_state_paths_do_not_bootstrap_default_home(tmp_path: Path, monkeypatch) -> None:
+ state_dir = tmp_path / "isolated-state"
+
+ def fail_default_bootstrap() -> None:
+ raise AssertionError("default Vibe home should not be bootstrapped for custom state paths")
+
+ monkeypatch.setattr(paths, "ensure_data_dirs", fail_default_bootstrap)
+
+ report = ensure_sqlite_state(db_path=state_dir / "vibe.sqlite", state_dir=state_dir)
+
+ assert report.imported is True
+ assert (state_dir / "vibe.sqlite").exists()
+
+
def test_legacy_sessions_import_requires_platform_when_not_inferable(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
state_dir.mkdir()