Skip to content

🤯 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.

🔨 chore(database): re-tighten getBuiltinAgent onConflict after 0109 (#15475)

Arvin Xuf77f31efc004
diff --git a/packages/database/src/models/agent.ts b/packages/database/src/models/agent.ts
index 603a7617347..6bc874abf28 100644
--- a/packages/database/src/models/agent.ts
+++ b/packages/database/src/models/agent.ts
@@ -673,13 +673,9 @@ export class AgentModel {
// `onConflictDoNothing`, the loser hits the `agents_slug_user_id_unique`
// constraint; with it, the loser's `.returning()` is empty and we re-read
// the row that won.
- // Bare `onConflictDoNothing()` (no target) does NOT pin an arbiter index,
- // so it works whether `agents_slug_user_id_unique` is the legacy full
- // unique or the migration-0109 partial (WHERE workspace_id IS NULL) — this
- // is the transition-safe form while 0109 rolls out. For this row only
- // (slug, user_id) can collide (client_id / workspace_id are NULL, id is
- // freshly generated). Tighten back to { target, where: isNull(workspaceId) }
- // once 0109 has flipped the index in every environment.
+ // `agents_slug_user_id_unique` is a partial index (WHERE workspace_id IS
+ // NULL) since migration 0109, so the conflict arbiter must carry the same
+ // predicate; builtin agents are always workspace-less (workspace_id NULL).
const result = await this.db
.insert(agents)
.values({
@@ -689,7 +685,10 @@ export class AgentModel {
userId: this.userId,
virtual: true,
})
- .onConflictDoNothing()
+ .onConflictDoNothing({
+ target: [agents.slug, agents.userId],
+ where: isNull(agents.workspaceId),
+ })
.returning();
if (result[0]) return result[0];