feat(memory-sdk): add memory event journal bridge

This commit is contained in:
Vincent Koc
2026-04-05 20:58:08 +01:00
parent fbbe2a1675
commit d1c7d9af80
14 changed files with 413 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ import {
type MemoryDreamingPhaseName,
type MemoryDreamingStorageConfig,
} from "openclaw/plugin-sdk/memory-core-host-status";
import { appendMemoryHostEvent } from "openclaw/plugin-sdk/memory-host-events";
import {
replaceManagedMarkdownBlock,
withTrailingNewline,
@@ -104,6 +105,16 @@ export async function writeDailyDreamingPhaseBlock(params: {
await fs.writeFile(reportPath, report, "utf-8");
}
await appendMemoryHostEvent(params.workspaceDir, {
type: "memory.dream.completed",
timestamp: new Date(nowMs).toISOString(),
phase: params.phase,
...(inlinePath ? { inlinePath } : {}),
...(reportPath ? { reportPath } : {}),
lineCount: params.bodyLines.length,
storageMode: params.storage.mode,
});
return {
...(inlinePath ? { inlinePath } : {}),
...(reportPath ? { reportPath } : {}),
@@ -125,5 +136,13 @@ export async function writeDeepDreamingReport(params: {
await fs.mkdir(path.dirname(reportPath), { recursive: true });
const body = params.bodyLines.length > 0 ? params.bodyLines.join("\n") : "- No durable changes.";
await fs.writeFile(reportPath, `# Deep Sleep\n\n${body}\n`, "utf-8");
await appendMemoryHostEvent(params.workspaceDir, {
type: "memory.dream.completed",
timestamp: new Date(nowMs).toISOString(),
phase: "deep",
reportPath,
lineCount: params.bodyLines.length,
storageMode: params.storage.mode,
});
return reportPath;
}

View File

@@ -0,0 +1,105 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { readMemoryHostEvents } from "openclaw/plugin-sdk/memory-host-events";
import { afterEach, describe, expect, it } from "vitest";
import { writeDailyDreamingPhaseBlock } from "./dreaming-markdown.js";
import {
applyShortTermPromotions,
rankShortTermPromotionCandidates,
recordShortTermRecalls,
} from "./short-term-promotion.js";
const tempDirs: string[] = [];
afterEach(async () => {
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
});
describe("memory host event journal integration", () => {
it("records recall and promotion events from short-term promotion flows", async () => {
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "memory-core-events-"));
tempDirs.push(workspaceDir);
await fs.mkdir(path.join(workspaceDir, "memory"), { recursive: true });
await fs.writeFile(
path.join(workspaceDir, "memory", "2026-04-05.md"),
"# Daily\n\nalpha\nbeta\ngamma\n",
"utf8",
);
await recordShortTermRecalls({
workspaceDir,
query: "alpha memory",
results: [
{
path: "memory/2026-04-05.md",
startLine: 3,
endLine: 4,
score: 0.92,
snippet: "alpha beta",
source: "memory",
},
],
nowMs: Date.UTC(2026, 3, 5, 12, 0, 0),
});
const candidates = await rankShortTermPromotionCandidates({
workspaceDir,
minScore: 0,
minRecallCount: 0,
minUniqueQueries: 0,
nowMs: Date.UTC(2026, 3, 5, 12, 5, 0),
});
const applied = await applyShortTermPromotions({
workspaceDir,
candidates,
minScore: 0,
minRecallCount: 0,
minUniqueQueries: 0,
nowMs: Date.UTC(2026, 3, 5, 12, 10, 0),
});
expect(applied.applied).toBe(1);
const events = await readMemoryHostEvents({ workspaceDir });
expect(events.map((event) => event.type)).toEqual([
"memory.recall.recorded",
"memory.promotion.applied",
]);
expect(events[0]).toMatchObject({
type: "memory.recall.recorded",
resultCount: 1,
query: "alpha memory",
});
expect(events[1]).toMatchObject({
type: "memory.promotion.applied",
applied: 1,
});
});
it("records dreaming completion events when phase artifacts are written", async () => {
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "memory-core-dream-events-"));
tempDirs.push(workspaceDir);
const written = await writeDailyDreamingPhaseBlock({
workspaceDir,
phase: "light",
bodyLines: ["- staged note", "- second note"],
nowMs: Date.UTC(2026, 3, 5, 13, 0, 0),
storage: { mode: "both", separateReports: true },
});
const events = await readMemoryHostEvents({ workspaceDir });
expect(written.inlinePath).toBeTruthy();
expect(written.reportPath).toBeTruthy();
expect(events).toHaveLength(1);
expect(events[0]).toMatchObject({
type: "memory.dream.completed",
phase: "light",
lineCount: 2,
storageMode: "both",
});
});
});

View File

@@ -3,6 +3,7 @@ import fs from "node:fs/promises";
import path from "node:path";
import type { MemorySearchResult } from "openclaw/plugin-sdk/memory-core-host-runtime-files";
import { formatMemoryDreamingDay } from "openclaw/plugin-sdk/memory-core-host-status";
import { appendMemoryHostEvent } from "openclaw/plugin-sdk/memory-host-events";
import {
deriveConceptTags,
MAX_CONCEPT_TAGS,
@@ -631,6 +632,18 @@ export async function recordShortTermRecalls(params: {
store.updatedAt = nowIso;
await writeStore(workspaceDir, store);
await appendMemoryHostEvent(workspaceDir, {
type: "memory.recall.recorded",
timestamp: nowIso,
query,
resultCount: relevant.length,
results: relevant.map((result) => ({
path: normalizeMemoryPath(result.path),
startLine: Math.max(1, Math.floor(result.startLine)),
endLine: Math.max(1, Math.floor(result.endLine)),
score: clampScore(result.score),
})),
});
});
}
@@ -1042,6 +1055,20 @@ export async function applyShortTermPromotions(
}
store.updatedAt = nowIso;
await writeStore(workspaceDir, store);
await appendMemoryHostEvent(workspaceDir, {
type: "memory.promotion.applied",
timestamp: nowIso,
memoryPath,
applied: rehydratedSelected.length,
candidates: rehydratedSelected.map((candidate) => ({
key: candidate.key,
path: candidate.path,
startLine: candidate.startLine,
endLine: candidate.endLine,
score: candidate.score,
recallCount: candidate.recallCount,
})),
});
return {
memoryPath,

View File

@@ -1,6 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { appendMemoryHostEvent } from "openclaw/plugin-sdk/memory-host-events";
import { afterEach, describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../api.js";
import { syncMemoryWikiBridgeSources } from "./bridge.js";
@@ -106,4 +107,58 @@ describe("syncMemoryWikiBridgeSources", () => {
pagePaths: [],
});
});
it("imports the public memory event journal when followMemoryEvents is enabled", async () => {
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "memory-wiki-bridge-events-ws-"));
const vaultDir = await fs.mkdtemp(path.join(os.tmpdir(), "memory-wiki-bridge-events-vault-"));
tempDirs.push(workspaceDir, vaultDir);
await appendMemoryHostEvent(workspaceDir, {
type: "memory.recall.recorded",
timestamp: "2026-04-05T12:00:00.000Z",
query: "bridge events",
resultCount: 1,
results: [
{
path: "memory/2026-04-05.md",
startLine: 1,
endLine: 2,
score: 0.8,
},
],
});
const config = resolveMemoryWikiConfig(
{
vaultMode: "bridge",
vault: { path: vaultDir },
bridge: {
enabled: true,
followMemoryEvents: true,
},
},
{ homedir: "/Users/tester" },
);
const appConfig: OpenClawConfig = {
plugins: {
entries: {
"memory-core": {
enabled: true,
config: {},
},
},
},
agents: {
list: [{ id: "main", default: true, workspace: workspaceDir }],
},
};
const result = await syncMemoryWikiBridgeSources({ config, appConfig });
expect(result.artifactCount).toBe(1);
expect(result.importedCount).toBe(1);
const page = await fs.readFile(path.join(vaultDir, result.pagePaths[0] ?? ""), "utf8");
expect(page).toContain("sourceType: memory-bridge-events");
expect(page).toContain('"type":"memory.recall.recorded"');
});
});

View File

@@ -1,16 +1,19 @@
import { createHash } from "node:crypto";
import fs from "node:fs/promises";
import path from "node:path";
import { resolveMemoryHostEventLogPath } from "openclaw/plugin-sdk/memory-host-events";
import {
resolveMemoryCorePluginConfig,
resolveMemoryDreamingWorkspaces,
} from "openclaw/plugin-sdk/memory-host-status";
import type { OpenClawConfig } from "../api.js";
import type { ResolvedMemoryWikiConfig } from "./config.js";
import { appendMemoryWikiLog } from "./log.js";
import { renderMarkdownFence, renderWikiMarkdown, slugifyWikiSegment } from "./markdown.js";
import { initializeMemoryWikiVault } from "./vault.js";
type BridgeArtifact = {
artifactType: "markdown" | "memory-events";
workspaceDir: string;
relativePath: string;
absolutePath: string;
@@ -65,6 +68,7 @@ async function collectWorkspaceArtifacts(
const absolutePath = path.join(workspaceDir, relPath);
if (await pathExists(absolutePath)) {
artifacts.push({
artifactType: "markdown",
workspaceDir,
relativePath: relPath,
absolutePath,
@@ -80,6 +84,7 @@ async function collectWorkspaceArtifacts(
const relativePath = path.relative(workspaceDir, absolutePath).replace(/\\/g, "/");
if (!relativePath.startsWith("memory/dreaming/")) {
artifacts.push({
artifactType: "markdown",
workspaceDir,
relativePath,
absolutePath,
@@ -94,6 +99,7 @@ async function collectWorkspaceArtifacts(
for (const absolutePath of files) {
const relativePath = path.relative(workspaceDir, absolutePath).replace(/\\/g, "/");
artifacts.push({
artifactType: "markdown",
workspaceDir,
relativePath,
absolutePath,
@@ -101,6 +107,18 @@ async function collectWorkspaceArtifacts(
}
}
if (bridgeConfig.followMemoryEvents) {
const eventLogPath = resolveMemoryHostEventLogPath(workspaceDir);
if (await pathExists(eventLogPath)) {
artifacts.push({
artifactType: "memory-events",
workspaceDir,
relativePath: path.relative(workspaceDir, eventLogPath).replace(/\\/g, "/"),
absolutePath: eventLogPath,
});
}
}
const deduped = new Map<string, BridgeArtifact>();
for (const artifact of artifacts) {
deduped.set(await resolveArtifactKey(artifact.absolutePath), artifact);
@@ -108,8 +126,14 @@ async function collectWorkspaceArtifacts(
return [...deduped.values()];
}
function resolveBridgeTitle(relativePath: string, agentIds: string[]): string {
const base = relativePath
function resolveBridgeTitle(artifact: BridgeArtifact, agentIds: string[]): string {
if (artifact.artifactType === "memory-events") {
if (agentIds.length === 0) {
return "Memory Bridge: event journal";
}
return `Memory Bridge (${agentIds.join(", ")}): event journal`;
}
const base = artifact.relativePath
.replace(/\.md$/i, "")
.replace(/^memory\//, "")
.replace(/\//g, " / ");
@@ -152,18 +176,20 @@ async function writeBridgeSourcePage(params: {
workspaceDir: params.artifact.workspaceDir,
relativePath: params.artifact.relativePath,
});
const title = resolveBridgeTitle(params.artifact.relativePath, params.agentIds);
const title = resolveBridgeTitle(params.artifact, params.agentIds);
const pageAbsPath = path.join(params.config.vault.path, pagePath);
const created = !(await pathExists(pageAbsPath));
const raw = await fs.readFile(params.artifact.absolutePath, "utf8");
const stats = await fs.stat(params.artifact.absolutePath);
const sourceUpdatedAt = stats.mtime.toISOString();
const contentLanguage = params.artifact.artifactType === "memory-events" ? "json" : "markdown";
const rendered = renderWikiMarkdown({
frontmatter: {
pageType: "source",
id: pageId,
title,
sourceType: "memory-bridge",
sourceType:
params.artifact.artifactType === "memory-events" ? "memory-bridge-events" : "memory-bridge",
sourcePath: params.artifact.absolutePath,
bridgeRelativePath: params.artifact.relativePath,
bridgeWorkspaceDir: params.artifact.workspaceDir,
@@ -177,11 +203,12 @@ async function writeBridgeSourcePage(params: {
"## Bridge Source",
`- Workspace: \`${params.artifact.workspaceDir}\``,
`- Relative path: \`${params.artifact.relativePath}\``,
`- Kind: \`${params.artifact.artifactType}\``,
`- Agents: ${params.agentIds.length > 0 ? params.agentIds.join(", ") : "unknown"}`,
`- Updated: ${sourceUpdatedAt}`,
"",
"## Content",
renderMarkdownFence(raw, "markdown"),
renderMarkdownFence(raw, contentLanguage),
"",
"## Notes",
"<!-- openclaw:human:start -->",