Skip to content

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

fix(app-events): keep shared trigger switches in sync

Afloat5ca1b7a25397
diff --git a/server/app-trigger-switches.test.ts b/server/app-trigger-switches.test.ts
new file mode 100644
index 0000000000..0f87c0ad1b
--- /dev/null
+++ b/server/app-trigger-switches.test.ts
@@ -0,0 +1,85 @@
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, expect, it } from "vitest";
+
+import type { AppTriggerBroker } from "./app-trigger-broker.ts";
+import { AppTriggerManager } from "./app-triggers.ts";
+
+const folders: string[] = [];
+afterEach(() => { for (const dir of folders.splice(0)) rmSync(dir, { recursive: true, force: true }); });
+
+function harness() {
+ const dir = mkdtempSync(join(tmpdir(), "omb-trigger-switch-"));
+ folders.push(dir);
+ let exists = false, enabled = false;
+ const broker: AppTriggerBroker = {
+ triggerTypes: async () => ({}),
+ triggerType: async () => ({ slug: "SLACK_CHANNEL_MESSAGE_RECEIVED", toolkit: "slack", name: "Message", description: "", config: {}, payload: {} }),
+ create: async () => { if (!exists) { exists = true; enabled = true; } return { triggerId: "shared-copy" }; },
+ setEnabled: async (_id, value) => { enabled = value; },
+ remove: async () => { exists = false; enabled = false; },
+ inventory: async () => ({ triggers: [], unbound: [], limits: { plan: "desktop", eventsPerDay: 100 }, usage: { eventsToday: 0 } }),
+ poll: async () => ({ events: [], cursor: 0 }),
+ testEvent: async () => {},
+ };
+ const options = {
+ file: join(dir, "triggers.json"), broker, service: () => ({ available: true as const }),
+ connectedApps: async () => ({ slack: { connected: true, accounts: [{ id: "account", status: "ACTIVE" }] } }),
+ botState: () => "ready" as const,
+ runs: { enqueue: () => ({ id: "run" }), merge: () => null, receipt: () => null, pending: () => 0, cancelQueued: () => {} },
+ };
+ const manager = new AppTriggerManager(options);
+ const create = (botId: string, on: boolean) => manager.create({ botId, name: `Watch ${botId}`, prompt: "Summarize new messages", app: "slack", event: "SLACK_CHANNEL_MESSAGE_RECEIVED", enabled: on });
+ return { manager, create, remoteEnabled: () => enabled, reopened: () => new AppTriggerManager(options) };
+}
+
+it.each([true, false])("restores a lone automation with its saved enabled state: %s", async (enabled) => {
+ const h = harness();
+ const first = await h.create("first", enabled);
+ await h.manager.remove(first.id);
+ const restored = await h.manager.restore(first.id);
+ expect(restored?.enabled).toBe(enabled);
+ expect(h.remoteEnabled()).toBe(enabled);
+ expect(h.reopened().get(first.id)?.enabled).toBe(enabled);
+});
+
+it("enables an existing paused service copy when a new active automation shares it", async () => {
+ const h = harness();
+ await h.create("paused", false);
+ expect(h.remoteEnabled()).toBe(false);
+ const active = await h.create("active", true);
+ expect(active.remote.triggerId).toBe("shared-copy");
+ expect(h.remoteEnabled()).toBe(true);
+ await h.manager.update(active.id, { enabled: false });
+ expect(h.remoteEnabled()).toBe(false);
+});
+
+it("enables a restored automation even when only a paused sibling remains", async () => {
+ const h = harness();
+ const active = await h.create("active", true);
+ await h.create("paused", false);
+ await h.manager.remove(active.id);
+ expect(h.remoteEnabled()).toBe(false);
+ await h.manager.restore(active.id);
+ expect(h.remoteEnabled()).toBe(true);
+});
+
+it("keeps a shared copy on when a paused automation is created or restored beside an active one", async () => {
+ const h = harness();
+ await h.create("active", true);
+ const paused = await h.create("paused", false);
+ expect(h.remoteEnabled()).toBe(true);
+ await h.manager.remove(paused.id);
+ await h.manager.restore(paused.id);
+ expect(h.remoteEnabled()).toBe(true);
+});
+
+it("switches a retained service copy off as soon as its last active automation is removed", async () => {
+ const h = harness();
+ const active = await h.create("active", true);
+ const paused = await h.create("paused", false);
+ await h.manager.remove(active.id);
+ expect(h.manager.get(paused.id)?.remote.triggerId).toBe("shared-copy");
+ expect(h.remoteEnabled()).toBe(false);
+});
diff --git a/server/app-triggers.ts b/server/app-triggers.ts
index 3f77000aaf..3bd83b249b 100644
--- a/server/app-triggers.ts
+++ b/server/app-triggers.ts
@@ -289,7 +289,7 @@ export class AppTriggerManager {
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);
+ await this.applyEnabled(trigger.remote.triggerId, trigger.enabled);
} catch (error) {
// Composio refused this config: nothing to save. Anything else is retried by reconciliation.
if (error instanceof BrokerError && !error.transient && error.status !== 403) throw new AppTriggerError(error.message, 400);
@@ -400,7 +400,7 @@ export class AppTriggerManager {
if (this.options.botState(entry.trigger.botId) === "missing") throw new AppTriggerError("The assigned bot no longer exists.", 410);
const trigger: StoredTrigger = { ...structuredClone(entry.trigger), remote: { state: "pending" }, updatedAt: this.now() };
await this.exclusive(async () => {
- try { await this.bind(trigger); await this.applyEnabled(trigger.remote.triggerId); }
+ try { await this.bind(trigger); await this.applyEnabled(trigger.remote.triggerId, trigger.enabled); }
catch (error) { this.markRemoteFailure(trigger, error); }
this.deleted = this.deleted.filter((candidate) => candidate !== entry);
this.triggers.unshift(trigger);
@@ -691,7 +691,11 @@ export class AppTriggerManager {
/** Several automations with the same settings share one service copy; it goes only when the last one does. */
private async release(triggerId: string | undefined): Promise<void> {
- if (!triggerId || this.triggers.some((trigger) => trigger.remote.triggerId === triggerId)) return;
+ if (!triggerId) return;
+ if (this.triggers.some((trigger) => trigger.remote.triggerId === triggerId)) {
+ await this.applyEnabled(triggerId);
+ return;
+ }
if (this.serviceOutOfReach()) {
this.releasePending = true;
return;
@@ -699,8 +703,9 @@ export class AppTriggerManager {
await this.options.broker.remove(triggerId);
}
- private async applyEnabled(triggerId: string | undefined): Promise<void> {
- if (triggerId && !this.serviceOutOfReach()) await this.options.broker.setEnabled(triggerId, this.wantsEnabled(triggerId));
+ /** A new or restored automation is not in this.triggers yet when its binding is configured. */
+ private async applyEnabled(triggerId: string | undefined, alsoEnabled = false): Promise<void> {
+ if (triggerId && !this.serviceOutOfReach()) await this.options.broker.setEnabled(triggerId, alsoEnabled || this.wantsEnabled(triggerId));
}
/** The owner's own key, or the service switched off. A service merely not reached yet still gets its calls and retries. */