diff --git a/src/auto-reply/reply/reply-state.test.ts b/src/auto-reply/reply/reply-state.test.ts index fee6b74fe70..5135428ce57 100644 --- a/src/auto-reply/reply/reply-state.test.ts +++ b/src/auto-reply/reply/reply-state.test.ts @@ -1,7 +1,8 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; -import { describe, expect, it } from "vitest"; +import { afterEach, describe, expect, it } from "vitest"; +import { DEFAULT_PI_COMPACTION_RESERVE_TOKENS_FLOOR } from "../../agents/pi-settings.js"; import type { SessionEntry } from "../../config/sessions.js"; import { appendHistoryEntry, @@ -22,6 +23,12 @@ import { import { CURRENT_MESSAGE_MARKER } from "./mentions.js"; import { incrementCompactionCount } from "./session-updates.js"; +const tempDirs: string[] = []; + +afterEach(async () => { + await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true }))); +}); + async function seedSessionStore(params: { storePath: string; sessionKey: string; @@ -37,6 +44,7 @@ async function seedSessionStore(params: { async function createCompactionSessionFixture(entry: SessionEntry) { const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-compact-")); + tempDirs.push(tmp); const storePath = path.join(tmp, "sessions.json"); const sessionKey = "main"; const sessionStore: Record = { [sessionKey]: entry }; @@ -219,6 +227,24 @@ describe("memory flush settings", () => { expect(settings?.prompt).toContain("NO_REPLY"); expect(settings?.systemPrompt).toContain("NO_REPLY"); }); + + it("falls back to defaults when numeric values are invalid", () => { + const settings = resolveMemoryFlushSettings({ + agents: { + defaults: { + compaction: { + reserveTokensFloor: Number.NaN, + memoryFlush: { + softThresholdTokens: -100, + }, + }, + }, + }, + }); + + expect(settings?.softThresholdTokens).toBe(DEFAULT_MEMORY_FLUSH_SOFT_TOKENS); + expect(settings?.reserveTokensFloor).toBe(DEFAULT_PI_COMPACTION_RESERVE_TOKENS_FLOOR); + }); }); describe("shouldRunMemoryFlush", () => { @@ -312,12 +338,8 @@ describe("resolveMemoryFlushContextWindowTokens", () => { describe("incrementCompactionCount", () => { it("increments compaction count", async () => { - const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-compact-")); - const storePath = path.join(tmp, "sessions.json"); - const sessionKey = "main"; const entry = { sessionId: "s1", updatedAt: Date.now(), compactionCount: 2 } as SessionEntry; - const sessionStore: Record = { [sessionKey]: entry }; - await seedSessionStore({ storePath, sessionKey, entry }); + const { storePath, sessionKey, sessionStore } = await createCompactionSessionFixture(entry); const count = await incrementCompactionCount({ sessionEntry: entry,