Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: add index for `run_id` on messages and `project_id` on agents (#5117)

Sarah Wooders65b33ae27f6b
diff --git a/alembic/versions/3bc3c031fbe4_create_new_runs_table_and_remove_legacy_.py b/alembic/versions/3bc3c031fbe4_create_new_runs_table_and_remove_legacy_.py
new file mode 100644
index 000000000..5339801bc
--- /dev/null
+++ b/alembic/versions/3bc3c031fbe4_create_new_runs_table_and_remove_legacy_.py
@@ -0,0 +1,33 @@
+"""create new runs table and remove legacy tables
+
+Revision ID: 3bc3c031fbe4
+Revises: 567e9fe06270
+Create Date: 2025-10-03 12:10:51.065067
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "3bc3c031fbe4"
+down_revision: Union[str, None] = "567e9fe06270"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.create_index("ix_agents_project_id", "agents", ["project_id"], unique=False)
+ op.create_index("ix_messages_run_id", "messages", ["run_id"], unique=False)
+ # ### end Alembic commands ###
+
+
+def downgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_index("ix_messages_run_id", table_name="messages")
+ op.drop_index("ix_agents_project_id", table_name="agents")
+ # ### end Alembic commands ###
diff --git a/letta/orm/agent.py b/letta/orm/agent.py
index fb6bf782f..782066ab7 100644
--- a/letta/orm/agent.py
+++ b/letta/orm/agent.py
@@ -40,6 +40,7 @@ class Agent(SqlalchemyBase, OrganizationMixin, ProjectMixin, TemplateEntityMixin
Index("ix_agents_created_at", "created_at", "id"),
Index("ix_agents_organization_id", "organization_id"),
Index("ix_agents_organization_id_deployment_id", "organization_id", "deployment_id"),
+ Index("ix_agents_project_id", "project_id"),
)
# agent generates its own id
diff --git a/letta/orm/message.py b/letta/orm/message.py
index 71028cb47..e7d57eacf 100644
--- a/letta/orm/message.py
+++ b/letta/orm/message.py
@@ -21,6 +21,7 @@ class Message(SqlalchemyBase, OrganizationMixin, AgentMixin):
Index("ix_messages_created_at", "created_at", "id"),
Index("ix_messages_agent_sequence", "agent_id", "sequence_id"),
Index("ix_messages_org_agent", "organization_id", "agent_id"),
+ Index("ix_messages_run_id", "run_id"),
)
__pydantic_model__ = PydanticMessage