mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-27 01:01:07 +03:00
144 lines
4.2 KiB
TypeScript
144 lines
4.2 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { describe, expect, it } from "vitest";
|
|
import { runReplyAgent } from "./agent-runner.js";
|
|
import {
|
|
createBaseRun,
|
|
getRunEmbeddedPiAgentMock,
|
|
seedSessionStore,
|
|
type EmbeddedRunParams,
|
|
} from "./agent-runner.memory-flush.test-harness.js";
|
|
|
|
describe("runReplyAgent memory flush", () => {
|
|
it("skips memory flush when the sandbox workspace is read-only", async () => {
|
|
const runEmbeddedPiAgentMock = getRunEmbeddedPiAgentMock();
|
|
runEmbeddedPiAgentMock.mockReset();
|
|
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-flush-"));
|
|
const storePath = path.join(tmp, "sessions.json");
|
|
const sessionKey = "main";
|
|
const sessionEntry = {
|
|
sessionId: "session",
|
|
updatedAt: Date.now(),
|
|
totalTokens: 80_000,
|
|
compactionCount: 1,
|
|
};
|
|
|
|
await seedSessionStore({ storePath, sessionKey, entry: sessionEntry });
|
|
|
|
const calls: Array<{ prompt?: string }> = [];
|
|
runEmbeddedPiAgentMock.mockImplementation(async (params: EmbeddedRunParams) => {
|
|
calls.push({ prompt: params.prompt });
|
|
return {
|
|
payloads: [{ text: "ok" }],
|
|
meta: { agentMeta: { usage: { input: 1, output: 1 } } },
|
|
};
|
|
});
|
|
|
|
const { typing, sessionCtx, resolvedQueue, followupRun } = createBaseRun({
|
|
storePath,
|
|
sessionEntry,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
sandbox: { mode: "all", workspaceAccess: "ro" },
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
await runReplyAgent({
|
|
commandBody: "hello",
|
|
followupRun,
|
|
queueKey: "main",
|
|
resolvedQueue,
|
|
shouldSteer: false,
|
|
shouldFollowup: false,
|
|
isActive: false,
|
|
isStreaming: false,
|
|
typing,
|
|
sessionCtx,
|
|
sessionEntry,
|
|
sessionStore: { [sessionKey]: sessionEntry },
|
|
sessionKey,
|
|
storePath,
|
|
defaultModel: "anthropic/claude-opus-4-5",
|
|
agentCfgContextTokens: 100_000,
|
|
resolvedVerboseLevel: "off",
|
|
isNewSession: false,
|
|
blockStreamingEnabled: false,
|
|
resolvedBlockStreamingBreak: "message_end",
|
|
shouldInjectGroupIntro: false,
|
|
typingMode: "instant",
|
|
});
|
|
|
|
expect(calls.map((call) => call.prompt)).toEqual(["hello"]);
|
|
|
|
const stored = JSON.parse(await fs.readFile(storePath, "utf-8"));
|
|
expect(stored[sessionKey].memoryFlushAt).toBeUndefined();
|
|
});
|
|
it("skips memory flush when the sandbox workspace is none", async () => {
|
|
const runEmbeddedPiAgentMock = getRunEmbeddedPiAgentMock();
|
|
runEmbeddedPiAgentMock.mockReset();
|
|
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-flush-"));
|
|
const storePath = path.join(tmp, "sessions.json");
|
|
const sessionKey = "main";
|
|
const sessionEntry = {
|
|
sessionId: "session",
|
|
updatedAt: Date.now(),
|
|
totalTokens: 80_000,
|
|
compactionCount: 1,
|
|
};
|
|
|
|
await seedSessionStore({ storePath, sessionKey, entry: sessionEntry });
|
|
|
|
const calls: Array<{ prompt?: string }> = [];
|
|
runEmbeddedPiAgentMock.mockImplementation(async (params: EmbeddedRunParams) => {
|
|
calls.push({ prompt: params.prompt });
|
|
return {
|
|
payloads: [{ text: "ok" }],
|
|
meta: { agentMeta: { usage: { input: 1, output: 1 } } },
|
|
};
|
|
});
|
|
|
|
const { typing, sessionCtx, resolvedQueue, followupRun } = createBaseRun({
|
|
storePath,
|
|
sessionEntry,
|
|
config: {
|
|
agents: {
|
|
defaults: {
|
|
sandbox: { mode: "all", workspaceAccess: "none" },
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
await runReplyAgent({
|
|
commandBody: "hello",
|
|
followupRun,
|
|
queueKey: "main",
|
|
resolvedQueue,
|
|
shouldSteer: false,
|
|
shouldFollowup: false,
|
|
isActive: false,
|
|
isStreaming: false,
|
|
typing,
|
|
sessionCtx,
|
|
sessionEntry,
|
|
sessionStore: { [sessionKey]: sessionEntry },
|
|
sessionKey,
|
|
storePath,
|
|
defaultModel: "anthropic/claude-opus-4-5",
|
|
agentCfgContextTokens: 100_000,
|
|
resolvedVerboseLevel: "off",
|
|
isNewSession: false,
|
|
blockStreamingEnabled: false,
|
|
resolvedBlockStreamingBreak: "message_end",
|
|
shouldInjectGroupIntro: false,
|
|
typingMode: "instant",
|
|
});
|
|
|
|
expect(calls.map((call) => call.prompt)).toEqual(["hello"]);
|
|
});
|
|
});
|