🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
⚡️ perf(database): scope the hetero tool-row lookup to the operation topic (#21698)
Arvin Xu2e577096d03e
diff --git a/packages/database/src/models/__tests__/messages/message.pluginsForOperation.test.ts b/packages/database/src/models/__tests__/messages/message.pluginsForOperation.test.tsindex 1019b398a16..014cf45e2a0 100644--- a/packages/database/src/models/__tests__/messages/message.pluginsForOperation.test.ts+++ b/packages/database/src/models/__tests__/messages/message.pluginsForOperation.test.ts@@ -187,6 +187,26 @@ describe('MessageModel.listMessagePluginsForOperation', () => {expect(rows.map((r) => r.id)).toEqual(['hetero']);});+ it('looks up heterogeneous rows only in the operation topic', async () => {+ // A copy of a stamped row in another topic (a fork, say) is not this op's.+ await seedToolCall({+ createdAt: T1,+ id: 'hetero-elsewhere',+ metadata: { heterogeneousToolStateOperationId: 'op-1' },+ topicId: 'topic2',+ });++ const rows = await messageModel.listMessagePluginsForOperation({+ completedAt: T3,+ operationId: 'op-1',+ startedAt: T0,+ threadId: 'thread1',+ topicId: 'topic1',+ });++ expect(rows).toEqual([]);+ });+it('falls back to now when completedAt is omitted', async () => {await seedToolCall({createdAt: new Date(),@@ -365,6 +385,26 @@ describe('MessageModel.listMessagePluginsForOperation', () => {`a message_plugins⋈messages statement examined ${examined} rows — it walked ~all of the user's ${NOISE}-row history instead of a bounded/PK access. A jsonb metadata match was resolved inside the plugin JOIN.`,).toBeLessThan(NOISE / 2);}++ // The heterogeneous id lookup reads `messages` alone. The metadata+ // expression has no index, so a user-wide lookup filtered every message the+ // user owns (40s–300s per `heteroFinish` for a heavy user); it must stay+ // within the op's topic.+ const lookupStatements = statements.filter(+ (s) => /heterogeneousToolStateOperationId/.test(s.sql) && !/"message_plugins"/i.test(s.sql),+ );+ expect(lookupStatements.length).toBeGreaterThan(0);+ for (const statement of lookupStatements) {+ const explained = await pool.query({+ text: `EXPLAIN (ANALYZE, FORMAT JSON) ${statement.sql}`,+ values: statement.params,+ });+ const examined = rowsExaminedOn(explained.rows[0]['QUERY PLAN'][0].Plan, 'messages');+ expect(+ examined,+ `the heterogeneous lookup examined ${examined} messages — it filtered the user's whole history instead of the op's topic.`,+ ).toBeLessThan(NOISE / 2);+ }});});});diff --git a/packages/database/src/models/message/plugin.ts b/packages/database/src/models/message/plugin.tsindex 63df2d446d6..2b2e4ff28c5 100644--- a/packages/database/src/models/message/plugin.ts+++ b/packages/database/src/models/message/plugin.ts@@ -258,7 +258,7 @@ export class MessagePluginStore extends MessageScope {* 1. Time window — same `topicId` + `threadId`, with the owning message's* `createdAt` inside `[startedAt, completedAt]` (`completedAt` falls back to* now for an op still finalizing). The primary path for in-process runs.- * 2. Heterogeneous match — the message carries+ * 2. Heterogeneous match — a message in the same `topicId` carries* `metadata.heterogeneousToolStateOperationId === operationId` directly,* which a CLI-backed run stamps regardless of when the row lands. Mirrors* the jsonb predicate in {@link findVerifyMessageByOperationId}.@@ -298,10 +298,13 @@ export class MessagePluginStore extends MessageScope {// every time, since most ops never stamp the key). Looking up the few// matching message ids in a JOIN-free query first, then fetching those rows// by primary key, leaves the planner no join to mis-order: the lookup is- // single-table (index-served once the metadata expression is indexed; a- // bounded scan otherwise) and the fetch is a pure PK access. Kept- // topic-agnostic to preserve the prior behaviour exactly — a late CLI row- // can land outside the op's window (and, rarely, its topic).+ // single-table and the fetch is a pure PK access.+ // The lookup is bounded by the op's topic, not its window: a late CLI row+ // can land after `completedAt`, but the tool message it stamps is always+ // one the run wrote into its own topic. The metadata expression has no+ // index, so a user-wide lookup filtered every message the user owns+ // (1.6M rows, 40s–300s per `heteroFinish` for a heavy user); the topic+ // index keeps it to the topic's few thousand rows at most.const projection = {apiName: messagePlugins.apiName,arguments: messagePlugins.arguments,@@ -336,6 +339,7 @@ export class MessagePluginStore extends MessageScope {.where(and(this.ownership(),+ eq(messages.topicId, params.topicId),sql`${messages.metadata}->>'heterogeneousToolStateOperationId' = ${params.operationId}`,),);