From 467e9540851bf95115a2d8668a61971a548d3e76 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 11 Jul 2026 19:42:37 +0000 Subject: [PATCH] fix(tasks): serialize template subtask creation Co-authored-by: Neil Raina --- src/constants/tasks.test.ts | 19 +++++++++++++++++++ src/constants/tasks.ts | 6 +++--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 src/constants/tasks.test.ts diff --git a/src/constants/tasks.test.ts b/src/constants/tasks.test.ts new file mode 100644 index 000000000..bceec99f5 --- /dev/null +++ b/src/constants/tasks.test.ts @@ -0,0 +1,19 @@ +import { subtaskTemplateBatchSize } from '@/constants/tasks' +import { runInBatches } from '@/utils/array' + +describe('task constants', () => { + it('keeps subtask template fan-out within the production Prisma pool', async () => { + expect(subtaskTemplateBatchSize).toBe(1) + + const state = { active: 0, max: 0 } + + await runInBatches([1, 2, 3], subtaskTemplateBatchSize, async () => { + state.active += 1 + state.max = Math.max(state.max, state.active) + await new Promise((resolve) => setTimeout(resolve, 1)) + state.active -= 1 + }) + + expect(state.max).toBe(1) + }) +}) diff --git a/src/constants/tasks.ts b/src/constants/tasks.ts index f511f1f6b..2b08a749f 100644 --- a/src/constants/tasks.ts +++ b/src/constants/tasks.ts @@ -1,5 +1,5 @@ export const maxSubTaskDepth = 1 -// Bounds how many subtasks we create concurrently when applying a template, so a -// template with many sub-templates can't exhaust the DB connection pool. -export const subtaskTemplateBatchSize = 5 +// Production Prisma pool limit is 2; each subtask creation performs multiple DB +// writes, so keep template application serial within a single request. +export const subtaskTemplateBatchSize = 1