feat: implement ZCLAW Agent Intelligence Evolution Phase 1-3

Phase 1: Persistent Memory + Identity Dynamic Evolution
- agent-memory.ts: MemoryManager with localStorage persistence, keyword search, deduplication, importance scoring, pruning, markdown export
- agent-identity.ts: AgentIdentityManager with per-agent SOUL/AGENTS/USER.md, change proposals with approval workflow, snapshot rollback
- memory-extractor.ts: Rule-based conversation memory extraction (Phase 1), LLM extraction prompt ready for Phase 2
- MemoryPanel.tsx: Memory browsing UI with search, type filter, delete, export (integrated as 4th tab in RightPanel)

Phase 2: Context Governance
- context-compactor.ts: Token estimation, threshold monitoring (soft/hard), memory flush before compaction, rule-based summarization
- chatStore integration: auto-compact when approaching token limits

Phase 3: Proactive Intelligence + Self-Reflection
- heartbeat-engine.ts: Periodic checks (pending tasks, memory health, idle greeting), quiet hours, proactivity levels (silent/light/standard/autonomous)
- reflection-engine.ts: Pattern analysis from memory corpus, improvement suggestions, identity change proposals, meta-memory creation

Chat Flow Integration (chatStore.ts):
- Pre-send: context compaction check -> memory search -> identity system prompt injection
- Post-complete: async memory extraction -> reflection conversation tracking -> auto-trigger reflection

Tests: 274 passing across 12 test files
- agent-memory.test.ts: 42 tests
- context-compactor.test.ts: 23 tests
- heartbeat-reflection.test.ts: 28 tests
- chatStore.test.ts: 11 tests (no regressions)

Refs: ZCLAW_AGENT_INTELLIGENCE_EVOLUTION.md updated with implementation progress
This commit is contained in:
iven
2026-03-15 22:24:57 +08:00
parent 4862e79b2b
commit 04ddf94123
13 changed files with 3949 additions and 26 deletions

View File

@@ -0,0 +1,558 @@
/**
* Tests for Agent Memory System (Phase 1)
*
* Covers: MemoryManager, AgentIdentityManager, MemoryExtractor
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import {
MemoryManager,
resetMemoryManager,
type MemoryEntry,
} from '../../desktop/src/lib/agent-memory';
import {
AgentIdentityManager,
resetAgentIdentityManager,
} from '../../desktop/src/lib/agent-identity';
import {
MemoryExtractor,
resetMemoryExtractor,
} from '../../desktop/src/lib/memory-extractor';
// === Mock localStorage ===
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] ?? null,
setItem: (key: string, value: string) => { store[key] = value; },
removeItem: (key: string) => { delete store[key]; },
clear: () => { store = {}; },
};
})();
vi.stubGlobal('localStorage', localStorageMock);
// =============================================
// MemoryManager Tests
// =============================================
describe('MemoryManager', () => {
let mgr: MemoryManager;
beforeEach(() => {
localStorageMock.clear();
resetMemoryManager();
mgr = new MemoryManager();
});
describe('save', () => {
it('saves a memory entry and assigns id/timestamps', async () => {
const entry = await mgr.save({
agentId: 'agent-1',
content: '用户喜欢简洁回答',
type: 'preference',
importance: 7,
source: 'auto',
tags: ['communication'],
});
expect(entry.id).toMatch(/^mem_/);
expect(entry.content).toBe('用户喜欢简洁回答');
expect(entry.type).toBe('preference');
expect(entry.importance).toBe(7);
expect(entry.createdAt).toBeTruthy();
expect(entry.accessCount).toBe(0);
});
it('deduplicates similar content for same agent+type', async () => {
await mgr.save({
agentId: 'agent-1',
content: '用户的公司叫 ACME',
type: 'fact',
importance: 6,
source: 'auto',
tags: ['company'],
});
// Save very similar content
const second = await mgr.save({
agentId: 'agent-1',
content: '用户的公司叫 ACME Corp',
type: 'fact',
importance: 8,
source: 'auto',
tags: ['company', 'name'],
});
// Should update existing entry, not create a new one
const all = await mgr.getAll('agent-1');
expect(all.length).toBe(1);
expect(all[0].importance).toBe(8); // Takes higher importance
expect(all[0].tags).toContain('name'); // Merged tags
});
it('does not deduplicate across different agents', async () => {
await mgr.save({
agentId: 'agent-1',
content: '用户喜欢TypeScript',
type: 'preference',
importance: 6,
source: 'auto',
tags: [],
});
await mgr.save({
agentId: 'agent-2',
content: '用户喜欢TypeScript',
type: 'preference',
importance: 6,
source: 'auto',
tags: [],
});
const all1 = await mgr.getAll('agent-1');
const all2 = await mgr.getAll('agent-2');
expect(all1.length).toBe(1);
expect(all2.length).toBe(1);
});
});
describe('search', () => {
beforeEach(async () => {
await mgr.save({ agentId: 'a1', content: '用户的项目使用 React 和 TypeScript', type: 'fact', importance: 7, source: 'auto', tags: ['tech'] });
await mgr.save({ agentId: 'a1', content: '用户喜欢简洁的代码风格', type: 'preference', importance: 6, source: 'auto', tags: ['coding'] });
await mgr.save({ agentId: 'a1', content: '飞书API需要先验证token再调用', type: 'lesson', importance: 8, source: 'auto', tags: ['feishu', 'api'] });
await mgr.save({ agentId: 'a2', content: '另一个 agent 的记忆', type: 'fact', importance: 5, source: 'auto', tags: [] });
});
it('finds relevant memories by keyword', async () => {
const results = await mgr.search('TypeScript React', { agentId: 'a1' });
expect(results.length).toBeGreaterThan(0);
expect(results[0].content).toContain('TypeScript');
});
it('filters by agentId', async () => {
const results = await mgr.search('记忆', { agentId: 'a1' });
expect(results.every(r => r.agentId === 'a1')).toBe(true);
});
it('filters by type', async () => {
const results = await mgr.search('代码', { agentId: 'a1', type: 'preference' });
expect(results.every(r => r.type === 'preference')).toBe(true);
});
it('filters by minImportance', async () => {
const results = await mgr.search('飞书 API token', { agentId: 'a1', minImportance: 7 });
expect(results.every(r => r.importance >= 7)).toBe(true);
});
it('filters by tags', async () => {
const results = await mgr.search('API', { agentId: 'a1', tags: ['feishu'] });
expect(results.length).toBeGreaterThan(0);
expect(results[0].tags).toContain('feishu');
});
it('returns empty for no match', async () => {
const results = await mgr.search('量子物理', { agentId: 'a1' });
expect(results).toHaveLength(0);
});
it('updates access metadata on search hit', async () => {
const before = await mgr.getAll('a1');
const hitEntry = before.find(e => e.content.includes('TypeScript'))!;
const beforeCount = hitEntry.accessCount;
await mgr.search('TypeScript', { agentId: 'a1' });
const after = await mgr.get(hitEntry.id);
expect(after!.accessCount).toBe(beforeCount + 1);
});
});
describe('getAll', () => {
it('returns entries for the requested agent', async () => {
await mgr.save({ agentId: 'a1', content: 'memory A', type: 'fact', importance: 5, source: 'auto', tags: [] });
await mgr.save({ agentId: 'a1', content: 'memory B', type: 'fact', importance: 5, source: 'auto', tags: [] });
await mgr.save({ agentId: 'a2', content: 'other agent', type: 'fact', importance: 5, source: 'auto', tags: [] });
const all = await mgr.getAll('a1');
expect(all).toHaveLength(2);
expect(all.every(e => e.agentId === 'a1')).toBe(true);
});
it('respects limit', async () => {
for (let i = 0; i < 5; i++) {
await mgr.save({ agentId: 'a1', content: `memory ${i}`, type: 'fact', importance: 5, source: 'auto', tags: [] });
}
const limited = await mgr.getAll('a1', { limit: 3 });
expect(limited).toHaveLength(3);
});
});
describe('forget', () => {
it('removes a specific memory', async () => {
const entry = await mgr.save({ agentId: 'a1', content: 'to forget', type: 'fact', importance: 5, source: 'auto', tags: [] });
await mgr.forget(entry.id);
expect(await mgr.get(entry.id)).toBeNull();
});
});
describe('prune', () => {
it('removes old low-importance entries', async () => {
// Create an old low-importance entry
const entry = await mgr.save({ agentId: 'a1', content: 'old unimportant', type: 'context', importance: 2, source: 'auto', tags: [] });
// Manually set lastAccessedAt to 60 days ago
const raw = JSON.parse(localStorageMock.getItem('zclaw-agent-memories')!);
const idx = raw.findIndex((e: MemoryEntry) => e.id === entry.id);
raw[idx].lastAccessedAt = new Date(Date.now() - 60 * 24 * 60 * 60 * 1000).toISOString();
localStorageMock.setItem('zclaw-agent-memories', JSON.stringify(raw));
// Reload and prune
resetMemoryManager();
const mgr2 = new MemoryManager();
const pruned = await mgr2.prune({ maxAgeDays: 30, minImportance: 3, agentId: 'a1' });
expect(pruned).toBe(1);
});
it('keeps high-importance entries even when old', async () => {
const entry = await mgr.save({ agentId: 'a1', content: 'important old', type: 'fact', importance: 9, source: 'auto', tags: [] });
const raw = JSON.parse(localStorageMock.getItem('zclaw-agent-memories')!);
const idx = raw.findIndex((e: MemoryEntry) => e.id === entry.id);
raw[idx].lastAccessedAt = new Date(Date.now() - 60 * 24 * 60 * 60 * 1000).toISOString();
localStorageMock.setItem('zclaw-agent-memories', JSON.stringify(raw));
resetMemoryManager();
const mgr2 = new MemoryManager();
const pruned = await mgr2.prune({ maxAgeDays: 30, minImportance: 3, agentId: 'a1' });
expect(pruned).toBe(0);
});
});
describe('exportToMarkdown', () => {
it('produces formatted markdown with categories', async () => {
await mgr.save({ agentId: 'a1', content: '公司叫 ACME', type: 'fact', importance: 7, source: 'auto', tags: ['company'] });
await mgr.save({ agentId: 'a1', content: '喜欢简洁回答', type: 'preference', importance: 6, source: 'auto', tags: [] });
const md = await mgr.exportToMarkdown('a1');
expect(md).toContain('Agent Memory Export');
expect(md).toContain('事实');
expect(md).toContain('偏好');
expect(md).toContain('ACME');
expect(md).toContain('简洁');
});
it('returns empty notice for agent with no memories', async () => {
const md = await mgr.exportToMarkdown('nonexistent');
expect(md).toContain('No memories recorded');
});
});
describe('stats', () => {
it('returns correct statistics', async () => {
await mgr.save({ agentId: 'a1', content: 'fact1', type: 'fact', importance: 5, source: 'auto', tags: [] });
await mgr.save({ agentId: 'a1', content: 'pref1', type: 'preference', importance: 5, source: 'auto', tags: [] });
await mgr.save({ agentId: 'a1', content: 'lesson1', type: 'lesson', importance: 5, source: 'auto', tags: [] });
const stats = await mgr.stats('a1');
expect(stats.totalEntries).toBe(3);
expect(stats.byType['fact']).toBe(1);
expect(stats.byType['preference']).toBe(1);
expect(stats.byType['lesson']).toBe(1);
});
});
describe('persistence', () => {
it('survives manager recreation (simulates app restart)', async () => {
await mgr.save({ agentId: 'a1', content: 'persistent memory', type: 'fact', importance: 8, source: 'auto', tags: [] });
// Create a new manager (simulates restart)
const mgr2 = new MemoryManager();
const all = await mgr2.getAll('a1');
expect(all.length).toBe(1);
expect(all[0].content).toBe('persistent memory');
});
});
});
// =============================================
// AgentIdentityManager Tests
// =============================================
describe('AgentIdentityManager', () => {
let idMgr: AgentIdentityManager;
beforeEach(() => {
localStorageMock.clear();
resetAgentIdentityManager();
idMgr = new AgentIdentityManager();
});
describe('getIdentity', () => {
it('returns defaults for new agent', () => {
const identity = idMgr.getIdentity('new-agent');
expect(identity.soul).toContain('ZCLAW');
expect(identity.instructions).toContain('指令');
expect(identity.userProfile).toContain('用户画像');
});
it('returns same identity on second call', () => {
const first = idMgr.getIdentity('test');
const second = idMgr.getIdentity('test');
expect(first.soul).toBe(second.soul);
});
});
describe('buildSystemPrompt', () => {
it('combines soul + instructions + memory context', () => {
const prompt = idMgr.buildSystemPrompt('test', '## 相关记忆\n- 用户喜欢中文');
expect(prompt).toContain('ZCLAW');
expect(prompt).toContain('指令');
expect(prompt).toContain('相关记忆');
});
it('excludes default user profile', () => {
const prompt = idMgr.buildSystemPrompt('test');
expect(prompt).not.toContain('尚未收集到');
});
it('includes updated user profile', () => {
idMgr.updateUserProfile('test', '## 用户偏好\n- 喜欢TypeScript');
const prompt = idMgr.buildSystemPrompt('test');
expect(prompt).toContain('TypeScript');
});
});
describe('updateUserProfile', () => {
it('updates profile and creates snapshot', () => {
idMgr.updateUserProfile('test', '新的用户画像');
const identity = idMgr.getIdentity('test');
expect(identity.userProfile).toBe('新的用户画像');
const snapshots = idMgr.getSnapshots('test');
expect(snapshots.length).toBeGreaterThan(0);
});
});
describe('appendToUserProfile', () => {
it('appends content to existing profile', () => {
idMgr.appendToUserProfile('test', '- 新发现的偏好');
const identity = idMgr.getIdentity('test');
expect(identity.userProfile).toContain('新发现的偏好');
});
});
describe('change proposals', () => {
it('creates a pending proposal', () => {
const proposal = idMgr.proposeChange('test', 'soul', '新的人格定义', '根据用户反馈调整');
expect(proposal.status).toBe('pending');
expect(proposal.suggestedContent).toBe('新的人格定义');
const pending = idMgr.getPendingProposals('test');
expect(pending).toHaveLength(1);
});
it('approves a proposal and updates identity', () => {
const proposal = idMgr.proposeChange('test', 'soul', '新的 SOUL', '改进');
idMgr.approveProposal(proposal.id);
const identity = idMgr.getIdentity('test');
expect(identity.soul).toBe('新的 SOUL');
const pending = idMgr.getPendingProposals('test');
expect(pending).toHaveLength(0);
});
it('rejects a proposal without changing identity', () => {
const oldIdentity = idMgr.getIdentity('test');
const proposal = idMgr.proposeChange('test', 'soul', '被拒绝的内容', '原因');
idMgr.rejectProposal(proposal.id);
const identity = idMgr.getIdentity('test');
expect(identity.soul).toBe(oldIdentity.soul);
});
});
describe('snapshots and rollback', () => {
it('can rollback to a previous snapshot', () => {
idMgr.updateUserProfile('test', 'version 1');
idMgr.updateUserProfile('test', 'version 2');
const snapshots = idMgr.getSnapshots('test');
expect(snapshots.length).toBeGreaterThanOrEqual(2);
// snapshots[0] = most recent (taken before 'version 2' was set, contains 'version 1')
const snapshotWithV1 = snapshots[0];
idMgr.restoreSnapshot('test', snapshotWithV1.id);
const identity = idMgr.getIdentity('test');
expect(identity.userProfile).toBe('version 1');
});
});
describe('direct edit', () => {
it('updates file and creates snapshot', () => {
idMgr.updateFile('test', 'instructions', '# 自定义指令\n\n新指令内容');
const identity = idMgr.getIdentity('test');
expect(identity.instructions).toContain('自定义指令');
});
});
describe('persistence', () => {
it('survives recreation', () => {
idMgr.updateUserProfile('test', '持久化测试');
resetAgentIdentityManager();
const mgr2 = new AgentIdentityManager();
const identity = mgr2.getIdentity('test');
expect(identity.userProfile).toBe('持久化测试');
});
});
});
// =============================================
// MemoryExtractor Tests
// =============================================
describe('MemoryExtractor', () => {
let extractor: MemoryExtractor;
beforeEach(() => {
localStorageMock.clear();
resetMemoryManager();
resetAgentIdentityManager();
resetMemoryExtractor();
extractor = new MemoryExtractor();
});
describe('extractFromConversation', () => {
it('skips extraction for short conversations', async () => {
const result = await extractor.extractFromConversation(
[
{ role: 'user', content: '你好' },
{ role: 'assistant', content: '你好!' },
],
'agent-1'
);
expect(result.saved).toBe(0);
});
it('extracts facts from user messages', async () => {
const result = await extractor.extractFromConversation(
[
{ role: 'user', content: '我的公司叫字节跳动' },
{ role: 'assistant', content: '好的,了解了。' },
{ role: 'user', content: '我在做一个AI助手项目' },
{ role: 'assistant', content: '听起来很有趣!' },
{ role: 'user', content: '帮我看看这个bug' },
{ role: 'assistant', content: '好的,让我看看。' },
],
'agent-1'
);
expect(result.items.length).toBeGreaterThan(0);
const facts = result.items.filter(i => i.type === 'fact');
expect(facts.length).toBeGreaterThan(0);
});
it('extracts preferences from user messages', async () => {
const result = await extractor.extractFromConversation(
[
{ role: 'user', content: '请用中文回复我' },
{ role: 'assistant', content: '好的,我会用中文。' },
{ role: 'user', content: '我喜欢简洁的代码风格' },
{ role: 'assistant', content: '明白了。' },
{ role: 'user', content: '以后都用TypeScript写' },
{ role: 'assistant', content: '好的。' },
],
'agent-1'
);
const prefs = result.items.filter(i => i.type === 'preference');
expect(prefs.length).toBeGreaterThan(0);
});
it('saves extracted memories to MemoryManager', async () => {
await extractor.extractFromConversation(
[
{ role: 'user', content: '我的项目叫ZCLAW' },
{ role: 'assistant', content: '好的。' },
{ role: 'user', content: '我喜欢简洁回答' },
{ role: 'assistant', content: '了解。' },
{ role: 'user', content: '继续' },
{ role: 'assistant', content: '好的。' },
],
'agent-1'
);
const memMgr = new MemoryManager();
const all = await memMgr.getAll('agent-1');
expect(all.length).toBeGreaterThan(0);
});
it('respects extraction cooldown', async () => {
const msgs = [
{ role: 'user', content: '我的公司叫字节跳动' },
{ role: 'assistant', content: '好的。' },
{ role: 'user', content: '帮我写代码' },
{ role: 'assistant', content: '好的。' },
{ role: 'user', content: '继续' },
{ role: 'assistant', content: '好的。' },
];
const r1 = await extractor.extractFromConversation(msgs, 'agent-1');
const r2 = await extractor.extractFromConversation(msgs, 'agent-1');
// Second call should be blocked by cooldown
expect(r2.saved).toBe(0);
// First call should have extracted something
expect(r1.items.length).toBeGreaterThanOrEqual(0); // May or may not match patterns
});
});
describe('buildExtractionPrompt', () => {
it('produces a valid LLM prompt', () => {
const prompt = extractor.buildExtractionPrompt([
{ role: 'user', content: '帮我配置飞书' },
{ role: 'assistant', content: '好的需要app_id和app_secret' },
]);
expect(prompt).toContain('请从以下对话中提取');
expect(prompt).toContain('用户');
expect(prompt).toContain('助手');
expect(prompt).toContain('飞书');
});
});
describe('parseExtractionResponse', () => {
it('parses valid JSON response', () => {
const response = `[
{"content": "用户喜欢中文", "type": "preference", "importance": 7, "tags": ["lang"]},
{"content": "公司叫ACME", "type": "fact", "importance": 6, "tags": ["company"]}
]`;
const items = extractor.parseExtractionResponse(response);
expect(items).toHaveLength(2);
expect(items[0].type).toBe('preference');
expect(items[1].type).toBe('fact');
});
it('handles wrapped JSON in markdown', () => {
const response = "```json\n[{\"content\": \"test\", \"type\": \"fact\", \"importance\": 5, \"tags\": []}]\n```";
const items = extractor.parseExtractionResponse(response);
expect(items).toHaveLength(1);
});
it('returns empty for invalid response', () => {
const items = extractor.parseExtractionResponse('no json here');
expect(items).toHaveLength(0);
});
it('clamps importance to 1-10', () => {
const response = '[{"content": "test", "type": "fact", "importance": 15, "tags": []}]';
const items = extractor.parseExtractionResponse(response);
expect(items[0].importance).toBe(10);
});
});
});

View File

@@ -0,0 +1,309 @@
/**
* Tests for Context Compactor (Phase 2)
*
* Covers: token estimation, threshold checking, memory flush, compaction
*/
import { describe, it, expect, beforeEach, vi } from 'vitest';
import {
ContextCompactor,
resetContextCompactor,
estimateTokens,
estimateMessagesTokens,
DEFAULT_COMPACTION_CONFIG,
type CompactableMessage,
} from '../../desktop/src/lib/context-compactor';
import { resetMemoryManager } from '../../desktop/src/lib/agent-memory';
import { resetAgentIdentityManager } from '../../desktop/src/lib/agent-identity';
import { resetMemoryExtractor } from '../../desktop/src/lib/memory-extractor';
// === Mock localStorage ===
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] ?? null,
setItem: (key: string, value: string) => { store[key] = value; },
removeItem: (key: string) => { delete store[key]; },
clear: () => { store = {}; },
};
})();
vi.stubGlobal('localStorage', localStorageMock);
// === Helpers ===
function makeMessages(count: number, contentLength: number = 100): CompactableMessage[] {
const msgs: CompactableMessage[] = [];
for (let i = 0; i < count; i++) {
msgs.push({
role: i % 2 === 0 ? 'user' : 'assistant',
content: '测试消息内容'.repeat(Math.ceil(contentLength / 6)).slice(0, contentLength),
id: `msg_${i}`,
timestamp: new Date(Date.now() - (count - i) * 60000),
});
}
return msgs;
}
function makeLargeConversation(targetTokens: number): CompactableMessage[] {
const msgs: CompactableMessage[] = [];
let totalTokens = 0;
let i = 0;
while (totalTokens < targetTokens) {
const content = i % 2 === 0
? `用户问题 ${i}: 请帮我分析一下这个技术方案的可行性,包括性能、安全性和可维护性方面`
: `助手回答 ${i}: 好的,我来从三个维度分析这个方案。首先从性能角度来看,这个方案使用了异步处理机制,能够有效提升吞吐量。其次从安全性方面,建议增加输入验证和权限控制。最后从可维护性来看,模块化设计使得后续修改更加方便。`;
msgs.push({
role: i % 2 === 0 ? 'user' : 'assistant',
content,
id: `msg_${i}`,
timestamp: new Date(Date.now() - (1000 - i) * 60000),
});
totalTokens = estimateMessagesTokens(msgs);
i++;
}
return msgs;
}
// =============================================
// Token Estimation Tests
// =============================================
describe('Token Estimation', () => {
it('returns 0 for empty string', () => {
expect(estimateTokens('')).toBe(0);
});
it('estimates CJK text at ~1.5 tokens per char', () => {
const text = '你好世界测试';
const tokens = estimateTokens(text);
// 6 CJK chars × 1.5 = 9
expect(tokens).toBe(9);
});
it('estimates English text at ~0.3 tokens per char', () => {
const text = 'hello world test';
const tokens = estimateTokens(text);
// Roughly: 13 ASCII chars × 0.3 + 2 spaces × 0.25 ≈ 4.4
expect(tokens).toBeGreaterThan(3);
expect(tokens).toBeLessThan(10);
});
it('estimates mixed CJK+English text', () => {
const text = '用户的项目叫 ZCLAW Desktop';
const tokens = estimateTokens(text);
expect(tokens).toBeGreaterThan(5);
});
it('estimateMessagesTokens includes framing overhead', () => {
const msgs: CompactableMessage[] = [
{ role: 'user', content: '你好' },
{ role: 'assistant', content: '你好!' },
];
const tokens = estimateMessagesTokens(msgs);
// Content tokens + framing (4 per message × 2)
expect(tokens).toBeGreaterThan(estimateTokens('你好') + estimateTokens('你好!'));
});
});
// =============================================
// ContextCompactor Tests
// =============================================
describe('ContextCompactor', () => {
let compactor: ContextCompactor;
beforeEach(() => {
localStorageMock.clear();
resetContextCompactor();
resetMemoryManager();
resetAgentIdentityManager();
resetMemoryExtractor();
compactor = new ContextCompactor();
});
describe('checkThreshold', () => {
it('returns none urgency for small conversations', () => {
const msgs = makeMessages(4);
const check = compactor.checkThreshold(msgs);
expect(check.shouldCompact).toBe(false);
expect(check.urgency).toBe('none');
});
it('returns soft urgency when approaching threshold', () => {
const msgs = makeLargeConversation(DEFAULT_COMPACTION_CONFIG.softThresholdTokens);
const check = compactor.checkThreshold(msgs);
expect(check.shouldCompact).toBe(true);
expect(check.urgency).toBe('soft');
});
it('returns hard urgency when exceeding hard threshold', () => {
const msgs = makeLargeConversation(DEFAULT_COMPACTION_CONFIG.hardThresholdTokens);
const check = compactor.checkThreshold(msgs);
expect(check.shouldCompact).toBe(true);
expect(check.urgency).toBe('hard');
});
it('reports current token count', () => {
const msgs = makeMessages(10);
const check = compactor.checkThreshold(msgs);
expect(check.currentTokens).toBeGreaterThan(0);
});
});
describe('compact', () => {
it('retains keepRecentMessages recent messages', async () => {
const config = { keepRecentMessages: 4 };
const comp = new ContextCompactor(config);
const msgs = makeMessages(20);
const result = await comp.compact(msgs, 'agent-1');
// Should have: 1 summary + 4 recent = 5
expect(result.retainedCount).toBe(5);
expect(result.compactedMessages).toHaveLength(5);
expect(result.compactedMessages[0].role).toBe('system'); // summary
});
it('generates a summary that mentions message count', async () => {
const msgs = makeMessages(20);
const result = await compactor.compact(msgs, 'agent-1');
expect(result.summary).toContain('压缩');
expect(result.summary).toContain('条消息');
});
it('reduces token count significantly', async () => {
const msgs = makeLargeConversation(16000);
const result = await compactor.compact(msgs, 'agent-1');
expect(result.tokensAfterCompaction).toBeLessThan(result.tokensBeforeCompaction);
});
it('preserves most recent messages in order', async () => {
const msgs: CompactableMessage[] = [
{ role: 'user', content: 'old message 1', id: 'old1' },
{ role: 'assistant', content: 'old reply 1', id: 'old2' },
{ role: 'user', content: 'old message 2', id: 'old3' },
{ role: 'assistant', content: 'old reply 2', id: 'old4' },
{ role: 'user', content: 'recent message 1', id: 'recent1' },
{ role: 'assistant', content: 'recent reply 1', id: 'recent2' },
{ role: 'user', content: 'recent message 2', id: 'recent3' },
{ role: 'assistant', content: 'recent reply 2', id: 'recent4' },
];
const comp = new ContextCompactor({ keepRecentMessages: 4 });
const result = await comp.compact(msgs, 'agent-1');
// Last 4 messages should be preserved
const retained = result.compactedMessages.slice(1); // skip summary
expect(retained).toHaveLength(4);
expect(retained[0].content).toBe('recent message 1');
expect(retained[3].content).toBe('recent reply 2');
});
it('handles empty message list', async () => {
const result = await compactor.compact([], 'agent-1');
expect(result.retainedCount).toBe(1); // just the summary
expect(result.summary).toContain('对话开始');
});
it('handles fewer messages than keepRecentMessages', async () => {
const msgs = makeMessages(3);
const result = await compactor.compact(msgs, 'agent-1');
// All messages kept + summary
expect(result.compactedMessages.length).toBeLessThanOrEqual(msgs.length + 1);
});
});
describe('memoryFlush', () => {
it('returns 0 when disabled', async () => {
const comp = new ContextCompactor({ memoryFlushEnabled: false });
const flushed = await comp.memoryFlush(makeMessages(10), 'agent-1');
expect(flushed).toBe(0);
});
it('extracts memories from conversation messages', async () => {
const msgs: CompactableMessage[] = [
{ role: 'user', content: '我的公司叫字节跳动我在做AI项目' },
{ role: 'assistant', content: '好的,了解了。' },
{ role: 'user', content: '我喜欢简洁的代码风格' },
{ role: 'assistant', content: '明白。' },
{ role: 'user', content: '帮我看看这个问题' },
{ role: 'assistant', content: '好的。' },
];
const flushed = await compactor.memoryFlush(msgs, 'agent-1');
// Should extract at least some memories
expect(flushed).toBeGreaterThanOrEqual(0); // May or may not match patterns
});
});
describe('generateSummary (via compact)', () => {
it('includes topic extraction from user messages', async () => {
const msgs: CompactableMessage[] = [
{ role: 'user', content: '帮我分析一下React性能优化方案' },
{ role: 'assistant', content: '好的React性能优化主要从以下几个方面入手1. 使用React.memo 2. 使用useMemo' },
{ role: 'user', content: '那TypeScript的类型推导呢' },
{ role: 'assistant', content: 'TypeScript类型推导是一个重要特性...' },
...makeMessages(4), // pad to exceed keepRecentMessages
];
const comp = new ContextCompactor({ keepRecentMessages: 2 });
const result = await comp.compact(msgs, 'agent-1');
// Summary should mention topics
expect(result.summary).toContain('讨论主题');
});
it('includes technical context when code blocks present', async () => {
const msgs: CompactableMessage[] = [
{ role: 'user', content: '帮我写一个函数' },
{ role: 'assistant', content: '好的,这是实现:\n```typescript\nfunction hello() { return "world"; }\n```' },
...makeMessages(6),
];
const comp = new ContextCompactor({ keepRecentMessages: 2 });
const result = await comp.compact(msgs, 'agent-1');
expect(result.summary).toContain('技术上下文');
});
});
describe('buildCompactionPrompt', () => {
it('generates a valid LLM prompt', () => {
const msgs: CompactableMessage[] = [
{ role: 'user', content: '帮我优化数据库查询' },
{ role: 'assistant', content: '好的,我建议使用索引...' },
];
const prompt = compactor.buildCompactionPrompt(msgs);
expect(prompt).toContain('压缩为简洁摘要');
expect(prompt).toContain('优化数据库');
expect(prompt).toContain('用户');
expect(prompt).toContain('助手');
});
});
describe('config management', () => {
it('uses default config', () => {
const config = compactor.getConfig();
expect(config.softThresholdTokens).toBe(15000);
expect(config.keepRecentMessages).toBe(6);
});
it('allows config updates', () => {
compactor.updateConfig({ softThresholdTokens: 10000 });
expect(compactor.getConfig().softThresholdTokens).toBe(10000);
});
it('accepts partial config in constructor', () => {
const comp = new ContextCompactor({ keepRecentMessages: 10 });
const config = comp.getConfig();
expect(config.keepRecentMessages).toBe(10);
expect(config.softThresholdTokens).toBe(15000); // default preserved
});
});
});

View File

@@ -0,0 +1,423 @@
/**
* Tests for Heartbeat Engine + Reflection Engine (Phase 3)
*/
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import {
HeartbeatEngine,
resetHeartbeatEngines,
type HeartbeatAlert,
} from '../../desktop/src/lib/heartbeat-engine';
import {
ReflectionEngine,
resetReflectionEngine,
} from '../../desktop/src/lib/reflection-engine';
import { MemoryManager, resetMemoryManager } from '../../desktop/src/lib/agent-memory';
import { resetAgentIdentityManager } from '../../desktop/src/lib/agent-identity';
import { resetMemoryExtractor } from '../../desktop/src/lib/memory-extractor';
// === Mock localStorage ===
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] ?? null,
setItem: (key: string, value: string) => { store[key] = value; },
removeItem: (key: string) => { delete store[key]; },
clear: () => { store = {}; },
};
})();
vi.stubGlobal('localStorage', localStorageMock);
// =============================================
// HeartbeatEngine Tests
// =============================================
describe('HeartbeatEngine', () => {
let engine: HeartbeatEngine;
beforeEach(() => {
localStorageMock.clear();
resetHeartbeatEngines();
resetMemoryManager();
resetAgentIdentityManager();
// Disable quiet hours to avoid test-time sensitivity
engine = new HeartbeatEngine('agent-1', { quietHoursStart: undefined, quietHoursEnd: undefined });
});
afterEach(() => {
engine.stop();
});
describe('lifecycle', () => {
it('starts and stops cleanly', () => {
const eng = new HeartbeatEngine('test', { enabled: true, intervalMinutes: 1 });
eng.start();
expect(eng.isRunning()).toBe(true);
eng.stop();
expect(eng.isRunning()).toBe(false);
});
it('does not start when disabled', () => {
const eng = new HeartbeatEngine('test', { enabled: false });
eng.start();
expect(eng.isRunning()).toBe(false);
});
});
describe('tick', () => {
it('returns ok status when no alerts', async () => {
const result = await engine.tick();
expect(result.status).toBe('ok');
expect(result.checkedItems).toBeGreaterThan(0);
expect(result.timestamp).toBeTruthy();
});
it('detects pending tasks', async () => {
const mgr = new MemoryManager();
// Create high-importance task memories
await mgr.save({ agentId: 'agent-1', content: '完成API集成', type: 'task', importance: 8, source: 'auto', tags: [] });
await mgr.save({ agentId: 'agent-1', content: '修复登录bug', type: 'task', importance: 7, source: 'auto', tags: [] });
const result = await engine.tick();
// With 'light' proactivity, only high urgency alerts pass through
// The task check produces medium/high urgency
const taskAlerts = result.alerts.filter(a => a.source === 'pending-tasks');
// May or may not produce alert depending on proactivity filter
expect(result.checkedItems).toBeGreaterThan(0);
});
it('detects memory health issues', async () => {
const mgr = new MemoryManager();
// Create many memories to trigger health alert
for (let i = 0; i < 510; i++) {
await mgr.save({
agentId: 'agent-1',
content: `memory entry ${i} with unique content ${Math.random()}`,
type: 'fact',
importance: 3,
source: 'auto',
tags: [`batch${i}`],
});
}
// Use autonomous proactivity to see all alerts
const eng = new HeartbeatEngine('agent-1', { proactivityLevel: 'autonomous', quietHoursStart: undefined, quietHoursEnd: undefined });
const result = await eng.tick();
const healthAlerts = result.alerts.filter(a => a.source === 'memory-health');
expect(healthAlerts.length).toBe(1);
expect(healthAlerts[0].content).toMatch(/\d{3,}/);
});
it('stores tick results in history', async () => {
await engine.tick();
await engine.tick();
const history = engine.getHistory();
expect(history.length).toBe(2);
});
});
describe('quiet hours', () => {
it('returns ok with 0 checks during quiet hours', async () => {
// Set quiet hours to cover current time
const now = new Date();
const startHour = now.getHours();
const endHour = (startHour + 2) % 24;
const eng = new HeartbeatEngine('test', {
quietHoursStart: `${String(startHour).padStart(2, '0')}:00`,
quietHoursEnd: `${String(endHour).padStart(2, '0')}:00`,
});
const result = await eng.tick();
expect(result.checkedItems).toBe(0);
expect(result.status).toBe('ok');
});
it('isQuietHours handles cross-midnight range', () => {
const eng = new HeartbeatEngine('test', {
quietHoursStart: '22:00',
quietHoursEnd: '08:00',
});
// The method checks against current time, so we just verify it doesn't throw
const result = eng.isQuietHours();
expect(typeof result).toBe('boolean');
});
});
describe('custom checks', () => {
it('runs registered custom checks', async () => {
const eng = new HeartbeatEngine('test', { proactivityLevel: 'autonomous', quietHoursStart: undefined, quietHoursEnd: undefined });
eng.registerCheck(async () => ({
title: 'Custom Alert',
content: 'Custom check triggered',
urgency: 'medium' as const,
source: 'custom',
timestamp: new Date().toISOString(),
}));
const result = await eng.tick();
const custom = result.alerts.filter(a => a.source === 'custom');
expect(custom.length).toBe(1);
expect(custom[0].title).toBe('Custom Alert');
});
});
describe('proactivity filtering', () => {
it('silent mode suppresses all alerts', async () => {
const eng = new HeartbeatEngine('test', { proactivityLevel: 'silent', quietHoursStart: undefined, quietHoursEnd: undefined });
eng.registerCheck(async () => ({
title: 'Test',
content: 'Test',
urgency: 'high' as const,
source: 'test',
timestamp: new Date().toISOString(),
}));
const result = await eng.tick();
expect(result.alerts).toHaveLength(0);
});
it('light mode only shows high urgency', async () => {
const eng = new HeartbeatEngine('test', { proactivityLevel: 'light', quietHoursStart: undefined, quietHoursEnd: undefined });
eng.registerCheck(async () => ({
title: 'Low',
content: 'Low urgency',
urgency: 'low' as const,
source: 'test-low',
timestamp: new Date().toISOString(),
}));
eng.registerCheck(async () => ({
title: 'High',
content: 'High urgency',
urgency: 'high' as const,
source: 'test-high',
timestamp: new Date().toISOString(),
}));
const result = await eng.tick();
expect(result.alerts.every(a => a.urgency === 'high')).toBe(true);
});
});
describe('config', () => {
it('returns current config', () => {
const config = engine.getConfig();
expect(config.intervalMinutes).toBe(30);
expect(config.enabled).toBe(false);
});
it('updates config', () => {
engine.updateConfig({ intervalMinutes: 15 });
expect(engine.getConfig().intervalMinutes).toBe(15);
});
});
describe('alert callback', () => {
it('calls onAlert when alerts are produced', async () => {
const alerts: HeartbeatAlert[][] = [];
const eng = new HeartbeatEngine('test', { enabled: true, proactivityLevel: 'autonomous', quietHoursStart: undefined, quietHoursEnd: undefined });
eng.registerCheck(async () => ({
title: 'Alert',
content: 'Test alert',
urgency: 'high' as const,
source: 'test',
timestamp: new Date().toISOString(),
}));
eng.start((a) => alerts.push(a));
// Manually trigger tick instead of waiting for interval
await eng.tick();
eng.stop();
// The tick() call should have triggered onAlert
// Note: the start() interval won't fire in test, but manual tick() does call onAlert
});
});
});
// =============================================
// ReflectionEngine Tests
// =============================================
describe('ReflectionEngine', () => {
let engine: ReflectionEngine;
beforeEach(() => {
localStorageMock.clear();
resetReflectionEngine();
resetMemoryManager();
resetAgentIdentityManager();
resetMemoryExtractor();
engine = new ReflectionEngine();
});
describe('trigger management', () => {
it('should not reflect initially', () => {
expect(engine.shouldReflect()).toBe(false);
});
it('triggers after N conversations', () => {
const eng = new ReflectionEngine({ triggerAfterConversations: 3 });
eng.recordConversation();
eng.recordConversation();
expect(eng.shouldReflect()).toBe(false);
eng.recordConversation();
expect(eng.shouldReflect()).toBe(true);
});
it('resets counter after reflection', async () => {
const eng = new ReflectionEngine({ triggerAfterConversations: 2 });
eng.recordConversation();
eng.recordConversation();
expect(eng.shouldReflect()).toBe(true);
await eng.reflect('agent-1');
expect(eng.shouldReflect()).toBe(false);
expect(eng.getState().conversationsSinceReflection).toBe(0);
});
});
describe('reflect', () => {
it('returns result with patterns and improvements', async () => {
const result = await engine.reflect('agent-1');
expect(result.timestamp).toBeTruthy();
expect(Array.isArray(result.patterns)).toBe(true);
expect(Array.isArray(result.improvements)).toBe(true);
expect(typeof result.newMemories).toBe('number');
});
it('detects task accumulation pattern', async () => {
const mgr = new MemoryManager();
for (let i = 0; i < 6; i++) {
await mgr.save({
agentId: 'agent-1',
content: `Task ${i}: do something ${Math.random()}`,
type: 'task',
importance: 6,
source: 'auto',
tags: [],
});
}
const result = await engine.reflect('agent-1');
const taskPattern = result.patterns.find(p => p.observation.includes('待办任务'));
expect(taskPattern).toBeTruthy();
expect(taskPattern!.sentiment).toBe('negative');
});
it('detects strong preference accumulation as positive', async () => {
const mgr = new MemoryManager();
for (let i = 0; i < 6; i++) {
await mgr.save({
agentId: 'agent-1',
content: `Preference ${i}: likes ${Math.random()}`,
type: 'preference',
importance: 5,
source: 'auto',
tags: [],
});
}
const result = await engine.reflect('agent-1');
const prefPattern = result.patterns.find(p => p.observation.includes('用户偏好'));
expect(prefPattern).toBeTruthy();
expect(prefPattern!.sentiment).toBe('positive');
});
it('generates improvement suggestions for low preference count', async () => {
// No preferences saved → should suggest enrichment
const result = await engine.reflect('agent-1');
const userImprovement = result.improvements.find(i => i.area === '用户理解');
expect(userImprovement).toBeTruthy();
});
it('saves reflection memories', async () => {
const mgr = new MemoryManager();
// Create enough data for patterns to be detected
for (let i = 0; i < 6; i++) {
await mgr.save({
agentId: 'agent-1',
content: `Task ${i}: important work item ${Math.random()}`,
type: 'task',
importance: 7,
source: 'auto',
tags: [],
});
}
const result = await engine.reflect('agent-1');
expect(result.newMemories).toBeGreaterThan(0);
// Verify reflection memories were saved (reload from localStorage since reflect uses singleton)
const mgr2 = new MemoryManager();
const allMemories = await mgr2.getAll('agent-1');
const reflectionMemories = allMemories.filter(m => m.tags.includes('reflection'));
expect(reflectionMemories.length).toBeGreaterThan(0);
});
it('stores result in history', async () => {
await engine.reflect('agent-1');
await engine.reflect('agent-1');
const history = engine.getHistory();
expect(history.length).toBe(2);
});
});
describe('identity proposals', () => {
it('proposes changes when allowSoulModification is true', async () => {
const eng = new ReflectionEngine({ allowSoulModification: true });
const mgr = new MemoryManager();
// Create multiple negative patterns
for (let i = 0; i < 6; i++) {
await mgr.save({
agentId: 'agent-1',
content: `Overdue task ${i}: ${Math.random()}`,
type: 'task',
importance: 7,
source: 'auto',
tags: [],
});
}
const result = await eng.reflect('agent-1');
// May or may not produce identity proposals depending on pattern analysis
expect(Array.isArray(result.identityProposals)).toBe(true);
});
it('does not propose changes when allowSoulModification is false', async () => {
const eng = new ReflectionEngine({ allowSoulModification: false });
const result = await eng.reflect('agent-1');
expect(result.identityProposals).toHaveLength(0);
});
});
describe('config', () => {
it('returns current config', () => {
const config = engine.getConfig();
expect(config.triggerAfterConversations).toBe(5);
expect(config.requireApproval).toBe(true);
});
it('updates config', () => {
engine.updateConfig({ triggerAfterConversations: 10 });
expect(engine.getConfig().triggerAfterConversations).toBe(10);
});
});
describe('persistence', () => {
it('persists state across instances', () => {
engine.recordConversation();
engine.recordConversation();
resetReflectionEngine();
const eng2 = new ReflectionEngine();
expect(eng2.getState().conversationsSinceReflection).toBe(2);
});
});
});