|
| 1 | +import { beforeEach, describe, expect, it, jest } from "bun:test" |
| 2 | +import type { AgentMessage } from "@mariozechner/pi-agent-core" |
| 3 | +import type { BmClient } from "../bm-client.ts" |
| 4 | +import type { BasicMemoryConfig } from "../config.ts" |
| 5 | +import { BasicMemoryContextEngine } from "./basic-memory-context-engine.ts" |
| 6 | + |
| 7 | +function makeConfig( |
| 8 | + overrides?: Partial<BasicMemoryConfig>, |
| 9 | +): BasicMemoryConfig { |
| 10 | + return { |
| 11 | + project: "test-project", |
| 12 | + bmPath: "bm", |
| 13 | + memoryDir: "memory/", |
| 14 | + memoryFile: "MEMORY.md", |
| 15 | + projectPath: "/tmp/test-project", |
| 16 | + autoCapture: true, |
| 17 | + captureMinChars: 10, |
| 18 | + autoRecall: true, |
| 19 | + recallPrompt: |
| 20 | + "Check for active tasks and recent activity. Summarize anything relevant to the current session.", |
| 21 | + debug: false, |
| 22 | + ...overrides, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function makeMessages(messages: Array<Record<string, unknown>>): AgentMessage[] { |
| 27 | + return messages as AgentMessage[] |
| 28 | +} |
| 29 | + |
| 30 | +describe("BasicMemoryContextEngine", () => { |
| 31 | + let mockClient: { |
| 32 | + search: jest.Mock |
| 33 | + recentActivity: jest.Mock |
| 34 | + indexConversation: jest.Mock |
| 35 | + } |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + mockClient = { |
| 39 | + search: jest.fn().mockResolvedValue([ |
| 40 | + { |
| 41 | + title: "Fix auth rollout", |
| 42 | + permalink: "fix-auth-rollout", |
| 43 | + content: "Continue staging verification for auth rollout.", |
| 44 | + file_path: "memory/tasks/fix-auth-rollout.md", |
| 45 | + }, |
| 46 | + ]), |
| 47 | + recentActivity: jest.fn().mockResolvedValue([ |
| 48 | + { |
| 49 | + title: "API review", |
| 50 | + permalink: "api-review", |
| 51 | + file_path: "memory/api-review.md", |
| 52 | + created_at: "2026-03-09T12:00:00Z", |
| 53 | + }, |
| 54 | + ]), |
| 55 | + indexConversation: jest.fn().mockResolvedValue(undefined), |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + it("bootstraps recall state from active tasks and recent activity", async () => { |
| 60 | + const engine = new BasicMemoryContextEngine( |
| 61 | + mockClient as unknown as BmClient, |
| 62 | + makeConfig(), |
| 63 | + ) |
| 64 | + |
| 65 | + await expect( |
| 66 | + engine.bootstrap({ |
| 67 | + sessionId: "session-1", |
| 68 | + sessionFile: "/tmp/session-1.jsonl", |
| 69 | + }), |
| 70 | + ).resolves.toEqual({ bootstrapped: true }) |
| 71 | + expect(mockClient.search).toHaveBeenCalledWith(undefined, 5, undefined, { |
| 72 | + note_types: ["Task"], |
| 73 | + status: "active", |
| 74 | + }) |
| 75 | + expect(mockClient.recentActivity).toHaveBeenCalledWith("1d") |
| 76 | + }) |
| 77 | + |
| 78 | + it("skips bootstrap when recall is disabled", async () => { |
| 79 | + const engine = new BasicMemoryContextEngine( |
| 80 | + mockClient as unknown as BmClient, |
| 81 | + makeConfig({ autoRecall: false }), |
| 82 | + ) |
| 83 | + |
| 84 | + await expect( |
| 85 | + engine.bootstrap({ |
| 86 | + sessionId: "session-2", |
| 87 | + sessionFile: "/tmp/session-2.jsonl", |
| 88 | + }), |
| 89 | + ).resolves.toEqual({ |
| 90 | + bootstrapped: false, |
| 91 | + reason: "autoRecall disabled", |
| 92 | + }) |
| 93 | + |
| 94 | + const result = await engine.assemble({ |
| 95 | + sessionId: "session-2", |
| 96 | + messages: makeMessages([{ role: "user", content: "hello" }]), |
| 97 | + }) |
| 98 | + |
| 99 | + expect(result).toEqual({ |
| 100 | + messages: makeMessages([{ role: "user", content: "hello" }]), |
| 101 | + estimatedTokens: 0, |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + it("returns a no-op bootstrap result when there is no recall context", async () => { |
| 106 | + mockClient.search.mockResolvedValue([]) |
| 107 | + mockClient.recentActivity.mockResolvedValue([]) |
| 108 | + |
| 109 | + const engine = new BasicMemoryContextEngine( |
| 110 | + mockClient as unknown as BmClient, |
| 111 | + makeConfig(), |
| 112 | + ) |
| 113 | + |
| 114 | + await expect( |
| 115 | + engine.bootstrap({ |
| 116 | + sessionId: "session-3", |
| 117 | + sessionFile: "/tmp/session-3.jsonl", |
| 118 | + }), |
| 119 | + ).resolves.toEqual({ |
| 120 | + bootstrapped: false, |
| 121 | + reason: "no recall context found", |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + it("captures only the current turn after prePromptMessageCount", async () => { |
| 126 | + const engine = new BasicMemoryContextEngine( |
| 127 | + mockClient as unknown as BmClient, |
| 128 | + makeConfig(), |
| 129 | + ) |
| 130 | + |
| 131 | + await engine.afterTurn({ |
| 132 | + sessionId: "session-4", |
| 133 | + sessionFile: "/tmp/session-4.jsonl", |
| 134 | + prePromptMessageCount: 2, |
| 135 | + messages: makeMessages([ |
| 136 | + { role: "user", content: "Old question" }, |
| 137 | + { role: "assistant", content: "Old answer" }, |
| 138 | + { role: "user", content: "Current question with enough detail" }, |
| 139 | + { role: "assistant", content: "Current answer with enough detail" }, |
| 140 | + ]), |
| 141 | + }) |
| 142 | + |
| 143 | + expect(mockClient.indexConversation).toHaveBeenCalledWith( |
| 144 | + "Current question with enough detail", |
| 145 | + "Current answer with enough detail", |
| 146 | + ) |
| 147 | + }) |
| 148 | + |
| 149 | + it("respects captureMinChars for afterTurn capture", async () => { |
| 150 | + const engine = new BasicMemoryContextEngine( |
| 151 | + mockClient as unknown as BmClient, |
| 152 | + makeConfig({ captureMinChars: 50 }), |
| 153 | + ) |
| 154 | + |
| 155 | + await engine.afterTurn({ |
| 156 | + sessionId: "session-5", |
| 157 | + sessionFile: "/tmp/session-5.jsonl", |
| 158 | + prePromptMessageCount: 0, |
| 159 | + messages: makeMessages([ |
| 160 | + { role: "user", content: "short" }, |
| 161 | + { role: "assistant", content: "tiny" }, |
| 162 | + ]), |
| 163 | + }) |
| 164 | + |
| 165 | + expect(mockClient.indexConversation).not.toHaveBeenCalled() |
| 166 | + }) |
| 167 | + |
| 168 | + it("swallows capture failures in afterTurn", async () => { |
| 169 | + mockClient.indexConversation.mockRejectedValue(new Error("BM down")) |
| 170 | + const engine = new BasicMemoryContextEngine( |
| 171 | + mockClient as unknown as BmClient, |
| 172 | + makeConfig(), |
| 173 | + ) |
| 174 | + |
| 175 | + await expect( |
| 176 | + engine.afterTurn({ |
| 177 | + sessionId: "session-6", |
| 178 | + sessionFile: "/tmp/session-6.jsonl", |
| 179 | + prePromptMessageCount: 0, |
| 180 | + messages: makeMessages([ |
| 181 | + { role: "user", content: "Current question with enough detail" }, |
| 182 | + { role: "assistant", content: "Current answer with enough detail" }, |
| 183 | + ]), |
| 184 | + }), |
| 185 | + ).resolves.toBeUndefined() |
| 186 | + }) |
| 187 | +}) |
0 commit comments