Skip to content

Commit dfa3d57

Browse files
committed
refactor(daemon): distinguish service home resolver
1 parent ce717eb commit dfa3d57

6 files changed

Lines changed: 16 additions & 23 deletions

File tree

‎scripts/lib/export-name-collision-baseline.json‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,14 +1312,6 @@
13121312
],
13131313
"sdk": true
13141314
},
1315-
{
1316-
"name": "resolveHomeDir",
1317-
"files": [
1318-
"src/daemon/paths.ts",
1319-
"src/utils.ts"
1320-
],
1321-
"sdk": true
1322-
},
13231315
{
13241316
"name": "resolveMarketplaceInstallShortcut",
13251317
"files": [

‎src/daemon/inspect.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { resolveLaunchAgentLabel } from "./launchd-label.js";
1313
import { parseLaunchdPlistLabel } from "./launchd-plist.js";
1414
import { readLaunchDaemonPlistLabel } from "./launchd-system.js";
15-
import { resolveHomeDir } from "./paths.js";
15+
import { resolveDaemonHomeDir } from "./paths.js";
1616
import { execSchtasks } from "./schtasks-exec.js";
1717
import { parseSystemdExecStart } from "./systemd-unit.js";
1818

@@ -495,7 +495,7 @@ export async function findExtraGatewayServices(
495495

496496
if (process.platform === "darwin") {
497497
try {
498-
const home = resolveHomeDir(env);
498+
const home = resolveDaemonHomeDir(env);
499499
const userDir = path.join(home, "Library", "LaunchAgents");
500500
for (const svc of await scanLaunchdDir({
501501
dir: userDir,
@@ -527,7 +527,7 @@ export async function findExtraGatewayServices(
527527

528528
if (process.platform === "linux") {
529529
try {
530-
const home = resolveHomeDir(env);
530+
const home = resolveDaemonHomeDir(env);
531531
const userDir = path.join(home, ".config", "systemd", "user");
532532
for (const svc of await scanSystemdDir({
533533
dir: userDir,

‎src/daemon/launchd.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
isSystemLaunchDaemonOwnershipError,
4646
} from "./launchd-system.js";
4747
import { formatLine, toPosixPath, writeFormattedLines } from "./output.js";
48-
import { resolveGatewayStateDir, resolveHomeDir } from "./paths.js";
48+
import { resolveDaemonHomeDir, resolveGatewayStateDir } from "./paths.js";
4949
import { resolveGatewaySupervisorLogPaths } from "./restart-logs.js";
5050
import { parseKeyValueOutput } from "./runtime-parse.js";
5151
import { createGatewayLifecycleMutationReporter } from "./service-mutation.js";
@@ -167,7 +167,7 @@ function resolveLaunchAgentPlistPathForLabel(
167167
env: Record<string, string | undefined>,
168168
label: string,
169169
): string {
170-
const home = toPosixPath(resolveHomeDir(env));
170+
const home = toPosixPath(resolveDaemonHomeDir(env));
171171
return path.posix.join(home, "Library", "LaunchAgents", `${label}.plist`);
172172
}
173173

@@ -1025,7 +1025,7 @@ export async function uninstallLaunchAgent({
10251025
return;
10261026
}
10271027

1028-
const home = toPosixPath(resolveHomeDir(env));
1028+
const home = toPosixPath(resolveDaemonHomeDir(env));
10291029
const trashDir = path.posix.join(home, ".Trash");
10301030
const dest = path.join(trashDir, `${label}.plist`);
10311031
try {
@@ -1338,7 +1338,7 @@ async function writeLaunchAgentPlist({
13381338
await ensureSecureDirectory(logDir);
13391339

13401340
const plistPath = resolveLaunchAgentPlistPathForLabel(env, label);
1341-
const home = toPosixPath(resolveHomeDir(env));
1341+
const home = toPosixPath(resolveDaemonHomeDir(env));
13421342
const libraryDir = path.posix.join(home, "Library");
13431343
await ensureSecureDirectory(home);
13441344
await ensureSecureDirectory(libraryDir);

‎src/daemon/paths.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const windowsAbsolutePath = /^[a-zA-Z]:[\\/]/;
77
const windowsUncPath = /^\\\\/;
88

99
/** Resolves the home directory used for daemon state paths. */
10-
export function resolveHomeDir(env: Record<string, string | undefined>): string {
10+
// Daemon unit files must not use infra/home-dir because runtime overrides cannot leak into services.
11+
export function resolveDaemonHomeDir(env: Record<string, string | undefined>): string {
1112
const home = normalizeOptionalString(env.HOME) || normalizeOptionalString(env.USERPROFILE);
1213
if (!home) {
1314
throw new Error("Missing HOME");
@@ -38,10 +39,10 @@ function resolveUserPathWithHome(input: string, home?: string): string {
3839
export function resolveGatewayStateDir(env: Record<string, string | undefined>): string {
3940
const override = normalizeOptionalString(env.OPENCLAW_STATE_DIR);
4041
if (override) {
41-
const home = override.startsWith("~") ? resolveHomeDir(env) : undefined;
42+
const home = override.startsWith("~") ? resolveDaemonHomeDir(env) : undefined;
4243
return resolveUserPathWithHome(override, home);
4344
}
44-
const home = resolveHomeDir(env);
45+
const home = resolveDaemonHomeDir(env);
4546
const suffix = resolveGatewayProfileSuffix(env.OPENCLAW_PROFILE);
4647
// Profile suffixes isolate managed service files while preserving the default
4748
// historical ~/.openclaw state path.

‎src/daemon/restart-logs.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from "node:fs";
33
import path from "node:path";
44
import { quoteCmdScriptArg } from "./cmd-argv.js";
55
import { resolveGatewayProfileSuffix } from "./constants.js";
6-
import { resolveGatewayStateDir, resolveHomeDir } from "./paths.js";
6+
import { resolveDaemonHomeDir, resolveGatewayStateDir } from "./paths.js";
77
import type { GatewayLifecycleMutationMode, GatewayServiceEnv } from "./service-types.js";
88

99
const GATEWAY_RESTART_LOG_FILENAME = "gateway-restart.log";
@@ -47,7 +47,7 @@ export function resolveGatewayLogPaths(env: GatewayServiceEnv): GatewayLogPaths
4747
}
4848

4949
function resolveMacLaunchAgentLogPaths(env: GatewayServiceEnv): GatewayLogPaths {
50-
const home = resolveHomeDir(env).replaceAll("\\", "/");
50+
const home = resolveDaemonHomeDir(env).replaceAll("\\", "/");
5151
const logDir = path.posix.join(home, "Library", "Logs", "openclaw");
5252
const prefix = resolveMacLaunchAgentLogPrefix(env);
5353
return {

‎src/daemon/systemd.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
} from "./constants.js";
2727
import { execFileUtf8 } from "./exec-file.js";
2828
import { formatLine, toPosixPath, writeFormattedLines } from "./output.js";
29-
import { resolveHomeDir } from "./paths.js";
29+
import { resolveDaemonHomeDir } from "./paths.js";
3030
import { parseKeyValueOutput } from "./runtime-parse.js";
3131
import {
3232
hasEnvironmentFileSource,
@@ -68,7 +68,7 @@ const SYSTEMD_GATEWAY_DOTENV_FILENAME = "gateway.systemd.env";
6868
const SYSTEMD_NODE_DOTENV_FILENAME = "node.systemd.env";
6969

7070
function resolveSystemdUnitPathForName(env: GatewayServiceEnv, name: string): string {
71-
const home = toPosixPath(resolveHomeDir(env));
71+
const home = toPosixPath(resolveDaemonHomeDir(env));
7272
return path.posix.join(home, ".config", "systemd", "user", `${name}.service`);
7373
}
7474

@@ -523,7 +523,7 @@ async function assertNoSystemGatewayOwnership(env: GatewayServiceEnv): Promise<v
523523

524524
function expandSystemdSpecifier(input: string, env: GatewayServiceEnv): string {
525525
// Support the common unit-specifier used in user services.
526-
return input.replaceAll("%h", toPosixPath(resolveHomeDir(env)));
526+
return input.replaceAll("%h", toPosixPath(resolveDaemonHomeDir(env)));
527527
}
528528

529529
function parseEnvironmentFileSpecs(raw: string): string[] {

0 commit comments

Comments
 (0)