|
| 1 | +// Real CLI processes must keep unrelated plugin runtimes cold on read-only command paths. |
| 2 | +import { execFile } from "node:child_process"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import { createServer, type Server } from "node:http"; |
| 5 | +import os from "node:os"; |
| 6 | +import path from "node:path"; |
| 7 | +import { promisify } from "node:util"; |
| 8 | +import { afterAll, beforeAll, expect, it } from "vitest"; |
| 9 | +import { |
| 10 | + createColdPluginFixture, |
| 11 | + isColdPluginRuntimeLoaded, |
| 12 | +} from "../plugins/test-helpers/cold-plugin-fixtures.js"; |
| 13 | + |
| 14 | +const execFileAsync = promisify(execFile); |
| 15 | +const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-cold-commands-")); |
| 16 | +let clawHubServer: Server; |
| 17 | +let clawHubUrl: string; |
| 18 | + |
| 19 | +beforeAll(async () => { |
| 20 | + clawHubServer = createServer((_request, response) => { |
| 21 | + response.writeHead(200, { "content-type": "application/json" }); |
| 22 | + response.end('{"results":[]}'); |
| 23 | + }); |
| 24 | + await new Promise<void>((resolve) => { |
| 25 | + clawHubServer.listen(0, "127.0.0.1", resolve); |
| 26 | + }); |
| 27 | + const address = clawHubServer.address(); |
| 28 | + if (!address || typeof address === "string") { |
| 29 | + throw new Error("failed to bind the ClawHub fixture server"); |
| 30 | + } |
| 31 | + clawHubUrl = `http://127.0.0.1:${address.port}`; |
| 32 | +}); |
| 33 | + |
| 34 | +afterAll(async () => { |
| 35 | + await new Promise<void>((resolve, reject) => { |
| 36 | + clawHubServer.close((error) => (error ? reject(error) : resolve())); |
| 37 | + }); |
| 38 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 39 | +}); |
| 40 | + |
| 41 | +const cases = [ |
| 42 | + { label: "hooks", args: ["hooks", "--json"], needsMemoryOwner: false }, |
| 43 | + { |
| 44 | + label: "skills-search", |
| 45 | + args: ["skills", "search", "fixture", "--json"], |
| 46 | + needsMemoryOwner: false, |
| 47 | + }, |
| 48 | + { |
| 49 | + label: "skills-info", |
| 50 | + args: ["skills", "info", "fixture-skill", "--json"], |
| 51 | + needsMemoryOwner: false, |
| 52 | + }, |
| 53 | + { |
| 54 | + label: "memory-status", |
| 55 | + args: ["memory", "status", "--agent", "main", "--json"], |
| 56 | + needsMemoryOwner: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + label: "memory-search", |
| 60 | + args: ["memory", "search", "no-hit", "--agent", "main", "--json"], |
| 61 | + needsMemoryOwner: true, |
| 62 | + }, |
| 63 | +] as const; |
| 64 | + |
| 65 | +it.each(cases)( |
| 66 | + "keeps an unrelated plugin runtime cold for $label", |
| 67 | + async ({ args, label, needsMemoryOwner }) => { |
| 68 | + const root = path.join(tempRoot, label); |
| 69 | + const stateDir = path.join(root, "state"); |
| 70 | + const workspaceDir = path.join(root, "workspace"); |
| 71 | + const pluginDir = path.join(root, "cold-plugin"); |
| 72 | + const memoryOwnerDir = path.join(root, "memory-owner"); |
| 73 | + fs.mkdirSync(path.join(workspaceDir, "skills", "fixture-skill"), { recursive: true }); |
| 74 | + fs.mkdirSync(pluginDir, { recursive: true }); |
| 75 | + fs.writeFileSync( |
| 76 | + path.join(workspaceDir, "skills", "fixture-skill", "SKILL.md"), |
| 77 | + "---\nname: fixture-skill\ndescription: Cold path fixture\n---\n", |
| 78 | + ); |
| 79 | + const fixture = createColdPluginFixture({ |
| 80 | + rootDir: pluginDir, |
| 81 | + pluginId: `cold-${label}`, |
| 82 | + runtimeMessage: `${label} imported an unrelated plugin runtime`, |
| 83 | + manifest: { providers: [], channels: [], providerAuthChoices: [], channelConfigs: {} }, |
| 84 | + }); |
| 85 | + if (needsMemoryOwner) { |
| 86 | + // Keep this fixture about the ownership boundary: the selected CLI owner must |
| 87 | + // execute while the unrelated sentinel stays cold. memory-core behavior has |
| 88 | + // its own focused CLI suite, including the stale-search result contract. |
| 89 | + fs.mkdirSync(memoryOwnerDir, { recursive: true }); |
| 90 | + fs.writeFileSync( |
| 91 | + path.join(memoryOwnerDir, "package.json"), |
| 92 | + JSON.stringify({ |
| 93 | + name: "@example/openclaw-memory-owner", |
| 94 | + version: "1.0.0", |
| 95 | + type: "commonjs", |
| 96 | + openclaw: { extensions: ["./index.cjs"] }, |
| 97 | + }), |
| 98 | + ); |
| 99 | + fs.writeFileSync( |
| 100 | + path.join(memoryOwnerDir, "openclaw.plugin.json"), |
| 101 | + JSON.stringify({ |
| 102 | + id: "memory-owner", |
| 103 | + name: "Memory Owner", |
| 104 | + configSchema: { type: "object", additionalProperties: false, properties: {} }, |
| 105 | + commandAliases: [{ name: "fixture-memory", kind: "runtime-slash", cliCommand: "memory" }], |
| 106 | + }), |
| 107 | + ); |
| 108 | + fs.writeFileSync( |
| 109 | + path.join(memoryOwnerDir, "index.cjs"), |
| 110 | + [ |
| 111 | + 'const fs = require("node:fs");', |
| 112 | + "module.exports = {", |
| 113 | + ' id: "memory-owner",', |
| 114 | + " register(api) {", |
| 115 | + ' fs.writeFileSync(process.env.MEMORY_OWNER_MARKER, "loaded", "utf8");', |
| 116 | + " api.registerCli(({ program }) => {", |
| 117 | + ' const memory = program.command("memory");', |
| 118 | + ' memory.command("status").option("--agent <id>").option("--json").action(() => console.log("[]"));', |
| 119 | + ' memory.command("search").argument("<query>").option("--agent <id>").option("--json").action(() => console.log("{\\\"results\\\":[]}"));', |
| 120 | + ' }, { descriptors: [{ name: "memory", description: "Memory fixture", hasSubcommands: true }] });', |
| 121 | + " },", |
| 122 | + "};", |
| 123 | + "", |
| 124 | + ].join("\n"), |
| 125 | + ); |
| 126 | + } |
| 127 | + fs.mkdirSync(stateDir, { recursive: true }); |
| 128 | + const configPath = path.join(stateDir, "openclaw.json"); |
| 129 | + fs.writeFileSync( |
| 130 | + configPath, |
| 131 | + JSON.stringify({ |
| 132 | + agents: { defaults: { workspace: workspaceDir } }, |
| 133 | + plugins: { |
| 134 | + load: { paths: [pluginDir, ...(needsMemoryOwner ? [memoryOwnerDir] : [])] }, |
| 135 | + entries: { |
| 136 | + [fixture.pluginId]: { enabled: true }, |
| 137 | + ...(needsMemoryOwner ? { "memory-owner": { enabled: true } } : {}), |
| 138 | + }, |
| 139 | + }, |
| 140 | + }), |
| 141 | + ); |
| 142 | + |
| 143 | + const result = await execFileAsync( |
| 144 | + process.execPath, |
| 145 | + ["--import", "tsx", "src/entry.ts", ...args], |
| 146 | + { |
| 147 | + cwd: path.resolve("."), |
| 148 | + env: { |
| 149 | + ...process.env, |
| 150 | + NODE_ENV: undefined, |
| 151 | + VITEST: undefined, |
| 152 | + OPENCLAW_CLAWHUB_URL: clawHubUrl, |
| 153 | + OPENCLAW_CONFIG_PATH: configPath, |
| 154 | + MEMORY_OWNER_MARKER: path.join(root, "memory-owner-loaded"), |
| 155 | + OPENCLAW_DISABLE_BUNDLED_PLUGINS: "1", |
| 156 | + OPENCLAW_DEV_SOURCE_ROOT: path.resolve("."), |
| 157 | + OPENCLAW_HOME: path.join(root, "home"), |
| 158 | + OPENCLAW_STATE_DIR: stateDir, |
| 159 | + }, |
| 160 | + maxBuffer: 4 * 1024 * 1024, |
| 161 | + timeout: 60_000, |
| 162 | + }, |
| 163 | + ); |
| 164 | + expect(`${result.stdout}\n${result.stderr}`).not.toContain( |
| 165 | + `${label} imported an unrelated plugin runtime`, |
| 166 | + ); |
| 167 | + expect(isColdPluginRuntimeLoaded(fixture)).toBe(false); |
| 168 | + if (needsMemoryOwner) { |
| 169 | + expect(fs.existsSync(path.join(root, "memory-owner-loaded"))).toBe(true); |
| 170 | + } |
| 171 | + }, |
| 172 | + 150_000, |
| 173 | +); |
0 commit comments