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.

refactor(storage): derive partial indexes from catalog (#1049)

Call Whiteda9cedd2e119
diff --git a/storage/models.py b/storage/models.py
index 0513a6877..690fc4f4f 100644
--- a/storage/models.py
+++ b/storage/models.py
@@ -17,6 +17,8 @@ from sqlalchemy import (
text,
)
+from vibe.message_types import build_partial_index_predicate
+
metadata = MetaData()
state_meta = Table(
@@ -382,9 +384,7 @@ messages = Table(
"session_id",
text("created_at desc"),
text("id desc"),
- sqlite_where=text(
- "session_id is not null and type not in ('queued', 'draft', 'pending', 'harness_dedupe', 'silent')"
- ),
+ sqlite_where=text(build_partial_index_predicate("ix_messages_inbox_activity")),
),
Index(
"ix_messages_inbox_agent_reply",
@@ -392,7 +392,7 @@ messages = Table(
"session_id",
text("created_at desc"),
text("id desc"),
- sqlite_where=text("session_id is not null and type in ('result', 'notify', 'error')"),
+ sqlite_where=text(build_partial_index_predicate("ix_messages_inbox_agent_reply")),
),
Index(
"ix_messages_inbox_user_send",
@@ -400,10 +400,7 @@ messages = Table(
"session_id",
text("created_at desc"),
text("id desc"),
- sqlite_where=text(
- "session_id is not null and ((author = 'user' and type = 'user') "
- "or (author = 'harness' and type = 'harness'))"
- ),
+ sqlite_where=text(build_partial_index_predicate("ix_messages_inbox_user_send")),
),
Index("ix_messages_scope_created", "scope_id", "created_at"),
Index("ix_messages_scope_unread", "scope_id", "read_at"),
diff --git a/tests/test_sqlite_state_migration.py b/tests/test_sqlite_state_migration.py
index b5b2f79eb..c494da48c 100644
--- a/tests/test_sqlite_state_migration.py
+++ b/tests/test_sqlite_state_migration.py
@@ -10,6 +10,8 @@ from pathlib import Path
import pytest
from alembic import command
from alembic.script import ScriptDirectory
+from sqlalchemy.dialects.sqlite import dialect as sqlite_dialect
+from sqlalchemy.schema import CreateIndex
from config import paths
from config.v2_settings import ChannelSettings, RoutingSettings, SettingsState, SettingsStore
@@ -19,9 +21,23 @@ from storage import migrations
from storage.migrations import UnsafeDefaultStateMigrationError, background_tables_ready, run_migrations
from storage.models import metadata
from storage.settings_service import SQLiteSettingsService
+from vibe.message_types import build_partial_index_predicate
HEAD_REVISION = "20260726_0037"
+MESSAGE_PARTIAL_INDEX_PREDICATES = {
+ "ix_messages_inbox_activity": (
+ "session_id is not null and type not in "
+ "('queued', 'draft', 'pending', 'harness_dedupe', 'silent')"
+ ),
+ "ix_messages_inbox_agent_reply": (
+ "session_id is not null and type in ('result', 'notify', 'error')"
+ ),
+ "ix_messages_inbox_user_send": (
+ "session_id is not null and ((author = 'user' and type = 'user') "
+ "or (author = 'harness' and type = 'harness'))"
+ ),
+}
def _index_sql(conn: sqlite3.Connection, name: str) -> str:
@@ -30,6 +46,26 @@ def _index_sql(conn: sqlite3.Connection, name: str) -> str:
return str(row[0] or "")
+ ("index_name", "expected_predicate"),
+ tuple(MESSAGE_PARTIAL_INDEX_PREDICATES.items()),
+)
+def test_message_partial_index_ddl_matches_catalog_contract(
+ index_name: str,
+ expected_predicate: str,
+) -> None:
+ index = next(index for index in metadata.tables["messages"].indexes if index.name == index_name)
+
+ catalog_predicate = build_partial_index_predicate(index_name)
+ ddl = str(CreateIndex(index).compile(dialect=sqlite_dialect()))
+
+ assert catalog_predicate == expected_predicate
+ assert ddl == (
+ f"CREATE INDEX {index_name} ON messages "
+ f"(platform, session_id, created_at desc, id desc) WHERE {expected_predicate}"
+ )
+
+
class _Pre335Cursor(sqlite3.Cursor):
def execute(self, sql, parameters=()):
normalized = " ".join(str(sql).upper().split())