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.

feat(workbench): add messages.source provenance column (user/agent/harness)

cyh10b0b835a0c9
diff --git a/storage/alembic/versions/20260531_0010_messages_source.py b/storage/alembic/versions/20260531_0010_messages_source.py
new file mode 100644
index 000000000..009b51d74
--- /dev/null
+++ b/storage/alembic/versions/20260531_0010_messages_source.py
@@ -0,0 +1,65 @@
+"""add messages.source column
+
+Adds a ``source`` column to ``messages`` recording the ORIGIN of each message
+(``user`` / ``agent`` / ``harness``), distinct from the coarse ``author`` role:
+a Harness-triggered prompt has ``author='user'`` but ``source='harness'``, so
+the origin cannot be derived from ``author`` alone. ``author_name`` carries the
+display name (username / agent_name / ``task``|``watch``) and ``author_id`` the
+precise id (user id / task or watch id / agent id).
+
+Backfills existing rows from ``author`` (user -> user; agent/system -> agent);
+``harness`` rows only appear going forward.
+
+Revision ID: 20260531_0010
+Revises: 20260531_0009
+Create Date: 2026-05-31
+"""
+
+from __future__ import annotations
+
+import sqlalchemy as sa
+from alembic import op
+
+revision = "20260531_0010"
+down_revision = "20260531_0009"
+branch_labels = None
+depends_on = None
+
+
+def _columns(bind, table: str) -> set[str]:
+ return {row[1] for row in bind.exec_driver_sql(f'pragma table_info("{table}")')}
+
+
+def _tables(bind) -> set[str]:
+ return {row[0] for row in bind.exec_driver_sql("select name from sqlite_master where type = 'table'")}
+
+
+def upgrade() -> None:
+ bind = op.get_bind()
+ if "messages" not in _tables(bind):
+ return
+ if "source" in _columns(bind, "messages"):
+ return
+
+ # Nullable: provenance is best-effort; historical rows are backfilled from
+ # ``author`` below, and ``harness`` can only be set on new inserts.
+ op.add_column("messages", sa.Column("source", sa.String(), nullable=True))
+
+ bind.exec_driver_sql(
+ """
+ update messages
+ set source = case
+ when author = 'user' then 'user'
+ when author = 'agent' then 'agent'
+ when author = 'system' then 'agent'
+ else null
+ end
+ where source is null
+ """
+ )
+
+
+def downgrade() -> None:
+ bind = op.get_bind()
+ if "messages" in _tables(bind) and "source" in _columns(bind, "messages"):
+ op.drop_column("messages", "source")
diff --git a/storage/messages_service.py b/storage/messages_service.py
index 6d1c279c4..09fefbe49 100644
--- a/storage/messages_service.py
+++ b/storage/messages_service.py
@@ -59,6 +59,7 @@ def _row_to_payload(row: dict[str, Any]) -> dict[str, Any]:
"type": row.get("type"),
"author_id": row.get("author_id"),
"author_name": row.get("author_name"),
+ "source": row.get("source"),
"native_message_id": row.get("native_message_id"),
"parent_native_message_id": row.get("parent_native_message_id"),
"text": row.get("content_text") or content.get("text") or "",
@@ -84,6 +85,7 @@ def append(
metadata: Optional[dict[str, Any]] = None,
author_id: Optional[str] = None,
author_name: Optional[str] = None,
+ source: Optional[str] = None,
native_message_id: Optional[str] = None,
parent_native_message_id: Optional[str] = None,
delivered_at: Optional[str] = None,
@@ -119,6 +121,7 @@ def append(
"type": resolved_type,
"author_id": author_id,
"author_name": author_name,
+ "source": source,
"native_message_id": native_message_id,
"parent_native_message_id": parent_native_message_id,
"content_text": plain,
diff --git a/storage/migrations.py b/storage/migrations.py
index b19817e9c..ef9c5362d 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 = "20260531_0009"
+LATEST_SCHEMA_REVISION = "20260531_0010"
REMOVE_LEGACY_DEFAULT_AGENT_REVISION = "20260530_0008"
INITIAL_TABLES = {
"state_meta",
@@ -29,7 +29,7 @@ HEAD_REQUIRED_COLUMNS = {
"agents": {"enabled"},
"scope_settings": {"agent_name"},
"agent_sessions": {"agent_id", "agent_name"},
- "messages": {"type"},
+ "messages": {"type", "source"},
"run_definitions": {
"deleted_at",
"definition_type",
@@ -358,6 +358,22 @@ def _repair_head_required_columns(conn: sqlite3.Connection, tables: set[str]) ->
)
changed = True
+ # messages.source (20260531_0010): origin (user/agent/harness), distinct
+ # from author. Mirror the migration's add + backfill for drifted heads.
+ if "messages" in tables and "source" not in _column_names(conn, "messages"):
+ conn.execute('alter table "messages" add column "source" VARCHAR')
+ conn.execute(
+ """
+ update messages set source = case
+ when author = 'user' then 'user'
+ when author = 'agent' then 'agent'
+ when author = 'system' then 'agent'
+ else null
+ end
+ """
+ )
+ changed = True
+
_ensure_new_background_indexes(conn)
return changed
diff --git a/storage/models.py b/storage/models.py
index f8aaca171..9876f9f9e 100644
--- a/storage/models.py
+++ b/storage/models.py
@@ -293,6 +293,11 @@ messages = Table(
Column("type", String, nullable=False, server_default="assistant"),
Column("author_id", String, nullable=True),
Column("author_name", Text, nullable=True),
+ # Origin of the message (user / agent / harness), distinct from the coarse
+ # ``author`` role — a Harness-triggered prompt is author='user' but
+ # source='harness'. ``author_name`` holds the display name (username /
+ # agent_name / task|watch), ``author_id`` the precise id.
+ Column("source", String, nullable=True),
Column("native_message_id", String, nullable=True),
Column("parent_native_message_id", String, nullable=True),
Column("content_text", Text, nullable=True),
diff --git a/tests/test_sqlite_state_migration.py b/tests/test_sqlite_state_migration.py
index 97e798cf2..e2c0b1998 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 = "20260531_0009"
+HEAD_REVISION = "20260531_0010"
def test_run_migrations_creates_initial_schema(tmp_path: Path) -> None: