Skip to content

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

Recheck protected instructions inside serialized app-event updates

Local contributor3eb0cc3cdc9c
diff --git a/docs/verification/routines.md b/docs/verification/routines.md
index 9c051709d8..7b7c5a2705 100644
--- a/docs/verification/routines.md
+++ b/docs/verification/routines.md
@@ -37,7 +37,9 @@ Secrets remain redacted, and `instructionsRedacted` reports whether the saved
text contains hidden content, including beyond a listing's preview. When true,
a bot instruction replacement fails with HTTP 409 before any field or receipt
changes. Ask the person to edit those instructions on the Automations page;
-other updates work when instructions are omitted.
+other updates work when instructions are omitted. App-event bot replacements
+recheck inside the serialized write after any network or queue wait, so a
+concurrent direct edit that adds protected instructions is preserved.
The disposable fake-engine fixture reads a long schedule and webhook through
the real MCP tools, changes one sentence, and checks the full saved text and
diff --git a/server/app-trigger-requests.ts b/server/app-trigger-requests.ts
index 7e0c2503a2..dd30a64bf2 100644
--- a/server/app-trigger-requests.ts
+++ b/server/app-trigger-requests.ts
@@ -139,7 +139,7 @@ export class AppTriggerRequestService {
}
case "update": {
const { instructions, ...changes } = operation.changes;
- after = await triggers.update(operation.triggerId, { ...changes, ...(instructions !== undefined ? { prompt: instructions } : {}) });
+ after = await triggers.update(operation.triggerId, { ...changes, ...(instructions !== undefined ? { prompt: instructions } : {}) }, { protectInstructions: true });
break;
}
case "pause":
diff --git a/server/app-triggers.test.ts b/server/app-triggers.test.ts
index 50df36955b..8a4b636384 100644
--- a/server/app-triggers.test.ts
+++ b/server/app-triggers.test.ts
@@ -73,6 +73,63 @@ async function gmailTrigger(triggers: AppTriggerManager, filter = { from: "acme.
}
describe("AppTriggerManager", () => {
+ 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 });
+ const saved = await gmailTrigger(triggers);
+ const store = { messagesFor: () => [], appendMessage: vi.fn(() => ({ id: "message-1" })), patchMessage: vi.fn() };
+ const service = new AppTriggerRequestService({ triggers, store, botName: () => "Fixture" });
+ const type = await client.triggerType(saved.event);
+ let entered!: () => void;
+ let release!: () => void;
+ const blocked = new Promise<void>(resolve => { entered = resolve; });
+ const resume = new Promise<void>(resolve => { release = resolve; });
+ vi.spyOn(client, "triggerType").mockImplementationOnce(async () => { entered(); await resume; return type; });
+ const outcome = service.submit({ botId: saved.botId, threadId: "thread-a", operation: {
+ action: "update", triggerId: saved.id, changes: { name: "Must not apply", instructions: "Bot replacement", filter: { from: "changed.example" } },
+ } }).then(value => ({ value, error: null }), error => ({ value: null, error }));
+ await blocked;
+ const protectedPrompt = "Use token=fixture-private-value.";
+ let before!: ReturnType<AppTriggerManager["list"]>;
+ let remote!: Array<unknown>;
+ try {
+ await triggers.update(saved.id, { prompt: protectedPrompt });
+ before = structuredClone(triggers.list());
+ remote = structuredClone([...broker.harness.composio.instances.entries()]);
+ } finally { release(); }
+ expect((await outcome).error).toMatchObject({ status: 409 });
+ expect(triggers.list()).toEqual(before);
+ expect([...broker.harness.composio.instances.entries()]).toEqual(remote);
+ expect(store.appendMessage).not.toHaveBeenCalled();
+ });
+
+ it("refuses a bot replacement queued behind a direct edit that stores protected instructions", async () => {
+ const client = brokerClient(() => ({ url: broker.url, token: owner.token }));
+ const triggers = manager({ broker: client });
+ const saved = await gmailTrigger(triggers);
+ const store = { messagesFor: () => [], appendMessage: vi.fn(() => ({ id: "message-1" })), patchMessage: vi.fn() };
+ const service = new AppTriggerRequestService({ triggers, store, botName: () => "Fixture" });
+ let entered!: () => void;
+ let release!: () => void;
+ const blocked = new Promise<void>(resolve => { entered = resolve; });
+ const resume = new Promise<void>(resolve => { release = resolve; });
+ const setEnabled = client.setEnabled.bind(client);
+ vi.spyOn(client, "setEnabled").mockImplementationOnce(async (...args) => { entered(); await resume; await setEnabled(...args); });
+ const holding = triggers.update(saved.id, { name: "Holding update" });
+ await blocked;
+ const protectedPrompt = "Use token=fixture-private-value.";
+ const manual = triggers.update(saved.id, { prompt: protectedPrompt });
+ const outcome = service.submit({ botId: saved.botId, threadId: "thread-a", operation: {
+ action: "update", triggerId: saved.id, changes: { name: "Must not apply", instructions: "Bot replacement" },
+ } }).then(value => ({ value, error: null }), error => ({ value: null, error }));
+ release();
+ await holding;
+ const expected = await manual;
+ expect((await outcome).error).toMatchObject({ status: 409 });
+ expect(triggers.get(saved.id)).toEqual(expected);
+ expect(store.appendMessage).not.toHaveBeenCalled();
+ });
+
it("refuses replacing redacted app instructions before changing local or remote state", async () => {
const triggers = manager();
const prompt = "Review the queue.\n".repeat(180) + "Use token=fixture-secret-value.";
@@ -89,6 +146,9 @@ describe("AppTriggerManager", () => {
expect(store.appendMessage).not.toHaveBeenCalled();
await submit({ name: "Safe rename" });
expect(triggers.get(saved.id)).toMatchObject({ name: "Safe rename", prompt });
+ const edited = prompt + " Changed by the person.";
+ await triggers.update(saved.id, { prompt: edited });
+ expect(triggers.get(saved.id)?.prompt).toBe(edited);
});
it("creates the service copy with a Gmail filter folded into Gmail's own search", async () => {
diff --git a/server/app-triggers.ts b/server/app-triggers.ts
index 01e684cb50..4e24235b8b 100644
--- a/server/app-triggers.ts
+++ b/server/app-triggers.ts
@@ -21,6 +21,7 @@ import { appLabel } from "./app-event-catalog.ts";
import { eventBlock, eventPreview, eventPrompt, matchesFilter, mergeEventBlock } from "./app-events.ts";
import { BrokerError, type AppTriggerBroker, type BrokerEvent, type BrokerInventory, type BrokerTriggerType } from "./app-trigger-broker.ts";
import { writeFileAtomic } from "./atomic.ts";
+import { instructionsUpdateRefusal } from "./automation-instructions.ts";
import { DATA_DIR } from "./config.ts";
import type { RoutineRunOn } from "./routines.ts";
@@ -315,7 +316,7 @@ export class AppTriggerManager {
...(record.sameConversation ? { sameConversation: true } : {}) });
}
- async update(id: string, patch: AppTriggerPatch): Promise<AppTrigger | null> {
+ async update(id: string, patch: AppTriggerPatch, options: { protectInstructions?: boolean } = {}): Promise<AppTrigger | null> {
this.assertWritable();
const trigger = this.triggers.find((candidate) => candidate.id === id);
if (!trigger) return null;
@@ -333,6 +334,10 @@ export class AppTriggerManager {
config = triggerConfig(type, Object.fromEntries(Object.entries(base).filter(([, value]) => value !== undefined)), filter);
}
await this.exclusive(async () => {
+ if (options.protectInstructions) {
+ const refusal = instructionsUpdateRefusal(trigger.prompt, patch.prompt);
+ if (refusal) throw new AppTriggerError(refusal, 409);
+ }
const previous = trigger.remote.triggerId;
const previousConfig = JSON.stringify(trigger.config);
Object.assign(trigger, {