fix: load config from moltbot and legacy dirs
This commit is contained in:
@@ -51,6 +51,8 @@ export function pickProbeHostForBind(
|
||||
}
|
||||
|
||||
const SAFE_DAEMON_ENV_KEYS = [
|
||||
"MOLTBOT_STATE_DIR",
|
||||
"MOLTBOT_CONFIG_PATH",
|
||||
"CLAWDBOT_PROFILE",
|
||||
"CLAWDBOT_STATE_DIR",
|
||||
"CLAWDBOT_CONFIG_PATH",
|
||||
|
||||
@@ -36,22 +36,43 @@ describe("Nix integration (U3, U5, U9)", () => {
|
||||
|
||||
describe("U5: CONFIG_PATH and STATE_DIR env var overrides", () => {
|
||||
it("STATE_DIR defaults to ~/.clawdbot when env not set", async () => {
|
||||
await withEnvOverride({ CLAWDBOT_STATE_DIR: undefined }, async () => {
|
||||
await withEnvOverride(
|
||||
{ MOLTBOT_STATE_DIR: undefined, CLAWDBOT_STATE_DIR: undefined },
|
||||
async () => {
|
||||
const { STATE_DIR } = await import("./config.js");
|
||||
expect(STATE_DIR).toMatch(/\.clawdbot$/);
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("STATE_DIR respects CLAWDBOT_STATE_DIR override", async () => {
|
||||
await withEnvOverride({ CLAWDBOT_STATE_DIR: "/custom/state/dir" }, async () => {
|
||||
await withEnvOverride(
|
||||
{ MOLTBOT_STATE_DIR: undefined, CLAWDBOT_STATE_DIR: "/custom/state/dir" },
|
||||
async () => {
|
||||
const { STATE_DIR } = await import("./config.js");
|
||||
expect(STATE_DIR).toBe(path.resolve("/custom/state/dir"));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("STATE_DIR prefers MOLTBOT_STATE_DIR over legacy override", async () => {
|
||||
await withEnvOverride(
|
||||
{ MOLTBOT_STATE_DIR: "/custom/new", CLAWDBOT_STATE_DIR: "/custom/legacy" },
|
||||
async () => {
|
||||
const { STATE_DIR } = await import("./config.js");
|
||||
expect(STATE_DIR).toBe(path.resolve("/custom/new"));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("CONFIG_PATH defaults to ~/.clawdbot/moltbot.json when env not set", async () => {
|
||||
await withEnvOverride(
|
||||
{ CLAWDBOT_CONFIG_PATH: undefined, CLAWDBOT_STATE_DIR: undefined },
|
||||
{
|
||||
MOLTBOT_CONFIG_PATH: undefined,
|
||||
MOLTBOT_STATE_DIR: undefined,
|
||||
CLAWDBOT_CONFIG_PATH: undefined,
|
||||
CLAWDBOT_STATE_DIR: undefined,
|
||||
},
|
||||
async () => {
|
||||
const { CONFIG_PATH } = await import("./config.js");
|
||||
expect(CONFIG_PATH).toMatch(/\.clawdbot[\\/]moltbot\.json$/);
|
||||
@@ -60,24 +81,45 @@ describe("Nix integration (U3, U5, U9)", () => {
|
||||
});
|
||||
|
||||
it("CONFIG_PATH respects CLAWDBOT_CONFIG_PATH override", async () => {
|
||||
await withEnvOverride({ CLAWDBOT_CONFIG_PATH: "/nix/store/abc/moltbot.json" }, async () => {
|
||||
await withEnvOverride(
|
||||
{ MOLTBOT_CONFIG_PATH: undefined, CLAWDBOT_CONFIG_PATH: "/nix/store/abc/moltbot.json" },
|
||||
async () => {
|
||||
const { CONFIG_PATH } = await import("./config.js");
|
||||
expect(CONFIG_PATH).toBe(path.resolve("/nix/store/abc/moltbot.json"));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("CONFIG_PATH prefers MOLTBOT_CONFIG_PATH over legacy override", async () => {
|
||||
await withEnvOverride(
|
||||
{
|
||||
MOLTBOT_CONFIG_PATH: "/nix/store/new/moltbot.json",
|
||||
CLAWDBOT_CONFIG_PATH: "/nix/store/legacy/moltbot.json",
|
||||
},
|
||||
async () => {
|
||||
const { CONFIG_PATH } = await import("./config.js");
|
||||
expect(CONFIG_PATH).toBe(path.resolve("/nix/store/new/moltbot.json"));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("CONFIG_PATH expands ~ in CLAWDBOT_CONFIG_PATH override", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withEnvOverride({ CLAWDBOT_CONFIG_PATH: "~/.clawdbot/custom.json" }, async () => {
|
||||
await withEnvOverride(
|
||||
{ MOLTBOT_CONFIG_PATH: undefined, CLAWDBOT_CONFIG_PATH: "~/.clawdbot/custom.json" },
|
||||
async () => {
|
||||
const { CONFIG_PATH } = await import("./config.js");
|
||||
expect(CONFIG_PATH).toBe(path.join(home, ".clawdbot", "custom.json"));
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it("CONFIG_PATH uses STATE_DIR when only state dir is overridden", async () => {
|
||||
await withEnvOverride(
|
||||
{
|
||||
MOLTBOT_CONFIG_PATH: undefined,
|
||||
MOLTBOT_STATE_DIR: undefined,
|
||||
CLAWDBOT_CONFIG_PATH: undefined,
|
||||
CLAWDBOT_STATE_DIR: "/custom/state",
|
||||
},
|
||||
|
||||
70
src/config/io.compat.test.ts
Normal file
70
src/config/io.compat.test.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { createConfigIO } from "./io.js";
|
||||
|
||||
async function withTempHome(run: (home: string) => Promise<void>): Promise<void> {
|
||||
const home = await fs.mkdtemp(path.join(os.tmpdir(), "moltbot-config-"));
|
||||
try {
|
||||
await run(home);
|
||||
} finally {
|
||||
await fs.rm(home, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function writeConfig(home: string, dirname: ".moltbot" | ".clawdbot", port: number) {
|
||||
const dir = path.join(home, dirname);
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
const configPath = path.join(dir, "moltbot.json");
|
||||
await fs.writeFile(configPath, JSON.stringify({ gateway: { port } }, null, 2));
|
||||
return configPath;
|
||||
}
|
||||
|
||||
describe("config io compat (new + legacy folders)", () => {
|
||||
it("prefers ~/.moltbot/moltbot.json when both configs exist", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
const newConfigPath = await writeConfig(home, ".moltbot", 19001);
|
||||
await writeConfig(home, ".clawdbot", 18789);
|
||||
|
||||
const io = createConfigIO({
|
||||
env: {} as NodeJS.ProcessEnv,
|
||||
homedir: () => home,
|
||||
});
|
||||
|
||||
expect(io.configPath).toBe(newConfigPath);
|
||||
expect(io.loadConfig().gateway?.port).toBe(19001);
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to ~/.clawdbot/moltbot.json when only legacy exists", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
const legacyConfigPath = await writeConfig(home, ".clawdbot", 20001);
|
||||
|
||||
const io = createConfigIO({
|
||||
env: {} as NodeJS.ProcessEnv,
|
||||
homedir: () => home,
|
||||
});
|
||||
|
||||
expect(io.configPath).toBe(legacyConfigPath);
|
||||
expect(io.loadConfig().gateway?.port).toBe(20001);
|
||||
});
|
||||
});
|
||||
|
||||
it("honors explicit legacy config path env override", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
const newConfigPath = await writeConfig(home, ".moltbot", 19002);
|
||||
const legacyConfigPath = await writeConfig(home, ".clawdbot", 20002);
|
||||
|
||||
const io = createConfigIO({
|
||||
env: { CLAWDBOT_CONFIG_PATH: legacyConfigPath } as NodeJS.ProcessEnv,
|
||||
homedir: () => home,
|
||||
});
|
||||
|
||||
expect(io.configPath).not.toBe(newConfigPath);
|
||||
expect(io.configPath).toBe(legacyConfigPath);
|
||||
expect(io.loadConfig().gateway?.port).toBe(20002);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -28,7 +28,7 @@ import { collectConfigEnvVars } from "./env-vars.js";
|
||||
import { ConfigIncludeError, resolveConfigIncludes } from "./includes.js";
|
||||
import { findLegacyConfigIssues } from "./legacy.js";
|
||||
import { normalizeConfigPaths } from "./normalize-paths.js";
|
||||
import { resolveConfigPath, resolveStateDir } from "./paths.js";
|
||||
import { resolveConfigPath, resolveDefaultConfigCandidates, resolveStateDir } from "./paths.js";
|
||||
import { applyConfigOverrides } from "./runtime-overrides.js";
|
||||
import type { MoltbotConfig, ConfigFileSnapshot, LegacyConfigIssue } from "./types.js";
|
||||
import { validateConfigObjectWithPlugins } from "./validation.js";
|
||||
@@ -186,7 +186,12 @@ export function parseConfigJson5(
|
||||
|
||||
export function createConfigIO(overrides: ConfigIoDeps = {}) {
|
||||
const deps = normalizeDeps(overrides);
|
||||
const configPath = resolveConfigPathForDeps(deps);
|
||||
const requestedConfigPath = resolveConfigPathForDeps(deps);
|
||||
const candidatePaths = deps.configPath
|
||||
? [requestedConfigPath]
|
||||
: resolveDefaultConfigCandidates(deps.env, deps.homedir);
|
||||
const configPath =
|
||||
candidatePaths.find((candidate) => deps.fs.existsSync(candidate)) ?? requestedConfigPath;
|
||||
|
||||
function loadConfig(): MoltbotConfig {
|
||||
try {
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { resolveOAuthDir, resolveOAuthPath } from "./paths.js";
|
||||
import {
|
||||
resolveDefaultConfigCandidates,
|
||||
resolveOAuthDir,
|
||||
resolveOAuthPath,
|
||||
resolveStateDir,
|
||||
} from "./paths.js";
|
||||
|
||||
describe("oauth paths", () => {
|
||||
it("prefers CLAWDBOT_OAUTH_DIR over CLAWDBOT_STATE_DIR", () => {
|
||||
@@ -27,3 +32,21 @@ describe("oauth paths", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("state + config path candidates", () => {
|
||||
it("prefers MOLTBOT_STATE_DIR over legacy state dir env", () => {
|
||||
const env = {
|
||||
MOLTBOT_STATE_DIR: "/new/state",
|
||||
CLAWDBOT_STATE_DIR: "/legacy/state",
|
||||
} as NodeJS.ProcessEnv;
|
||||
|
||||
expect(resolveStateDir(env, () => "/home/test")).toBe(path.resolve("/new/state"));
|
||||
});
|
||||
|
||||
it("orders default config candidates as new then legacy", () => {
|
||||
const home = "/home/test";
|
||||
const candidates = resolveDefaultConfigCandidates({} as NodeJS.ProcessEnv, () => home);
|
||||
expect(candidates[0]).toBe(path.join(home, ".moltbot", "moltbot.json"));
|
||||
expect(candidates[1]).toBe(path.join(home, ".clawdbot", "moltbot.json"));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,18 +15,30 @@ export function resolveIsNixMode(env: NodeJS.ProcessEnv = process.env): boolean
|
||||
|
||||
export const isNixMode = resolveIsNixMode();
|
||||
|
||||
const LEGACY_STATE_DIRNAME = ".clawdbot";
|
||||
const NEW_STATE_DIRNAME = ".moltbot";
|
||||
const CONFIG_FILENAME = "moltbot.json";
|
||||
|
||||
function legacyStateDir(homedir: () => string = os.homedir): string {
|
||||
return path.join(homedir(), LEGACY_STATE_DIRNAME);
|
||||
}
|
||||
|
||||
function newStateDir(homedir: () => string = os.homedir): string {
|
||||
return path.join(homedir(), NEW_STATE_DIRNAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* State directory for mutable data (sessions, logs, caches).
|
||||
* Can be overridden via CLAWDBOT_STATE_DIR environment variable.
|
||||
* Default: ~/.clawdbot
|
||||
* Can be overridden via MOLTBOT_STATE_DIR (preferred) or CLAWDBOT_STATE_DIR (legacy).
|
||||
* Default: ~/.clawdbot (legacy default for compatibility)
|
||||
*/
|
||||
export function resolveStateDir(
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
homedir: () => string = os.homedir,
|
||||
): string {
|
||||
const override = env.CLAWDBOT_STATE_DIR?.trim();
|
||||
const override = env.MOLTBOT_STATE_DIR?.trim() || env.CLAWDBOT_STATE_DIR?.trim();
|
||||
if (override) return resolveUserPath(override);
|
||||
return path.join(homedir(), ".clawdbot");
|
||||
return legacyStateDir(homedir);
|
||||
}
|
||||
|
||||
function resolveUserPath(input: string): string {
|
||||
@@ -43,20 +55,46 @@ export const STATE_DIR = resolveStateDir();
|
||||
|
||||
/**
|
||||
* Config file path (JSON5).
|
||||
* Can be overridden via CLAWDBOT_CONFIG_PATH environment variable.
|
||||
* Default: ~/.clawdbot/moltbot.json (or $CLAWDBOT_STATE_DIR/moltbot.json)
|
||||
* Can be overridden via MOLTBOT_CONFIG_PATH (preferred) or CLAWDBOT_CONFIG_PATH (legacy).
|
||||
* Default: ~/.clawdbot/moltbot.json (or $*_STATE_DIR/moltbot.json)
|
||||
*/
|
||||
export function resolveConfigPath(
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
stateDir: string = resolveStateDir(env, os.homedir),
|
||||
): string {
|
||||
const override = env.CLAWDBOT_CONFIG_PATH?.trim();
|
||||
const override = env.MOLTBOT_CONFIG_PATH?.trim() || env.CLAWDBOT_CONFIG_PATH?.trim();
|
||||
if (override) return resolveUserPath(override);
|
||||
return path.join(stateDir, "moltbot.json");
|
||||
return path.join(stateDir, CONFIG_FILENAME);
|
||||
}
|
||||
|
||||
export const CONFIG_PATH = resolveConfigPath();
|
||||
|
||||
/**
|
||||
* Resolve default config path candidates across new + legacy locations.
|
||||
* Order: explicit config path → state-dir-derived paths → new default → legacy default.
|
||||
*/
|
||||
export function resolveDefaultConfigCandidates(
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
homedir: () => string = os.homedir,
|
||||
): string[] {
|
||||
const explicit = env.MOLTBOT_CONFIG_PATH?.trim() || env.CLAWDBOT_CONFIG_PATH?.trim();
|
||||
if (explicit) return [resolveUserPath(explicit)];
|
||||
|
||||
const candidates: string[] = [];
|
||||
const moltbotStateDir = env.MOLTBOT_STATE_DIR?.trim();
|
||||
if (moltbotStateDir) {
|
||||
candidates.push(path.join(resolveUserPath(moltbotStateDir), CONFIG_FILENAME));
|
||||
}
|
||||
const legacyStateDirOverride = env.CLAWDBOT_STATE_DIR?.trim();
|
||||
if (legacyStateDirOverride) {
|
||||
candidates.push(path.join(resolveUserPath(legacyStateDirOverride), CONFIG_FILENAME));
|
||||
}
|
||||
|
||||
candidates.push(path.join(newStateDir(homedir), CONFIG_FILENAME));
|
||||
candidates.push(path.join(legacyStateDir(homedir), CONFIG_FILENAME));
|
||||
return candidates;
|
||||
}
|
||||
|
||||
export const DEFAULT_GATEWAY_PORT = 18789;
|
||||
|
||||
/**
|
||||
@@ -77,7 +115,7 @@ const OAUTH_FILENAME = "oauth.json";
|
||||
*
|
||||
* Precedence:
|
||||
* - `CLAWDBOT_OAUTH_DIR` (explicit override)
|
||||
* - `CLAWDBOT_STATE_DIR/credentials` (canonical server/default)
|
||||
* - `$*_STATE_DIR/credentials` (canonical server/default)
|
||||
* - `~/.clawdbot/credentials` (legacy default)
|
||||
*/
|
||||
export function resolveOAuthDir(
|
||||
|
||||
Reference in New Issue
Block a user