Skip to content

Commit d549b12

Browse files
fix: gateway status normalizes runtime inspection errors (#131495)
1 parent fa57231 commit d549b12

14 files changed

Lines changed: 220 additions & 40 deletions

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ describe("gatherDaemonStatus", () => {
836836
expect((status.service.runtime as { detail?: string }).detail).toBe("19001");
837837
});
838838

839-
it("bounds all service-manager reads and still emits JSON after they time out", async () => {
839+
it("renders Gateway-specific recovery in text and JSON after service reads time out", async () => {
840840
serviceIsLoaded.mockImplementationOnce(async (args?: { timeoutMs?: number }) => {
841841
if (args?.timeoutMs === undefined) {
842842
return await new Promise<boolean>(() => {});
@@ -847,7 +847,7 @@ describe("gatherDaemonStatus", () => {
847847
if (opts?.timeoutMs === undefined) {
848848
return await new Promise<{ status: string }>(() => {});
849849
}
850-
throw new Error("systemctl show timed out");
850+
throw new Error("錯誤: 系統找不到指定的檔案。");
851851
});
852852

853853
const status = await gatherStatus({
@@ -868,7 +868,11 @@ describe("gatherDaemonStatus", () => {
868868
expect(status.service.loaded).toBeNull();
869869
expect(status.service.runtime).toEqual({
870870
status: "unknown",
871-
detail: "Error: systemctl show timed out",
871+
detail: "service runtime inspection failed; retry with openclaw gateway status --deep",
872+
inspectionFailure: {
873+
code: "service-runtime-inspection-failed",
874+
detail: "錯誤: 系統找不到指定的檔案。",
875+
},
872876
});
873877

874878
const writeJson = vi.spyOn(defaultRuntime, "writeJson").mockImplementation(() => {});
@@ -888,7 +892,11 @@ describe("gatherDaemonStatus", () => {
888892
},
889893
runtime: {
890894
status: "unknown",
891-
detail: "Error: systemctl show timed out",
895+
detail: "service runtime inspection failed; retry with openclaw gateway status --deep",
896+
inspectionFailure: {
897+
code: "service-runtime-inspection-failed",
898+
detail: "錯誤: 系統找不到指定的檔案。",
899+
},
892900
},
893901
},
894902
});
@@ -903,7 +911,10 @@ describe("gatherDaemonStatus", () => {
903911
const output = log.mock.calls.flat().join("\n");
904912
expect(output).toContain("Service: LaunchAgent (unknown)");
905913
expect(output).not.toContain("Service: LaunchAgent (not loaded)");
906-
expect(output).toContain("Runtime: unknown (Error: systemctl show timed out)");
914+
expect(output).toContain(
915+
"Runtime: unknown (service runtime inspection failed; retry with openclaw gateway status --deep)",
916+
);
917+
expect(output).not.toContain("系統找不到指定的檔案");
907918
} finally {
908919
log.mockRestore();
909920
error.mockRestore();

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,12 @@ export async function gatherDaemonStatus(
796796
notLoadedText: service.notLoadedText,
797797
targetRole: serviceTargetsProbe ? "target" : "diagnostic-only",
798798
command,
799-
runtime,
799+
runtime: runtime?.inspectionFailure
800+
? {
801+
...runtime,
802+
detail: `${runtime.detail}; retry with openclaw gateway status --deep`,
803+
}
804+
: runtime,
800805
configAudit,
801806
...(command
802807
? {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,10 @@ export function printDaemonStatus(status: DaemonStatus, opts: { json: boolean; d
400400
defaultRuntime.error(errorText(`Retry: ${formatCliCommand("openclaw gateway status --deep")}`));
401401
spacer();
402402
}
403-
const systemdUnavailableDetail = serviceInspectionDetail ?? service.runtime?.detail;
403+
const systemdUnavailableDetail =
404+
serviceInspectionDetail ??
405+
service.runtime?.inspectionFailure?.detail ??
406+
service.runtime?.detail;
404407
const systemdUnavailable =
405408
process.platform === "linux" &&
406409
(serviceInspectionDetail !== undefined || rpc?.ok !== true) &&

‎src/commands/doctor-format.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ describe("buildGatewayRuntimeHints", () => {
5858
expect(hints.join("\n")).not.toContain("systemd user services are unavailable");
5959
});
6060

61+
it("classifies systemd recovery from structured inspection diagnostics", () => {
62+
const hints = buildGatewayRuntimeHints(
63+
{
64+
status: "unknown",
65+
detail: "service runtime inspection failed; retry with openclaw status --deep",
66+
inspectionFailure: {
67+
code: "service-runtime-inspection-failed",
68+
detail: "systemctl --user unavailable: Failed to connect to bus",
69+
},
70+
},
71+
{ platform: "linux", env: {} },
72+
);
73+
74+
expect(hints.some((hint) => hint.includes("systemd user services are unavailable"))).toBe(true);
75+
});
76+
6177
it.each([
6278
{
6379
env: { OPENCLAW_PROFILE: "blue" },

‎src/commands/doctor-format.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ export function buildGatewayRuntimeHints(
5151
return null;
5252
}
5353
})();
54-
if (platform === "linux" && isSystemdUnavailableDetail(runtime.detail)) {
54+
const systemdDetail = runtime.inspectionFailure?.detail ?? runtime.detail;
55+
if (platform === "linux" && isSystemdUnavailableDetail(systemdDetail)) {
5556
hints.push(
5657
...renderSystemdUnavailableHints({
5758
wsl: isWSLEnv(env),
58-
kind: classifySystemdUnavailableDetail(runtime.detail),
59+
kind: classifySystemdUnavailableDetail(systemdDetail),
5960
env,
6061
}),
6162
);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Status daemon real-summary tests cover the shared service-state path for node services.
2+
import { expect, it, vi } from "vitest";
3+
import { createMockGatewayService } from "../daemon/service.test-helpers.js";
4+
import { formatStatusServiceValue } from "./status-all/format.js";
5+
6+
const mocks = vi.hoisted(() => ({
7+
resolveNodeService: vi.fn(),
8+
}));
9+
10+
vi.mock("../daemon/node-service.js", () => ({
11+
resolveNodeService: mocks.resolveNodeService,
12+
}));
13+
14+
const { getNodeDaemonStatusSummary } = await import("./status.daemon.js");
15+
16+
it("renders root-status recovery guidance for a rejected node runtime", async () => {
17+
mocks.resolveNodeService.mockReturnValue(
18+
createMockGatewayService({
19+
label: "systemd user",
20+
loadedText: "enabled",
21+
notLoadedText: "disabled",
22+
readRuntime: vi.fn(async () => {
23+
throw new Error("node service manager unavailable");
24+
}),
25+
}),
26+
);
27+
28+
const summary = await getNodeDaemonStatusSummary();
29+
30+
expect(summary.runtime).toEqual({
31+
status: "unknown",
32+
detail: "service runtime inspection failed; retry with openclaw status --deep",
33+
inspectionFailure: {
34+
code: "service-runtime-inspection-failed",
35+
detail: "node service manager unavailable",
36+
},
37+
});
38+
expect(formatStatusServiceValue(summary)).toBe(
39+
"systemd user disabled (inspection failed: service runtime inspection failed; retry with openclaw status --deep) · unknown",
40+
);
41+
});
42+
43+
it("preserves legitimate node runtime detail", async () => {
44+
mocks.resolveNodeService.mockReturnValue(
45+
createMockGatewayService({
46+
readRuntime: vi.fn(async () => ({
47+
status: "unknown",
48+
detail: "node process state is inconclusive",
49+
})),
50+
}),
51+
);
52+
53+
const summary = await getNodeDaemonStatusSummary();
54+
55+
expect(summary.runtime?.detail).toBe("node process state is inconclusive");
56+
expect(summary.runtimeShort).toBe("unknown (node process state is inconclusive)");
57+
});

‎src/commands/status.daemon.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ async function buildDaemonStatusSummary(
2727
const service = serviceLabel === "gateway" ? resolveGatewayService() : resolveNodeService();
2828
const fallbackLabel = serviceLabel === "gateway" ? "Daemon" : "Node";
2929
const summary = await readServiceStatusSummary(service, fallbackLabel, timeoutMs);
30+
const runtime = summary.runtime?.inspectionFailure
31+
? { ...summary.runtime, detail: `${summary.runtime.detail}; retry with openclaw status --deep` }
32+
: summary.runtime;
3033
const loaded =
3134
summary.loadState.status === "unknown" ? null : summary.loadState.status === "loaded";
3235
return {
@@ -37,8 +40,8 @@ async function buildDaemonStatusSummary(
3740
managedByOpenClaw: summary.managedByOpenClaw,
3841
externallyManaged: summary.externallyManaged,
3942
loadedText: summary.loadedText,
40-
runtime: summary.runtime,
41-
runtimeShort: formatDaemonRuntimeShort(summary.runtime),
43+
runtime,
44+
runtimeShort: formatDaemonRuntimeShort(runtime),
4245
layout: summary.layout,
4346
wrapperPath: summary.wrapperPath,
4447
};

‎src/commands/status.format.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { sanitizeTerminalText } from "../../packages/terminal-core/src/safe-text
66
import { formatCliCommand } from "../cli/command-format.js";
77
import type { BestEffortConfigSnapshot } from "../config/io.js";
88
import { formatConfigIssueLines } from "../config/issue-format.js";
9+
import type { GatewayServiceRuntime } from "../daemon/service-runtime.js";
910
import { getSystemdCgroupHygieneSummary } from "../daemon/service-runtime.js";
1011
import { formatDurationPrecise } from "../infra/format-time/format-duration.ts";
1112
import { formatRuntimeStatusWithDetails } from "../infra/runtime-status.ts";
@@ -118,19 +119,12 @@ function resolvePromptCacheStats(
118119
}
119120

120121
/** Formats daemon runtime status plus launchd/systemd details into one compact string. */
121-
export const formatDaemonRuntimeShort = (runtime?: {
122-
status?: string;
123-
pid?: number;
124-
state?: string;
125-
systemd?: { killMode?: string; tasksCurrent?: number; memoryCurrent?: number };
126-
detail?: string;
127-
missingUnit?: boolean;
128-
}) => {
122+
export const formatDaemonRuntimeShort = (runtime?: GatewayServiceRuntime) => {
129123
if (!runtime) {
130124
return null;
131125
}
132126
const details: string[] = [];
133-
const detail = runtime.detail?.replace(/\s+/g, " ").trim() || "";
127+
const detail = runtime.inspectionFailure ? "" : runtime.detail?.replace(/\s+/g, " ").trim() || "";
134128
const noisyLaunchctlDetail =
135129
runtime.missingUnit === true &&
136130
normalizeLowercaseStringOrEmpty(detail).includes("could not find service");

‎src/commands/status.service-summary.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,16 @@ describe("readServiceStatusSummary", () => {
111111
expect(formatStatusServiceValue(summary)).toBe(
112112
probe === "load"
113113
? "systemd unknown (inspection failed: Error: service manager permission denied) · stopped"
114-
: "systemd disabled (inspection failed: Error: service manager permission denied) · unknown",
114+
: probe === "runtime"
115+
? "systemd disabled (inspection failed: service runtime inspection failed) · unknown"
116+
: "systemd disabled (inspection failed: Error: service manager permission denied) · unknown",
115117
);
118+
if (probe === "runtime") {
119+
expect(summary.runtime?.inspectionFailure).toEqual({
120+
code: "service-runtime-inspection-failed",
121+
detail: "service manager permission denied",
122+
});
123+
}
116124
},
117125
);
118126

‎src/daemon/schtasks-runtime.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ import {
3131
shouldManageGatewayListenerPort,
3232
terminateGatewayProcessTree,
3333
} from "./schtasks-process.js";
34-
import type { GatewayServiceRuntime } from "./service-runtime.js";
34+
import {
35+
createServiceRuntimeInspectionFailure,
36+
type GatewayServiceRuntime,
37+
} from "./service-runtime.js";
3538
import type {
3639
GatewayServiceCommandConfig,
3740
GatewayServiceEnv,
@@ -538,7 +541,7 @@ export async function readScheduledTaskRuntime(
538541
if (await isStartupEntryInstalled(env)) {
539542
return resolveFallbackRuntime(env);
540543
}
541-
return { status: "unknown", detail: String(err) };
544+
return createServiceRuntimeInspectionFailure(err);
542545
}
543546
const taskName = resolveTaskName(env);
544547
const res = await execSchtasks(["/Query", "/TN", taskName, "/V", "/FO", "LIST"]);
@@ -548,11 +551,9 @@ export async function readScheduledTaskRuntime(
548551
}
549552
const detail = (res.stderr || res.stdout).trim();
550553
const missing = probeScheduledTaskExists(taskName) === false;
551-
return {
552-
status: missing ? "stopped" : "unknown",
553-
...(!missing && detail ? { detail } : {}),
554-
missingUnit: missing,
555-
};
554+
return missing
555+
? { status: "stopped", missingUnit: true }
556+
: { ...createServiceRuntimeInspectionFailure(detail), missingUnit: false };
556557
}
557558
const parsed = parseSchtasksQuery(res.stdout || "");
558559
const derived = deriveScheduledTaskRuntimeStatus(parsed);

0 commit comments

Comments
 (0)