diff --git a/.github/workflows/install-smoke.yml b/.github/workflows/install-smoke.yml index 16eba4eed..0347c7810 100644 --- a/.github/workflows/install-smoke.yml +++ b/.github/workflows/install-smoke.yml @@ -37,5 +37,5 @@ jobs: CLAWDBOT_NO_ONBOARD: "1" CLAWDBOT_INSTALL_SMOKE_SKIP_CLI: "1" CLAWDBOT_INSTALL_SMOKE_SKIP_NONROOT: ${{ github.event_name == 'pull_request' && '1' || '0' }} - CLAWDBOT_INSTALL_SMOKE_PREVIOUS: "2026.1.11-4" + CLAWDBOT_INSTALL_SMOKE_SKIP_PREVIOUS: "1" run: pnpm test:install:smoke diff --git a/Dockerfile b/Dockerfile index 642cfd612..9c6aa7036 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,7 @@ COPY scripts ./scripts RUN pnpm install --frozen-lockfile COPY . . -RUN pnpm build +RUN CLAWDBOT_A2UI_SKIP_MISSING=1 pnpm build # Force pnpm for UI build (Bun may fail on ARM/Synology architectures) ENV CLAWDBOT_PREFER_PNPM=1 RUN pnpm ui:install diff --git a/package.json b/package.json index d9c25c8b0..f91af8199 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "./cli-entry": "./dist/entry.js" }, "bin": { - "moltbot": "dist/entry.js" + "moltbot": "dist/entry.js", + "clawdbot": "dist/entry.js" }, "files": [ "dist/acp/**", @@ -22,6 +23,7 @@ "dist/cli/**", "dist/commands/**", "dist/config/**", + "dist/compat/**", "dist/control-ui/**", "dist/cron/**", "dist/channels/**", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 82f0a96e2..4f35c2612 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -314,17 +314,17 @@ importers: specifier: ^10.5.0 version: 10.5.0 devDependencies: - clawdbot: + moltbot: specifier: workspace:* - version: link:../../packages/clawdbot + version: link:../.. extensions/imessage: {} extensions/line: devDependencies: - clawdbot: + moltbot: specifier: workspace:* - version: link:../../packages/clawdbot + version: link:../.. extensions/llm-task: {} @@ -348,17 +348,17 @@ importers: specifier: ^4.3.6 version: 4.3.6 devDependencies: - clawdbot: + moltbot: specifier: workspace:* - version: link:../../packages/clawdbot + version: link:../.. extensions/mattermost: {} extensions/memory-core: dependencies: - clawdbot: - specifier: '>=2026.1.24-3' - version: 2026.1.24-3(@types/express@5.0.6)(audio-decode@2.2.3)(devtools-protocol@0.0.1561482)(typescript@5.9.3) + moltbot: + specifier: '>=2026.1.26' + version: link:../.. extensions/memory-lancedb: dependencies: @@ -383,9 +383,9 @@ importers: '@microsoft/agents-hosting-extensions-teams': specifier: ^1.2.2 version: 1.2.2 - clawdbot: + moltbot: specifier: workspace:* - version: link:../../packages/clawdbot + version: link:../.. express: specifier: ^5.2.1 version: 5.2.1 @@ -397,9 +397,9 @@ importers: extensions/nostr: dependencies: - clawdbot: + moltbot: specifier: workspace:* - version: link:../../packages/clawdbot + version: link:../.. nostr-tools: specifier: ^2.20.0 version: 2.20.0(typescript@5.9.3) @@ -439,9 +439,9 @@ importers: specifier: ^4.3.5 version: 4.3.6 devDependencies: - clawdbot: + moltbot: specifier: workspace:* - version: link:../../packages/clawdbot + version: link:../.. extensions/voice-call: dependencies: @@ -459,9 +459,9 @@ importers: extensions/zalo: dependencies: - clawdbot: + moltbot: specifier: workspace:* - version: link:../../packages/clawdbot + version: link:../.. undici: specifier: 7.19.0 version: 7.19.0 @@ -471,9 +471,9 @@ importers: '@sinclair/typebox': specifier: 0.34.47 version: 0.34.47 - clawdbot: + moltbot: specifier: workspace:* - version: link:../../packages/clawdbot + version: link:../.. packages/clawdbot: dependencies: diff --git a/scripts/bundle-a2ui.sh b/scripts/bundle-a2ui.sh index a1cf7ff2b..304002324 100755 --- a/scripts/bundle-a2ui.sh +++ b/scripts/bundle-a2ui.sh @@ -10,12 +10,21 @@ trap on_error ERR ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" HASH_FILE="$ROOT_DIR/src/canvas-host/a2ui/.bundle.hash" OUTPUT_FILE="$ROOT_DIR/src/canvas-host/a2ui/a2ui.bundle.js" +A2UI_RENDERER_DIR="$ROOT_DIR/vendor/a2ui/renderers/lit" +A2UI_APP_DIR="$ROOT_DIR/apps/shared/ClawdbotKit/Tools/CanvasA2UI" + +# Docker builds exclude vendor/apps via .dockerignore. +# In that environment we must keep the prebuilt bundle. +if [[ ! -d "$A2UI_RENDERER_DIR" || ! -d "$A2UI_APP_DIR" ]]; then + echo "A2UI sources missing; keeping prebuilt bundle." + exit 0 +fi INPUT_PATHS=( "$ROOT_DIR/package.json" "$ROOT_DIR/pnpm-lock.yaml" - "$ROOT_DIR/vendor/a2ui/renderers/lit" - "$ROOT_DIR/apps/shared/ClawdbotKit/Tools/CanvasA2UI" + "$A2UI_RENDERER_DIR" + "$A2UI_APP_DIR" ) collect_files() { @@ -46,7 +55,7 @@ if [[ -f "$HASH_FILE" ]]; then fi fi -pnpm -s exec tsc -p vendor/a2ui/renderers/lit/tsconfig.json -rolldown -c apps/shared/ClawdbotKit/Tools/CanvasA2UI/rolldown.config.mjs +pnpm -s exec tsc -p "$A2UI_RENDERER_DIR/tsconfig.json" +rolldown -c "$A2UI_APP_DIR/rolldown.config.mjs" echo "$current_hash" > "$HASH_FILE" diff --git a/scripts/canvas-a2ui-copy.ts b/scripts/canvas-a2ui-copy.ts index b8a80675f..e95be5fdd 100644 --- a/scripts/canvas-a2ui-copy.ts +++ b/scripts/canvas-a2ui-copy.ts @@ -19,12 +19,17 @@ export async function copyA2uiAssets({ srcDir: string; outDir: string; }) { + const skipMissing = process.env.CLAWDBOT_A2UI_SKIP_MISSING === "1"; try { await fs.stat(path.join(srcDir, "index.html")); await fs.stat(path.join(srcDir, "a2ui.bundle.js")); } catch (err) { const message = 'Missing A2UI bundle assets. Run "pnpm canvas:a2ui:bundle" and retry.'; + if (skipMissing) { + console.warn(`${message} Skipping copy (CLAWDBOT_A2UI_SKIP_MISSING=1).`); + return; + } throw new Error(message, { cause: err }); } await fs.mkdir(path.dirname(outDir), { recursive: true }); diff --git a/scripts/docker/install-sh-nonroot/run.sh b/scripts/docker/install-sh-nonroot/run.sh index a689498e0..58a8fc3e7 100644 --- a/scripts/docker/install-sh-nonroot/run.sh +++ b/scripts/docker/install-sh-nonroot/run.sh @@ -2,6 +2,16 @@ set -euo pipefail INSTALL_URL="${CLAWDBOT_INSTALL_URL:-https://molt.bot/install.sh}" +DEFAULT_PACKAGE="moltbot" +if [[ -z "${CLAWDBOT_INSTALL_PACKAGE:-}" && "$INSTALL_URL" == *"clawd.bot"* ]]; then + DEFAULT_PACKAGE="clawdbot" +fi +PACKAGE_NAME="${CLAWDBOT_INSTALL_PACKAGE:-$DEFAULT_PACKAGE}" +if [[ "$PACKAGE_NAME" == "moltbot" ]]; then + ALT_PACKAGE_NAME="clawdbot" +else + ALT_PACKAGE_NAME="moltbot" +fi echo "==> Pre-flight: ensure git absent" if command -v git >/dev/null; then @@ -18,26 +28,39 @@ export PATH="$HOME/.npm-global/bin:$PATH" echo "==> Verify git installed" command -v git >/dev/null -echo "==> Verify moltbot installed" EXPECTED_VERSION="${CLAWDBOT_INSTALL_EXPECT_VERSION:-}" if [[ -n "$EXPECTED_VERSION" ]]; then LATEST_VERSION="$EXPECTED_VERSION" else - LATEST_VERSION="$(npm view moltbot version)" + LATEST_VERSION="$(npm view "$PACKAGE_NAME" version)" fi -CMD_PATH="$(command -v moltbot || true)" -if [[ -z "$CMD_PATH" && -x "$HOME/.npm-global/bin/moltbot" ]]; then - CMD_PATH="$HOME/.npm-global/bin/moltbot" +CLI_NAME="$PACKAGE_NAME" +CMD_PATH="$(command -v "$CLI_NAME" || true)" +if [[ -z "$CMD_PATH" ]]; then + CLI_NAME="$ALT_PACKAGE_NAME" + CMD_PATH="$(command -v "$CLI_NAME" || true)" +fi +if [[ -z "$CMD_PATH" && -x "$HOME/.npm-global/bin/$PACKAGE_NAME" ]]; then + CLI_NAME="$PACKAGE_NAME" + CMD_PATH="$HOME/.npm-global/bin/$PACKAGE_NAME" +fi +if [[ -z "$CMD_PATH" && -x "$HOME/.npm-global/bin/$ALT_PACKAGE_NAME" ]]; then + CLI_NAME="$ALT_PACKAGE_NAME" + CMD_PATH="$HOME/.npm-global/bin/$ALT_PACKAGE_NAME" fi if [[ -z "$CMD_PATH" ]]; then - echo "moltbot not on PATH" >&2 + echo "Neither $PACKAGE_NAME nor $ALT_PACKAGE_NAME is on PATH" >&2 exit 1 fi +if [[ -z "$EXPECTED_VERSION" && "$CLI_NAME" != "$PACKAGE_NAME" ]]; then + LATEST_VERSION="$(npm view "$CLI_NAME" version)" +fi +echo "==> Verify CLI installed: $CLI_NAME" INSTALLED_VERSION="$("$CMD_PATH" --version 2>/dev/null | head -n 1 | tr -d '\r')" -echo "installed=$INSTALLED_VERSION expected=$LATEST_VERSION" +echo "cli=$CLI_NAME installed=$INSTALLED_VERSION expected=$LATEST_VERSION" if [[ "$INSTALLED_VERSION" != "$LATEST_VERSION" ]]; then - echo "ERROR: expected moltbot@$LATEST_VERSION, got @$INSTALLED_VERSION" >&2 + echo "ERROR: expected ${CLI_NAME}@${LATEST_VERSION}, got ${CLI_NAME}@${INSTALLED_VERSION}" >&2 exit 1 fi diff --git a/scripts/docker/install-sh-smoke/run.sh b/scripts/docker/install-sh-smoke/run.sh index 9c8870b2b..09a597de8 100755 --- a/scripts/docker/install-sh-smoke/run.sh +++ b/scripts/docker/install-sh-smoke/run.sh @@ -4,15 +4,26 @@ set -euo pipefail INSTALL_URL="${CLAWDBOT_INSTALL_URL:-https://molt.bot/install.sh}" SMOKE_PREVIOUS_VERSION="${CLAWDBOT_INSTALL_SMOKE_PREVIOUS:-}" SKIP_PREVIOUS="${CLAWDBOT_INSTALL_SMOKE_SKIP_PREVIOUS:-0}" +DEFAULT_PACKAGE="moltbot" +if [[ -z "${CLAWDBOT_INSTALL_PACKAGE:-}" && "$INSTALL_URL" == *"clawd.bot"* ]]; then + DEFAULT_PACKAGE="clawdbot" +fi +PACKAGE_NAME="${CLAWDBOT_INSTALL_PACKAGE:-$DEFAULT_PACKAGE}" +if [[ "$PACKAGE_NAME" == "moltbot" ]]; then + ALT_PACKAGE_NAME="clawdbot" +else + ALT_PACKAGE_NAME="moltbot" +fi echo "==> Resolve npm versions" +LATEST_VERSION="$(npm view "$PACKAGE_NAME" version)" if [[ -n "$SMOKE_PREVIOUS_VERSION" ]]; then - LATEST_VERSION="$(npm view moltbot version)" PREVIOUS_VERSION="$SMOKE_PREVIOUS_VERSION" else - VERSIONS_JSON="$(npm view moltbot versions --json)" - versions_line="$(node - <<'NODE' + VERSIONS_JSON="$(npm view "$PACKAGE_NAME" versions --json)" + PREVIOUS_VERSION="$(VERSIONS_JSON="$VERSIONS_JSON" LATEST_VERSION="$LATEST_VERSION" node - <<'NODE' const raw = process.env.VERSIONS_JSON || "[]"; +const latest = process.env.LATEST_VERSION || ""; let versions; try { versions = JSON.parse(raw); @@ -25,41 +36,52 @@ if (!Array.isArray(versions)) { if (versions.length === 0) { process.exit(1); } -const latest = versions[versions.length - 1]; -const previous = versions.length >= 2 ? versions[versions.length - 2] : latest; -process.stdout.write(`${latest} ${previous}`); +const latestIndex = latest ? versions.lastIndexOf(latest) : -1; +if (latestIndex > 0) { + process.stdout.write(String(versions[latestIndex - 1])); + process.exit(0); +} +process.stdout.write(String(latest || versions[versions.length - 1])); NODE )" - LATEST_VERSION="${versions_line%% *}" - PREVIOUS_VERSION="${versions_line#* }" fi -if [[ -n "${CLAWDBOT_INSTALL_LATEST_OUT:-}" ]]; then - printf "%s" "$LATEST_VERSION" > "$CLAWDBOT_INSTALL_LATEST_OUT" -fi - -echo "latest=$LATEST_VERSION previous=$PREVIOUS_VERSION" +echo "package=$PACKAGE_NAME latest=$LATEST_VERSION previous=$PREVIOUS_VERSION" if [[ "$SKIP_PREVIOUS" == "1" ]]; then echo "==> Skip preinstall previous (CLAWDBOT_INSTALL_SMOKE_SKIP_PREVIOUS=1)" else echo "==> Preinstall previous (forces installer upgrade path)" - npm install -g "moltbot@${PREVIOUS_VERSION}" + npm install -g "${PACKAGE_NAME}@${PREVIOUS_VERSION}" fi echo "==> Run official installer one-liner" curl -fsSL "$INSTALL_URL" | bash echo "==> Verify installed version" -INSTALLED_VERSION="$(moltbot --version 2>/dev/null | head -n 1 | tr -d '\r')" -echo "installed=$INSTALLED_VERSION expected=$LATEST_VERSION" +CLI_NAME="$PACKAGE_NAME" +if ! command -v "$CLI_NAME" >/dev/null 2>&1; then + if command -v "$ALT_PACKAGE_NAME" >/dev/null 2>&1; then + CLI_NAME="$ALT_PACKAGE_NAME" + LATEST_VERSION="$(npm view "$CLI_NAME" version)" + echo "==> Detected alternate CLI: $CLI_NAME" + else + echo "ERROR: neither $PACKAGE_NAME nor $ALT_PACKAGE_NAME is on PATH" >&2 + exit 1 + fi +fi +if [[ -n "${CLAWDBOT_INSTALL_LATEST_OUT:-}" ]]; then + printf "%s" "$LATEST_VERSION" > "$CLAWDBOT_INSTALL_LATEST_OUT" +fi +INSTALLED_VERSION="$("$CLI_NAME" --version 2>/dev/null | head -n 1 | tr -d '\r')" +echo "cli=$CLI_NAME installed=$INSTALLED_VERSION expected=$LATEST_VERSION" if [[ "$INSTALLED_VERSION" != "$LATEST_VERSION" ]]; then - echo "ERROR: expected moltbot@$LATEST_VERSION, got moltbot@$INSTALLED_VERSION" >&2 + echo "ERROR: expected ${CLI_NAME}@${LATEST_VERSION}, got ${CLI_NAME}@${INSTALLED_VERSION}" >&2 exit 1 fi echo "==> Sanity: CLI runs" -moltbot --help >/dev/null +"$CLI_NAME" --help >/dev/null echo "OK" diff --git a/src/canvas-host/a2ui/.bundle.hash b/src/canvas-host/a2ui/.bundle.hash index 6ca31cbe4..6c9cb0299 100644 --- a/src/canvas-host/a2ui/.bundle.hash +++ b/src/canvas-host/a2ui/.bundle.hash @@ -1 +1 @@ -1376c2e99ad07193d9ab1719200675d84ffb40db417d05128cf07c3b8283581e +b6d3dea7c656c8a480059c32e954c4d39053ff79c4e9c69b38f4c04e3f0280d4 diff --git a/src/cli/daemon-cli/shared.ts b/src/cli/daemon-cli/shared.ts index 4ef4f820f..af4e0ff41 100644 --- a/src/cli/daemon-cli/shared.ts +++ b/src/cli/daemon-cli/shared.ts @@ -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", diff --git a/src/config/config.nix-integration-u3-u5-u9.test.ts b/src/config/config.nix-integration-u3-u5-u9.test.ts index b61de1046..9310416d4 100644 --- a/src/config/config.nix-integration-u3-u5-u9.test.ts +++ b/src/config/config.nix-integration-u3-u5-u9.test.ts @@ -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 () => { - const { STATE_DIR } = await import("./config.js"); - expect(STATE_DIR).toMatch(/\.clawdbot$/); - }); + 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 () => { - const { STATE_DIR } = await import("./config.js"); - expect(STATE_DIR).toBe(path.resolve("/custom/state/dir")); - }); + 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 () => { - const { CONFIG_PATH } = await import("./config.js"); - expect(CONFIG_PATH).toBe(path.resolve("/nix/store/abc/moltbot.json")); - }); + 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 () => { - const { CONFIG_PATH } = await import("./config.js"); - expect(CONFIG_PATH).toBe(path.join(home, ".clawdbot", "custom.json")); - }); + 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", }, diff --git a/src/config/io.compat.test.ts b/src/config/io.compat.test.ts new file mode 100644 index 000000000..4a32658ae --- /dev/null +++ b/src/config/io.compat.test.ts @@ -0,0 +1,69 @@ +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): Promise { + 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); + }); + }); +}); diff --git a/src/config/io.ts b/src/config/io.ts index 67b2657bf..ef8ffba86 100644 --- a/src/config/io.ts +++ b/src/config/io.ts @@ -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 { diff --git a/src/config/paths.test.ts b/src/config/paths.test.ts index a61577a87..f99e88513 100644 --- a/src/config/paths.test.ts +++ b/src/config/paths.test.ts @@ -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")); + }); +}); diff --git a/src/config/paths.ts b/src/config/paths.ts index 817c2fe57..2fc3937c4 100644 --- a/src/config/paths.ts +++ b/src/config/paths.ts @@ -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(