Skip to content

Commit b13ba94

Browse files
zhangLei99586steipete
authored andcommitted
fix(proxy-capture): make path-based session cleanup atomic
1 parent 8b994fc commit b13ba94

2 files changed

Lines changed: 72 additions & 7 deletions

File tree

‎src/proxy-capture/store.sqlite.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Proxy capture SQLite store tests cover persisted capture reads and writes.
22
import fs from "node:fs";
33
import path from "node:path";
4+
import { constants } from "node:sqlite";
45
import { afterEach, describe, expect, it, vi } from "vitest";
56
import { cleanupTempDirs, makeTempDir } from "../../test/helpers/temp-dir.js";
67
import { resolveSqliteDatabaseFilePaths } from "../infra/sqlite-files.js";
@@ -161,6 +162,66 @@ describe("DebugProxyCaptureStore", () => {
161162
expect(lease.store.isClosed).toBe(true);
162163
});
163164

165+
it.each(["deleteSessions", "purgeAll"] as const)(
166+
"rolls back path-based %s when session deletion fails",
167+
(operation) => {
168+
const root = makeTempDir(cleanupDirs, "openclaw-proxy-capture-rollback-");
169+
const dbPath = path.join(root, "capture.sqlite");
170+
const blobDir = path.join(root, "blobs");
171+
const lease = acquireDebugProxyCaptureStore(dbPath, blobDir);
172+
const sessionId = "path-based-rollback-session";
173+
174+
try {
175+
lease.store.upsertSession({
176+
id: sessionId,
177+
startedAt: 1,
178+
mode: "sdk",
179+
sourceScope: "openclaw",
180+
sourceProcess: "plugin",
181+
dbPath,
182+
blobDir,
183+
});
184+
const blob = lease.store.persistPayload(Buffer.from("rollback payload"), "text/plain");
185+
lease.store.recordEvent({
186+
sessionId,
187+
ts: 2,
188+
sourceScope: "openclaw",
189+
sourceProcess: "plugin",
190+
protocol: "https",
191+
direction: "outbound",
192+
kind: "request",
193+
flowId: "path-based-rollback-flow",
194+
dataBlobId: blob.blobId,
195+
dataSha256: blob.sha256,
196+
});
197+
198+
const cleanup = () =>
199+
operation === "deleteSessions"
200+
? lease.store.deleteSessions([sessionId])
201+
: lease.store.purgeAll();
202+
lease.store.db.setAuthorizer((action, table) =>
203+
action === constants.SQLITE_DELETE && table === "capture_sessions"
204+
? constants.SQLITE_DENY
205+
: constants.SQLITE_OK,
206+
);
207+
208+
expect(cleanup).toThrow(/not authorized/u);
209+
lease.store.db.setAuthorizer(null);
210+
expect(lease.store.listSessions()).toHaveLength(1);
211+
expect(lease.store.getSessionEvents(sessionId)).toHaveLength(1);
212+
expect(fs.existsSync(blob.path)).toBe(true);
213+
214+
expect(cleanup()).toEqual({ sessions: 1, events: 1, blobs: 1 });
215+
expect(lease.store.listSessions()).toEqual([]);
216+
expect(lease.store.getSessionEvents(sessionId)).toEqual([]);
217+
expect(fs.existsSync(blob.path)).toBe(false);
218+
} finally {
219+
lease.store.db.setAuthorizer(null);
220+
lease.release();
221+
}
222+
},
223+
);
224+
164225
it("uses rollback journaling for captures on NFS-backed volumes", () => {
165226
vi.spyOn(fs, "statfsSync").mockReturnValue({
166227
type: 0x6969,

‎src/proxy-capture/store.sqlite.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,9 @@ class DebugProxyCaptureStoreImpl {
616616
const eventCount =
617617
(this.db.prepare(`SELECT COUNT(*) AS count FROM capture_events`).get() as { count: number })
618618
.count ?? 0;
619-
this.db.exec(`DELETE FROM capture_events; DELETE FROM capture_sessions;`);
619+
runSqliteImmediateTransactionSync(this.db, () => {
620+
this.db.exec(`DELETE FROM capture_events; DELETE FROM capture_sessions;`);
621+
});
620622
let blobs = 0;
621623
if (fs.existsSync(this.pathBased.blobDir)) {
622624
for (const entry of fs.readdirSync(this.pathBased.blobDir)) {
@@ -764,12 +766,14 @@ class DebugProxyCaptureStoreImpl {
764766
)
765767
.get(...sessionIds) as { count: number }
766768
).count ?? 0;
767-
this.db
768-
.prepare(`DELETE FROM capture_events WHERE session_id IN (${placeholders})`)
769-
.run(...sessionIds);
770-
this.db
771-
.prepare(`DELETE FROM capture_sessions WHERE id IN (${placeholders})`)
772-
.run(...sessionIds);
769+
runSqliteImmediateTransactionSync(this.db, () => {
770+
this.db
771+
.prepare(`DELETE FROM capture_events WHERE session_id IN (${placeholders})`)
772+
.run(...sessionIds);
773+
this.db
774+
.prepare(`DELETE FROM capture_sessions WHERE id IN (${placeholders})`)
775+
.run(...sessionIds);
776+
});
773777
const candidateBlobIds = blobRows
774778
.map((row) => row.blobId?.trim())
775779
.filter((blobId): blobId is string => Boolean(blobId));

0 commit comments

Comments
 (0)