Skip to content

feat(storage): tags as proper many-to-many, migrate from JSON metadata #776

Closed

Description

@Sinity

Problem

Tags are currently stored as JSON in conversations.metadata->'$.tags' (queried via json_each(json_extract(metadata, '$.tags')) in conversations_identity.py:128). The DDL already defines normalized tags(id, name UNIQUE) and conversation_tags(conversation_id, tag_id) tables (schema_ddl_archive.py:295-304) but these tables are never inserted into and never queried. The migration-ready schema exists but is dead.

This creates several problems:

  • Tag queries require full table scans with JSON extraction (every conversation, every time)
  • No referential integrity for tags (misspellings create phantom tags)
  • No way to index or efficiently filter by tag
  • The tag surface is inconsistent: CLI uses --tag/--add-tag/--remove-tag, MCP has add_tag/remove_tag/list_tags/bulk_tag_conversations, but the storage is JSON-only

Solution

From v2 architecture decisions: implement tags as proper many-to-many. Queryable fields should be proper columns, not JSON blobs.

Schema already exists:

CREATE TABLE tags (id INTEGER PRIMARY KEY, name TEXT UNIQUE NOT NULL);
CREATE TABLE conversation_tags (conversation_id TEXT, tag_id INTEGER, PRIMARY KEY (conversation_id, tag_id),
    FOREIGN KEY (conversation_id) REFERENCES conversations(conversation_id) ON DELETE CASCADE,
    FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE);

Acceptance criteria

  • Migrate existing JSON tag data to normalized tables
  • list_tags queries the normalized tables, not JSON extraction
  • add_tag/remove_tag write to normalized tables
  • Tag filters (--tag, --exclude-tag) use indexed joins
  • Full table scan on conversations with json_each eliminated
  • CLI, MCP, and daemon API all use the same tag storage

Ref: deep audit P3-7. Ref #750 (closed, schema landed but migration from JSON may not be complete).

Activity

  1. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor

    Resolved by PR #788. Tags normalized via conversation_tags/tags tables with JSON fallback, idempotent migration backfill.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions