Skip to content

Commit 4a285d5

Browse files
hussein1362Hussein Nourelddine
andauthored
feat(status): detect external plugin version drift
Surface active official external plugin version drift in gateway status diagnostics so users can see when a host/package update left npm or ClawHub plugins behind the running local gateway. The advisory uses the daemon service install records, compares against the running gateway version, gives detailed fix commands in deep status, and avoids local-state drift checks for remote gateway mode or explicit status probe URLs. Co-authored-by: Hussein Nourelddine <[email protected]>
1 parent 07821e4 commit 4a285d5

13 files changed

Lines changed: 819 additions & 53 deletions

‎src/cli/daemon-cli/status.gather.test.ts‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ const inspectPortConnections = vi.fn<(port: number) => Promise<PortConnections>>
4747
}),
4848
);
4949
const readLastGatewayErrorLine = vi.fn(async (_env?: NodeJS.ProcessEnv) => null);
50+
const loadInstalledPluginIndexInstallRecords = vi.fn<
51+
(params?: {
52+
env?: NodeJS.ProcessEnv;
53+
stateDir?: string;
54+
filePath?: string;
55+
}) => Promise<Record<string, unknown>>
56+
>(async (_params?) => ({}));
5057
const readGatewayRestartHandoffSync = vi.fn<
5158
(_env?: NodeJS.ProcessEnv) => GatewayRestartHandoff | null
5259
>(() => null);
@@ -196,6 +203,14 @@ vi.mock("./probe.js", () => ({
196203
probeGatewayStatus: (opts: unknown) => callGatewayStatusProbe(opts),
197204
}));
198205

206+
vi.mock("../../plugins/installed-plugin-index-record-reader.js", () => ({
207+
loadInstalledPluginIndexInstallRecords: (params?: {
208+
env?: NodeJS.ProcessEnv;
209+
stateDir?: string;
210+
filePath?: string;
211+
}) => loadInstalledPluginIndexInstallRecords(params),
212+
}));
213+
199214
vi.mock("./restart-health.js", () => ({
200215
inspectGatewayRestart: (opts: unknown) => inspectGatewayRestart(opts),
201216
}));
@@ -230,6 +245,8 @@ describe("gatherDaemonStatus", () => {
230245
createConfigIOCalls.mockClear();
231246
findStaleOpenClawUpdateLaunchdJobs.mockReset();
232247
findStaleOpenClawUpdateLaunchdJobs.mockResolvedValue([]);
248+
loadInstalledPluginIndexInstallRecords.mockClear();
249+
loadInstalledPluginIndexInstallRecords.mockResolvedValue({});
233250
loadGatewayTlsRuntime.mockClear();
234251
inspectGatewayRestart.mockClear();
235252
inspectPortConnections.mockClear();
@@ -396,6 +413,8 @@ describe("gatherDaemonStatus", () => {
396413
expect(probeInput.tlsFingerprint).toBeUndefined();
397414
expect(status.gateway?.probeUrl).toBe("wss://override.example:18790");
398415
expect(status.rpc?.url).toBe("wss://override.example:18790");
416+
expect(loadInstalledPluginIndexInstallRecords).not.toHaveBeenCalled();
417+
expect(status.pluginVersionDrift).toBeUndefined();
399418
});
400419

401420
it("uses fallback network details when interface discovery throws during status inspection", async () => {
@@ -587,7 +606,9 @@ describe("gatherDaemonStatus", () => {
587606
});
588607

589608
expect(inspectPortConnections).not.toHaveBeenCalled();
609+
expect(loadInstalledPluginIndexInstallRecords).not.toHaveBeenCalled();
590610
expect(status.connections).toBeUndefined();
611+
expect(status.pluginVersionDrift).toBeUndefined();
591612
});
592613

593614
it("uses the fast config path for plain same-file status reads", async () => {
@@ -900,4 +921,101 @@ describe("gatherDaemonStatus", () => {
900921
staleGatewayPids: [9000],
901922
});
902923
});
924+
925+
it("compares plugin drift against the running gateway version from the probe, not the CLI VERSION", async () => {
926+
// Gateway is still running an older version than the invoking CLI.
927+
// An npm plugin pinned to the running gateway version must NOT be
928+
// reported as drifted just because the CLI package is newer.
929+
callGatewayStatusProbe.mockResolvedValueOnce({
930+
ok: true,
931+
url: "ws://127.0.0.1:19001",
932+
error: null,
933+
server: { version: "2026.5.4", connId: "c1" },
934+
} as never);
935+
loadInstalledPluginIndexInstallRecords.mockResolvedValueOnce({
936+
whatsapp: {
937+
source: "npm",
938+
resolvedName: "@openclaw/whatsapp",
939+
resolvedVersion: "2026.5.4",
940+
},
941+
} as never);
942+
943+
const status = await gatherDaemonStatus({
944+
rpc: {},
945+
probe: true,
946+
deep: true,
947+
});
948+
949+
expect(status.pluginVersionDrift?.gatewayVersion).toBe("2026.5.4");
950+
expect(status.pluginVersionDrift?.drifts).toEqual([]);
951+
});
952+
953+
it("flags drift against the running gateway version when an npm plugin lags behind it", async () => {
954+
callGatewayStatusProbe.mockResolvedValueOnce({
955+
ok: true,
956+
url: "ws://127.0.0.1:19001",
957+
error: null,
958+
server: { version: "2026.5.4", connId: "c1" },
959+
} as never);
960+
loadInstalledPluginIndexInstallRecords.mockResolvedValueOnce({
961+
whatsapp: {
962+
source: "npm",
963+
resolvedName: "@openclaw/whatsapp",
964+
resolvedVersion: "2026.5.3",
965+
},
966+
} as never);
967+
968+
const status = await gatherDaemonStatus({
969+
rpc: {},
970+
probe: true,
971+
deep: true,
972+
});
973+
974+
expect(status.pluginVersionDrift?.gatewayVersion).toBe("2026.5.4");
975+
expect(status.pluginVersionDrift?.drifts.map((d) => d.pluginId)).toEqual(["whatsapp"]);
976+
});
977+
978+
it("reads install records from the merged daemon service environment, not the CLI process env", async () => {
979+
await gatherDaemonStatus({
980+
rpc: {},
981+
probe: true,
982+
deep: true,
983+
});
984+
985+
// The mock daemon service command sets OPENCLAW_STATE_DIR=/tmp/openclaw-daemon,
986+
// distinct from the CLI process OPENCLAW_STATE_DIR=/tmp/openclaw-cli. Drift
987+
// detection must inspect the daemon profile's install records.
988+
expect(loadInstalledPluginIndexInstallRecords).toHaveBeenCalledWith(
989+
expect.objectContaining({
990+
env: expect.objectContaining({
991+
OPENCLAW_STATE_DIR: "/tmp/openclaw-daemon",
992+
}),
993+
}),
994+
);
995+
});
996+
997+
it("reads install records and computes drift outside deep mode", async () => {
998+
loadInstalledPluginIndexInstallRecords.mockResolvedValueOnce({
999+
whatsapp: {
1000+
source: "npm",
1001+
resolvedName: "@openclaw/whatsapp",
1002+
resolvedVersion: "2026.5.3",
1003+
},
1004+
} as never);
1005+
1006+
const status = await gatherDaemonStatus({
1007+
rpc: {},
1008+
probe: true,
1009+
deep: false,
1010+
});
1011+
1012+
expect(loadInstalledPluginIndexInstallRecords).toHaveBeenCalledWith(
1013+
expect.objectContaining({
1014+
env: expect.objectContaining({
1015+
OPENCLAW_STATE_DIR: "/tmp/openclaw-daemon",
1016+
}),
1017+
}),
1018+
);
1019+
expect(status.pluginVersionDrift?.drifts.map((d) => d.pluginId)).toEqual(["whatsapp"]);
1020+
});
9031021
});

‎src/cli/daemon-cli/status.gather.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ import {
3838
type GatewayRestartHandoff,
3939
} from "../../infra/restart-handoff.js";
4040
import { resolveConfiguredLogFilePath } from "../../logging/log-file-path.js";
41+
import { loadInstalledPluginIndexInstallRecords } from "../../plugins/installed-plugin-index-record-reader.js";
42+
import {
43+
detectPluginVersionDrift,
44+
type PluginVersionDriftReport,
45+
} from "../../plugins/plugin-version-drift.js";
4146
import { createLazyImportLoader } from "../../shared/lazy-promise.js";
4247
import { VERSION } from "../../version.js";
4348
import { normalizeListenerAddress, parsePortFromArgs, pickProbeHostForBind } from "./shared.js";
@@ -323,6 +328,13 @@ export type DaemonStatus = {
323328
staleGatewayPids: number[];
324329
};
325330
extraServices: Array<{ label: string; detail: string; scope: string }>;
331+
/**
332+
* Plugin version drift report. Surfaces active official external plugins
333+
* whose installed version does not match the running gateway version, which
334+
* can happen after `npm install -g openclaw@<v>` updates the gateway binary
335+
* without a corresponding `openclaw plugins update`.
336+
*/
337+
pluginVersionDrift?: PluginVersionDriftReport;
326338
};
327339

328340
function shouldReportPortUsage(status: PortUsageStatus | undefined, rpcOk?: boolean) {
@@ -637,6 +649,30 @@ export async function gatherDaemonStatus(
637649
lastError = (await readLastGatewayErrorLine(mergedDaemonEnv as NodeJS.ProcessEnv)) ?? undefined;
638650
}
639651

652+
// Plugin version drift detection.
653+
// Compares active official external plugins against the *running* local
654+
// gateway version reported by the probe handshake, falling back to the
655+
// invoking CLI VERSION only when no gateway version is available. Reading
656+
// records with the merged daemon environment inspects the managed service's
657+
// profile/state dir, so remote/explicit URL probes need remote-owned
658+
// diagnostics instead.
659+
// Best-effort: unreadable install records omit this advisory report.
660+
let pluginVersionDrift: PluginVersionDriftReport | undefined;
661+
if (daemonCfg.gateway?.mode !== "remote" && !probeUrlOverride) {
662+
try {
663+
const installRecords = await loadInstalledPluginIndexInstallRecords({
664+
env: mergedDaemonEnv as NodeJS.ProcessEnv,
665+
});
666+
pluginVersionDrift = detectPluginVersionDrift({
667+
gatewayVersion: gatewayVersion ?? VERSION,
668+
installRecords,
669+
config: daemonCfg,
670+
});
671+
} catch {
672+
pluginVersionDrift = undefined;
673+
}
674+
}
675+
640676
return {
641677
cli: resolveCliStatusSummary(),
642678
logFile: resolveConfiguredLogFilePath(cliCfg),
@@ -686,6 +722,7 @@ export async function gatherDaemonStatus(
686722
}
687723
: {}),
688724
extraServices,
725+
...(pluginVersionDrift ? { pluginVersionDrift } : {}),
689726
};
690727
}
691728

‎src/cli/daemon-cli/status.print.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,68 @@ describe("printDaemonStatus", () => {
517517
expect(runtime.error).not.toHaveBeenCalled();
518518
});
519519

520+
it("prints a terse plugin drift warning outside deep mode", () => {
521+
printDaemonStatus(
522+
{
523+
service: {
524+
label: "LaunchAgent",
525+
loaded: true,
526+
loadedText: "loaded",
527+
notLoadedText: "not loaded",
528+
runtime: { status: "running", pid: 8000 },
529+
},
530+
pluginVersionDrift: {
531+
gatewayVersion: "2026.5.4",
532+
drifts: [
533+
{
534+
pluginId: "whatsapp",
535+
installedVersion: "2026.5.3",
536+
gatewayVersion: "2026.5.4",
537+
source: "npm",
538+
},
539+
],
540+
},
541+
extraServices: [],
542+
},
543+
{ json: false, deep: false },
544+
);
545+
546+
expectMockLineContains(runtime.log, "Plugin version drift: 1 active official plugin");
547+
expectMockLineContains(runtime.log, "openclaw gateway status --deep");
548+
expect(runtime.log.mock.calls.map(([line]) => line).join("\n")).not.toContain("whatsapp:");
549+
});
550+
551+
it("prints detailed plugin drift entries in deep mode", () => {
552+
printDaemonStatus(
553+
{
554+
service: {
555+
label: "LaunchAgent",
556+
loaded: true,
557+
loadedText: "loaded",
558+
notLoadedText: "not loaded",
559+
runtime: { status: "running", pid: 8000 },
560+
},
561+
pluginVersionDrift: {
562+
gatewayVersion: "2026.5.4",
563+
drifts: [
564+
{
565+
pluginId: "whatsapp",
566+
installedVersion: "2026.5.3",
567+
gatewayVersion: "2026.5.4",
568+
source: "clawhub",
569+
},
570+
],
571+
},
572+
extraServices: [],
573+
},
574+
{ json: false, deep: true },
575+
);
576+
577+
expectMockLineContains(runtime.log, "- whatsapp: 2026.5.3 (clawhub)");
578+
expectMockLineContains(runtime.log, "openclaw plugins update <plugin-id>");
579+
expectMockLineContains(runtime.log, "openclaw gateway restart");
580+
});
581+
520582
it("does not print systemd user-service hints when a gateway responds", () => {
521583
const platform = vi.spyOn(process, "platform", "get").mockReturnValue("linux");
522584
isSystemdUnavailableDetailMock.mockReturnValue(true);

‎src/cli/daemon-cli/status.print.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function formatConnectionLine(
8686
return `${pid}${ppid}${direction}${command}${address}${commandLine}`;
8787
}
8888

89-
export function printDaemonStatus(status: DaemonStatus, opts: { json: boolean }) {
89+
export function printDaemonStatus(status: DaemonStatus, opts: { json: boolean; deep?: boolean }) {
9090
if (opts.json) {
9191
const sanitized = sanitizeDaemonStatusForJson(status);
9292
defaultRuntime.writeJson(sanitized);
@@ -453,6 +453,35 @@ export function printDaemonStatus(status: DaemonStatus, opts: { json: boolean })
453453
spacer();
454454
}
455455

456+
const drift = status.pluginVersionDrift;
457+
if (drift && drift.drifts.length > 0) {
458+
defaultRuntime.log(
459+
warnText(
460+
`Plugin version drift: ${drift.drifts.length} active official plugin${
461+
drift.drifts.length === 1 ? "" : "s"
462+
} not on gateway ${drift.gatewayVersion}`,
463+
),
464+
);
465+
if (opts.deep) {
466+
for (const entry of drift.drifts) {
467+
const sourceLabel = entry.source === "clawhub" ? "clawhub" : "npm";
468+
defaultRuntime.log(
469+
`- ${warnText(entry.pluginId)}: ${entry.installedVersion} (${sourceLabel}) → expected ${drift.gatewayVersion}`,
470+
);
471+
}
472+
defaultRuntime.log(
473+
`${label("Fix:")} ${formatCliCommand("openclaw plugins update <plugin-id>")} for each drifted plugin, then ${formatCliCommand("openclaw gateway restart")}.`,
474+
);
475+
} else {
476+
defaultRuntime.log(
477+
infoText(
478+
`Run ${formatCliCommand("openclaw gateway status --deep")} for affected plugin ids and fix commands.`,
479+
),
480+
);
481+
}
482+
spacer();
483+
}
484+
456485
if (extraServices.length > 0) {
457486
defaultRuntime.log(
458487
infoText(

‎src/cli/daemon-cli/status.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ describe("runDaemonStatus", () => {
7474
).rejects.toThrow("__exit__:1");
7575

7676
expect(printDaemonStatus).toHaveBeenCalledTimes(1);
77+
expect(printDaemonStatus).toHaveBeenCalledWith(expect.any(Object), {
78+
json: false,
79+
deep: false,
80+
});
7781
});
7882

7983
it("forwards require-rpc to daemon status gathering", async () => {

‎src/cli/daemon-cli/status.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function runDaemonStatus(opts: DaemonStatusOptions) {
1919
requireRpc: opts.requireRpc,
2020
deep: opts.deep === true,
2121
});
22-
printDaemonStatus(status, { json: opts.json });
22+
printDaemonStatus(status, { json: opts.json, deep: opts.deep === true });
2323
if (opts.requireRpc && !status.rpc?.ok) {
2424
defaultRuntime.exit(1);
2525
}

‎src/cli/update-cli.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,12 @@ vi.mock("../utils.js", async (importOriginal) => {
211211
};
212212
});
213213

214-
vi.mock("../plugins/update.js", () => ({
214+
vi.mock("../plugins/official-external-install-records.js", () => ({
215215
resolveTrustedSourceLinkedOfficialClawHubSpec: vi.fn(() => undefined),
216216
resolveTrustedSourceLinkedOfficialNpmSpec: vi.fn(() => undefined),
217+
}));
218+
219+
vi.mock("../plugins/update.js", () => ({
217220
syncPluginsForUpdateChannel: (...args: unknown[]) => syncPluginsForUpdateChannel(...args),
218221
updateNpmInstalledPlugins: (...args: unknown[]) => updateNpmInstalledPlugins(...args),
219222
}));

‎src/cli/update-cli/post-core-plugin-convergence.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import type { PluginInstallRecord } from "../../config/types.plugins.js";
55
import { normalizePluginsConfig, resolveEffectiveEnableState } from "../../plugins/config-state.js";
66
import { resolveDefaultPluginNpmDir } from "../../plugins/install-paths.js";
77
import { listManagedPluginNpmRoots } from "../../plugins/npm-project-roots.js";
8-
import { relinkOpenClawPeerDependenciesInManagedNpmRoot } from "../../plugins/plugin-peer-link.js";
9-
import { pruneStaleLocalBundledPluginInstallRecords } from "../../plugins/stale-local-bundled-plugin-install-records.js";
108
import {
119
resolveTrustedSourceLinkedOfficialClawHubSpec,
1210
resolveTrustedSourceLinkedOfficialNpmSpec,
13-
} from "../../plugins/update.js";
11+
} from "../../plugins/official-external-install-records.js";
12+
import { relinkOpenClawPeerDependenciesInManagedNpmRoot } from "../../plugins/plugin-peer-link.js";
13+
import { pruneStaleLocalBundledPluginInstallRecords } from "../../plugins/stale-local-bundled-plugin-install-records.js";
1414
import { VERSION } from "../../version.js";
1515
import {
1616
runPluginPayloadSmokeCheck,

‎src/cli/update-cli/update-command.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ import {
9494
import {
9595
resolveTrustedSourceLinkedOfficialClawHubSpec,
9696
resolveTrustedSourceLinkedOfficialNpmSpec,
97+
} from "../../plugins/official-external-install-records.js";
98+
import {
9799
syncPluginsForUpdateChannel,
98100
updateNpmInstalledPlugins,
99101
type PluginUpdateIntegrityDriftParams,

0 commit comments

Comments
 (0)