Skip to content

Commit 3b1c8f2

Browse files
authored
test(memory-core): release cached databases before removing fixture state dirs (#128310)
* test(memory-core): release cached state handles before removing fixture dirs Six memory-core fixtures remove their temporary state directory while the per-agent or shared SQLite handles opened underneath it are still cached, so Windows fails the removal with EBUSY while Linux unlinks the open files and stays green. Release both handles before each removal, in the ordering merged for this class: the agent close releases its leases through shared state and reopens it, so the shared store has to be released after it. * test(memory-core): release cached handles in the shared fixture harness and cli teardown
1 parent d1177a7 commit 3b1c8f2

8 files changed

Lines changed: 45 additions & 1 deletion

‎extensions/memory-core/src/cli.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from "node:path";
55
import { Command } from "commander";
66
import type { OpenClawConfig } from "openclaw/plugin-sdk/memory-core-host-engine-foundation";
77
import { resolveSessionTranscriptsDirForAgent as resolveTestSessionTranscriptsDirForAgent } from "openclaw/plugin-sdk/memory-core-host-runtime-core";
8+
import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
89
import { upsertSessionEntry } from "openclaw/plugin-sdk/session-store-runtime";
910
import { appendSessionTranscriptMessageByIdentity } from "openclaw/plugin-sdk/session-transcript-runtime";
1011
import { resolveOpenClawAgentSqlitePath } from "openclaw/plugin-sdk/sqlite-runtime";
@@ -161,6 +162,10 @@ afterAll(async () => {
161162
if (!fixtureRoot) {
162163
return;
163164
}
165+
// The agent close releases its leases through shared state and reopens it, so the
166+
// shared handle is released second; otherwise Windows fails the removal with EBUSY.
167+
closeOpenClawAgentDatabasesForTest();
168+
resetPluginStateStoreForTests();
164169
await fs.rm(fixtureRoot, { recursive: true, force: true });
165170
resetMemoryCoreDreamingStateForTests();
166171
});

‎extensions/memory-core/src/dreaming-startup-cleanup.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import os from "node:os";
33
import path from "node:path";
44
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
55
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/plugin-entry";
6+
import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
67
import { getSessionEntry, upsertSessionEntry } from "openclaw/plugin-sdk/session-store-runtime";
78
import {
89
appendSqliteSessionTranscriptEventForTest,
@@ -30,6 +31,7 @@ afterEach(async () => {
3031
vi.useRealTimers();
3132
vi.unstubAllEnvs();
3233
vi.restoreAllMocks();
34+
resetPluginStateStoreForTests();
3335
await fs.rm(stateDir, { recursive: true, force: true });
3436
});
3537

‎extensions/memory-core/src/memory/manager-sync-ops.startup-catchup.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type MemorySyncParams,
2020
type MemorySyncProgressUpdate,
2121
} from "openclaw/plugin-sdk/memory-core-host-engine-storage";
22+
import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
2223
import {
2324
clearConfigCache,
2425
clearRuntimeConfigSnapshot,
@@ -413,6 +414,10 @@ describe("session startup catch-up", () => {
413414
}
414415
startupHarnessDatabases.clear();
415416
closeOpenClawAgentDatabasesForTest();
417+
// Closing the agent databases releases their leases through shared state, which
418+
// reopens it, so the shared handle has to be released after that and before the
419+
// removal or Windows fails the unlink with EBUSY.
420+
resetPluginStateStoreForTests();
416421
await fs.rm(stateDir, { recursive: true, force: true });
417422
});
418423

‎extensions/memory-core/src/memory/manager.fts-only-reindex.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import os from "node:os";
44
import path from "node:path";
55
import { DatabaseSync } from "node:sqlite";
66
import type { OpenClawConfig } from "openclaw/plugin-sdk/memory-core-host-engine-foundation";
7+
import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
78
import { resolveOpenClawAgentSqlitePath } from "openclaw/plugin-sdk/sqlite-runtime";
9+
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
810
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
911
import { closeAllMemorySearchManagers, getMemorySearchManager } from "./index.js";
1012
import type { MemoryIndexMeta } from "./manager-reindex-state.js";
@@ -107,6 +109,10 @@ describe("memory manager FTS-only reindex", () => {
107109

108110
afterAll(async () => {
109111
await closeAllMemorySearchManagers();
112+
// The agent close releases its leases through shared state and reopens it, so the
113+
// shared handle is released second; otherwise Windows fails the removal with EBUSY.
114+
closeOpenClawAgentDatabasesForTest();
115+
resetPluginStateStoreForTests();
110116
if (fixtureRoot) {
111117
await fs.rm(fixtureRoot, { recursive: true, force: true });
112118
}

‎extensions/memory-core/src/memory/manager.reindex-recovery.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import os from "node:os";
44
import path from "node:path";
55
import { DatabaseSync } from "node:sqlite";
66
import type { OpenClawConfig } from "openclaw/plugin-sdk/memory-core-host-engine-foundation";
7+
import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
78
import { resolveOpenClawAgentSqlitePath } from "openclaw/plugin-sdk/sqlite-runtime";
9+
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
810
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
911
import { resetEmbeddingMocks } from "./embedding.test-mocks.js";
1012
import { acquireMemoryReindexLock } from "./manager-reindex-lock.js";
@@ -52,6 +54,10 @@ describe("memory manager reindex recovery", () => {
5254
}
5355
const { closeAllMemorySearchManagers } = await import("./index.js");
5456
await closeAllMemorySearchManagers();
57+
// The agent close releases its leases through shared state and reopens it, so the
58+
// shared handle is released second; otherwise Windows fails the removal with EBUSY.
59+
closeOpenClawAgentDatabasesForTest();
60+
resetPluginStateStoreForTests();
5561
await fs.rm(fixtureRoot, { recursive: true, force: true });
5662
});
5763

‎extensions/memory-core/src/memory/manager.self-heal-missing-identity.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import os from "node:os";
33
import path from "node:path";
44
import { DatabaseSync } from "node:sqlite";
55
import type { OpenClawConfig } from "openclaw/plugin-sdk/memory-core-host-engine-foundation";
6+
import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
67
import { resolveOpenClawAgentSqlitePath } from "openclaw/plugin-sdk/sqlite-runtime";
8+
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
79
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
810
import { closeAllMemorySearchManagers, getMemorySearchManager } from "./index.js";
911
import type { MemoryIndexManager } from "./manager.js";
@@ -78,6 +80,10 @@ describe("memory manager self-heal missing identity with FTS-only chunks", () =>
7880

7981
afterAll(async () => {
8082
await closeAllMemorySearchManagers();
83+
// The agent close releases its leases through shared state and reopens it, so the
84+
// shared handle is released second; otherwise Windows fails the removal with EBUSY.
85+
closeOpenClawAgentDatabasesForTest();
86+
resetPluginStateStoreForTests();
8187
if (fixtureRoot) {
8288
await fs.rm(fixtureRoot, { recursive: true, force: true });
8389
}

‎extensions/memory-core/src/memory/manager.watcher-config.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type {
77
MemorySearchConfig,
88
OpenClawConfig,
99
} from "openclaw/plugin-sdk/memory-core-host-engine-foundation";
10+
import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
11+
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
1012
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
1113

1214
type WatchIgnoredFn = (watchPath: string, stats?: { isDirectory?: () => boolean }) => boolean;
@@ -208,6 +210,10 @@ describe("memory watcher config", () => {
208210
await closeAllMemorySearchManagers();
209211
clearRegistry();
210212
restoreWatcherStateDir();
213+
// The agent close releases its leases through shared state and reopens it, so the
214+
// shared handle is released second; otherwise Windows fails the removal with EBUSY.
215+
closeOpenClawAgentDatabasesForTest();
216+
resetPluginStateStoreForTests();
211217
if (workspaceDir) {
212218
await fs.rm(workspaceDir, { recursive: true, force: true });
213219
workspaceDir = "";

‎extensions/memory-core/src/test-helpers.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import fs from "node:fs/promises";
33
import path from "node:path";
44
import type { OpenKeyedStoreOptions } from "openclaw/plugin-sdk/plugin-state-runtime";
5-
import { createPluginStateKeyedStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
5+
import {
6+
createPluginStateKeyedStoreForTests,
7+
resetPluginStateStoreForTests,
8+
} from "openclaw/plugin-sdk/plugin-state-test-runtime";
9+
import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing";
610
import { asOptionalRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
711
import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path";
812
import { afterAll, beforeAll } from "vitest";
@@ -210,6 +214,10 @@ export function createMemoryCoreTestHarness() {
210214
if (!fixtureRoot) {
211215
return;
212216
}
217+
// The agent close releases its leases through shared state and reopens it, so the
218+
// shared handle is released second; otherwise Windows fails the removal with EBUSY.
219+
closeOpenClawAgentDatabasesForTest();
220+
resetPluginStateStoreForTests();
213221
await fs.rm(fixtureRoot, { recursive: true, force: true });
214222
resetMemoryCoreDreamingStateForTests();
215223
});

0 commit comments

Comments
 (0)