























@@ -4,6 +4,7 @@ import fs from "node:fs/promises";
44import os from "node:os";
55import path from "node:path";
66import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
7+import { testing as cliBackendsTesting } from "../../agents/cli-backends.js";
78import type { SessionEntry } from "../../config/sessions.js";
89import {
910clearMemoryPluginState,
@@ -226,6 +227,7 @@ describe("runMemoryFlushIfNeeded", () => {
226227227228afterEach(async () => {
228229setAgentRunnerMemoryTestDeps();
230+cliBackendsTesting.resetDepsForTest();
229231clearMemoryPluginState();
230232await fs.rm(rootDir, { recursive: true, force: true });
231233});
@@ -912,6 +914,46 @@ describe("runMemoryFlushIfNeeded", () => {
912914expect(runEmbeddedAgentMock).not.toHaveBeenCalled();
913915});
914916917+it("skips memory flush for compatible CLI session runtime pins", async () => {
918+cliBackendsTesting.setDepsForTest({
919+resolveRuntimeCliBackends: () => [
920+{
921+id: "claude-cli",
922+modelProvider: "anthropic",
923+pluginId: "anthropic",
924+config: { command: "claude" },
925+},
926+],
927+});
928+const sessionEntry: SessionEntry = {
929+sessionId: "session",
930+updatedAt: Date.now(),
931+totalTokens: 80_000,
932+compactionCount: 1,
933+agentRuntimeOverride: "claude-cli",
934+};
935+936+const entry = await runMemoryFlushIfNeeded({
937+cfg: { agents: { defaults: { compaction: { memoryFlush: {} } } } },
938+followupRun: createTestFollowupRun({
939+provider: "anthropic",
940+model: "claude-opus-4-6",
941+}),
942+sessionCtx: { Provider: "whatsapp" } as unknown as TemplateContext,
943+defaultModel: "anthropic/claude-opus-4-6",
944+agentCfgContextTokens: 100_000,
945+resolvedVerboseLevel: "off",
946+ sessionEntry,
947+sessionStore: { main: sessionEntry },
948+sessionKey: "main",
949+isHeartbeat: false,
950+replyOperation: createReplyOperation(),
951+});
952+953+expect(entry).toBe(sessionEntry);
954+expect(runEmbeddedAgentMock).not.toHaveBeenCalled();
955+});
956+915957it("uses runtime policy session key when checking memory-flush sandbox writability", async () => {
916958const sessionEntry: SessionEntry = {
917959sessionId: "session",
@@ -1619,6 +1661,61 @@ describe("runMemoryFlushIfNeeded", () => {
16191661expect(compactEmbeddedAgentSessionMock).not.toHaveBeenCalled();
16201662});
162116631664+it("skips preflight compaction for compatible CLI session runtime pins", async () => {
1665+cliBackendsTesting.setDepsForTest({
1666+resolveRuntimeCliBackends: () => [
1667+{
1668+id: "claude-cli",
1669+modelProvider: "anthropic",
1670+pluginId: "anthropic",
1671+config: { command: "claude" },
1672+},
1673+],
1674+});
1675+registerMemoryFlushPlanResolverForTest(() => ({
1676+softThresholdTokens: 4_000,
1677+forceFlushTranscriptBytes: 1_000_000_000,
1678+reserveTokensFloor: 0,
1679+prompt: "Pre-compaction memory flush.\nNO_REPLY",
1680+systemPrompt: "Write memory to memory/YYYY-MM-DD.md.",
1681+relativePath: "memory/2023-11-14.md",
1682+}));
1683+const sessionEntry: SessionEntry = {
1684+sessionId: "session",
1685+updatedAt: Date.now(),
1686+totalTokens: 347_000,
1687+totalTokensFresh: true,
1688+agentRuntimeOverride: "claude-cli",
1689+};
1690+1691+const entry = await runPreflightCompactionIfNeeded({
1692+cfg: {
1693+models: {
1694+providers: {
1695+anthropic: { models: [{ id: "claude-opus-4-6", contextWindow: 350_000 }] },
1696+},
1697+},
1698+agents: { defaults: { compaction: { memoryFlush: {} } } },
1699+} as never,
1700+followupRun: createTestFollowupRun({
1701+provider: "anthropic",
1702+model: "claude-opus-4-6",
1703+sessionId: "session",
1704+sessionKey: "main",
1705+}),
1706+defaultModel: "anthropic/claude-opus-4-6",
1707+ sessionEntry,
1708+sessionStore: { main: sessionEntry },
1709+sessionKey: "main",
1710+storePath: path.join(rootDir, "sessions.json"),
1711+isHeartbeat: false,
1712+replyOperation: createReplyOperation(),
1713+});
1714+1715+expect(entry).toBe(sessionEntry);
1716+expect(compactEmbeddedAgentSessionMock).not.toHaveBeenCalled();
1717+});
1718+16221719it("keeps the OpenAI API context window for persisted OpenClaw runtime overrides", async () => {
16231720registerMemoryFlushPlanResolverForTest(() => ({
16241721softThresholdTokens: 4_000,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。