Compare commits

...

3 Commits

Author SHA1 Message Date
Gustavo Madeira Santana
6ff9e4830c fix: scope state-dir patch + add regression tests (#4824) (thanks @kossoy) 2026-02-07 21:59:06 -05:00
Oleg Kossoy
6ffa76ed81 fix: format and remove duplicate imports 2026-02-07 21:57:14 -05:00
Oleg Kossoy
c923fb0d5b 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.
2026-02-07 21:57:14 -05:00
5 changed files with 107 additions and 4 deletions

View File

@@ -15,6 +15,7 @@ Docs: https://docs.openclaw.ai
- Memory: set Voyage embeddings `input_type` for improved retrieval. (#10818) Thanks @mcinteerj.
- Memory/QMD: run boot refresh in background by default, add configurable QMD maintenance timeouts, and retry QMD after fallback failures. (#9690, #9705)
- Media understanding: recognize `.caf` audio attachments for transcription. (#10982) Thanks @succ985.
- State dir: honor `OPENCLAW_STATE_DIR` for default device identity and canvas storage paths. (#4824) Thanks @kossoy.
- Tests: harden flaky hotspots by removing timer sleeps, consolidating onboarding provider-auth coverage, and improving memory test realism. (#11598) Thanks @gumadeiras.
## 2026.2.6

View File

@@ -0,0 +1,55 @@
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";
describe("canvas host state dir defaults", () => {
let previousStateDir: string | undefined;
let previousLegacyStateDir: string | undefined;
beforeEach(() => {
previousStateDir = process.env.OPENCLAW_STATE_DIR;
previousLegacyStateDir = process.env.CLAWDBOT_STATE_DIR;
});
afterEach(() => {
vi.resetModules();
if (previousStateDir === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = previousStateDir;
}
if (previousLegacyStateDir === undefined) {
delete process.env.CLAWDBOT_STATE_DIR;
} else {
process.env.CLAWDBOT_STATE_DIR = previousLegacyStateDir;
}
});
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");
process.env.OPENCLAW_STATE_DIR = stateDir;
delete process.env.CLAWDBOT_STATE_DIR;
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();

View File

@@ -0,0 +1,47 @@
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";
describe("device identity state dir defaults", () => {
let previousStateDir: string | undefined;
let previousLegacyStateDir: string | undefined;
beforeEach(() => {
previousStateDir = process.env.OPENCLAW_STATE_DIR;
previousLegacyStateDir = process.env.CLAWDBOT_STATE_DIR;
});
afterEach(() => {
vi.resetModules();
if (previousStateDir === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = previousStateDir;
}
if (previousLegacyStateDir === undefined) {
delete process.env.CLAWDBOT_STATE_DIR;
} else {
process.env.CLAWDBOT_STATE_DIR = previousLegacyStateDir;
}
});
it("writes the default identity file under OPENCLAW_STATE_DIR", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-identity-state-"));
const stateDir = path.join(tempRoot, "state");
process.env.OPENCLAW_STATE_DIR = stateDir;
delete process.env.CLAWDBOT_STATE_DIR;
vi.resetModules();
const { loadOrCreateDeviceIdentity } = await import("./device-identity.js");
const identity = loadOrCreateDeviceIdentity();
try {
const identityPath = path.join(stateDir, "identity", "device.json");
const raw = JSON.parse(await fs.readFile(identityPath, "utf8")) as { deviceId?: string };
expect(raw.deviceId).toBe(identity.deviceId);
} finally {
await fs.rm(tempRoot, { recursive: true, force: true });
}
});
});

View File

@@ -1,7 +1,7 @@
import crypto from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { STATE_DIR } from "../config/paths.js";
export type DeviceIdentity = {
deviceId: string;
@@ -17,7 +17,7 @@ type StoredIdentity = {
createdAtMs: number;
};
const DEFAULT_DIR = path.join(os.homedir(), ".openclaw", "identity");
const DEFAULT_DIR = path.join(STATE_DIR, "identity");
const DEFAULT_FILE = path.join(DEFAULT_DIR, "device.json");
function ensureDir(filePath: string) {