Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(app-events): serialize duplicate automation checks

pqminh27c623eeac54f0
diff --git a/server/app-triggers.test.ts b/server/app-triggers.test.ts
index 8a4b636384..cbb19cd77d 100644
--- a/server/app-triggers.test.ts
+++ b/server/app-triggers.test.ts
@@ -73,6 +73,22 @@ async function gmailTrigger(triggers: AppTriggerManager, filter = { from: "acme.
}
describe("AppTriggerManager", () => {
+ it("rejects a concurrent duplicate before one event can queue two runs", async () => {
+ const client = brokerClient(() => ({ url: broker.url, token: owner.token }));
+ const type = await client.triggerType("GMAIL_NEW_GMAIL_MESSAGE");
+ vi.spyOn(client, "triggerType").mockResolvedValue(type);
+ const triggers = manager({ broker: client });
+ const results = await Promise.allSettled([gmailTrigger(triggers), gmailTrigger(triggers)]);
+ const created = results.flatMap((result) => result.status === "fulfilled" ? [result.value] : []);
+ expect(created).toHaveLength(1);
+ expect(results.filter((result) => result.status === "rejected")).toMatchObject([{ reason: { status: 409 } }]);
+ expect(triggers.list()).toHaveLength(1);
+ expect(broker.harness.composio.instances.size).toBe(1);
+ await broker.deliver(created[0]!.remote.triggerId!, { subject: "Order #7", sender: "[email protected]" }, "msg_concurrent");
+ await triggers.pollOnce(undefined, 0);
+ expect(routines.listRuns()).toHaveLength(1);
+ });
+
it("refuses a bot replacement when a direct edit adds protected instructions during its network wait", async () => {
const client = brokerClient(() => ({ url: broker.url, token: owner.token }));
const triggers = manager({ broker: client });
diff --git a/server/app-triggers.ts b/server/app-triggers.ts
index 4e24235b8b..3f77000aaf 100644
--- a/server/app-triggers.ts
+++ b/server/app-triggers.ts
@@ -274,9 +274,6 @@ export class AppTriggerManager {
const account = await this.resolveAccount(app, input.account);
const filter = cleanFilter(input.filter);
const config = triggerConfig(type, input.config ?? {}, filter);
- const duplicate = this.triggers.find((trigger) => trigger.enabled && trigger.botId === input.botId && trigger.event === type.slug &&
- trigger.accountId === account.id && trigger.prompt === prompt && JSON.stringify(trigger.config) === JSON.stringify(config));
- if (duplicate) throw new AppTriggerError(`The same automation already exists (${duplicate.id}). Use trigger_info to review it, then change that one instead.`, 409);
const now = this.now();
const trigger: StoredTrigger = {
id: input.id ?? randomUUID(), name, prompt, botId: input.botId, runOn: input.runOn ?? "maus", enabled: input.enabled ?? true,
@@ -287,6 +284,9 @@ export class AppTriggerManager {
received: 0, filtered: 0, createdAt: now, updatedAt: now,
};
await this.exclusive(async () => {
+ const duplicate = this.triggers.find((trigger) => trigger.enabled && trigger.botId === input.botId && trigger.event === type.slug &&
+ trigger.accountId === account.id && trigger.prompt === prompt && JSON.stringify(trigger.config) === JSON.stringify(config));
+ if (duplicate) throw new AppTriggerError(`The same automation already exists (${duplicate.id}). Use trigger_info to review it, then change that one instead.`, 409);
try {
await this.bind(trigger);
if (!trigger.enabled) await this.applyEnabled(trigger.remote.triggerId);