Sidecar lifecycle:
- Move startSideCar() out of register() into an api.on("gateway_start", ...)
handler. register() runs in every CLI subprocess that loads plugins
(e.g. `openclaw completion`, `openclaw doctor`); eagerly spawning a
long-lived process there hung `openclaw update`'s post-update steps.
- Spawn the sidecar with detached: true, stdio routed to a log file fd,
and call .unref() so the host's event loop is never held by the child.
Even if a future caller invokes startSideCar in a non-gateway context,
it can no longer block that host from exiting.
- Sidecar logs now go to ~/.openclaw/logs/dirigent-sidecar.log instead of
being piped through the host logger.
Plugin SDK convention update:
- Wrap default export with definePluginEntry({ id, name, description, register })
per the current openclaw plugin authoring contract.
- Switch all imports from the deprecated root barrel "openclaw/plugin-sdk"
to focused subpaths "openclaw/plugin-sdk/core" and
"openclaw/plugin-sdk/plugin-entry".
- Modernize openclaw.plugin.json: drop entry/version, add description,
declare contracts.tools[] for the 6 tools, set activation.onStartup: true
so gateway_start fires for this plugin at boot.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
132 lines
3.7 KiB
TypeScript
132 lines
3.7 KiB
TypeScript
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { spawn, type ChildProcess } from "node:child_process";
|
|
|
|
let noReplyProcess: ChildProcess | null = null;
|
|
|
|
const LOCK_FILE = path.join(os.tmpdir(), "dirigent-sidecar.lock");
|
|
const LOG_FILE = path.join(os.homedir(), ".openclaw", "logs", "dirigent-sidecar.log");
|
|
|
|
function readLock(): { pid: number } | null {
|
|
try {
|
|
const raw = fs.readFileSync(LOCK_FILE, "utf8").trim();
|
|
return { pid: Number(raw) };
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function writeLock(pid: number): void {
|
|
try { fs.writeFileSync(LOCK_FILE, String(pid)); } catch { /* ignore */ }
|
|
}
|
|
|
|
function clearLock(): void {
|
|
try { fs.unlinkSync(LOCK_FILE); } catch { /* ignore */ }
|
|
}
|
|
|
|
function isLockHeld(): boolean {
|
|
const lock = readLock();
|
|
if (!lock) return false;
|
|
try {
|
|
process.kill(lock.pid, 0);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function startSideCar(
|
|
logger: { info: (m: string) => void; warn: (m: string) => void },
|
|
pluginDir: string,
|
|
port = 8787,
|
|
moderatorToken?: string,
|
|
pluginApiToken?: string,
|
|
gatewayPort?: number,
|
|
debugMode?: boolean,
|
|
): void {
|
|
logger.info(`dirigent: startSideCar called, pluginDir=${pluginDir}`);
|
|
|
|
if (noReplyProcess) {
|
|
logger.info("dirigent: no-reply API already running (local ref), skipping");
|
|
return;
|
|
}
|
|
|
|
if (isLockHeld()) {
|
|
logger.info("dirigent: no-reply API already running (lock file), skipping");
|
|
return;
|
|
}
|
|
|
|
// services/main.mjs lives alongside the plugin directory in the distribution
|
|
const serverPath = path.resolve(pluginDir, "services", "main.mjs");
|
|
logger.info(`dirigent: resolved serverPath=${serverPath}`);
|
|
|
|
if (!fs.existsSync(serverPath)) {
|
|
logger.warn(`dirigent: services/main.mjs not found at ${serverPath}, skipping`);
|
|
return;
|
|
}
|
|
|
|
logger.info("dirigent: services/main.mjs found, spawning process...");
|
|
|
|
// Build plugin API URL from gateway port, or use a default
|
|
const pluginApiUrl = gatewayPort
|
|
? `http://127.0.0.1:${gatewayPort}`
|
|
: "http://127.0.0.1:18789";
|
|
|
|
const env: NodeJS.ProcessEnv = {
|
|
...process.env,
|
|
SERVICES_PORT: String(port),
|
|
PLUGIN_API_URL: pluginApiUrl,
|
|
};
|
|
|
|
if (moderatorToken) {
|
|
env.MODERATOR_TOKEN = moderatorToken;
|
|
}
|
|
if (pluginApiToken) {
|
|
env.PLUGIN_API_TOKEN = pluginApiToken;
|
|
}
|
|
if (debugMode !== undefined) {
|
|
env.DEBUG_MODE = debugMode ? "true" : "false";
|
|
}
|
|
|
|
// Spawn so the host process is never blocked from exiting:
|
|
// - detached: sidecar is its own process group leader
|
|
// - stdio piped to a log file fd (no parent-side pipes to drain)
|
|
// - unref(): Node's event loop will not wait on this child
|
|
// stopSideCar() still works because we keep a ChildProcess handle for
|
|
// signalling, but the OS lifecycle of the sidecar is decoupled from us.
|
|
fs.mkdirSync(path.dirname(LOG_FILE), { recursive: true });
|
|
const logFd = fs.openSync(LOG_FILE, "a");
|
|
try {
|
|
noReplyProcess = spawn(process.execPath, [serverPath], {
|
|
env,
|
|
stdio: ["ignore", logFd, logFd],
|
|
detached: true,
|
|
});
|
|
} finally {
|
|
fs.closeSync(logFd);
|
|
}
|
|
|
|
if (noReplyProcess.pid) {
|
|
writeLock(noReplyProcess.pid);
|
|
}
|
|
|
|
noReplyProcess.unref();
|
|
|
|
noReplyProcess.on("exit", (code, signal) => {
|
|
logger.info(`dirigent: services exited (code=${code}, signal=${signal})`);
|
|
clearLock();
|
|
noReplyProcess = null;
|
|
});
|
|
|
|
logger.info(`dirigent: services started (pid=${noReplyProcess.pid}, port=${port}, log=${LOG_FILE})`);
|
|
}
|
|
|
|
export function stopSideCar(logger: { info: (m: string) => void }): void {
|
|
if (!noReplyProcess) return;
|
|
logger.info("dirigent: stopping sidecar");
|
|
noReplyProcess.kill("SIGTERM");
|
|
noReplyProcess = null;
|
|
clearLock();
|
|
}
|