Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
fix: desktop channel no longer loops broadcasts into hundreds of messages
Salman Qureshi478b61828364
diff --git a/plugins/optimus/companion/Cargo.toml b/plugins/optimus/companion/Cargo.tomlindex 3eaf69e3..148700e4 100644--- a/plugins/optimus/companion/Cargo.toml+++ b/plugins/optimus/companion/Cargo.toml@@ -1,6 +1,6 @@[package]name = "mercury-optimus"-version = "0.2.0"+version = "0.2.1"edition = "2024"rust-version = "1.94"description = "Optimus: full computer control for Mercury (screen, mouse, keyboard, apps, voice)"diff --git a/plugins/optimus/companion/src/core.rs b/plugins/optimus/companion/src/core.rsindex aec02604..fca9283d 100644--- a/plugins/optimus/companion/src/core.rs+++ b/plugins/optimus/companion/src/core.rs@@ -22,7 +22,14 @@ use tokio::sync::oneshot;use crate::ops::Ops;pub fn log(msg: impl AsRef<str>) {- eprintln!("optimus: {}", msg.as_ref());+ let t = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0);+ eprintln!("[{t}] optimus: {}", msg.as_ref());+}++/// A short "that didn't work" sound, for a talk-key press that couldn't start.+fn error_sound() {+ #[cfg(target_os = "macos")]+ let _ = std::process::Command::new("afplay").arg("/System/Library/Sounds/Basso.aiff").spawn();}pub fn settings() -> Value {@@ -165,6 +172,7 @@ fn start_hotkey(shared: &Arc<Shared>) {sh.rt.spawn(async move {if let Err(e) = crate::voice::press(sh2.clone(), false).await {log(format!("listening: {e}"));+ error_sound();}});}) {diff --git a/plugins/optimus/companion/src/links/gemini.rs b/plugins/optimus/companion/src/links/gemini.rsindex 51629876..86f96fae 100644--- a/plugins/optimus/companion/src/links/gemini.rs+++ b/plugins/optimus/companion/src/links/gemini.rs@@ -146,7 +146,12 @@ async fn connect(key: &str, cfg: &LinkConfig, handle: Option<&str>) -> Result<wslet m = m.map_err(|e| e.to_string())?;if m.is_close() {let reason = m.as_close().map(|(_, r)| r.to_string()).unwrap_or_default();- return Err(format!("Gemini refused the session: {reason}"));+ let model = setting(&cfg.settings, "geminiModel", "gemini-3.8-live");+ return Err(if reason.to_lowercase().contains("not found") || reason.to_lowercase().contains("not supported") {+ format!("Gemini doesn't accept the model \"{model}\" for live voice ({reason}). Set the Gemini Live model to gemini-3.8-live (or gemini-3.1-flash-live-preview)")+ } else {+ format!("Gemini refused the session (model {model}): {reason}")+ });}let text = if let Some(t) = m.as_text() { t.to_string() } else { String::from_utf8_lossy(&m.as_payload().to_vec()).to_string() };if text.contains("setupComplete") { return Ok(()); }diff --git a/plugins/optimus/companion/src/mac.rs b/plugins/optimus/companion/src/mac.rsindex 53fc8503..1fef46ae 100644--- a/plugins/optimus/companion/src/mac.rs+++ b/plugins/optimus/companion/src/mac.rs@@ -153,8 +153,18 @@ pub fn start_agent() -> Result<UnixStream, String> {// Short path: Unix socket paths are limited to ~104 bytes.let socket = std::env::temp_dir().join(format!("mercury-optimus-{}.sock", std::process::id()));let _ = fs::remove_file(&socket);+ // LaunchServices drops our stderr: Optimus.app logs to a file next to it.+ let log_file = dir.join("optimus.log");+ if fs::metadata(&log_file).map(|m| m.len() > 2_000_000).unwrap_or(false) {+ let _ = fs::rename(&log_file, dir.join("optimus.log.1"));+ }+ let _ = fs::OpenOptions::new().create(true).append(true).open(&log_file);let status = Command::new("open").args(["-n", "-g"])+ .arg("--stdout")+ .arg(&log_file)+ .arg("--stderr")+ .arg(&log_file).arg(&app).args(["--args", "agent", "--socket"]).arg(&socket)diff --git a/plugins/optimus/companion/src/voice.rs b/plugins/optimus/companion/src/voice.rsindex 037fc44c..7d87389d 100644--- a/plugins/optimus/companion/src/voice.rs+++ b/plugins/optimus/companion/src/voice.rs@@ -142,10 +142,12 @@ async fn run_session(sh: Arc<Shared>, audio: Audio, ctl: mpsc::UnboundedReceiverlet started = Instant::now();let mut transcript: Vec<Value> = Vec::new();audio.out.chime(true);+ let engine = links::engine(&settings());+ log(format!("session {call_id} starting with {engine}"));let outcome = match bridge(&sh, audio, ctl, &call_id, &instructions, &mut transcript).await {Ok(()) => "completed".to_string(),Err(e) => {- log(format!("voice session ended: {e}"));+ log(format!("session {call_id} ({engine}) failed: {e}"));format!("failed: {e}")}};diff --git a/plugins/optimus/mercury-plugin.template.json b/plugins/optimus/mercury-plugin.template.jsonindex 4e537d66..71203cd5 100644--- a/plugins/optimus/mercury-plugin.template.json+++ b/plugins/optimus/mercury-plugin.template.json@@ -2,7 +2,7 @@"manifestVersion": 1,"id": "optimus","name": "Optimus",- "version": "0.2.0",+ "version": "0.2.1","description": "Mercury uses your computer like a person: sees the screen, moves the mouse, types and opens apps, by chat or out loud with a talk key. macOS, Windows and Linux. Asks before anything that submits, sends, pays or deletes.","kind": "out-of-process","author": "Cosmic Stack",diff --git a/src/channels/base.ts b/src/channels/base.tsindex bf7986e4..d15b4924 100644--- a/src/channels/base.ts+++ b/src/channels/base.ts@@ -22,6 +22,12 @@ export interface Channel {askPermissionMode?(): Promise<PermissionMode>;isReady(): boolean;onMessage(handler: (msg: ChannelMessage) => void): void;+ /**+ * No chat of its own (e.g. the desktop voice channel): broadcasts skip it+ * and it's never picked for notifications. Its own send() forwards to the+ * owner's chats, so including it in a broadcast would loop.+ */+ readonly noBroadcast?: boolean;}export abstract class BaseChannel implements Channel {diff --git a/src/channels/registry.ts b/src/channels/registry.tsindex f50b1b73..63602201 100644--- a/src/channels/registry.ts+++ b/src/channels/registry.ts@@ -123,7 +123,7 @@ export class ChannelRegistry {if (whatsapp?.isReady()) return whatsapp;const cli = this.channels.get('cli');if (cli?.isReady()) return cli;- return this.channels.values().next().value;+ return [...this.channels.values()].find((c) => !c.noBroadcast);}private incomingHandler?: (msg: ChannelMessage) => void;diff --git a/src/core/agent.ts b/src/core/agent.tsindex dae022c1..7a56b81c 100644--- a/src/core/agent.ts+++ b/src/core/agent.ts@@ -97,6 +97,7 @@ import { buildFileChangePreview } from '../utils/file-preview.js';import { whatsNewText } from '../utils/whats-new.js';import { handleBotsCommand as handleBotsCommandImpl } from './commands/bots-command.js';import { modelSeesImages, withAttachments } from './attachments.js';+import { AlertGate } from './alert-gate.js';import { setPluginImagesAllowed } from '../plugins/tool-bridge.js';import { handleCallCommand } from '../plugins/call-schedule.js';import { handlePluginsCommand, tryPluginCommand, pluginCommandHelp, hasPluginDialog, answerPluginDialog } from '../plugins/commands.js';@@ -529,6 +530,7 @@ export function looksLikeCodingTask(text: string): boolean {return (intent.test(t) && artifact.test(t)) || pathOrCommand.test(t) || fence.test(t);}+export class Agent {readonly lifecycle: Lifecycle;readonly scheduler: Scheduler;@@ -4581,10 +4583,18 @@ RULES:* what happened to their task.*/async notifyAllChannels(message: string): Promise<void> {+ const outcome = await this.alertGate.run(message, () => this.broadcastToChannels(message));+ if (outcome !== 'sent') logger.warn({ outcome, message: message.slice(0, 120) }, 'Broadcast not sent');+ }++ /** Broadcasts never nest or repeat (see AlertGate). */+ private alertGate = new AlertGate();++ private async broadcastToChannels(message: string): Promise<void> {const active = this.channels.getActiveChannels();const sends = active.map((type) => {const ch = this.channels.get(type);- if (!ch) return Promise.resolve();+ if (!ch || ch.noBroadcast) return Promise.resolve();return ch.send(message).catch((e) => {logger.warn({ e, channel: type }, 'Failed to notify channel before exit');});diff --git a/src/core/alert-gate.test.ts b/src/core/alert-gate.test.tsnew file mode 100644index 00000000..6eb19662--- /dev/null+++ b/src/core/alert-gate.test.ts@@ -0,0 +1,34 @@+import { describe, expect, it } from 'vitest';+import { AlertGate } from './alert-gate.js';++describe('AlertGate (broadcasts never loop or repeat)', () => {+ it('drops a broadcast started from inside another (a channel forwarding back to all channels)', async () => {+ const gate = new AlertGate();+ const sent: string[] = [];+ const broadcast = (m: string): Promise<string> => gate.run(m, async () => {+ sent.push(m);+ // The desktop channel forwards to "notify the owner", which broadcasts again.+ await broadcast(`${m} (forwarded)`);+ });+ expect(await broadcast('Mercury is shutting down')).toBe('sent');+ expect(sent).toEqual(['Mercury is shutting down']);+ });++ it('sends the same alert once per window', async () => {+ let t = 0;+ const gate = new AlertGate(60_000, () => t);+ const send = async () => {};+ expect(await gate.run('Optimus can\'t listen yet', send)).toBe('sent');+ t = 30_000;+ expect(await gate.run('Optimus can\'t listen yet', send)).toBe('repeat');+ expect(await gate.run('something else', send)).toBe('sent');+ t = 61_000;+ expect(await gate.run('Optimus can\'t listen yet', send)).toBe('sent');+ });++ it('a failed send still releases the gate', async () => {+ const gate = new AlertGate();+ await expect(gate.run('a', async () => { throw new Error('offline'); })).rejects.toThrow('offline');+ expect(await gate.run('b', async () => {})).toBe('sent');+ });+});diff --git a/src/core/alert-gate.ts b/src/core/alert-gate.tsnew file mode 100644index 00000000..d682ce5d--- /dev/null+++ b/src/core/alert-gate.ts@@ -0,0 +1,28 @@+/**+ * Guards broadcasts to every channel ("Mercury is shutting down", plugin+ * alerts): a broadcast started while another is in flight is dropped (a+ * channel that forwards to the owner's chats can't loop), and the same+ * message isn't sent again within `repeatMs`.+ */+export class AlertGate {+ private active = false;+ private recent = new Map<string, number>();++ constructor(private readonly repeatMs = 5 * 60_000, private readonly now: () => number = Date.now) {}++ /** Run `send` unless this broadcast is nested or a recent repeat. Returns whether it ran. */+ async run(message: string, send: () => Promise<void>): Promise<'sent' | 'nested' | 'repeat'> {+ if (this.active) return 'nested';+ const t = this.now();+ for (const [m, at] of this.recent) if (t - at > this.repeatMs) this.recent.delete(m);+ if (this.recent.has(message)) return 'repeat';+ this.recent.set(message, t);+ this.active = true;+ try {+ await send();+ } finally {+ this.active = false;+ }+ return 'sent';+ }+}diff --git a/src/plugins/channel-bridge.ts b/src/plugins/channel-bridge.tsindex f363ab28..c4eb0cd8 100644--- a/src/plugins/channel-bridge.ts+++ b/src/plugins/channel-bridge.ts@@ -223,6 +223,11 @@ export class PluginChannel extends BaseChannel {return this.type === 'desktop';}+ /** The desktop has no chat: broadcasts must skip it (its send() forwards to the owner's chats). */+ get noBroadcast(): boolean {+ return this.desktop;+ }+get pluginId(): string {return this.manifest.id;@@ -274,7 +279,8 @@ export class PluginChannel extends BaseChannel {if (this.roleOf(from) !== 'admin') return { accept: false, reason: 'only the owner can call Mercury' };if (p.voiceReady !== true) {const why = typeof p.voiceProblem === 'string' && p.voiceProblem ? p.voiceProblem : 'voice calls need an OpenAI provider: add your OpenAI key once in the dashboard (Providers) or with `mercury doctor`';- void this.send(`π I couldn't answer your call: ${why}.`).catch(() => {});+ if (this.desktop) this.notifyOwner(`π Optimus can't listen yet: ${why}.`);+ else void this.send(`π I couldn't answer your call: ${why}.`).catch(() => {});return { accept: false, reason: 'voice engine not ready' };}this.callStarted(String(p.callId ?? ''));diff --git a/src/plugins/channel.test.ts b/src/plugins/channel.test.tsindex c782c91e..15a2f08b 100644--- a/src/plugins/channel.test.ts+++ b/src/plugins/channel.test.ts@@ -237,7 +237,7 @@ describe('plugin channels (plugin API 1.1)', () => {const callsim = (method: string, params: Record<string, unknown>) => inject({ method, params }, 'callsim');it('answers only the owner, and only with a voice provider', async () => {- await installChannel();+ expect((await installChannel()).noBroadcast).toBe(false);await callsim('mercury/call/incoming', { callId: 'c1', from: 'x@lid', fromPhone: '15550002', voiceReady: true });await callsim('mercury/call/incoming', { callId: 'c2', from: '[email protected]', voiceReady: true });await callsim('mercury/call/incoming', { callId: 'c3', from: 'x@lid', fromPhone: '15550002', voiceReady: false });@@ -515,6 +515,7 @@ describe('plugin channels (plugin API 1.1)', () => {it('only the person at the computer is the owner', async () => {const ch = await installDesktop();+ expect(ch.noBroadcast).toBe(true); // broadcasts skip it: its send() forwards to the owner's chatsexpect(ch.roleOf('local')).toBe('admin');expect(ch.roleOf('15550002')).toBeNull();expect(ch.accessFor('admin')).toBe('extended');