From a5e9461b26a6bf716b7a8703f5c9346a52e2ba2e Mon Sep 17 00:00:00 2001 From: Marc J Saint-jour <82672745+Junebugg1214@users.noreply.github.com> Date: Thu, 12 Mar 2026 18:40:58 -0400 Subject: [PATCH] feat: integrate Cortex local memory into OpenClaw --- src/agents/cortex-history.test.ts | 82 +++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/agents/cortex-history.test.ts diff --git a/src/agents/cortex-history.test.ts b/src/agents/cortex-history.test.ts new file mode 100644 index 00000000000..f98332a0206 --- /dev/null +++ b/src/agents/cortex-history.test.ts @@ -0,0 +1,82 @@ +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { + appendCortexCaptureHistory, + getLatestCortexCaptureHistoryEntry, + getLatestCortexCaptureHistoryEntrySync, + readRecentCortexCaptureHistory, +} from "./cortex-history.js"; + +describe("cortex capture history", () => { + afterEach(() => { + vi.unstubAllEnvs(); + }); + + it("appends and reads recent capture history", async () => { + const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cortex-history-")); + vi.stubEnv("OPENCLAW_STATE_DIR", stateDir); + + await appendCortexCaptureHistory({ + agentId: "main", + sessionId: "session-1", + channelId: "channel-1", + captured: true, + score: 0.7, + reason: "high-signal memory candidate", + timestamp: 1_000, + }); + + const recent = await readRecentCortexCaptureHistory({ limit: 5 }); + + expect(recent).toHaveLength(1); + expect(recent[0]).toMatchObject({ + agentId: "main", + captured: true, + reason: "high-signal memory candidate", + }); + }); + + it("returns the latest matching capture entry in async and sync modes", async () => { + const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cortex-history-sync-")); + vi.stubEnv("OPENCLAW_STATE_DIR", stateDir); + + await appendCortexCaptureHistory({ + agentId: "main", + sessionId: "session-1", + channelId: "channel-1", + captured: false, + score: 0.1, + reason: "low-signal short reply", + timestamp: 1_000, + }); + await appendCortexCaptureHistory({ + agentId: "main", + sessionId: "session-1", + channelId: "channel-1", + captured: true, + score: 0.7, + reason: "high-signal memory candidate", + syncedCodingContext: true, + syncPlatforms: ["claude-code", "cursor", "copilot"], + timestamp: 2_000, + }); + + const asyncEntry = await getLatestCortexCaptureHistoryEntry({ + agentId: "main", + sessionId: "session-1", + channelId: "channel-1", + }); + const syncEntry = getLatestCortexCaptureHistoryEntrySync({ + agentId: "main", + sessionId: "session-1", + channelId: "channel-1", + }); + + expect(asyncEntry?.timestamp).toBe(2_000); + expect(asyncEntry?.syncedCodingContext).toBe(true); + expect(syncEntry?.timestamp).toBe(2_000); + expect(syncEntry?.syncPlatforms).toEqual(["claude-code", "cursor", "copilot"]); + }); +});