refactor(threads): dedup maintenance threads by stable key via index

This commit is contained in:
Gabriel Brown
2026-07-11 14:00:16 -04:00
parent c3f73ac656
commit 0e894f8e67
2 changed files with 157 additions and 7 deletions
+11 -7
View File
@@ -387,19 +387,20 @@ export const findOpenMaintenanceThread = internalQuery({
args: {
spoonId: v.id('spoons'),
ownerId: v.id('users'),
upstreamTo: v.string(),
dedupKey: v.string(),
},
handler: async (ctx, { spoonId, ownerId, upstreamTo }) => {
handler: async (ctx, { spoonId, ownerId, dedupKey }) => {
const threads = await ctx.db
.query('threads')
.withIndex('by_spoon', (q) => q.eq('spoonId', spoonId))
.withIndex('by_spoon_dedup', (q) =>
q.eq('spoonId', spoonId).eq('dedupKey', dedupKey),
)
.order('desc')
.collect();
return (
threads.find(
(thread) =>
thread.ownerId === ownerId &&
thread.upstreamTo === upstreamTo &&
!['resolved', 'ignored', 'failed', 'cancelled'].includes(
thread.status,
),
@@ -416,7 +417,8 @@ export const createMaintenanceThread = internalMutation({
title: v.string(),
summary: v.string(),
upstreamFrom: v.optional(v.string()),
upstreamTo: v.string(),
upstreamTo: v.optional(v.string()),
dedupKey: v.string(),
forkHeadAtCreation: v.optional(v.string()),
mergeBaseAtCreation: v.optional(v.string()),
relatedSyncRunId: v.optional(v.id('syncRuns')),
@@ -429,14 +431,15 @@ export const createMaintenanceThread = internalMutation({
const now = Date.now();
const existing = await ctx.db
.query('threads')
.withIndex('by_spoon', (q) => q.eq('spoonId', args.spoonId))
.withIndex('by_spoon_dedup', (q) =>
q.eq('spoonId', args.spoonId).eq('dedupKey', args.dedupKey),
)
.order('desc')
.collect()
.then((threads) =>
threads.find(
(thread) =>
thread.ownerId === args.ownerId &&
thread.upstreamTo === args.upstreamTo &&
!['resolved', 'ignored', 'failed', 'cancelled'].includes(
thread.status,
),
@@ -470,6 +473,7 @@ export const createMaintenanceThread = internalMutation({
priority: args.source === 'merge_conflict' ? 'high' : 'normal',
upstreamFrom: args.upstreamFrom,
upstreamTo: args.upstreamTo,
dedupKey: args.dedupKey,
forkHeadAtCreation: args.forkHeadAtCreation,
mergeBaseAtCreation: args.mergeBaseAtCreation,
relatedSyncRunId: args.relatedSyncRunId,