forked from OtterMind/youclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory-enhanced.test.ts
More file actions
193 lines (157 loc) · 7.56 KB
/
Copy pathmemory-enhanced.test.ts
File metadata and controls
193 lines (157 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { describe, test, expect, beforeEach, afterEach } from 'bun:test'
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import './setup-light.ts'
import { getPaths } from '../src/config/index.ts'
import { MemoryManager } from '../src/memory/manager.ts'
const memoryManager = new MemoryManager()
const createdAgentIds = new Set<string>()
function createAgentId(prefix: string) {
const agentId = `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
createdAgentIds.add(agentId)
return agentId
}
function getAgentMemoryDir(agentId: string) {
return resolve(getPaths().agents, agentId, 'memory')
}
function getMemoryFile(agentId: string) {
return resolve(getAgentMemoryDir(agentId), 'MEMORY.md')
}
function getLogsDir(agentId: string) {
return resolve(getAgentMemoryDir(agentId), 'logs')
}
describe('MemoryManager enhanced features', () => {
beforeEach(() => {
for (const agentId of createdAgentIds) {
rmSync(resolve(getPaths().agents, agentId), { recursive: true, force: true })
}
createdAgentIds.clear()
})
afterEach(() => {
for (const agentId of createdAgentIds) {
rmSync(resolve(getPaths().agents, agentId), { recursive: true, force: true })
}
createdAgentIds.clear()
})
// === recentDays parameter tests ===
test('getMemoryContext supports recentDays parameter', () => {
const agentId = createAgentId('mem-days')
mkdirSync(getLogsDir(agentId), { recursive: true })
writeFileSync(getMemoryFile(agentId), 'long-term memory')
writeFileSync(resolve(getLogsDir(agentId), '2026-03-07.md'), '# 2026-03-07\nday7')
writeFileSync(resolve(getLogsDir(agentId), '2026-03-08.md'), '# 2026-03-08\nday8')
writeFileSync(resolve(getLogsDir(agentId), '2026-03-09.md'), '# 2026-03-09\nday9')
writeFileSync(resolve(getLogsDir(agentId), '2026-03-10.md'), '# 2026-03-10\nday10')
writeFileSync(resolve(getLogsDir(agentId), '2026-03-11.md'), '# 2026-03-11\nday11')
// recentDays=2: only includes the most recent 2 days
const context2 = memoryManager.getMemoryContext(agentId, { recentDays: 2 })
expect(context2).toContain('day11')
expect(context2).toContain('day10')
expect(context2).not.toContain('day9')
expect(context2).not.toContain('day8')
// recentDays=5: includes all 5 days
const context5 = memoryManager.getMemoryContext(agentId, { recentDays: 5 })
expect(context5).toContain('day11')
expect(context5).toContain('day7')
// recentDays=1: only includes the most recent 1 day
const context1 = memoryManager.getMemoryContext(agentId, { recentDays: 1 })
expect(context1).toContain('day11')
expect(context1).not.toContain('day10')
})
test('default recentDays=3', () => {
const agentId = createAgentId('mem-default-days')
mkdirSync(getLogsDir(agentId), { recursive: true })
writeFileSync(getMemoryFile(agentId), '')
writeFileSync(resolve(getLogsDir(agentId), '2026-03-08.md'), 'day8')
writeFileSync(resolve(getLogsDir(agentId), '2026-03-09.md'), 'day9')
writeFileSync(resolve(getLogsDir(agentId), '2026-03-10.md'), 'day10')
writeFileSync(resolve(getLogsDir(agentId), '2026-03-11.md'), 'day11')
const context = memoryManager.getMemoryContext(agentId)
expect(context).toContain('day11')
expect(context).toContain('day10')
expect(context).toContain('day9')
expect(context).not.toContain('day8')
})
// === maxContextChars parameter tests ===
test('getMemoryContext supports maxContextChars truncation', () => {
const agentId = createAgentId('mem-chars')
mkdirSync(getLogsDir(agentId), { recursive: true })
writeFileSync(getMemoryFile(agentId), 'M'.repeat(100))
// 200 characters per day
writeFileSync(resolve(getLogsDir(agentId), '2026-03-10.md'), 'A'.repeat(200))
writeFileSync(resolve(getLogsDir(agentId), '2026-03-11.md'), 'B'.repeat(200))
// maxContextChars=250: long-term memory 100 + first day log 200 = 300 > 250
// so the first day's log will be truncated
const context = memoryManager.getMemoryContext(agentId, { maxContextChars: 250 })
expect(context).toContain('B') // most recent day (11th)
expect(context).toContain('logs truncated')
})
test('no truncation when maxContextChars is large enough', () => {
const agentId = createAgentId('mem-no-truncate')
mkdirSync(getLogsDir(agentId), { recursive: true })
writeFileSync(getMemoryFile(agentId), 'short memory')
writeFileSync(resolve(getLogsDir(agentId), '2026-03-11.md'), 'short log')
const context = memoryManager.getMemoryContext(agentId, { maxContextChars: 100000 })
expect(context).toContain('short memory')
expect(context).toContain('short log')
expect(context).not.toContain('truncated')
})
// === conversation archive tests ===
test('archiveConversation creates archive file', () => {
const agentId = createAgentId('mem-archive')
memoryManager.archiveConversation(
agentId,
'web:chat-1',
'session-abc',
'User: hello\nAssistant: hi',
)
const conversations = memoryManager.getArchivedConversations(agentId)
expect(conversations.length).toBe(1)
expect(conversations[0]!.sessionId).toBe('session-abc')
expect(conversations[0]!.size).toBeGreaterThan(0)
})
test('getArchivedConversation returns archive content', () => {
const agentId = createAgentId('mem-archive-get')
memoryManager.archiveConversation(
agentId,
'web:chat-1',
'session-xyz',
'conversation content A B C',
)
const content = memoryManager.getArchivedConversation(agentId, 'web:chat-1', 'session-xyz')
expect(content).toContain('session-xyz')
expect(content).toContain('conversation content A B C')
})
test('getArchivedConversation returns empty string when not found', () => {
const agentId = createAgentId('mem-archive-missing')
const content = memoryManager.getArchivedConversation(agentId, 'web:chat-1', 'nonexistent')
expect(content).toBe('')
})
test('getArchivedConversations filters by chatId', () => {
const agentId = createAgentId('mem-archive-filter')
memoryManager.archiveConversation(agentId, 'web:chat-1', 'session-1', 'content 1')
memoryManager.archiveConversation(agentId, 'web:chat-2', 'session-2', 'content 2')
memoryManager.archiveConversation(agentId, 'web:chat-1', 'session-3', 'content 3')
const all = memoryManager.getArchivedConversations(agentId)
expect(all.length).toBe(3)
const chat1Only = memoryManager.getArchivedConversations(agentId, 'web:chat-1')
expect(chat1Only.length).toBe(2)
expect(chat1Only.every((c) => c.sessionId === 'session-1' || c.sessionId === 'session-3')).toBe(true)
})
test('getArchivedConversations returns empty array when no archives exist', () => {
const agentId = createAgentId('mem-archive-empty')
const conversations = memoryManager.getArchivedConversations(agentId)
expect(conversations).toEqual([])
})
test('multiple archives of different sessions for the same chatId', () => {
const agentId = createAgentId('mem-archive-multi')
memoryManager.archiveConversation(agentId, 'web:chat-1', 'session-a', 'A')
memoryManager.archiveConversation(agentId, 'web:chat-1', 'session-b', 'B')
const conversations = memoryManager.getArchivedConversations(agentId, 'web:chat-1')
expect(conversations.length).toBe(2)
const contentA = memoryManager.getArchivedConversation(agentId, 'web:chat-1', 'session-a')
const contentB = memoryManager.getArchivedConversation(agentId, 'web:chat-1', 'session-b')
expect(contentA).toContain('A')
expect(contentB).toContain('B')
})
})