Files
adolf/extensions/memory-core/index.test.ts
alvis bedb527145
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Vendor OpenClaw source as Adolf fork baseline
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11),
free to diverge. Tree copied sans upstream .git; upstream remote added for
future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19.
Preserves docs/ARCHITECTURE.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
2026-07-05 09:36:54 +00:00

263 lines
8.1 KiB
TypeScript

// Memory Core tests cover index plugin behavior.
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
import type { OpenClawPluginCommandDefinition } from "openclaw/plugin-sdk/core";
import type { MemoryPluginRuntime } from "openclaw/plugin-sdk/memory-core-host-runtime-core";
import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api";
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
buildMemoryFlushPlan,
DEFAULT_MEMORY_FLUSH_FORCE_TRANSCRIPT_BYTES,
DEFAULT_MEMORY_FLUSH_PROMPT,
DEFAULT_MEMORY_FLUSH_SOFT_TOKENS,
} from "./src/flush-plan.js";
import { buildPromptSection } from "./src/prompt-section.js";
const closeMemorySearchManagerMock = vi.hoisted(() => vi.fn(async () => {}));
vi.mock("./src/runtime-provider.js", () => ({
memoryRuntime: {
closeAllMemorySearchManagers: vi.fn(async () => {}),
closeMemorySearchManager: closeMemorySearchManagerMock,
getMemorySearchManager: vi.fn(async () => null),
},
}));
import plugin from "./index.js";
function registerMemoryCoreRuntime(): MemoryPluginRuntime {
let runtime: MemoryPluginRuntime | undefined;
plugin.register(
createTestPluginApi({
registerMemoryCapability(capability) {
runtime = capability.runtime;
},
}),
);
if (!runtime) {
throw new Error("expected memory-core to register a memory runtime");
}
return runtime;
}
describe("buildPromptSection", () => {
it("returns empty when no memory tools are available", () => {
expect(buildPromptSection({ availableTools: new Set() })).toStrictEqual([]);
});
it("describes the two-step flow when both memory tools are available", () => {
const result = buildPromptSection({
availableTools: new Set(["memory_search", "memory_get"]),
});
expect(result[0]).toBe("## Memory Recall");
expect(result[1]).toContain("run memory_search");
expect(result[1]).toContain("then use memory_get");
expect(result[1]).toContain("indexed session transcripts");
expect(result).toContain(
"Citations: include Source: <path#line> when it helps the user verify memory snippets.",
);
expect(result.at(-1)).toBe("");
});
it("limits the guidance to memory_search when only search is available", () => {
const result = buildPromptSection({ availableTools: new Set(["memory_search"]) });
expect(result[0]).toBe("## Memory Recall");
expect(result[1]).toContain("run memory_search");
expect(result[1]).toContain("indexed session transcripts");
expect(result[1]).not.toContain("then use memory_get");
});
it("limits the guidance to memory_get when only get is available", () => {
const result = buildPromptSection({ availableTools: new Set(["memory_get"]) });
expect(result[0]).toBe("## Memory Recall");
expect(result[1]).toContain("run memory_get");
expect(result[1]).not.toContain("run memory_search");
});
it("includes citations-off instruction when citationsMode is off", () => {
const result = buildPromptSection({
availableTools: new Set(["memory_search"]),
citationsMode: "off",
});
expect(result).toContain(
"Citations are disabled: do not mention file paths or line numbers in replies unless the user explicitly asks.",
);
});
});
describe("memory-core plugin runtime registration", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("registers the dreaming runtime slash command", () => {
let command: OpenClawPluginCommandDefinition | undefined;
plugin.register(
createTestPluginApi({
registerCommand(definition) {
command = definition;
},
}),
);
expect(command?.name).toBe("dreaming");
expect(command?.acceptsArgs).toBe(true);
expect(command?.exposeSenderIsOwner).toBe(true);
expect(command?.description).toContain("Enable or disable");
});
it("wires scoped memory search cleanup through the lazy runtime", async () => {
const runtime = registerMemoryCoreRuntime();
const cfg = {} as OpenClawConfig;
await runtime.closeMemorySearchManager?.({ cfg, agentId: "main" });
expect(closeMemorySearchManagerMock).toHaveBeenCalledWith({ cfg, agentId: "main" });
});
});
describe("buildMemoryFlushPlan", () => {
const cfg = {
agents: {
defaults: {
userTimezone: "America/New_York",
timeFormat: "12",
},
},
} as OpenClawConfig;
it("replaces YYYY-MM-DD using user timezone and appends current time", () => {
const plan = buildMemoryFlushPlan({
cfg: {
...cfg,
agents: {
...cfg.agents,
defaults: {
...cfg.agents?.defaults,
compaction: {
memoryFlush: {
prompt: "Store durable notes in memory/YYYY-MM-DD.md",
},
},
},
},
},
nowMs: Date.UTC(2026, 1, 16, 15, 0, 0),
});
expect(plan?.prompt).toContain("memory/2026-02-16.md");
expect(plan?.prompt).toContain(
"Current time: Monday, February 16th, 2026 - 10:00 AM (America/New_York)",
);
expect(plan?.prompt).toContain("Reference UTC: 2026-02-16 15:00 UTC");
expect(plan?.relativePath).toBe("memory/2026-02-16.md");
});
it("does not append a duplicate current time line", () => {
const plan = buildMemoryFlushPlan({
cfg: {
...cfg,
agents: {
...cfg.agents,
defaults: {
...cfg.agents?.defaults,
compaction: {
memoryFlush: {
prompt: "Store notes.\nCurrent time: already present",
},
},
},
},
},
nowMs: Date.UTC(2026, 1, 16, 15, 0, 0),
});
expect(plan?.prompt).toContain("Current time: already present");
expect((plan?.prompt.match(/Current time:/g) ?? []).length).toBe(1);
});
it("defaults to safe prompts and gating values", () => {
const plan = buildMemoryFlushPlan();
expect(plan?.softThresholdTokens).toBe(DEFAULT_MEMORY_FLUSH_SOFT_TOKENS);
expect(plan?.forceFlushTranscriptBytes).toBe(DEFAULT_MEMORY_FLUSH_FORCE_TRANSCRIPT_BYTES);
expect(plan?.prompt).toContain("memory/");
expect(plan?.prompt).toContain("MEMORY.md");
expect(plan?.systemPrompt).toContain("MEMORY.md");
});
it("respects disable flag", () => {
expect(
buildMemoryFlushPlan({
cfg: {
agents: {
defaults: { compaction: { memoryFlush: { enabled: false } } },
},
},
}),
).toBeNull();
});
it("carries configured memory flush model override", () => {
const plan = buildMemoryFlushPlan({
cfg: {
agents: {
defaults: {
compaction: {
memoryFlush: {
model: "ollama/qwen3:8b",
},
},
},
},
},
});
expect(plan?.model).toBe("ollama/qwen3:8b");
});
it("falls back to defaults when numeric values are invalid", () => {
const plan = buildMemoryFlushPlan({
cfg: {
agents: {
defaults: {
compaction: {
reserveTokensFloor: Number.NaN,
memoryFlush: {
softThresholdTokens: -100,
},
},
},
},
},
});
expect(plan?.softThresholdTokens).toBe(DEFAULT_MEMORY_FLUSH_SOFT_TOKENS);
expect(plan?.forceFlushTranscriptBytes).toBe(DEFAULT_MEMORY_FLUSH_FORCE_TRANSCRIPT_BYTES);
expect(plan?.reserveTokensFloor).toBe(20_000);
});
it("parses forceFlushTranscriptBytes from byte-size strings", () => {
const plan = buildMemoryFlushPlan({
cfg: {
agents: {
defaults: {
compaction: {
memoryFlush: {
forceFlushTranscriptBytes: "3mb",
},
},
},
},
},
});
expect(plan?.forceFlushTranscriptBytes).toBe(3 * 1024 * 1024);
});
it("keeps overwrite guards in the default prompt", () => {
expect(DEFAULT_MEMORY_FLUSH_PROMPT).toMatch(/APPEND/i);
expect(DEFAULT_MEMORY_FLUSH_PROMPT).toContain("do not overwrite");
expect(DEFAULT_MEMORY_FLUSH_PROMPT).toContain("timestamped variant");
expect(DEFAULT_MEMORY_FLUSH_PROMPT).toContain("YYYY-MM-DD.md");
});
});