fix(daemon): support MOLTBOT_STATE_DIR environment variable

The daemon service installation (launchd/systemd/schtasks) now supports
the MOLTBOT_STATE_DIR environment variable, matching the behavior of the
main config system which already supports both MOLTBOT_STATE_DIR (preferred)
and CLAWDBOT_STATE_DIR (legacy).

Changes:
- src/daemon/paths.ts: Check MOLTBOT_STATE_DIR first, fallback to CLAWDBOT_STATE_DIR
- src/daemon/service-env.ts: Pass both MOLTBOT_STATE_DIR and CLAWDBOT_STATE_DIR to service environment
- src/daemon/paths.test.ts: Add tests for MOLTBOT_STATE_DIR behavior

Fixes inconsistency where daemon install would only respect CLAWDBOT_STATE_DIR
while the rest of the system supports the new MOLTBOT_STATE_DIR variable.
This commit is contained in:
Jonathan Wilkins 2026-01-28 18:43:12 +00:00
parent 109ac1c549
commit 949f703e81
No known key found for this signature in database
3 changed files with 22 additions and 1 deletions

View File

@ -34,4 +34,23 @@ describe("resolveGatewayStateDir", () => {
const env = { CLAWDBOT_STATE_DIR: "C:\\State\\moltbot" };
expect(resolveGatewayStateDir(env)).toBe("C:\\State\\moltbot");
});
it("uses MOLTBOT_STATE_DIR when provided", () => {
const env = { HOME: "/Users/test", MOLTBOT_STATE_DIR: "/var/lib/moltbot" };
expect(resolveGatewayStateDir(env)).toBe(path.resolve("/var/lib/moltbot"));
});
it("prefers MOLTBOT_STATE_DIR over CLAWDBOT_STATE_DIR", () => {
const env = {
HOME: "/Users/test",
MOLTBOT_STATE_DIR: "/var/lib/moltbot-new",
CLAWDBOT_STATE_DIR: "/var/lib/moltbot-old",
};
expect(resolveGatewayStateDir(env)).toBe(path.resolve("/var/lib/moltbot-new"));
});
it("expands ~ in MOLTBOT_STATE_DIR", () => {
const env = { HOME: "/Users/test", MOLTBOT_STATE_DIR: "~/.moltbot" };
expect(resolveGatewayStateDir(env)).toBe(path.resolve("/Users/test/.moltbot"));
});
});

View File

@ -26,7 +26,7 @@ export function resolveUserPathWithHome(input: string, home?: string): string {
}
export function resolveGatewayStateDir(env: Record<string, string | undefined>): string {
const override = env.CLAWDBOT_STATE_DIR?.trim();
const override = env.MOLTBOT_STATE_DIR?.trim() || env.CLAWDBOT_STATE_DIR?.trim();
if (override) {
const home = override.startsWith("~") ? resolveHomeDir(env) : undefined;
return resolveUserPathWithHome(override, home);

View File

@ -140,6 +140,7 @@ export function buildServiceEnvironment(params: {
HOME: env.HOME,
PATH: buildMinimalServicePath({ env }),
CLAWDBOT_PROFILE: profile,
MOLTBOT_STATE_DIR: env.MOLTBOT_STATE_DIR,
CLAWDBOT_STATE_DIR: env.CLAWDBOT_STATE_DIR,
CLAWDBOT_CONFIG_PATH: env.CLAWDBOT_CONFIG_PATH,
CLAWDBOT_GATEWAY_PORT: String(port),
@ -159,6 +160,7 @@ export function buildNodeServiceEnvironment(params: {
return {
HOME: env.HOME,
PATH: buildMinimalServicePath({ env }),
MOLTBOT_STATE_DIR: env.MOLTBOT_STATE_DIR,
CLAWDBOT_STATE_DIR: env.CLAWDBOT_STATE_DIR,
CLAWDBOT_CONFIG_PATH: env.CLAWDBOT_CONFIG_PATH,
CLAWDBOT_LAUNCHD_LABEL: resolveNodeLaunchAgentLabel(),