|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { createPluginRuntimeMock } from "openclaw/plugin-sdk/channel-test-helpers"; |
| 4 | +import { |
| 5 | + clearMemoryPluginState, |
| 6 | + registerMemoryCorpusSupplement, |
| 7 | +} from "openclaw/plugin-sdk/memory-host-core"; |
| 8 | +import type { AnyAgentTool, OpenClawPluginToolFactory } from "openclaw/plugin-sdk/plugin-entry"; |
| 9 | +import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api"; |
| 10 | +import { describe, expect, it } from "vitest"; |
| 11 | +import memoryCorePlugin from "../../memory-core/index.js"; |
| 12 | +import type { OpenClawConfig } from "../api.js"; |
| 13 | +import { resolveMemoryWikiAgentConfig } from "./config.js"; |
| 14 | +import { createWikiCorpusSupplement } from "./corpus-supplement.js"; |
| 15 | +import { renderWikiMarkdown } from "./markdown.js"; |
| 16 | +import { createMemoryWikiTestHarness } from "./test-helpers.js"; |
| 17 | + |
| 18 | +const { createVault } = createMemoryWikiTestHarness(); |
| 19 | + |
| 20 | +const appConfig = { |
| 21 | + agents: { list: [{ id: "main", default: true }, { id: "secondary" }] }, |
| 22 | +} as OpenClawConfig; |
| 23 | + |
| 24 | +async function writeBridgePage(params: { |
| 25 | + rootDir: string; |
| 26 | + slug: string; |
| 27 | + title: string; |
| 28 | + agentIds: string[]; |
| 29 | + marker: string; |
| 30 | +}): Promise<void> { |
| 31 | + await fs.writeFile( |
| 32 | + path.join(params.rootDir, "sources", `${params.slug}.md`), |
| 33 | + renderWikiMarkdown({ |
| 34 | + frontmatter: { |
| 35 | + pageType: "source", |
| 36 | + id: `source.${params.slug}`, |
| 37 | + title: params.title, |
| 38 | + sourceType: "memory-bridge", |
| 39 | + sourcePath: `/tmp/${params.slug}/MEMORY.md`, |
| 40 | + bridgeRelativePath: "MEMORY.md", |
| 41 | + bridgeWorkspaceDir: `/tmp/${params.slug}`, |
| 42 | + bridgeAgentIds: params.agentIds, |
| 43 | + }, |
| 44 | + body: `# ${params.title}\n\n${params.marker}\n`, |
| 45 | + }), |
| 46 | + "utf8", |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +function asRecord(value: unknown): Record<string, unknown> { |
| 51 | + if (!value || typeof value !== "object" || Array.isArray(value)) { |
| 52 | + throw new Error("Expected tool details object"); |
| 53 | + } |
| 54 | + return value as Record<string, unknown>; |
| 55 | +} |
| 56 | + |
| 57 | +function registerMemoryCoreToolFactories(): Map<string, OpenClawPluginToolFactory> { |
| 58 | + const factories = new Map<string, OpenClawPluginToolFactory>(); |
| 59 | + memoryCorePlugin.register( |
| 60 | + createTestPluginApi({ |
| 61 | + id: "memory-core", |
| 62 | + config: appConfig, |
| 63 | + runtime: createPluginRuntimeMock(), |
| 64 | + registerTool(tool, options) { |
| 65 | + if (typeof tool !== "function") { |
| 66 | + return; |
| 67 | + } |
| 68 | + for (const name of options?.names ?? []) { |
| 69 | + factories.set(name, tool); |
| 70 | + } |
| 71 | + }, |
| 72 | + }), |
| 73 | + ); |
| 74 | + return factories; |
| 75 | +} |
| 76 | + |
| 77 | +function createMemoryCoreTool(params: { |
| 78 | + factories: Map<string, OpenClawPluginToolFactory>; |
| 79 | + name: "memory_search" | "memory_get"; |
| 80 | + agentId: string; |
| 81 | + sandboxed: boolean; |
| 82 | +}): AnyAgentTool { |
| 83 | + const factory = params.factories.get(params.name); |
| 84 | + if (!factory) { |
| 85 | + throw new Error(`Expected memory-core to register ${params.name}`); |
| 86 | + } |
| 87 | + const tool = factory({ |
| 88 | + config: appConfig, |
| 89 | + runtimeConfig: appConfig, |
| 90 | + getRuntimeConfig: () => appConfig, |
| 91 | + agentId: params.agentId, |
| 92 | + sessionKey: `agent:${params.agentId}:child-session`, |
| 93 | + sandboxed: params.sandboxed, |
| 94 | + }); |
| 95 | + if (!tool || Array.isArray(tool)) { |
| 96 | + throw new Error(`Expected one ${params.name} tool`); |
| 97 | + } |
| 98 | + return tool; |
| 99 | +} |
| 100 | + |
| 101 | +describe("memory-wiki corpus supplement visibility", () => { |
| 102 | + it("enforces bridge ownership through direct and registered corpus fallbacks", async () => { |
| 103 | + const { rootDir, config } = await createVault({ |
| 104 | + initialize: true, |
| 105 | + config: { vault: { scope: "global" } }, |
| 106 | + }); |
| 107 | + await writeBridgePage({ |
| 108 | + rootDir, |
| 109 | + slug: "secondary-private", |
| 110 | + title: "Secondary Private", |
| 111 | + agentIds: ["secondary"], |
| 112 | + marker: "REDACTED-FOREIGN-MARKER", |
| 113 | + }); |
| 114 | + await writeBridgePage({ |
| 115 | + rootDir, |
| 116 | + slug: "main-private", |
| 117 | + title: "Main Private", |
| 118 | + agentIds: ["main"], |
| 119 | + marker: "REDACTED-OWN-MARKER", |
| 120 | + }); |
| 121 | + await writeBridgePage({ |
| 122 | + rootDir, |
| 123 | + slug: "unowned-private", |
| 124 | + title: "Unowned Private", |
| 125 | + agentIds: [], |
| 126 | + marker: "REDACTED-UNOWNED-MARKER", |
| 127 | + }); |
| 128 | + const supplement = createWikiCorpusSupplement({ |
| 129 | + getAppConfig: () => appConfig, |
| 130 | + resolveConfig: (agentId, currentAppConfig) => |
| 131 | + resolveMemoryWikiAgentConfig({ config, appConfig: currentAppConfig, agentId }), |
| 132 | + }); |
| 133 | + const caller = { |
| 134 | + agentId: "main", |
| 135 | + agentSessionKey: "agent:main:child-session", |
| 136 | + query: "REDACTED-FOREIGN-MARKER", |
| 137 | + }; |
| 138 | + |
| 139 | + expect(await supplement.search({ ...caller, sandboxed: true })).toEqual([]); |
| 140 | + expect( |
| 141 | + await supplement.get({ |
| 142 | + ...caller, |
| 143 | + sandboxed: true, |
| 144 | + lookup: "secondary-private", |
| 145 | + }), |
| 146 | + ).toBeNull(); |
| 147 | + expect( |
| 148 | + (await supplement.search({ ...caller, sandboxed: false })).map((result) => result.path), |
| 149 | + ).toEqual(["sources/secondary-private.md"]); |
| 150 | + expect( |
| 151 | + ( |
| 152 | + await supplement.get({ |
| 153 | + ...caller, |
| 154 | + sandboxed: false, |
| 155 | + lookup: "secondary-private", |
| 156 | + }) |
| 157 | + )?.content, |
| 158 | + ).toContain("REDACTED-FOREIGN-MARKER"); |
| 159 | + |
| 160 | + clearMemoryPluginState(); |
| 161 | + try { |
| 162 | + registerMemoryCorpusSupplement("memory-wiki", supplement); |
| 163 | + const factories = registerMemoryCoreToolFactories(); |
| 164 | + const sandboxedGet = createMemoryCoreTool({ |
| 165 | + factories, |
| 166 | + name: "memory_get", |
| 167 | + agentId: "main", |
| 168 | + sandboxed: true, |
| 169 | + }); |
| 170 | + const sandboxedSearch = createMemoryCoreTool({ |
| 171 | + factories, |
| 172 | + name: "memory_search", |
| 173 | + agentId: "main", |
| 174 | + sandboxed: true, |
| 175 | + }); |
| 176 | + const openGet = createMemoryCoreTool({ |
| 177 | + factories, |
| 178 | + name: "memory_get", |
| 179 | + agentId: "main", |
| 180 | + sandboxed: false, |
| 181 | + }); |
| 182 | + const openSearch = createMemoryCoreTool({ |
| 183 | + factories, |
| 184 | + name: "memory_search", |
| 185 | + agentId: "main", |
| 186 | + sandboxed: false, |
| 187 | + }); |
| 188 | + |
| 189 | + const foreignGet = await sandboxedGet.execute("memory-get-foreign", { |
| 190 | + path: "sources/secondary-private.md", |
| 191 | + corpus: "wiki", |
| 192 | + }); |
| 193 | + const unownedGet = await sandboxedGet.execute("memory-get-unowned", { |
| 194 | + path: "sources/unowned-private.md", |
| 195 | + corpus: "wiki", |
| 196 | + }); |
| 197 | + const ownGet = await sandboxedGet.execute("memory-get-own", { |
| 198 | + path: "sources/main-private.md", |
| 199 | + corpus: "wiki", |
| 200 | + }); |
| 201 | + const foreignSearch = await sandboxedSearch.execute("memory-search-foreign", { |
| 202 | + query: "REDACTED-FOREIGN-MARKER", |
| 203 | + corpus: "wiki", |
| 204 | + }); |
| 205 | + const ownSearch = await sandboxedSearch.execute("memory-search-own", { |
| 206 | + query: "REDACTED-OWN-MARKER", |
| 207 | + corpus: "wiki", |
| 208 | + }); |
| 209 | + const openForeignGet = await openGet.execute("memory-get-open-foreign", { |
| 210 | + path: "sources/secondary-private.md", |
| 211 | + corpus: "wiki", |
| 212 | + }); |
| 213 | + const openForeignSearch = await openSearch.execute("memory-search-open-foreign", { |
| 214 | + query: "REDACTED-FOREIGN-MARKER", |
| 215 | + corpus: "wiki", |
| 216 | + }); |
| 217 | + |
| 218 | + expect(asRecord(foreignGet.details)).toMatchObject({ |
| 219 | + text: "", |
| 220 | + disabled: true, |
| 221 | + error: "wiki corpus result not found", |
| 222 | + }); |
| 223 | + expect(asRecord(unownedGet.details)).toMatchObject({ |
| 224 | + text: "", |
| 225 | + disabled: true, |
| 226 | + error: "wiki corpus result not found", |
| 227 | + }); |
| 228 | + expect(asRecord(ownGet.details)).toMatchObject({ |
| 229 | + path: "sources/main-private.md", |
| 230 | + text: expect.stringContaining("REDACTED-OWN-MARKER"), |
| 231 | + }); |
| 232 | + expect(asRecord(foreignSearch.details).results).toEqual([]); |
| 233 | + expect(asRecord(ownSearch.details).results).toEqual([ |
| 234 | + expect.objectContaining({ path: "sources/main-private.md" }), |
| 235 | + ]); |
| 236 | + expect(asRecord(openForeignGet.details)).toMatchObject({ |
| 237 | + path: "sources/secondary-private.md", |
| 238 | + text: expect.stringContaining("REDACTED-FOREIGN-MARKER"), |
| 239 | + }); |
| 240 | + expect(asRecord(openForeignSearch.details).results).toEqual([ |
| 241 | + expect.objectContaining({ path: "sources/secondary-private.md" }), |
| 242 | + ]); |
| 243 | + } finally { |
| 244 | + clearMemoryPluginState(); |
| 245 | + } |
| 246 | + }, 240_000); |
| 247 | +}); |
0 commit comments