Skip to content

Commit b855934

Browse files
authored
fix(test): isolate embedded runner SQLite state (#120459)
1 parent 263ad62 commit b855934

3 files changed

Lines changed: 133 additions & 36 deletions

File tree

‎src/agents/embedded-agent-runner/runs.force-clear-terminal.test.ts‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from "node:path";
22
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3-
import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js";
43
import {
54
createReplyOperation,
65
isReplyRunActiveForSessionId,
@@ -9,6 +8,10 @@ import {
98
import { testing as replyRunTesting } from "../../auto-reply/reply/reply-run-registry.test-support.js";
109
import { clearRuntimeConfigSnapshot, setRuntimeConfigSnapshot } from "../../config/io.js";
1110
import { loadSessionEntry, upsertSessionEntry } from "../../config/sessions/session-accessor.js";
11+
import {
12+
createOpenClawTestState,
13+
type OpenClawTestState,
14+
} from "../../test-utils/openclaw-test-state.js";
1215
import {
1316
abortAndDrainEmbeddedAgentRun,
1417
clearActiveEmbeddedRun,
@@ -34,19 +37,28 @@ function createRunHandle(
3437
}
3538

3639
describe("force-clear terminal state persistence", () => {
37-
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
40+
let testState: OpenClawTestState | undefined;
3841
let storePath: string;
3942

40-
beforeEach(() => {
41-
const tempDir = tempDirs.make("openclaw-forceclear-");
42-
storePath = path.join(tempDir, "sessions.json");
43+
beforeEach(async () => {
44+
testState = await createOpenClawTestState({
45+
layout: "state-only",
46+
prefix: "openclaw-forceclear-",
47+
});
48+
storePath = path.join(testState.sessionsDir(), "sessions.json");
4349
setRuntimeConfigSnapshot({ session: { store: storePath } });
4450
});
4551

46-
afterEach(() => {
47-
clearRuntimeConfigSnapshot();
48-
testing.resetActiveEmbeddedRuns();
49-
replyRunTesting.resetReplyRunRegistry();
52+
afterEach(async () => {
53+
const state = testState;
54+
testState = undefined;
55+
try {
56+
clearRuntimeConfigSnapshot();
57+
testing.resetActiveEmbeddedRuns();
58+
replyRunTesting.resetReplyRunRegistry();
59+
} finally {
60+
await state?.cleanup();
61+
}
5062
});
5163

5264
it("delays stale-owner followups until the old reply owner settles", async () => {
Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import fs from "node:fs/promises";
2-
import os from "node:os";
31
import path from "node:path";
42
import { afterEach, describe, expect, it } from "vitest";
53
import { loadSessionEntry, replaceSessionEntry } from "../../config/sessions/session-accessor.js";
64
import type { InternalSessionEntry } from "../../config/sessions/types.js";
5+
import { withOpenClawTestState } from "../../test-utils/openclaw-test-state.js";
76
import { testing } from "./runs.test-support.js";
87

98
describe("embedded-agent runner persistence", () => {
@@ -12,31 +11,31 @@ describe("embedded-agent runner persistence", () => {
1211
});
1312

1413
it("clears lifecycle ownership when a forced run clear persists killed state", async () => {
15-
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-force-clear-"));
16-
const storePath = path.join(root, "sessions.json");
17-
const sessionKey = "agent:main:main";
18-
const entry: InternalSessionEntry = {
19-
lifecycleRunId: "stuck-run",
20-
sessionId: "session-stuck",
21-
startedAt: 10,
22-
status: "running",
23-
updatedAt: 20,
24-
};
25-
try {
26-
await replaceSessionEntry({ storePath, sessionKey }, entry);
27-
await testing.persistForceClearedEmbeddedRunTerminalState({
28-
sessionId: entry.sessionId,
29-
sessionKey,
30-
startedAt: entry.startedAt,
31-
storePath,
32-
updatedAt: entry.updatedAt,
33-
});
14+
await withOpenClawTestState(
15+
{ layout: "state-only", prefix: "openclaw-force-clear-" },
16+
async (state) => {
17+
const storePath = path.join(state.sessionsDir(), "sessions.json");
18+
const sessionKey = "agent:main:main";
19+
const entry: InternalSessionEntry = {
20+
lifecycleRunId: "stuck-run",
21+
sessionId: "session-stuck",
22+
startedAt: 10,
23+
status: "running",
24+
updatedAt: 20,
25+
};
26+
await replaceSessionEntry({ storePath, sessionKey }, entry);
27+
await testing.persistForceClearedEmbeddedRunTerminalState({
28+
sessionId: entry.sessionId,
29+
sessionKey,
30+
startedAt: entry.startedAt,
31+
storePath,
32+
updatedAt: entry.updatedAt,
33+
});
3434

35-
const persisted = loadSessionEntry({ storePath, sessionKey }) as InternalSessionEntry;
36-
expect(persisted.status).toBe("killed");
37-
expect(persisted.lifecycleRunId).toBeUndefined();
38-
} finally {
39-
await fs.rm(root, { recursive: true, force: true });
40-
}
35+
const persisted = loadSessionEntry({ storePath, sessionKey }) as InternalSessionEntry;
36+
expect(persisted.status).toBe("killed");
37+
expect(persisted.lifecycleRunId).toBeUndefined();
38+
},
39+
);
4140
});
4241
});

‎test/non-isolated-runner.test.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,89 @@ it("clears session suspension state between files", async () => {
280280
await fs.rm(root, { recursive: true, force: true });
281281
}
282282
});
283+
284+
it("disposes embedded-agent SQLite state before running the next file", async () => {
285+
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sqlite-lifecycle-runner-"));
286+
const orderLogPath = path.join(root, "order.log");
287+
const forceClearPath = path.join(
288+
repoRoot,
289+
"src",
290+
"agents",
291+
"embedded-agent-runner",
292+
"runs.force-clear-terminal.test.ts",
293+
);
294+
const persistencePath = path.join(
295+
repoRoot,
296+
"src",
297+
"agents",
298+
"embedded-agent-runner",
299+
"runs.persistence.test.ts",
300+
);
301+
try {
302+
const embeddedConfigPath = JSON.stringify(
303+
path.join(repoRoot, "test", "vitest", "vitest.agents-embedded-agent.config.ts"),
304+
);
305+
await fs.symlink(
306+
path.join(repoRoot, "node_modules"),
307+
path.join(root, "node_modules"),
308+
"junction",
309+
);
310+
await fs.writeFile(
311+
path.join(root, "vitest.config.ts"),
312+
[
313+
`import { createAgentsEmbeddedVitestConfig } from ${embeddedConfigPath};`,
314+
'import { BaseSequencer } from "vitest/node";',
315+
"class AlphabeticalSequencer extends BaseSequencer {",
316+
' override async sort(files: Parameters<BaseSequencer["sort"]>[0]) {',
317+
" return [...files].sort((a, b) => a.moduleId.localeCompare(b.moduleId));",
318+
" }",
319+
"}",
320+
"const base = createAgentsEmbeddedVitestConfig();",
321+
"export default {",
322+
" ...base,",
323+
` cacheDir: ${JSON.stringify(path.join(root, ".vite"))},`,
324+
" test: {",
325+
" ...base.test,",
326+
" exclude: [],",
327+
" isolate: false,",
328+
" fileParallelism: false,",
329+
" maxWorkers: 1,",
330+
" sequence: { ...base.test?.sequence, sequencer: AlphabeticalSequencer },",
331+
" },",
332+
"};",
333+
"",
334+
].join("\n"),
335+
"utf8",
336+
);
337+
338+
const vitestEntry = path.join(repoRoot, "node_modules", "vitest", "vitest.mjs");
339+
const result = await execFileAsync(
340+
process.execPath,
341+
[
342+
vitestEntry,
343+
"run",
344+
"src/agents/embedded-agent-runner/runs.force-clear-terminal.test.ts",
345+
"src/agents/embedded-agent-runner/runs.persistence.test.ts",
346+
"--config",
347+
path.join(root, "vitest.config.ts"),
348+
],
349+
{
350+
cwd: repoRoot,
351+
env: { ...childEnv(), OPENCLAW_VITEST_FILE_ORDER_LOG: orderLogPath },
352+
maxBuffer: 16 * 1024 * 1024,
353+
},
354+
);
355+
expect(`${result.stdout}\n${result.stderr}`).toMatch(/Test Files\s+2 passed \(2\)/u);
356+
await expect(fs.readFile(orderLogPath, "utf8")).resolves.toBe(
357+
[
358+
`START ${forceClearPath}`,
359+
`END ${forceClearPath}`,
360+
`START ${persistencePath}`,
361+
`END ${persistencePath}`,
362+
"",
363+
].join("\n"),
364+
);
365+
} finally {
366+
await fs.rm(root, { recursive: true, force: true });
367+
}
368+
});

0 commit comments

Comments
 (0)