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): remove legacy default agent

cyh6f569c5bce4e
diff --git a/storage/alembic/versions/20260530_0008_remove_legacy_default_agent.py b/storage/alembic/versions/20260530_0008_remove_legacy_default_agent.py
new file mode 100644
index 000000000..87e11ee47
--- /dev/null
+++ b/storage/alembic/versions/20260530_0008_remove_legacy_default_agent.py
@@ -0,0 +1,255 @@
+"""remove legacy builtin default Agent
+
+Revision ID: 20260530_0008
+Revises: 20260529_0007
+Create Date: 2026-05-30
+"""
+
+from __future__ import annotations
+
+import json
+from datetime import datetime, timezone
+from typing import Any
+
+from alembic import op
+
+revision = "20260530_0008"
+down_revision = "20260529_0007"
+branch_labels = None
+depends_on = None
+
+_BACKENDS = {"opencode", "claude", "codex"}
+_LEGACY_DEFAULT_AGENT_NAME = "default"
+_DEFAULT_AGENT_META_KEY = "default_agent_name"
+
+
+def upgrade() -> None:
+ bind = op.get_bind()
+ tables = _tables(bind)
+ if "agents" not in tables:
+ return
+
+ legacy = _legacy_default_agent(bind)
+ if legacy is None or not _is_unmodified_legacy_default_agent(legacy):
+ return
+
+ backend = str(legacy["backend"])
+ target = _backend_default_agent(bind, backend)
+ if target is None:
+ return
+
+ _retarget_agent_references(bind, tables, legacy, target)
+ _retarget_default_agent_pointer(bind, tables, target["name"])
+ bind.exec_driver_sql("delete from agents where id = ?", (legacy["id"],))
+
+
+def downgrade() -> None:
+ pass
+
+
+def _tables(bind) -> set[str]:
+ return {row[0] for row in bind.exec_driver_sql("select name from sqlite_master where type = 'table'")}
+
+
+def _columns(bind, table: str) -> set[str]:
+ return {row[1] for row in bind.exec_driver_sql(f'pragma table_info("{table}")')}
+
+
+def _legacy_default_agent(bind) -> dict[str, Any] | None:
+ row = bind.exec_driver_sql(
+ """
+ select id, name, normalized_name, description, backend, model, reasoning_effort,
+ system_prompt, enabled, source, source_ref, metadata_json
+ from agents
+ where normalized_name = ?
+ limit 1
+ """,
+ (_LEGACY_DEFAULT_AGENT_NAME,),
+ ).fetchone()
+ if row is None:
+ return None
+ return {
+ "id": row[0],
+ "name": row[1],
+ "normalized_name": row[2],
+ "description": row[3],
+ "backend": row[4],
+ "model": row[5],
+ "reasoning_effort": row[6],
+ "system_prompt": row[7],
+ "enabled": row[8],
+ "source": row[9],
+ "source_ref": row[10],
+ "metadata": _json_loads(row[11], {}),
+ }
+
+
+def _is_unmodified_legacy_default_agent(agent: dict[str, Any]) -> bool:
+ metadata = agent["metadata"]
+ if not isinstance(metadata, dict):
+ return False
+ if metadata != {"builtin": True}:
+ return False
+ return (
+ agent["name"] == _LEGACY_DEFAULT_AGENT_NAME
+ and agent["normalized_name"] == _LEGACY_DEFAULT_AGENT_NAME
+ and agent["backend"] in _BACKENDS
+ and agent["source"] == "builtin"
+ and _is_empty(agent["source_ref"])
+ and agent["description"] == "Default Vibe Remote agent."
+ and _is_empty(agent["model"])
+ and _is_empty(agent["reasoning_effort"])
+ and _is_empty(agent["system_prompt"])
+ )
+
+
+def _backend_default_agent(bind, backend: str) -> dict[str, Any] | None:
+ row = bind.exec_driver_sql(
+ """
+ select id, name, backend, enabled, source, metadata_json
+ from agents
+ where normalized_name = ?
+ limit 1
+ """,
+ (backend,),
+ ).fetchone()
+ if row is None:
+ return None
+
+ agent_id, name, existing_backend, enabled, source, metadata_json = row
+ metadata = _json_loads(metadata_json, {})
+ if (
+ existing_backend != backend
+ or not bool(enabled)
+ or source != "builtin"
+ or not isinstance(metadata, dict)
+ or metadata.get("backend") != backend
+ or not bool(metadata.get("builtin_default") or metadata.get("lock_delete"))
+ ):
+ return None
+ return {"id": agent_id, "name": str(name), "backend": existing_backend}
+
+
+def _retarget_agent_references(bind, tables: set[str], legacy: dict[str, Any], target: dict[str, Any]) -> None:
+ legacy_name = str(legacy["name"])
+ legacy_id = str(legacy["id"])
+ target_name = str(target["name"])
+ target_id = str(target["id"])
+
+ if "scope_settings" in tables:
+ columns = _columns(bind, "scope_settings")
+ if "agent_name" in columns:
+ _retarget_scope_settings(bind, legacy_name, target_name)
+ if "agent_variant" in columns:
+ _retarget_agent_variant(bind, "scope_settings", legacy_name, target_name)
+ if "agent_sessions" in tables:
+ columns = _columns(bind, "agent_sessions")
+ _retarget_agent_table(bind, "agent_sessions", columns, legacy_name, target_name, legacy_id, target_id)
+ if "run_definitions" in tables:
+ columns = _columns(bind, "run_definitions")
+ _retarget_agent_table(bind, "run_definitions", columns, legacy_name, target_name, legacy_id, target_id)
+ if "agent_runs" in tables:
+ columns = _columns(bind, "agent_runs")
+ _retarget_agent_table(bind, "agent_runs", columns, legacy_name, target_name, legacy_id, target_id)
+
+
+def _retarget_scope_settings(bind, legacy_name: str, target_name: str) -> None:
+ rows = bind.exec_driver_sql(
+ """
+ select scope_id, settings_json
+ from scope_settings
+ where agent_name = ?
+ """,
+ (legacy_name,),
+ ).fetchall()
+ for scope_id, settings_json in rows:
+ bind.exec_driver_sql(
+ """
+ update scope_settings
+ set agent_name = ?, settings_json = ?
+ where scope_id = ?
+ """,
+ (target_name, _settings_json_retarget_agent(settings_json, legacy_name, target_name), scope_id),
+ )
+
+
+def _retarget_agent_table(
+ bind,
+ table: str,
+ columns: set[str],
+ legacy_name: str,
+ target_name: str,
+ legacy_id: str,
+ target_id: str,
+) -> None:
+ if "agent_name" in columns:
+ bind.exec_driver_sql(
+ f'update "{table}" set agent_name = ? where agent_name = ?',
+ (target_name, legacy_name),
+ )
+ if "agent_id" in columns:
+ bind.exec_driver_sql(
+ f'update "{table}" set agent_id = ? where agent_id = ?',
+ (target_id, legacy_id),
+ )
+ if "agent_variant" in columns:
+ _retarget_agent_variant(bind, table, legacy_name, target_name)
+
+
+def _retarget_agent_variant(bind, table: str, legacy_name: str, target_name: str) -> None:
+ bind.exec_driver_sql(
+ f'update "{table}" set agent_variant = ? where agent_variant = ?',
+ (target_name, legacy_name),
+ )
+
+
+def _retarget_default_agent_pointer(bind, tables: set[str], target_name: str) -> None:
+ if "state_meta" not in tables:
+ return
+ row = bind.exec_driver_sql(
+ "select value_json from state_meta where key = ? limit 1",
+ (_DEFAULT_AGENT_META_KEY,),
+ ).fetchone()
+ if row is None or _json_loads(row[0], None) != _LEGACY_DEFAULT_AGENT_NAME:
+ return
+ now = _utc_now_iso()
+ bind.exec_driver_sql(
+ "update state_meta set value_json = ?, updated_at = ? where key = ?",
+ (_json_dumps(target_name), now, _DEFAULT_AGENT_META_KEY),
+ )
+
+
+def _settings_json_retarget_agent(value: str | None, legacy_name: str, target_name: str) -> str:
+ payload = _json_loads(value, None)
+ if not isinstance(payload, dict):
+ return value or "{}"
+ routing = payload.get("routing")
+ if not isinstance(routing, dict):
+ return _json_dumps(payload)
+ if routing.get("agent_name") == legacy_name:
+ routing["agent_name"] = target_name
+ if routing.get("agent") == legacy_name:
+ routing["agent"] = target_name
+ payload["routing"] = routing
+ return _json_dumps(payload)
+
+
+def _json_loads(value: str | None, default: Any) -> Any:
+ if not value:
+ return default
+ try:
+ return json.loads(value)
+ except (TypeError, ValueError):
+ return default
+
+
+def _json_dumps(value: object) -> str:
+ return json.dumps(value, ensure_ascii=False, separators=(",", ":"))
+
+
+def _is_empty(value: Any) -> bool:
+ return value is None or value == ""
+
+
+def _utc_now_iso() -> str:
+ return datetime.now(timezone.utc).isoformat()
diff --git a/tests/test_sqlite_state_migration.py b/tests/test_sqlite_state_migration.py
index bfc44672f..2a83a35b4 100644
--- a/tests/test_sqlite_state_migration.py
+++ b/tests/test_sqlite_state_migration.py
@@ -16,7 +16,7 @@ from storage.models import metadata
from storage.settings_service import SQLiteSettingsService
-HEAD_REVISION = "20260529_0007"
+HEAD_REVISION = "20260530_0008"
def test_run_migrations_creates_initial_schema(tmp_path: Path) -> None:
@@ -253,6 +253,222 @@ def test_run_migrations_backfills_existing_session_policy_only_for_targeted_defi
assert rows["without-target"] is None
+def test_run_migrations_removes_legacy_builtin_default_agent(tmp_path: Path) -> None:
+ db_path = tmp_path / "vibe.sqlite"
+ engine = create_sqlite_engine(db_path)
+ try:
+ metadata.create_all(engine)
+ finally:
+ engine.dispose()
+
+ with sqlite3.connect(db_path) as conn:
+ conn.executescript(
+ """
+ insert into agents (
+ id, name, normalized_name, description, backend, model, reasoning_effort,
+ system_prompt, enabled, source, source_ref, metadata_json, created_at, updated_at
+ ) values
+ (
+ 'agent-default', 'default', 'default', 'Default Vibe Remote agent.', 'opencode',
+ null, null, null, 0, 'builtin', null, '{"builtin":true}', 'now', 'now'
+ ),
+ (
+ 'agent-opencode', 'opencode', 'opencode', 'Default opencode agent.', 'opencode',
+ null, null, null, 1, 'builtin', null,
+ '{"builtin":true,"builtin_default":true,"lock_delete":true,"backend":"opencode","backend_enabled":true}',
+ 'now', 'now'
+ );
+ insert into state_meta (key, value_json, updated_at)
+ values ('default_agent_name', '"default"', 'now');
+ insert into scopes (
+ id, platform, scope_type, native_id, parent_scope_id, display_name, native_type,
+ is_private, supports_threads, metadata_json, first_seen_at, last_seen_at, updated_at
+ ) values (
+ 'slack::channel::C1', 'slack', 'channel', 'C1', null, null, null, 0, 1, '{}', 'now', 'now', 'now'
+ );
+ insert into scope_settings (
+ scope_id, enabled, role, workdir, agent_name, agent_backend, agent_variant,
+ model, reasoning_effort, require_mention, settings_version, settings_json, created_at, updated_at
+ ) values (
+ 'slack::channel::C1', 1, null, '/repo', 'default', 'opencode', 'default', null, null, null, 1,
+ '{"routing":{"agent_name":"default","agent":"default","agent_backend":"opencode"}}', 'now', 'now'
+ );
+ insert into agent_sessions (
+ id, scope_id, agent_id, agent_name, agent_backend, agent_variant, model, reasoning_effort,
+ session_anchor, workdir, native_session_id, title, status, metadata_json, created_at, updated_at
+ ) values (
+ 'session-1', 'slack::channel::C1', 'agent-default', 'default', 'opencode', 'default',
+ null, null, 'thread-1', '/repo', 'native-1', null, 'active', '{}', 'now', 'now'
+ );
+ insert into run_definitions (
+ id, definition_type, name, agent_name, session_policy, message, enabled, created_at, updated_at,
+ metadata_json
+ ) values (
+ 'definition-1', 'task', 'task', 'default', 'new', 'hello', 1, 'now', 'now', '{}'
+ );
+ insert into agent_runs (
+ id, run_type, status, agent_name, agent_id, agent_backend, message, created_at, updated_at,
+ metadata_json, cancel_requested
+ ) values (
+ 'run-1', 'task', 'queued', 'default', 'agent-default', 'opencode', 'hello', 'now', 'now', '{}', 0
+ );
+ create table alembic_version (version_num varchar(32) not null);
+ insert into alembic_version values ('20260529_0007');
+ """
+ )
+ conn.commit()
+
+ run_migrations(db_path)
+
+ with sqlite3.connect(db_path) as conn:
+ agents = dict(conn.execute("select name, id from agents"))
+ default_pointer = conn.execute(
+ "select value_json from state_meta where key = 'default_agent_name'"
+ ).fetchone()[0]
+ scope_agent, scope_variant, settings_json = conn.execute(
+ "select agent_name, agent_variant, settings_json from scope_settings where scope_id = 'slack::channel::C1'"
+ ).fetchone()
+ session_agent = conn.execute(
+ "select agent_id, agent_name, agent_variant from agent_sessions where id = 'session-1'"
+ ).fetchone()
+ definition_agent = conn.execute(
+ "select agent_name from run_definitions where id = 'definition-1'"
+ ).fetchone()[0]
+ run_agent = conn.execute(
+ "select agent_id, agent_name from agent_runs where id = 'run-1'"
+ ).fetchone()
+ version = conn.execute("select version_num from alembic_version").fetchone()
+
+ payload = json.loads(settings_json)
+ assert version == (HEAD_REVISION,)
+ assert "default" not in agents
+ assert agents["opencode"] == "agent-opencode"
+ assert json.loads(default_pointer) == "opencode"
+ assert scope_agent == "opencode"
+ assert scope_variant == "opencode"
+ assert payload["routing"]["agent_name"] == "opencode"
+ assert payload["routing"]["agent"] == "opencode"
+ assert session_agent == ("agent-opencode", "opencode", "opencode")
+ assert definition_agent == "opencode"
+ assert run_agent == ("agent-opencode", "opencode")
+
+
+def test_run_migrations_skips_user_owned_default_agent(tmp_path: Path) -> None:
+ db_path = tmp_path / "vibe.sqlite"
+ engine = create_sqlite_engine(db_path)
+ try:
+ metadata.create_all(engine)
+ finally:
+ engine.dispose()
+
+ with sqlite3.connect(db_path) as conn:
+ conn.executescript(
+ """
+ insert into agents (
+ id, name, normalized_name, description, backend, model, reasoning_effort,
+ system_prompt, enabled, source, source_ref, metadata_json, created_at, updated_at
+ ) values (
+ 'agent-default', 'default', 'default', 'User default agent.', 'opencode',
+ null, null, 'custom prompt', 1, 'user', null, '{}', 'now', 'now'
+ );
+ create table alembic_version (version_num varchar(32) not null);
+ insert into alembic_version values ('20260529_0007');
+ """
+ )
+ conn.commit()
+
+ run_migrations(db_path)
+
+ with sqlite3.connect(db_path) as conn:
+ default_agent = conn.execute(
+ "select source, system_prompt from agents where normalized_name = 'default'"
+ ).fetchone()
+ version = conn.execute("select version_num from alembic_version").fetchone()
+
+ assert version == (HEAD_REVISION,)
+ assert default_agent == ("user", "custom prompt")
+
+
+def test_run_migrations_skips_legacy_default_when_backend_target_is_user_agent(tmp_path: Path) -> None:
+ db_path = tmp_path / "vibe.sqlite"
+ engine = create_sqlite_engine(db_path)
+ try:
+ metadata.create_all(engine)
+ finally:
+ engine.dispose()
+
+ with sqlite3.connect(db_path) as conn:
+ conn.executescript(
+ """
+ insert into agents (
+ id, name, normalized_name, description, backend, model, reasoning_effort,
+ system_prompt, enabled, source, source_ref, metadata_json, created_at, updated_at
+ ) values
+ (
+ 'agent-default', 'default', 'default', 'Default Vibe Remote agent.', 'opencode',
+ null, null, null, 1, 'builtin', null, '{"builtin":true}', 'now', 'now'
+ ),
+ (
+ 'agent-opencode-user', 'opencode', 'opencode', 'User opencode agent.', 'opencode',
+ null, null, 'custom prompt', 1, 'user', null, '{}', 'now', 'now'
+ );
+ create table alembic_version (version_num varchar(32) not null);
+ insert into alembic_version values ('20260529_0007');
+ """
+ )
+ conn.commit()
+
+ run_migrations(db_path)
+
+ with sqlite3.connect(db_path) as conn:
+ rows = dict(conn.execute("select name, source from agents order by name"))
+ version = conn.execute("select version_num from alembic_version").fetchone()
+
+ assert version == (HEAD_REVISION,)
+ assert rows == {"default": "builtin", "opencode": "user"}
+
+
+def test_run_migrations_skips_legacy_default_when_backend_target_is_disabled(tmp_path: Path) -> None:
+ db_path = tmp_path / "vibe.sqlite"
+ engine = create_sqlite_engine(db_path)
+ try:
+ metadata.create_all(engine)
+ finally:
+ engine.dispose()
+
+ with sqlite3.connect(db_path) as conn:
+ conn.executescript(
+ """
+ insert into agents (
+ id, name, normalized_name, description, backend, model, reasoning_effort,
+ system_prompt, enabled, source, source_ref, metadata_json, created_at, updated_at
+ ) values
+ (
+ 'agent-default', 'default', 'default', 'Default Vibe Remote agent.', 'opencode',
+ null, null, null, 1, 'builtin', null, '{"builtin":true}', 'now', 'now'
+ ),
+ (
+ 'agent-opencode-disabled', 'opencode', 'opencode', 'Default opencode agent.', 'opencode',
+ null, null, null, 0, 'builtin', null,
+ '{"builtin":true,"builtin_default":true,"lock_delete":true,"backend":"opencode","backend_enabled":true}',
+ 'now', 'now'
+ );
+ create table alembic_version (version_num varchar(32) not null);
+ insert into alembic_version values ('20260529_0007');
+ """
+ )
+ conn.commit()
+
+ run_migrations(db_path)
+
+ with sqlite3.connect(db_path) as conn:
+ rows = dict(conn.execute("select name, enabled from agents order by name"))
+ version = conn.execute("select version_num from alembic_version").fetchone()
+
+ assert version == (HEAD_REVISION,)
+ assert rows == {"default": 1, "opencode": 0}
+
+
def test_run_migrations_backfills_scope_agent_names_from_legacy_backend(tmp_path: Path) -> None:
db_path = tmp_path / "vibe.sqlite"
engine = create_sqlite_engine(db_path)