Skip to content

Commit c7f70f3

Browse files
authored
fix(sqlite): stage read-only snapshots in private user cache (#128635)
* fix(sqlite): stage read-only snapshots in private user cache * fix(sqlite): correct snapshot cache and staging diagnostics * fix(sqlite): diagnose snapshot cache allocation failures * fix(sqlite): unwrap Windows snapshot staging errors * fix(sqlite): diagnose sanitized staging allocation errors * fix(sqlite): scope snapshot staging errors to allocated directory
1 parent ca3c3db commit c7f70f3

6 files changed

Lines changed: 542 additions & 32 deletions

‎src/infra/sqlite-private-directory.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import path from "node:path";
12
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { withEnv } from "../test-utils/env.js";
24

35
const childProcess = vi.hoisted(() => ({
46
execFile: vi.fn(),
@@ -21,7 +23,9 @@ vi.mock("./windows-encoding.js", async (importOriginal) => {
2123
import {
2224
createPrivateSqliteDirectory,
2325
createPrivateSqliteTempDirectorySync,
26+
resolvePrivateSqliteSnapshotStagingRoot,
2427
} from "./sqlite-private-directory.js";
28+
import * as tmpOpenClawDir from "./tmp-openclaw-dir.js";
2529

2630
function errorCause(error: unknown): Error {
2731
expect(error).toBeInstanceOf(Error);
@@ -37,6 +41,62 @@ describe("private Windows SQLite directory diagnostics", () => {
3741
childProcess.execFileSync.mockReset();
3842
});
3943

44+
it.each([
45+
{
46+
label: "an absolute LOCALAPPDATA root",
47+
xdgCacheHome: undefined,
48+
localAppData: path.resolve("sqlite-local-app-data"),
49+
expectedRoot: path.resolve("sqlite-local-app-data"),
50+
},
51+
{
52+
label: "LOCALAPPDATA when XDG_CACHE_HOME is relative",
53+
xdgCacheHome: "relative/cache",
54+
localAppData: path.resolve("sqlite-local-app-data"),
55+
expectedRoot: path.resolve("sqlite-local-app-data"),
56+
},
57+
{
58+
label: "HOME/AppData/Local when LOCALAPPDATA is absent",
59+
xdgCacheHome: undefined,
60+
localAppData: undefined,
61+
expectedRoot: path.join(path.resolve("sqlite-home"), "AppData", "Local"),
62+
},
63+
{
64+
label: "HOME/AppData/Local when LOCALAPPDATA is relative",
65+
xdgCacheHome: undefined,
66+
localAppData: "relative/local-app-data",
67+
expectedRoot: path.join(path.resolve("sqlite-home"), "AppData", "Local"),
68+
},
69+
{
70+
label: "absolute XDG_CACHE_HOME ahead of LOCALAPPDATA",
71+
xdgCacheHome: path.resolve("sqlite-xdg-cache"),
72+
localAppData: path.resolve("sqlite-local-app-data"),
73+
expectedRoot: path.resolve("sqlite-xdg-cache"),
74+
},
75+
])(
76+
"selects $label for the Windows snapshot cache",
77+
({ xdgCacheHome, localAppData, expectedRoot }) => {
78+
vi.spyOn(process, "platform", "get").mockReturnValue("win32");
79+
const resolveTempRoot = vi
80+
.spyOn(tmpOpenClawDir, "resolvePreferredOpenClawTmpDir")
81+
.mockImplementation((options) => options?.preferredDir ?? "");
82+
83+
withEnv(
84+
{
85+
HOME: path.resolve("sqlite-home"),
86+
LOCALAPPDATA: localAppData,
87+
XDG_CACHE_HOME: xdgCacheHome,
88+
},
89+
() => {
90+
expect(resolvePrivateSqliteSnapshotStagingRoot()).toBe(
91+
path.join(expectedRoot, "openclaw"),
92+
);
93+
},
94+
);
95+
96+
expect(resolveTempRoot.mock.calls[0]?.[0]?.tmpdir?.()).toBe(expectedRoot);
97+
},
98+
);
99+
40100
it("reports bounded async stderr without retaining the child-process error", async () => {
41101
vi.spyOn(process, "platform", "get").mockReturnValue("win32");
42102
const original = Object.assign(

‎src/infra/sqlite-private-directory.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { randomUUID } from "node:crypto";
44
import fsSync from "node:fs";
55
import fs from "node:fs/promises";
66
import path from "node:path";
7+
import { resolveRequiredOsHomeDir } from "./home-dir.js";
78
import { resolveSystemBin } from "./resolve-system-bin.js";
9+
import { resolvePreferredOpenClawTmpDir } from "./tmp-openclaw-dir.js";
810
import { decodeWindowsOutputBuffer } from "./windows-encoding.js";
911
import {
1012
buildEncodedPowerShellArgs,
@@ -206,6 +208,19 @@ function createPrivateSqliteDirectorySync(directoryPath: string): void {
206208
}
207209
}
208210

211+
export function resolvePrivateSqliteSnapshotStagingRoot(): string {
212+
const appData = process.platform === "win32" ? process.env.LOCALAPPDATA?.trim() : undefined;
213+
const defaultRoot = process.platform === "win32" ? "AppData/Local" : ".cache";
214+
const platformRoot = process.platform === "darwin" ? "Library/Caches" : defaultRoot;
215+
const cacheRoot =
216+
[process.env.XDG_CACHE_HOME?.trim(), appData].find((root) => root && path.isAbsolute(root)) ??
217+
path.join(resolveRequiredOsHomeDir(), platformRoot);
218+
return resolvePreferredOpenClawTmpDir({
219+
preferredDir: path.join(cacheRoot, "openclaw"),
220+
tmpdir: () => cacheRoot,
221+
});
222+
}
223+
209224
export async function createPrivateSqliteTempDirectory(
210225
rootPath: string,
211226
prefix: string,

0 commit comments

Comments
 (0)