fix: use STATE_DIR instead of hardcoded ~/.openclaw for identity and canvas (#4824)

* fix: use STATE_DIR instead of hardcoded ~/.openclaw for identity and canvas

device-identity.ts and canvas-host/server.ts used hardcoded
path.join(os.homedir(), '.openclaw', ...) ignoring OPENCLAW_STATE_DIR
env var and the resolveStateDir() logic from config/paths.ts.

This caused ~/.openclaw/identity and ~/.openclaw/canvas directories
to be created even when state dir was overridden or resided elsewhere.

* fix: format and remove duplicate imports

* fix: scope state-dir patch + add regression tests (#4824) (thanks @kossoy)

* fix: align state-dir fallbacks in hooks and agent paths (#4824) (thanks @kossoy)

---------

Co-authored-by: Gustavo Madeira Santana <gumadeiras@gmail.com>
This commit is contained in:
Oleg Kossoy
2026-02-08 05:16:59 +02:00
committed by GitHub
parent 0499656c59
commit ebe5730401
13 changed files with 140 additions and 16 deletions

View File

@@ -0,0 +1,48 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { defaultRuntime } from "../runtime.js";
import {
restoreStateDirEnv,
setStateDirEnv,
snapshotStateDirEnv,
} from "../test-helpers/state-dir-env.js";
describe("canvas host state dir defaults", () => {
let envSnapshot: ReturnType<typeof snapshotStateDirEnv>;
beforeEach(() => {
envSnapshot = snapshotStateDirEnv();
});
afterEach(() => {
vi.resetModules();
restoreStateDirEnv(envSnapshot);
});
it("uses OPENCLAW_STATE_DIR for the default canvas root", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-canvas-state-"));
const stateDir = path.join(tempRoot, "state");
setStateDirEnv(stateDir);
vi.resetModules();
const { createCanvasHostHandler } = await import("./server.js");
const handler = await createCanvasHostHandler({
runtime: defaultRuntime,
allowInTests: true,
});
try {
const expectedRoot = await fs.realpath(path.join(stateDir, "canvas"));
const actualRoot = await fs.realpath(handler.rootDir);
expect(actualRoot).toBe(expectedRoot);
const indexPath = path.join(expectedRoot, "index.html");
const indexContents = await fs.readFile(indexPath, "utf8");
expect(indexContents).toContain("OpenClaw Canvas");
} finally {
await handler.close();
await fs.rm(tempRoot, { recursive: true, force: true });
}
});
});

View File

@@ -4,10 +4,10 @@ import chokidar from "chokidar";
import * as fsSync from "node:fs";
import fs from "node:fs/promises";
import http, { type IncomingMessage, type Server, type ServerResponse } from "node:http";
import os from "node:os";
import path from "node:path";
import { type WebSocket, WebSocketServer } from "ws";
import type { RuntimeEnv } from "../runtime.js";
import { STATE_DIR } from "../config/paths.js";
import { isTruthyEnvValue } from "../infra/env.js";
import { SafeOpenError, openFileWithinRoot } from "../infra/fs-safe.js";
import { detectMime } from "../media/mime.js";
@@ -235,7 +235,7 @@ async function prepareCanvasRoot(rootDir: string) {
}
function resolveDefaultCanvasRoot(): string {
const candidates = [path.join(os.homedir(), ".openclaw", "canvas")];
const candidates = [path.join(STATE_DIR, "canvas")];
const existing = candidates.find((dir) => {
try {
return fsSync.statSync(dir).isDirectory();