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.

perf(workbench): index inbox message scans (#474)

Call Whiteda805d83a575
diff --git a/docs/plans/inbox-query-indexes.md b/docs/plans/inbox-query-indexes.md
new file mode 100644
index 000000000..c45f4424f
--- /dev/null
+++ b/docs/plans/inbox-query-indexes.md
@@ -0,0 +1,27 @@
+# Inbox Query Indexes Plan
+
+## Goal
+
+Speed up `GET /api/inbox`, which drives the Workbench inbox feed and sidebar
+badges. The feed computes latest activity, latest terminal agent reply, latest
+user send, and unread counts from the `messages` table.
+
+## Evidence
+
+`EXPLAIN QUERY PLAN` on a seeded SQLite store showed the inbox query reading
+`messages` through the generic `(platform, session_id, created_at, id)` index
+and building temporary B-trees for the window-function orderings.
+
+## Scope
+
+- Add inbox-specific SQLite indexes for the three ranked message scans:
+ conversation activity, terminal agent replies, and user sends.
+- Add the indexes to SQLAlchemy metadata, Alembic migration, and the head-schema
+ repair helper used before stamping existing schemas.
+- Keep query semantics unchanged.
+
+## Validation
+
+- Migration/index tests.
+- Message service inbox tests.
+- Ruff on touched Python files.
diff --git a/storage/alembic/versions/20260606_0019_inbox_query_indexes.py b/storage/alembic/versions/20260606_0019_inbox_query_indexes.py
new file mode 100644
index 000000000..de6248b48
--- /dev/null
+++ b/storage/alembic/versions/20260606_0019_inbox_query_indexes.py
@@ -0,0 +1,40 @@
+"""add inbox query indexes
+
+Revision ID: 20260606_0019
+Revises: 20260606_0018
+Create Date: 2026-06-06
+"""
+
+from __future__ import annotations
+
+from alembic import op
+
+revision = "20260606_0019"
+down_revision = "20260606_0018"
+branch_labels = None
+depends_on = None
+
+
+def upgrade() -> None:
+ bind = op.get_bind()
+ bind.exec_driver_sql(
+ "create index if not exists ix_messages_inbox_activity "
+ "on messages (platform, session_id, created_at desc, id desc) "
+ "where session_id is not null and type not in ('queued', 'draft', 'pending')"
+ )
+ bind.exec_driver_sql(
+ "create index if not exists ix_messages_inbox_agent_reply "
+ "on messages (platform, session_id, created_at desc, id desc) "
+ "where session_id is not null and type in ('result', 'notify', 'error')"
+ )
+ bind.exec_driver_sql(
+ "create index if not exists ix_messages_inbox_user_send "
+ "on messages (platform, session_id, created_at desc, id desc) "
+ "where session_id is not null and author = 'user' and type not in ('queued', 'draft', 'pending')"
+ )
+
+
+def downgrade() -> None:
+ op.drop_index("ix_messages_inbox_user_send", table_name="messages")
+ op.drop_index("ix_messages_inbox_agent_reply", table_name="messages")
+ op.drop_index("ix_messages_inbox_activity", table_name="messages")
diff --git a/storage/migrations.py b/storage/migrations.py
index 17796657b..ca138b326 100644
--- a/storage/migrations.py
+++ b/storage/migrations.py
@@ -12,7 +12,7 @@ from config import paths
from storage.db import create_sqlite_engine, sqlite_url
INITIAL_REVISION = "20260501_0001"
-LATEST_SCHEMA_REVISION = "20260606_0018"
+LATEST_SCHEMA_REVISION = "20260606_0019"
REMOVE_LEGACY_DEFAULT_AGENT_REVISION = "20260530_0008"
INITIAL_TABLES = {
"state_meta",
@@ -495,6 +495,21 @@ def _ensure_messages_query_indexes(conn: sqlite3.Connection, tables: set[str]) -
'create index if not exists ix_messages_mark_read '
'on messages (session_id, author, read_at, created_at, id)'
)
+ conn.execute(
+ "create index if not exists ix_messages_inbox_activity "
+ "on messages (platform, session_id, created_at desc, id desc) "
+ "where session_id is not null and type not in ('queued', 'draft', 'pending')"
+ )
+ conn.execute(
+ "create index if not exists ix_messages_inbox_agent_reply "
+ "on messages (platform, session_id, created_at desc, id desc) "
+ "where session_id is not null and type in ('result', 'notify', 'error')"
+ )
+ conn.execute(
+ "create index if not exists ix_messages_inbox_user_send "
+ "on messages (platform, session_id, created_at desc, id desc) "
+ "where session_id is not null and author = 'user' and type not in ('queued', 'draft', 'pending')"
+ )
def _ensure_head_indexes(conn: sqlite3.Connection, tables: set[str]) -> None:
diff --git a/storage/models.py b/storage/models.py
index bd83f82a5..3d1abddec 100644
--- a/storage/models.py
+++ b/storage/models.py
@@ -11,6 +11,7 @@ from sqlalchemy import (
Table,
Text,
UniqueConstraint,
+ text,
)
metadata = MetaData()
@@ -320,6 +321,30 @@ messages = Table(
Index("ix_messages_platform_session_created_id", "platform", "session_id", "created_at", "id"),
Index("ix_messages_unread_session", "platform", "type", "author", "read_at", "session_id"),
Index("ix_messages_mark_read", "session_id", "author", "read_at", "created_at", "id"),
+ Index(
+ "ix_messages_inbox_activity",
+ "platform",
+ "session_id",
+ text("created_at desc"),
+ text("id desc"),
+ sqlite_where=text("session_id is not null and type not in ('queued', 'draft', 'pending')"),
+ ),
+ Index(
+ "ix_messages_inbox_agent_reply",
+ "platform",
+ "session_id",
+ text("created_at desc"),
+ text("id desc"),
+ sqlite_where=text("session_id is not null and type in ('result', 'notify', 'error')"),
+ ),
+ Index(
+ "ix_messages_inbox_user_send",
+ "platform",
+ "session_id",
+ text("created_at desc"),
+ text("id desc"),
+ sqlite_where=text("session_id is not null and author = 'user' and type not in ('queued', 'draft', 'pending')"),
+ ),
Index("ix_messages_scope_created", "scope_id", "created_at"),
Index("ix_messages_scope_unread", "scope_id", "read_at"),
Index("ix_messages_author_created", "author", "created_at"),
diff --git a/tests/test_sqlite_state_migration.py b/tests/test_sqlite_state_migration.py
index b2ef42ef6..e3a3dd333 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 = "20260606_0018"
+HEAD_REVISION = "20260606_0019"
def test_run_migrations_creates_initial_schema(tmp_path: Path) -> None:
@@ -53,6 +53,9 @@ def test_run_migrations_creates_initial_schema(tmp_path: Path) -> None:
assert "ix_messages_platform_session_created_id" in message_indexes
assert "ix_messages_unread_session" in message_indexes
assert "ix_messages_mark_read" in message_indexes
+ assert "ix_messages_inbox_activity" in message_indexes
+ assert "ix_messages_inbox_agent_reply" in message_indexes
+ assert "ix_messages_inbox_user_send" in message_indexes
agent_session_indexes = {
row[1]
for row in conn.execute(
@@ -130,6 +133,9 @@ def test_run_migrations_repairs_head_indexes_before_stamping_head(tmp_path: Path
conn.execute("drop index if exists ix_messages_platform_session_created_id")
conn.execute("drop index if exists ix_messages_unread_session")
conn.execute("drop index if exists ix_messages_mark_read")
+ conn.execute("drop index if exists ix_messages_inbox_activity")
+ conn.execute("drop index if exists ix_messages_inbox_agent_reply")
+ conn.execute("drop index if exists ix_messages_inbox_user_send")
conn.commit()
assert conn.execute("select name from sqlite_master where name = 'alembic_version'").fetchone() is None
@@ -155,6 +161,9 @@ def test_run_migrations_repairs_head_indexes_before_stamping_head(tmp_path: Path
assert "ix_messages_platform_session_created_id" in message_indexes
assert "ix_messages_unread_session" in message_indexes
assert "ix_messages_mark_read" in message_indexes
+ assert "ix_messages_inbox_activity" in message_indexes
+ assert "ix_messages_inbox_agent_reply" in message_indexes
+ assert "ix_messages_inbox_user_send" in message_indexes
assert "ix_agent_sessions_scope_status_activity" in agent_session_indexes
@@ -368,6 +377,9 @@ def test_background_tables_ready_requires_messages_type(tmp_path: Path) -> None:
conn.execute("drop index if exists ix_messages_session_type")
conn.execute("drop index if exists ix_messages_session_type_created_id")
conn.execute("drop index if exists ix_messages_unread_session")
+ conn.execute("drop index if exists ix_messages_inbox_activity")
+ conn.execute("drop index if exists ix_messages_inbox_agent_reply")
+ conn.execute("drop index if exists ix_messages_inbox_user_send")
conn.execute('alter table "messages" drop column "type"')
conn.execute("create table if not exists alembic_version (version_num varchar(32) not null)")
conn.execute("delete from alembic_version")