Skip to content

Commit a7767a4

Browse files
committed
fix(doctor): resolve legacy systemd unit for the target profile (#119648)
1 parent 1eca944 commit a7767a4

4 files changed

Lines changed: 161 additions & 4 deletions

File tree

‎src/daemon/constants.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
resolveGatewayServiceDescription,
1010
resolveGatewaySystemdServiceName,
1111
resolveGatewayWindowsTaskName,
12+
resolveLegacyGatewaySystemdServiceName,
1213
} from "./constants.js";
1314

1415
describe("resolveGatewayLaunchAgentLabel", () => {
@@ -36,6 +37,23 @@ describe("resolveGatewaySystemdServiceName", () => {
3637
});
3738
});
3839

40+
describe("resolveLegacyGatewaySystemdServiceName", () => {
41+
it("returns the unqualified legacy name for the default profile", () => {
42+
const result = resolveLegacyGatewaySystemdServiceName();
43+
expect(result).toBe("openclaw");
44+
});
45+
46+
it("returns the openclaw-<profile> legacy name for a named profile", () => {
47+
const result = resolveLegacyGatewaySystemdServiceName("lisa");
48+
expect(result).toBe("openclaw-lisa");
49+
});
50+
51+
it("normalizes the default profile to the unqualified legacy name", () => {
52+
const result = resolveLegacyGatewaySystemdServiceName("default");
53+
expect(result).toBe("openclaw");
54+
});
55+
});
56+
3957
describe("resolveGatewayWindowsTaskName", () => {
4058
it("returns default task name when no profile is set", () => {
4159
const result = resolveGatewayWindowsTaskName();

‎src/daemon/constants.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ export function resolveGatewaySystemdServiceName(profile?: string): string {
5151
return `openclaw-gateway${suffix}`;
5252
}
5353

54+
/**
55+
* Legacy systemd service name (`openclaw[-<profile>]`), used by installs that
56+
* predate the `openclaw-gateway-` rename. The default profile maps to
57+
* `openclaw`; the pre-rename Clawdbot era is covered separately by
58+
* {@link LEGACY_GATEWAY_SYSTEMD_SERVICE_NAMES}.
59+
*/
60+
export function resolveLegacyGatewaySystemdServiceName(profile?: string): string {
61+
const normalized = normalizeGatewayProfile(profile);
62+
return normalized ? `openclaw-${normalized}` : "openclaw";
63+
}
64+
5465
export function resolveGatewayWindowsTaskName(profile?: string): string {
5566
const normalized = normalizeGatewayProfile(profile);
5667
if (!normalized) {

‎src/daemon/systemd.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,80 @@ describe("system-scope gateway unit detection (openclaw#87577)", () => {
733733
});
734734
});
735735

736+
it("findInstalledSystemdGatewayScope resolves the legacy openclaw-<profile> system unit for a named profile", async () => {
737+
mockUnitFileLayout({ system: "/etc/systemd/system/openclaw-lisa.service" });
738+
const result = await findInstalledSystemdGatewayScope({
739+
HOME: TEST_MANAGED_HOME,
740+
OPENCLAW_PROFILE: "lisa",
741+
});
742+
expect(result).toEqual({
743+
scope: "system",
744+
unitName: "openclaw-lisa.service",
745+
unitPath: "/etc/systemd/system/openclaw-lisa.service",
746+
});
747+
});
748+
749+
it("refuses to borrow an unrelated marker-owned unit as the target profile's gateway (#119648)", async () => {
750+
mockUnitFileLayout({ system: false });
751+
findSystemGatewayServicesMock.mockResolvedValueOnce([
752+
{
753+
platform: "linux",
754+
label: "openclaw-darlene.service",
755+
detail: "unit: /etc/systemd/system/openclaw-darlene.service",
756+
scope: "system",
757+
marker: "openclaw",
758+
},
759+
]);
760+
const result = await findInstalledSystemdGatewayScope({
761+
HOME: TEST_MANAGED_HOME,
762+
OPENCLAW_PROFILE: "lisa",
763+
});
764+
expect(result).toBeNull();
765+
});
766+
767+
it("accepts a marker-owned unit matching the target profile's legacy name", async () => {
768+
mockUnitFileLayout({ system: false });
769+
findSystemGatewayServicesMock.mockResolvedValueOnce([
770+
{
771+
platform: "linux",
772+
label: "openclaw-lisa.service",
773+
detail: "unit: /etc/systemd/system/openclaw-lisa.service",
774+
scope: "system",
775+
marker: "openclaw",
776+
},
777+
]);
778+
const result = await findInstalledSystemdGatewayScope({
779+
HOME: TEST_MANAGED_HOME,
780+
OPENCLAW_PROFILE: "lisa",
781+
});
782+
expect(result).toEqual({
783+
scope: "system",
784+
unitName: "openclaw-lisa.service",
785+
unitPath: "/etc/systemd/system/openclaw-lisa.service",
786+
});
787+
});
788+
789+
it("readSystemdServiceRuntime reads the legacy unit's own status instead of an unrelated agent's (#119648)", async () => {
790+
mockUnitFileLayout({ system: "/etc/systemd/system/openclaw-lisa.service" });
791+
execFileMock.mockImplementationOnce((_cmd, args, _opts, cb) => {
792+
expect(args[0]).toBe("show");
793+
expect(args).toContain("openclaw-lisa.service");
794+
cb(
795+
null,
796+
["Id=openclaw-lisa.service", "ActiveState=inactive", "SubState=dead", "MainPID=0"].join(
797+
"\n",
798+
),
799+
"",
800+
);
801+
});
802+
const runtime = await readSystemdServiceRuntime({
803+
HOME: TEST_MANAGED_HOME,
804+
OPENCLAW_PROFILE: "lisa",
805+
});
806+
expect(runtime.status).toBe("stopped");
807+
expect(runtime.systemd?.unit).toBe("openclaw-lisa.service");
808+
});
809+
736810
it("findInstalledSystemdGatewayScope ignores legacy clawdbot system units in the marker fallback", async () => {
737811
mockUnitFileLayout({ system: false });
738812
findSystemGatewayServicesMock.mockResolvedValueOnce([

‎src/daemon/systemd.ts‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
LEGACY_GATEWAY_SYSTEMD_SERVICE_NAMES,
2525
resolveGatewayServiceDescription,
2626
resolveGatewaySystemdServiceName,
27+
resolveLegacyGatewaySystemdServiceName,
2728
} from "./constants.js";
2829
import { execFileUtf8 } from "./exec-file.js";
2930
import { formatLine, toPosixPath, writeFormattedLines } from "./output.js";
@@ -93,8 +94,11 @@ const SYSTEM_SYSTEMD_UNIT_DIRS = [
9394
"/lib/systemd/system",
9495
] as const;
9596

96-
async function findSystemSystemdUnitPath(env: GatewayServiceEnv): Promise<string | null> {
97-
const serviceFile = `${resolveSystemdServiceName(env)}.service`;
97+
async function findSystemSystemdUnitPath(
98+
env: GatewayServiceEnv,
99+
name = resolveSystemdServiceName(env),
100+
): Promise<string | null> {
101+
const serviceFile = `${name}.service`;
98102
for (const dir of SYSTEM_SYSTEMD_UNIT_DIRS) {
99103
const candidate = path.posix.join(dir, serviceFile);
100104
try {
@@ -144,6 +148,30 @@ async function findMarkerOwnedSystemSystemdUnit(): Promise<{
144148
return null;
145149
}
146150

151+
/**
152+
* True when a scanned system unit name belongs to the gateway of THIS profile.
153+
*
154+
* The system-wide marker scan (`findSystemGatewayServices`) collects every
155+
* `openclaw-*` unit on the host; on multi-agent hosts that includes unrelated
156+
* running agents. A unit must match the canonical (`openclaw-gateway[-profile]`)
157+
* or legacy (`openclaw-<profile>`) name for the target profile before it may be
158+
* treated as the target's installation — otherwise doctor could report another
159+
* agent's PID as this gateway's runtime and recommend restarting the wrong
160+
* service (#119648).
161+
*/
162+
function isSystemdGatewayUnitNameForProfile(
163+
unitName: string,
164+
canonicalName: string,
165+
profile: string | undefined,
166+
): boolean {
167+
const normalized = unitName.replace(/\.service$/i, "");
168+
if (normalized === canonicalName) {
169+
return true;
170+
}
171+
const legacyName = resolveLegacyGatewaySystemdServiceName(profile);
172+
return legacyName !== null && normalized === legacyName;
173+
}
174+
147175
/**
148176
* The full installed-gateway picture across both systemd scopes.
149177
*
@@ -193,10 +221,36 @@ async function findSystemSystemdGatewayScope(
193221
if (systemPath) {
194222
return { scope: "system", unitName: canonicalUnitName, unitPath: systemPath };
195223
}
224+
// Older installs predate the `openclaw-gateway-` rename and use
225+
// `openclaw-<profile>`; resolve the same profile under that convention
226+
// before scanning system-wide (#119648).
227+
const hasUnitOverride = Boolean(env.OPENCLAW_SYSTEMD_UNIT?.trim());
228+
const legacyName = hasUnitOverride
229+
? null
230+
: resolveLegacyGatewaySystemdServiceName(env.OPENCLAW_PROFILE);
231+
if (legacyName) {
232+
const legacyPath = await findSystemSystemdUnitPath(env, legacyName);
233+
if (legacyPath) {
234+
return { scope: "system", unitName: `${legacyName}.service`, unitPath: legacyPath };
235+
}
236+
}
196237
// System-scope installs may use a non-canonical unit name; fall back to a
197-
// marker-owned lookup before declaring no system unit exists.
238+
// marker-owned lookup, but only accept a unit that plausibly belongs to THIS
239+
// profile. An unrelated running `openclaw-*` unit (another agent on the same
240+
// host) must never be reported as this gateway's runtime, and doctor must
241+
// never recommend restarting it (#119648).
198242
const owned = await findMarkerOwnedSystemSystemdUnit();
199-
return owned ? { scope: "system", unitName: owned.unitName, unitPath: owned.unitPath } : null;
243+
if (
244+
owned &&
245+
isSystemdGatewayUnitNameForProfile(
246+
owned.unitName,
247+
resolveSystemdServiceName(env),
248+
env.OPENCLAW_PROFILE,
249+
)
250+
) {
251+
return { scope: "system", unitName: owned.unitName, unitPath: owned.unitPath };
252+
}
253+
return null;
200254
}
201255

202256
/**

0 commit comments

Comments
 (0)