fix: don't block one-shot openclaw subcommands; migrate to current plugin SDK

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>
This commit is contained in:
zhi
2026-05-08 07:36:00 +00:00
parent 1319f45aaf
commit 5b05e91d4e
13 changed files with 67 additions and 38 deletions

View File

@@ -1,6 +1,7 @@
import path from "node:path";
import os from "node:os";
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/core";
import { IdentityRegistry } from "./core/identity-registry.js";
import { ChannelStore } from "./core/channel-store.js";
import { scanPaddedCell } from "./core/padded-cell.js";
@@ -75,9 +76,10 @@ function markGatewayLifecycleRegistered(): void {
_G[_GATEWAY_LIFECYCLE_KEY] = true;
}
export default {
export default definePluginEntry({
id: "dirigent",
name: "Dirigent",
description: "Rule-based no-reply gate with provider/model override and turn management",
register(api: OpenClawPluginApi) {
const config = normalizeConfig(api);
const pluginDir = path.dirname(new URL(import.meta.url).pathname);
@@ -108,22 +110,26 @@ export default {
if (!isGatewayLifecycleRegistered()) {
markGatewayLifecycleRegistered();
const gatewayPort = getGatewayPort(api);
// Sidecar must only be started when the gateway actually boots.
// register() also runs inside one-shot CLI subprocesses that load
// plugins (e.g. `openclaw completion`, `openclaw doctor`); spawning a
// long-lived service from those would block them from exiting.
api.on("gateway_start", () => {
const gatewayPort = getGatewayPort(api);
startSideCar(
api.logger,
pluginDir,
config.sideCarPort,
moderatorBotToken,
undefined, // pluginApiToken — gateway handles auth for plugin routes
gatewayPort,
config.debugMode,
);
// Start unified services (no-reply API + moderator bot)
startSideCar(
api.logger,
pluginDir,
config.sideCarPort,
moderatorBotToken,
undefined, // pluginApiToken — gateway handles auth for plugin routes
gatewayPort,
config.debugMode,
);
if (!moderatorBotToken) {
api.logger.info("dirigent: moderatorBotToken not set — moderator features disabled");
}
if (!moderatorBotToken) {
api.logger.info("dirigent: moderatorBotToken not set — moderator features disabled");
}
});
tryAutoScanPaddedCell();
@@ -296,4 +302,4 @@ export default {
api.logger.info("dirigent: plugin registered (v2)");
},
};
});