Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(optimus): start on Linux systems without Wayland, ALSA or X11 libraries (0.4.8)

Salman Qureshibdf1ec3d1aa7
diff --git a/.github/workflows/plugin-optimus.yml b/.github/workflows/plugin-optimus.yml
index 3c226201..826895e6 100644
--- a/.github/workflows/plugin-optimus.yml
+++ b/.github/workflows/plugin-optimus.yml
@@ -52,10 +52,26 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: 22
+ - name: Fallback libraries (Linux)
+ # Copies of the non-glibc libraries the companion links (ALSA, libxcb and
+ # their dependencies), used only on systems that lack them.
+ if: runner.os == 'Linux'
+ run: |
+ bin="plugins/optimus/companion/target/${{ matrix.target }}/release/mercury-optimus"
+ ldd "$bin"
+ mkdir -p fallback-libs
+ ldd "$bin" | awk '$2 == "=>" && $3 ~ /^\// {print $1, $3}' | while read -r name path; do
+ case "$name" in
+ libc.so.*|libm.so.*|libgcc_s.so.*|libpthread.so.*|libdl.so.*|librt.so.*|libresolv.so.*|ld-linux*|linux-vdso*) continue ;;
+ esac
+ mkdir -p "fallback-libs/$name" && cp -L "$path" "fallback-libs/$name/$name"
+ done
+ find fallback-libs -type f
+ if ldd "$bin" | grep -q libwayland; then echo "::error::libwayland-client must be loaded at run time (wayland-sys dlopen)"; exit 1; fi
- name: Package
run: |
ext=""; [ "${{ matrix.platform }}" = "win32-x64" ] && ext=".exe"
- node plugins/optimus/package.mjs "plugins/optimus/companion/target/${{ matrix.target }}/release/mercury-optimus$ext" ${{ matrix.platform }} dist
+ node plugins/optimus/package.mjs "plugins/optimus/companion/target/${{ matrix.target }}/release/mercury-optimus$ext" ${{ matrix.platform }} dist fallback-libs
ls -la dist
- uses: actions/upload-artifact@v4
with:
@@ -85,3 +101,25 @@ jobs:
run: |
mkdir pkg && tar -xzf dist-plugin/mercury-plugin-linux-x64.tgz -C pkg
MERCURY_HOME="$RUNNER_TEMP/mh" MERCURY_PLUGIN_CATALOG_URL=off node dist/index.js plugins validate pkg
+
+ bare-linux:
+ # A plain ubuntu:24.04 container has no ALSA, libxcb or Wayland: the
+ # launcher must still start the companion, using the bundled copies.
+ needs: build
+ runs-on: ubuntu-24.04
+ steps:
+ - uses: actions/download-artifact@v4
+ with:
+ name: mercury-plugin-optimus-linux-x64
+ path: dist-plugin
+ - name: Starts without the desktop libraries
+ run: |
+ mkdir pkg && tar -xzf dist-plugin/mercury-plugin-linux-x64.tgz -C pkg
+ docker run --rm -v "$PWD/pkg:/p:ro" ubuntu:24.04 sh -c '
+ set -e
+ if ldconfig -p | grep -E "libasound|libxcb|libwayland"; then echo "the image already has these libraries"; exit 1; fi
+ /p/bin/mercury-optimus --probe && echo "launcher: probe ok"
+ printf "%s\n" "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2025-06-18\",\"capabilities\":{},\"clientInfo\":{\"name\":\"ci\",\"version\":\"0\"}}}" | timeout 10 /p/bin/mercury-optimus > out.txt 2> err.txt || true
+ cat err.txt; head -c 400 out.txt; echo
+ grep -q "\"id\":1" out.txt
+ '
diff --git a/plugins/optimus/companion/Cargo.lock b/plugins/optimus/companion/Cargo.lock
index f50cb831..d5e7d059 100644
--- a/plugins/optimus/companion/Cargo.lock
+++ b/plugins/optimus/companion/Cargo.lock
@@ -1280,7 +1280,7 @@ dependencies = [
[[package]]
name = "mercury-optimus"
-version = "0.4.7"
+version = "0.4.8"
dependencies = [
"base64 0.22.1",
"core-foundation",
@@ -1299,6 +1299,7 @@ dependencies = [
"tokio",
"tokio-websockets",
"ureq",
+ "wayland-sys",
"windows-sys 0.61.2",
"x11rb",
"xcap",
@@ -2695,6 +2696,7 @@ dependencies = [
"libc",
"log",
"memoffset",
+ "once_cell",
"pkg-config",
]
diff --git a/plugins/optimus/companion/Cargo.toml b/plugins/optimus/companion/Cargo.toml
index 2015b46b..8d20b57e 100644
--- a/plugins/optimus/companion/Cargo.toml
+++ b/plugins/optimus/companion/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "mercury-optimus"
-version = "0.4.7"
+version = "0.4.8"
edition = "2024"
rust-version = "1.94"
description = "Optimus: full computer control for Mercury (screen, mouse, keyboard, apps, voice)"
@@ -34,6 +34,9 @@ dispatch2 = "0.3"
[target.'cfg(target_os = "linux")'.dependencies]
x11rb = { version = "0.13", features = ["xfixes", "shape"] }
+# Load libwayland-client at run time (xcap's Wayland capture), so Optimus also
+# starts on X11-only and headless systems that don't have it.
+wayland-sys = { version = "0.31", features = ["client", "dlopen"] }
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.61", features = ["Win32_UI_HiDpi", "Win32_Foundation", "Win32_UI_WindowsAndMessaging", "Win32_UI_Input_KeyboardAndMouse", "Win32_Graphics_Gdi", "Win32_System_LibraryLoader"] }
diff --git a/plugins/optimus/companion/src/main.rs b/plugins/optimus/companion/src/main.rs
index 309b7004..455a1726 100644
--- a/plugins/optimus/companion/src/main.rs
+++ b/plugins/optimus/companion/src/main.rs
@@ -276,6 +276,12 @@ fn channel_request(method: &str, params: &Value) -> Result<Value, String> {
fn main() {
let args: Vec<String> = std::env::args().collect();
+ // linux/mercury-optimus.sh runs this to check that every library loads.
+ if args.get(1).map(String::as_str) == Some("--probe") {
+ return;
+ }
+ #[cfg(target_os = "linux")]
+ restore_library_path();
if args.get(1).map(String::as_str) == Some("agent") {
#[cfg(target_os = "macos")]
{
@@ -354,3 +360,21 @@ fn main() {
let _ = shell.send_core(json!({ "quit": true }));
std::thread::sleep(Duration::from_millis(300));
}
+
+/// The Linux launcher may point LD_LIBRARY_PATH at the libraries bundled with
+/// the plugin. They're loaded by now, so put the owner's own value back before
+/// any app Optimus opens inherits it.
+#[cfg(target_os = "linux")]
+fn restore_library_path() {
+ let Some(original) = std::env::var_os("OPTIMUS_SYSTEM_LD_LIBRARY_PATH") else { return };
+ // SAFETY: called first thing in main, before any other thread exists.
+ unsafe {
+ if std::env::var_os("OPTIMUS_LD_LIBRARY_PATH_SET").is_some() {
+ std::env::set_var("LD_LIBRARY_PATH", original);
+ } else {
+ std::env::remove_var("LD_LIBRARY_PATH");
+ }
+ std::env::remove_var("OPTIMUS_SYSTEM_LD_LIBRARY_PATH");
+ std::env::remove_var("OPTIMUS_LD_LIBRARY_PATH_SET");
+ }
+}
diff --git a/plugins/optimus/linux/mercury-optimus.sh b/plugins/optimus/linux/mercury-optimus.sh
new file mode 100755
index 00000000..4f55205a
--- /dev/null
+++ b/plugins/optimus/linux/mercury-optimus.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+# Optimus launcher for Linux (installed as bin/mercury-optimus).
+#
+# The companion links ALSA and libxcb. Desktops have both; servers and minimal
+# installs often don't. Start it with the system's own libraries, and when the
+# loader reports one missing, add the copy shipped in ../lib/<name>/ for that
+# library only, then try again. System libraries always win when present.
+here=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
+bin="$here/mercury-optimus-bin"
+libs="$here/../lib"
+
+extra=""
+tries=0
+while [ "$tries" -lt 12 ]; do
+ path="$extra"
+ [ -n "${LD_LIBRARY_PATH:-}" ] && path="${path:+$path:}$LD_LIBRARY_PATH"
+ err=$(LD_LIBRARY_PATH="$path" "$bin" --probe 2>&1 >/dev/null) && break
+ lib=$(printf '%s\n' "$err" | sed -n 's/.*error while loading shared libraries: \([^:]*\):.*/\1/p' | head -n 1)
+ # Not a missing library, or one we don't ship: start anyway so Mercury sees the real error.
+ { [ -n "$lib" ] && [ -d "$libs/$lib" ]; } || break
+ case ":$extra:" in *":$libs/$lib:"*) break ;; esac
+ extra="${extra:+$extra:}$libs/$lib"
+ tries=$((tries + 1))
+done
+
+if [ -n "$extra" ]; then
+ # The companion puts the owner's value back before it opens any app.
+ export OPTIMUS_SYSTEM_LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}"
+ [ -n "${LD_LIBRARY_PATH+x}" ] && export OPTIMUS_LD_LIBRARY_PATH_SET=1
+ LD_LIBRARY_PATH="$extra${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
+ export LD_LIBRARY_PATH
+fi
+exec "$bin" "$@"
diff --git a/plugins/optimus/mercury-plugin.template.json b/plugins/optimus/mercury-plugin.template.json
index 7bdc0fd2..774bef01 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.4.7",
+ "version": "0.4.8",
"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/plugins/optimus/package.mjs b/plugins/optimus/package.mjs
index 4534c320..5add5846 100644
--- a/plugins/optimus/package.mjs
+++ b/plugins/optimus/package.mjs
@@ -1,16 +1,20 @@
// Build a per-platform plugin package from a compiled companion binary:
-// node plugins/optimus/package.mjs <binary> <platform-key> <out-dir>
+// node plugins/optimus/package.mjs <binary> <platform-key> <out-dir> [fallback-libs-dir]
// Writes <out-dir>/mercury-plugin-<platform-key>.tgz containing the
-// manifest (with this platform and the binary's SHA-256 pinned), the
+// manifest (with this platform and every file's SHA-256 pinned), the
// binary under bin/, and the README.
+//
+// Linux packages start through linux/mercury-optimus.sh (bin/mercury-optimus),
+// which runs bin/mercury-optimus-bin and falls back to the libraries in
+// fallback-libs-dir (copied to lib/<soname>/) only when the system lacks one.
import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';
-import { chmodSync, copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { chmodSync, copyFileSync, cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { basename, dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
-const [binary, platform, outDir] = process.argv.slice(2);
+const [binary, platform, outDir, fallbackLibs] = process.argv.slice(2);
if (!binary || !platform || !outDir) {
console.error('usage: node package.mjs <binary> <platform-key> <out-dir>');
process.exit(2);
@@ -20,20 +24,41 @@ const manifest = JSON.parse(readFileSync(join(here, 'mercury-plugin.template.jso
const exe = platform.startsWith('win32') ? 'mercury-optimus.exe' : 'mercury-optimus';
const rel = `bin/${exe}`;
+const linux = platform.startsWith('linux');
+
const stage = mkdtempSync(join(tmpdir(), 'mercury-optimus-pkg-'));
mkdirSync(join(stage, 'bin'));
-copyFileSync(binary, join(stage, rel));
+const entries = ['mercury-plugin.json', 'bin', 'README.md'];
+if (linux) {
+ copyFileSync(binary, join(stage, 'bin/mercury-optimus-bin'));
+ chmodSync(join(stage, 'bin/mercury-optimus-bin'), 0o755);
+ copyFileSync(join(here, 'linux/mercury-optimus.sh'), join(stage, rel));
+ if (fallbackLibs && existsSync(fallbackLibs)) {
+ cpSync(fallbackLibs, join(stage, 'lib'), { recursive: true });
+ entries.push('lib');
+ }
+} else {
+ copyFileSync(binary, join(stage, rel));
+}
chmodSync(join(stage, rel), 0o755);
copyFileSync(join(here, 'README.md'), join(stage, 'README.md'));
+/** Every file under bin/ and lib/, relative to the package root. */
+const files = (dir) => readdirSync(join(stage, dir)).flatMap((n) => {
+ const r = `${dir}/${n}`;
+ return statSync(join(stage, r)).isDirectory() ? files(r) : [r];
+});
manifest.platforms = [platform];
-manifest.checksums = { [rel]: createHash('sha256').update(readFileSync(join(stage, rel))).digest('hex') };
+manifest.checksums = Object.fromEntries(
+ entries.filter((e) => e === 'bin' || e === 'lib').flatMap(files).sort()
+ .map((f) => [f, createHash('sha256').update(readFileSync(join(stage, f))).digest('hex')]),
+);
writeFileSync(join(stage, 'mercury-plugin.json'), `${JSON.stringify(manifest, null, 2)}\n`);
mkdirSync(outDir, { recursive: true });
const out = resolve(outDir, `mercury-plugin-${platform}.tgz`);
// Archive name relative to cwd: GNU tar on Windows reads "D:\\…" as a remote host.
-execFileSync('tar', ['-czf', basename(out), '-C', stage, 'mercury-plugin.json', 'bin', 'README.md'], { cwd: dirname(out) });
+execFileSync('tar', ['-czf', basename(out), '-C', stage, ...entries], { cwd: dirname(out) });
const sha = createHash('sha256').update(readFileSync(out)).digest('hex');
writeFileSync(`${out}.sha256`, `${sha} mercury-plugin-${platform}.tgz\n`);
rmSync(stage, { recursive: true, force: true });
diff --git a/website/docs/integrations/optimus.mdx b/website/docs/integrations/optimus.mdx
index ae476f33..192faee8 100644
--- a/website/docs/integrations/optimus.mdx
+++ b/website/docs/integrations/optimus.mdx
@@ -8,7 +8,7 @@ keywords: [mercury, optimus, computer use, desktop agent, voice, talk key, plugi
Optimus is a plugin that lets Mercury use your computer. It looks at the screen, then moves the pointer, scrolls and types the way a person does, and opens apps, URLs and files. Ask in any Mercury chat ("look at my screen", "open Safari and search for…"), or press a **talk key**, say what you want, and hear Mercury answer while it works.
-It's an optional [plugin](../cli-commands/plugins), version **0.4.7**, from the official plugin catalog. It needs Mercury **1.4.0** or newer.
+It's an optional [plugin](../cli-commands/plugins), version **0.4.8**, from the official plugin catalog. It needs Mercury **1.4.0** or newer.
## Platform support
@@ -68,7 +68,7 @@ Nothing to grant. Optimus can't control windows that run as administrator, such
### Linux
-On **X11** nothing to grant. On **Wayland**, see the limits in [Platform support](#platform-support). Pasting uses `xclip` on X11 and `wl-copy` on Wayland, so install the one for your session if you want the paste fallback.
+On **X11** nothing to grant. On **Wayland**, see the limits in [Platform support](#platform-support). Optimus also starts on servers and minimal installs that lack ALSA or the X11 libraries: the package carries its own copies and uses them only when your system has none. Seeing and using the screen still needs a running X11 or Wayland session, and voice needs a sound device. Pasting uses `xclip` on X11 and `wl-copy` on Wayland, so install the one for your session if you want the paste fallback.
## Chat and listening