refactor(core): Index agent sessions by caller

Serves "the sessions this caller had with this agent", the lookup the
own-sessions tool scopes its reads by.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Robin Braumann 2026-07-27 14:47:02 +02:00
parent 293d7dc699
commit 88ca2fe0cd
4 changed files with 9 additions and 0 deletions

View File

@ -53,6 +53,7 @@
| ---- | ---------- |
| IDX_0468a9dc35597314e641d4722a | CREATE INDEX "IDX_0468a9dc35597314e641d4722a" ON public.agent_execution_threads USING btree ("agentId") |
| IDX_0e2f8bf92a7a9c88b89670f701 | CREATE INDEX "IDX_0e2f8bf92a7a9c88b89670f701" ON public.agent_execution_threads USING btree ("projectId") |
| IDX_agent_execution_threads_agentId_createdByResourceId | CREATE INDEX "IDX_agent_execution_threads_agentId_createdByResourceId" ON public.agent_execution_threads USING btree ("agentId", "createdByResourceId") |
| IDX_agent_execution_threads_agentId_origin_originRef_externalKe | CREATE UNIQUE INDEX "IDX_agent_execution_threads_agentId_origin_originRef_externalKe" ON public.agent_execution_threads USING btree ("agentId", origin, "originRef", "externalKey") WHERE ("externalKey" IS NOT NULL) |
| IDX_agent_execution_threads_taskVersionId | CREATE INDEX "IDX_agent_execution_threads_taskVersionId" ON public.agent_execution_threads USING btree ("taskVersionId") |
| PK_22373dbf6ba6929d8ac50093309 | CREATE UNIQUE INDEX "PK_22373dbf6ba6929d8ac50093309" ON public.agent_execution_threads USING btree (id) |

View File

@ -53,6 +53,7 @@ CREATE TABLE "agent_execution_threads" ("id" varchar(128) PRIMARY KEY NOT NULL,
| ---- | ---------- |
| IDX_0468a9dc35597314e641d4722a | CREATE INDEX "IDX_0468a9dc35597314e641d4722a" ON "agent_execution_threads" ("agentId") |
| IDX_0e2f8bf92a7a9c88b89670f701 | CREATE INDEX "IDX_0e2f8bf92a7a9c88b89670f701" ON "agent_execution_threads" ("projectId") |
| IDX_agent_execution_threads_agentId_createdByResourceId | CREATE INDEX "IDX_agent_execution_threads_agentId_createdByResourceId" ON "agent_execution_threads" ("agentId", "createdByResourceId") |
| IDX_agent_execution_threads_agentId_origin_originRef_externalKey | CREATE UNIQUE INDEX "IDX_agent_execution_threads_agentId_origin_originRef_externalKey" ON "agent_execution_threads" ("agentId", "origin", "originRef", "externalKey") WHERE "externalKey" IS NOT NULL |
| IDX_agent_execution_threads_taskVersionId | CREATE INDEX "IDX_agent_execution_threads_taskVersionId" ON "agent_execution_threads" ("taskVersionId") |
| sqlite_autoindex_agent_execution_threads_1 | PRIMARY KEY (id) |

View File

@ -6,6 +6,9 @@ const NEW_COLUMNS = ['origin', 'originRef', 'externalKey', 'createdByResourceId'
const NATURAL_KEY = ['agentId', 'origin', 'originRef', 'externalKey'];
/** Serves "the sessions this caller had with this agent". */
const CALLER_SCOPE = ['agentId', 'createdByResourceId'];
const COLUMN_COMMENTS: Array<[column: string, comment: string]> = [
['origin', 'Surface that started the session: chat/integration/workflow/task/subagent/test'],
['originRef', 'Namespace of externalKey (workflowId for workflow threads); empty when unscoped'],
@ -70,9 +73,12 @@ export class AddThreadIdentityToAgentExecutionThreads1785151856791 implements Re
undefined,
`${escape.columnName('externalKey')} IS NOT NULL`,
);
await createIndex(TABLE, CALLER_SCOPE);
}
async down({ escape, runQuery, schemaBuilder: { dropIndex } }: MigrationContext) {
await dropIndex(TABLE, CALLER_SCOPE, { skipIfMissing: true });
await dropIndex(TABLE, NATURAL_KEY, { skipIfMissing: true });
const table = escape.tableName(TABLE);

View File

@ -25,6 +25,7 @@ export type AgentThreadOrigin = 'chat' | 'integration' | 'workflow' | 'task' | '
unique: true,
where: '"externalKey" IS NOT NULL',
})
@Index(['agentId', 'createdByResourceId'])
@Entity({ name: 'agent_execution_threads' })
export class AgentExecutionThread extends WithTimestampsAndStringId {
@ManyToOne(() => Agent, { onDelete: 'CASCADE' })