mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-17 01:11:38 +00:00
* refactor(sessions): migrate runtime storage to sqlite * test(sessions): fix sqlite CI regressions * test(sessions): align remaining sqlite fixtures * fix(codex): require sqlite trajectory recorder * test(sessions): align orphan recovery sqlite fixture * test(sessions): align sqlite rebase fixtures * fix(sessions): finish current-main integration of the sqlite flip Resolve the whole-store SDK removal across its owner boundary: drop the loadSessionStore re-export and the registry whole-store wrappers, wire hasTrackedActiveSessionRun into gateway chat, complete the preserveLockedHarnessIds cleanup contract, flip the codex thread-history import to storePath targets, and port remaining main-side tests from file-store helpers to session accessor reads. * chore: drop committed pebbles log, revert plugin-inspector bump, refresh generated docs Remove the 1.8k-line .pebbles/events.jsonl work log from the branch, restore the plugin-inspector advisory lane to main's pinned 0.3.10 so the supply-chain bump gets its own review, and regenerate docs_map, the plugin SDK API baseline, and the export-surface ratchet for the merged tree. * feat(sessions): keep archived transcripts by default with zstd cold storage Codex-style retention: deleting or resetting a session archives its transcript as a zstd-compressed JSONL artifact (plain when the runtime lacks node:zlib zstd) and keeps it until the disk budget evicts oldest first. resetArchiveRetention now governs both deleted and reset archives and defaults to keep; maxDiskBytes defaults to 2gb so retention stays bounded, with archives evicted before live sessions. The cron reaper follows the same knob instead of deleting archives on its own timer. * fix(state): converge agent DB migration lineages and bound database growth Merge coherence: run both structure-gated legacy memory-schema repairs (flip-lineage drop, main-lineage identity rebuild) before the flip migration so pre-flip v1/v2 and pre-merge flip v1/v4 databases all converge, and hoist foreign_keys=OFF outside the schema transaction where the pragma was silently ignored and the v1 sessions rebuild cascade-deleted session_entries. Growth guards: fresh agent DBs enable auto_vacuum=INCREMENTAL, WAL maintenance releases freed pages in bounded passes (never a blocking full VACUUM), and doctor reports state/agent DB bloat from freelist stats. * fix(codex): resolve the store path for thread-history import via the SDK The supervision catalog passed the legacy sessionFile locator to the storePath-targeted transcript mirror; resolve the agent store path with the session-store SDK helper instead of a runtime-object seam so test fakes and headless callers need no extra surface. Drop the obsolete missing-session-id preprocessing case: sessions rows are NOT NULL on session_id and upsert repairs id-less patches at write time. * fix(sessions): fail safe on malformed disk-budget config and doctor stat errors A malformed explicit maxDiskBytes disables the budget instead of falling back to the destructive 2gb default the user never chose, and the doctor bloat check skips databases whose paths stat-fail instead of aborting doctor. * fix(sessions): complete sqlite conflict translations * test(sqlite): align hardening checks with maintenance * test(sessions): inspect compressed transcript archives * fix(tests): await session seeds and drop unused helpers flagged by CI lint The five unawaited writeSessionStoreSeed calls raced their SQLite seeds against the assertions, failing compact shards; the bloat probe drops a useless initializer and the merged tests drop now-unused helpers. * test(sessions): type legacy proof events directly * test(sessions): align hardening contracts * perf(sessions): read usage transcript sizes from SQL aggregates Usage/cost scans walked every session and materialized every transcript event just to re-stringify it for a byte estimate — the #86718 stall class reborn on the DB. readTranscriptStatsSync sums stored JSON bytes in SQLite without loading a single row. * fix(sessions): re-root foreign-root transcript paths onto the current sessions dir Restored backups, moved OPENCLAW_STATE_DIR, and rehearsal copies carry absolute sessionFile paths from the old root; the containment fallback kept those foreign paths, so migration read (and would archive) files in the original root and reported local copies missing. Re-root the canonical agents/<id>/sessions suffix onto the current dir when the file exists there; genuine cross-root layouts still fall through unchanged. * test(agents): seed harness admission through sqlite * fix(sqlite): close agent db on pragma setup failure * fix(doctor): compact and retrofit incremental auto-vacuum after session import The migration is the sanctioned offline window: post-import compact reclaims import churn and applies auto_vacuum=INCREMENTAL to databases created before the fresh-DB pragma existed, so runtime maintenance can release pages in bounded passes on every install. --------- Co-authored-by: Peter Steinberger <steipete@gmail.com>
566 lines
20 KiB
TypeScript
566 lines
20 KiB
TypeScript
// Covers SQLite WAL maintenance configuration.
|
|
import childProcess from "node:child_process";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import type { DatabaseSync } from "node:sqlite";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { MAX_TIMER_TIMEOUT_MS } from "../shared/number-coercion.js";
|
|
import {
|
|
DEFAULT_SQLITE_WAL_AUTOCHECKPOINT_PAGES,
|
|
configureSqliteConnectionPragmas,
|
|
configureSqliteWalMaintenance,
|
|
} from "./sqlite-wal.js";
|
|
|
|
function createMockDb(): DatabaseSync {
|
|
return {
|
|
exec: vi.fn(),
|
|
prepare: vi.fn(() => ({
|
|
get: vi.fn(() => ({ journal_mode: "delete" })),
|
|
})),
|
|
} as unknown as DatabaseSync;
|
|
}
|
|
|
|
function statfsFixture(type: number): ReturnType<typeof fs.statfsSync> {
|
|
return {
|
|
type,
|
|
bsize: 1024,
|
|
blocks: 1,
|
|
bfree: 1,
|
|
bavail: 1,
|
|
files: 0,
|
|
frsize: 1024,
|
|
ffree: 0,
|
|
};
|
|
}
|
|
|
|
describe("sqlite WAL maintenance", () => {
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("enables WAL mode and explicit autocheckpointing", () => {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
|
|
configureSqliteWalMaintenance(db, { checkpointIntervalMs: 0 });
|
|
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(1, "PRAGMA journal_mode = WAL;");
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(
|
|
2,
|
|
`PRAGMA wal_autocheckpoint = ${DEFAULT_SQLITE_WAL_AUTOCHECKPOINT_PAGES};`,
|
|
);
|
|
});
|
|
|
|
it("enables fullfsync barriers for WAL checkpoints on macOS", () => {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
|
|
|
configureSqliteWalMaintenance(db, { checkpointIntervalMs: 0 });
|
|
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(1, "PRAGMA journal_mode = WAL;");
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(2, "PRAGMA checkpoint_fullfsync = 1;");
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(
|
|
3,
|
|
`PRAGMA wal_autocheckpoint = ${DEFAULT_SQLITE_WAL_AUTOCHECKPOINT_PAGES};`,
|
|
);
|
|
});
|
|
|
|
it("continues WAL setup if macOS checkpoint fullfsync is unavailable", () => {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
|
vi.mocked(db["exec"]).mockImplementation((sql) => {
|
|
if (sql.includes("checkpoint_fullfsync")) {
|
|
throw new Error("unsupported pragma");
|
|
}
|
|
});
|
|
|
|
configureSqliteWalMaintenance(db, { checkpointIntervalMs: 0 });
|
|
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(1, "PRAGMA journal_mode = WAL;");
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(2, "PRAGMA checkpoint_fullfsync = 1;");
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(
|
|
3,
|
|
`PRAGMA wal_autocheckpoint = ${DEFAULT_SQLITE_WAL_AUTOCHECKPOINT_PAGES};`,
|
|
);
|
|
});
|
|
|
|
it("uses rollback journaling for databases on NFS-backed volumes", () => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-nfs-"));
|
|
try {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
const statfs = vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0x6969));
|
|
|
|
const maintenance = configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath: path.join(tempDir, "missing", "openclaw.sqlite"),
|
|
});
|
|
|
|
expect(statfs).toHaveBeenCalledWith(fs.realpathSync(tempDir));
|
|
expect(db["prepare"]).toHaveBeenCalledWith("PRAGMA journal_mode = DELETE;");
|
|
expect(db["exec"]).not.toHaveBeenCalled();
|
|
expect(maintenance.checkpoint()).toBe(true);
|
|
expect(maintenance.close()).toBe(true);
|
|
expect(db["exec"]).not.toHaveBeenCalled();
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it.each([
|
|
["SMB", 0x517b],
|
|
["CIFS", 0xff534d42],
|
|
["SMB2", 0xfe534d42],
|
|
])("uses rollback journaling for databases on Linux %s volumes", (_label, fsType) => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-network-"));
|
|
try {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(fsType));
|
|
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath: path.join(tempDir, "openclaw.sqlite"),
|
|
});
|
|
|
|
expect(db["prepare"]).toHaveBeenCalledWith("PRAGMA journal_mode = DELETE;");
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it.each([
|
|
String.raw`\\server\share\openclaw.sqlite`,
|
|
String.raw`\\?\UNC\server\share\openclaw.sqlite`,
|
|
"//server/share/openclaw.sqlite",
|
|
"//?/UNC/server/share/openclaw.sqlite",
|
|
])("uses rollback journaling for databases on Windows UNC paths: %s", (databasePath) => {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("win32");
|
|
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath,
|
|
});
|
|
|
|
expect(db["prepare"]).toHaveBeenCalledWith("PRAGMA journal_mode = DELETE;");
|
|
expect(db["exec"]).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("uses rollback journaling for mapped Windows network drives", () => {
|
|
const db = createMockDb();
|
|
const databasePath = String.raw`Z:\state\openclaw.sqlite`;
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("win32");
|
|
const realpath = vi
|
|
.spyOn(fs.realpathSync, "native")
|
|
.mockReturnValue(String.raw`\\server\share\state\openclaw.sqlite`);
|
|
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath,
|
|
});
|
|
|
|
expect(realpath).toHaveBeenCalledWith(databasePath);
|
|
expect(db["prepare"]).toHaveBeenCalledWith("PRAGMA journal_mode = DELETE;");
|
|
expect(db["exec"]).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not treat namespaced Windows local drives as UNC paths", () => {
|
|
const db = createMockDb();
|
|
const databasePath = String.raw`\\?\C:\state\openclaw.sqlite`;
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("win32");
|
|
const realpath = vi.spyOn(fs.realpathSync, "native").mockReturnValue(databasePath);
|
|
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath,
|
|
});
|
|
|
|
expect(realpath).toHaveBeenCalledWith(databasePath);
|
|
expect(db["prepare"]).not.toHaveBeenCalled();
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(1, "PRAGMA journal_mode = WAL;");
|
|
});
|
|
|
|
it("uses rollback journaling when Windows cannot classify an opened drive path", () => {
|
|
const db = createMockDb();
|
|
const databasePath = String.raw`Z:\restricted\openclaw.sqlite`;
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("win32");
|
|
vi.spyOn(fs.realpathSync, "native").mockImplementation(() => {
|
|
throw new Error("access denied");
|
|
});
|
|
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath,
|
|
});
|
|
|
|
expect(db["prepare"]).toHaveBeenCalledWith("PRAGMA journal_mode = DELETE;");
|
|
expect(db["exec"]).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("refuses network-backed databases when SQLite keeps WAL active", () => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-nfs-"));
|
|
try {
|
|
const db = createMockDb();
|
|
vi.mocked(db["prepare"]).mockReturnValue({
|
|
get: vi.fn(() => ({ journal_mode: "wal" })),
|
|
} as unknown as ReturnType<DatabaseSync["prepare"]>);
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0x6969));
|
|
|
|
expect(() =>
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databaseLabel: "test-db",
|
|
databasePath: path.join(tempDir, "openclaw.sqlite"),
|
|
}),
|
|
).toThrow(/test-db .*journal_mode=wal/);
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("uses mountinfo filesystem names when statfs magic is not enough", () => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-nfs-"));
|
|
try {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0));
|
|
vi.spyOn(fs, "readFileSync").mockReturnValue(
|
|
`42 12 0:41 / ${tempDir} rw,relatime - nfs4 server:/share rw\n`,
|
|
);
|
|
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath: path.join(tempDir, "openclaw.sqlite"),
|
|
});
|
|
|
|
expect(db["prepare"]).toHaveBeenCalledWith("PRAGMA journal_mode = DELETE;");
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("refuses fuse.sshfs mountinfo entries", () => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-sshfs-"));
|
|
try {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0));
|
|
vi.spyOn(fs, "readFileSync").mockReturnValue(
|
|
`42 12 0:41 / ${tempDir} rw,relatime - fuse.sshfs user@host:/share rw\n`,
|
|
);
|
|
|
|
expect(() =>
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databaseLabel: "test-db",
|
|
databasePath: path.join(tempDir, "openclaw.sqlite"),
|
|
}),
|
|
).toThrow(/test-db .*SSHFS.*refusing to open/);
|
|
|
|
expect(db["prepare"]).not.toHaveBeenCalled();
|
|
expect(db["exec"]).not.toHaveBeenCalled();
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("refuses symlinked paths into fuse.sshfs mounts", () => {
|
|
if (process.platform === "win32") {
|
|
return;
|
|
}
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-sshfs-link-"));
|
|
const mountDir = path.join(tempDir, "mount");
|
|
const linkedDir = path.join(tempDir, "linked");
|
|
try {
|
|
fs.mkdirSync(mountDir);
|
|
fs.symlinkSync(mountDir, linkedDir);
|
|
const canonicalMountDir = fs.realpathSync(mountDir);
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0));
|
|
vi.spyOn(fs, "readFileSync").mockReturnValue(
|
|
`42 12 0:41 / ${canonicalMountDir} rw,relatime - fuse.sshfs user@host:/share rw\n`,
|
|
);
|
|
|
|
expect(() =>
|
|
configureSqliteWalMaintenance(createMockDb(), {
|
|
checkpointIntervalMs: 0,
|
|
databasePath: path.join(linkedDir, "openclaw.sqlite"),
|
|
}),
|
|
).toThrow(/SSHFS.*refusing to open/);
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("matches raw mount paths when the existing path canonicalizes elsewhere", () => {
|
|
if (process.platform === "win32") {
|
|
return;
|
|
}
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-sshfs-prefix-"));
|
|
const canonicalMountDir = path.join(tempDir, "canonical-mount");
|
|
const rawMountDir = path.join(tempDir, "raw-mount");
|
|
try {
|
|
fs.mkdirSync(canonicalMountDir);
|
|
fs.symlinkSync(canonicalMountDir, rawMountDir);
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0));
|
|
vi.spyOn(fs, "readFileSync").mockReturnValue(
|
|
`42 12 0:41 / ${rawMountDir} rw,relatime - fuse.sshfs user@host:/share rw\n`,
|
|
);
|
|
|
|
expect(() =>
|
|
configureSqliteWalMaintenance(createMockDb(), {
|
|
checkpointIntervalMs: 0,
|
|
databasePath: path.join(rawMountDir, "openclaw.sqlite"),
|
|
}),
|
|
).toThrow(/SSHFS.*refusing to open/);
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("uses mount command filesystem names on platforms without proc mountinfo", () => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-nfs-"));
|
|
try {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0));
|
|
vi.spyOn(fs, "readFileSync").mockImplementation(() => {
|
|
throw new Error("no proc mountinfo");
|
|
});
|
|
vi.spyOn(childProcess, "execFileSync").mockReturnValue(
|
|
Buffer.from(`server:/share on ${tempDir} (nfs, nodev, nosuid)\n`),
|
|
);
|
|
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath: path.join(tempDir, "openclaw.sqlite"),
|
|
});
|
|
|
|
expect(db["prepare"]).toHaveBeenCalledWith("PRAGMA journal_mode = DELETE;");
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("uses macOS SMB mount filesystem names", () => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-smb-"));
|
|
try {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0));
|
|
vi.spyOn(fs, "readFileSync").mockImplementation(() => {
|
|
throw new Error("no proc mountinfo");
|
|
});
|
|
vi.spyOn(childProcess, "execFileSync").mockReturnValue(
|
|
Buffer.from(`//server/share on ${tempDir} (smbfs, nodev, nosuid)\n`),
|
|
);
|
|
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath: path.join(tempDir, "openclaw.sqlite"),
|
|
});
|
|
|
|
expect(db["prepare"]).toHaveBeenCalledWith("PRAGMA journal_mode = DELETE;");
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it.each([
|
|
["macfuse", "sshfs#user@host:/share"],
|
|
["macfuse", "host:/share"],
|
|
["macfuse", "user@host:"],
|
|
["osxfuse", "user@host:/share"],
|
|
["osxfuse", "sshfs@osxfuse0"],
|
|
])("refuses SSHFS reported as %s by mount", (fsType, source) => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-sshfs-macfuse-"));
|
|
try {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0));
|
|
vi.spyOn(fs, "readFileSync").mockImplementation(() => {
|
|
throw new Error("no proc mountinfo");
|
|
});
|
|
vi.spyOn(childProcess, "execFileSync").mockReturnValue(
|
|
Buffer.from(`${source} on ${tempDir} (${fsType}, nodev, nosuid)\n`),
|
|
);
|
|
|
|
expect(() =>
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath: path.join(tempDir, "openclaw.sqlite"),
|
|
}),
|
|
).toThrow(/refusing to open/);
|
|
|
|
expect(db["exec"]).not.toHaveBeenCalled();
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("keeps WAL enabled for non-remote macFUSE mounts", () => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-macfuse-"));
|
|
try {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0));
|
|
vi.spyOn(fs, "readFileSync").mockImplementation(() => {
|
|
throw new Error("no proc mountinfo");
|
|
});
|
|
vi.spyOn(childProcess, "execFileSync").mockReturnValue(
|
|
Buffer.from(`remote-volume on ${tempDir} (macfuse, nodev, nosuid)\n`),
|
|
);
|
|
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath: path.join(tempDir, "openclaw.sqlite"),
|
|
});
|
|
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(1, "PRAGMA journal_mode = WAL;");
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("parses Linux mount command filesystem names when proc mountinfo is unavailable", () => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-nfs-"));
|
|
try {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0));
|
|
vi.spyOn(fs, "readFileSync").mockImplementation(() => {
|
|
throw new Error("no proc mountinfo");
|
|
});
|
|
vi.spyOn(childProcess, "execFileSync").mockReturnValue(
|
|
Buffer.from(`server:/share on ${tempDir} type nfs4 (rw,relatime)\n`),
|
|
);
|
|
|
|
configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
databasePath: path.join(tempDir, "openclaw.sqlite"),
|
|
});
|
|
|
|
expect(db["prepare"]).toHaveBeenCalledWith("PRAGMA journal_mode = DELETE;");
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it("runs lightweight periodic PASSIVE checkpoints and TRUNCATE on close", () => {
|
|
vi.useFakeTimers();
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
|
|
const maintenance = configureSqliteWalMaintenance(db, { checkpointIntervalMs: 100 });
|
|
expect(db["exec"]).toHaveBeenCalledTimes(2);
|
|
|
|
vi.advanceTimersByTime(100);
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(3, "PRAGMA wal_checkpoint(PASSIVE);");
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(4, "PRAGMA incremental_vacuum(512);");
|
|
expect(db["exec"]).toHaveBeenCalledTimes(4);
|
|
|
|
expect(maintenance.close()).toBe(true);
|
|
expect(db["exec"]).toHaveBeenLastCalledWith("PRAGMA wal_checkpoint(TRUNCATE);");
|
|
expect(db["exec"]).toHaveBeenCalledTimes(5);
|
|
|
|
vi.advanceTimersByTime(200);
|
|
expect(db["exec"]).toHaveBeenCalledTimes(5);
|
|
});
|
|
|
|
it("clamps oversized checkpoint intervals before arming timers", () => {
|
|
vi.useFakeTimers();
|
|
const setIntervalSpy = vi.spyOn(globalThis, "setInterval");
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
|
|
const maintenance = configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: Number.MAX_SAFE_INTEGER,
|
|
});
|
|
|
|
expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
|
|
maintenance.close();
|
|
});
|
|
|
|
it("honors explicit checkpoint mode overrides for periodic and close checkpoints", () => {
|
|
vi.useFakeTimers();
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
|
|
const maintenance = configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 100,
|
|
checkpointMode: "FULL",
|
|
});
|
|
|
|
vi.advanceTimersByTime(100);
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(3, "PRAGMA wal_checkpoint(FULL);");
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(4, "PRAGMA incremental_vacuum(512);");
|
|
|
|
expect(maintenance.close()).toBe(true);
|
|
expect(db["exec"]).toHaveBeenLastCalledWith("PRAGMA wal_checkpoint(FULL);");
|
|
});
|
|
|
|
it("reports checkpoint errors without throwing from background maintenance", () => {
|
|
const db = createMockDb();
|
|
const error = new Error("busy");
|
|
const onCheckpointError = vi.fn();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
vi.mocked(db["exec"]).mockImplementation((sql) => {
|
|
if (sql.includes("wal_checkpoint")) {
|
|
throw error;
|
|
}
|
|
});
|
|
|
|
const maintenance = configureSqliteWalMaintenance(db, {
|
|
checkpointIntervalMs: 0,
|
|
onCheckpointError,
|
|
});
|
|
|
|
expect(maintenance.checkpoint()).toBe(false);
|
|
expect(onCheckpointError).toHaveBeenCalledWith(error);
|
|
});
|
|
|
|
it("configures connection pragmas before WAL maintenance", () => {
|
|
const db = createMockDb();
|
|
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
|
|
|
configureSqliteConnectionPragmas(db, {
|
|
busyTimeoutMs: 30_000,
|
|
checkpointIntervalMs: 0,
|
|
foreignKeys: true,
|
|
synchronous: "NORMAL",
|
|
});
|
|
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(1, "PRAGMA busy_timeout = 30000;");
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(2, "PRAGMA journal_mode = WAL;");
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(
|
|
3,
|
|
`PRAGMA wal_autocheckpoint = ${DEFAULT_SQLITE_WAL_AUTOCHECKPOINT_PAGES};`,
|
|
);
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(4, "PRAGMA synchronous = NORMAL;");
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(5, "PRAGMA foreign_keys = ON;");
|
|
});
|
|
|
|
it("sets busy timeout before rollback journaling on NFS-backed volumes", () => {
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sqlite-nfs-"));
|
|
try {
|
|
const db = createMockDb();
|
|
vi.spyOn(fs, "statfsSync").mockReturnValue(statfsFixture(0x6969));
|
|
|
|
configureSqliteConnectionPragmas(db, {
|
|
busyTimeoutMs: 5000,
|
|
checkpointIntervalMs: 0,
|
|
databasePath: path.join(tempDir, "openclaw.sqlite"),
|
|
synchronous: "NORMAL",
|
|
});
|
|
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(1, "PRAGMA busy_timeout = 5000;");
|
|
expect(db["prepare"]).toHaveBeenCalledWith("PRAGMA journal_mode = DELETE;");
|
|
expect(db["exec"]).toHaveBeenNthCalledWith(2, "PRAGMA synchronous = NORMAL;");
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|