From acab148a45d184104e2b9e29f1df279bfdd017bf Mon Sep 17 00:00:00 2001 From: Dotta Date: Mon, 16 Nov 2026 17:49:36 -0600 Subject: [PATCH] fix: lock company agents before Muse mirror mutation --- server/src/services/companies.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server/src/services/companies.ts b/server/src/services/companies.ts index 174e66d954b..f8ff8613896 100644 --- a/server/src/services/companies.ts +++ b/server/src/services/companies.ts @@ -347,6 +347,9 @@ export function companyService(db: Db, _budgetHooks: BudgetServiceHooks = {}) { const renamedPrefix = await resolveRenamedIssuePrefix(tx, id, companyPatch); if (companyPatch.status !== undefined && companyPatch.status !== existing.status) { + // Lock the complete agent set before any receiver mirror mutation. Stop + // fencing visits batches and can mutate the mirror inside each batch. + await tx.select({ id: agents.id }).from(agents).where(eq(agents.companyId, id)).orderBy(agents.id).for("update"); await prepareMuseReceiverAuthorityMutation(tx,{companyId:id}); if(willReactivate)await notifyMuseContactVerification(tx,{companyId:id}); // A resumed company's tasks become eligible again without any task row changing. @@ -512,6 +515,8 @@ export function companyService(db: Db, _budgetHooks: BudgetServiceHooks = {}) { const wasAlreadyArchived = existing.status === "archived"; if (!wasAlreadyArchived) { + // The separate archive entry shares update()'s agent-before-mirror order. + await tx.select({ id: agents.id }).from(agents).where(eq(agents.companyId, id)).orderBy(agents.id).for("update"); await notifyRunDispatch(tx, { companyId: id }); await notifyGitHubCompanyChange(tx, id); await notifyDeferredExecutionEligibility(tx, { companyId: id });