recallnest 2.2.1 → 2.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +150 -0
- package/README.md +46 -35
- package/README_CN.md +41 -26
- package/assets/ui/app.js +1031 -0
- package/assets/ui/index.html +224 -0
- package/assets/ui/styles.css +781 -0
- package/config.json.example +4 -1
- package/connectors/examples/email-adapter.ts +73 -0
- package/connectors/examples/logs-adapter.ts +103 -0
- package/connectors/examples/rss-adapter.ts +95 -0
- package/docs/api-reference.md +1291 -0
- package/docs/architecture.md +168 -0
- package/docs/connector-spec.md +126 -0
- package/eval/cases-canary.json +58 -0
- package/eval/cases-scope-robustness.json +278 -0
- package/eval/cases.json +117 -20
- package/package.json +22 -1
- package/src/__tests__/access-tracker-novelty.test.ts +238 -0
- package/src/__tests__/activity-counter.test.ts +125 -0
- package/src/__tests__/adaptive-fidelity.test.ts +102 -0
- package/src/__tests__/admission-control.test.ts +206 -0
- package/src/__tests__/anchor-generator.test.ts +67 -0
- package/src/__tests__/auto-consolidation.test.ts +168 -0
- package/src/__tests__/auto-recall.test.ts +131 -0
- package/src/__tests__/babel-integration.test.ts +144 -0
- package/src/__tests__/batch-dedup.test.ts +65 -0
- package/src/__tests__/canary-eval.test.ts +114 -0
- package/src/__tests__/capture-engine.test.ts +1238 -0
- package/src/__tests__/capture-heuristic.test.ts +171 -0
- package/src/__tests__/cascade-forget.test.ts +210 -0
- package/src/__tests__/category-min-scores.test.ts +33 -0
- package/src/__tests__/cc8-cc1-essential-context.test.ts +298 -0
- package/src/__tests__/circuit-breaker.test.ts +99 -0
- package/src/__tests__/cluster-consolidation.test.ts +782 -0
- package/src/__tests__/confidence-metadata.test.ts +125 -0
- package/src/__tests__/confidence-tracker.test.ts +261 -0
- package/src/__tests__/conflict-advisor.test.ts +206 -0
- package/src/__tests__/conflict-escalation.test.ts +149 -0
- package/src/__tests__/conflict-lifecycle.test.ts +83 -0
- package/src/__tests__/conflict-output.test.ts +85 -0
- package/src/__tests__/conflict-store.test.ts +374 -0
- package/src/__tests__/connector-v1.test.ts +181 -0
- package/src/__tests__/consolidation-engine.test.ts +333 -0
- package/src/__tests__/constructive-retrieval.test.ts +509 -0
- package/src/__tests__/context-collapse-renderer.test.ts +214 -0
- package/src/__tests__/context-composer-pins.test.ts +64 -0
- package/src/__tests__/context-composer-task-selection.test.ts +24 -0
- package/src/__tests__/context-reconstructor.test.ts +65 -0
- package/src/__tests__/context-renderer.test.ts +91 -0
- package/src/__tests__/continuity-eval.test.ts +275 -0
- package/src/__tests__/continuity-schema.test.ts +97 -0
- package/src/__tests__/contracts.test.ts +216 -0
- package/src/__tests__/conversation-importer.test.ts +692 -0
- package/src/__tests__/core-recall-eval.test.ts +235 -0
- package/src/__tests__/core-summary.test.ts +109 -0
- package/src/__tests__/data-checkup.test.ts +276 -0
- package/src/__tests__/decay-exemption.test.ts +84 -0
- package/src/__tests__/dedup-actions.test.ts +58 -0
- package/src/__tests__/dedup-guard.test.ts +39 -0
- package/src/__tests__/deterministic-id.test.ts +39 -0
- package/src/__tests__/distill-lock.test.ts +305 -0
- package/src/__tests__/distill-session-argshape.test.ts +79 -0
- package/src/__tests__/doctor.test.ts +319 -0
- package/src/__tests__/dream-pipeline.test.ts +321 -0
- package/src/__tests__/drill-down.test.ts +108 -0
- package/src/__tests__/embedder.test.ts +122 -0
- package/src/__tests__/emotion-decay.test.ts +46 -0
- package/src/__tests__/emotion-detector.test.ts +56 -0
- package/src/__tests__/emotion-phase1-integration.test.ts +153 -0
- package/src/__tests__/emotion-retrieval.test.ts +56 -0
- package/src/__tests__/entity-resolver.test.ts +58 -0
- package/src/__tests__/env-config.test.ts +151 -0
- package/src/__tests__/error-signature-boost.test.ts +87 -0
- package/src/__tests__/error-signature.test.ts +95 -0
- package/src/__tests__/error-taxonomy.test.ts +129 -0
- package/src/__tests__/ethics-layer.test.ts +476 -0
- package/src/__tests__/event-segmenter.test.ts +153 -0
- package/src/__tests__/feature-flags-integration.test.ts +54 -0
- package/src/__tests__/frequency-tracker.test.ts +113 -0
- package/src/__tests__/graph-export.test.ts +489 -0
- package/src/__tests__/hotness-blend.test.ts +76 -0
- package/src/__tests__/ingest-chunking.test.ts +44 -0
- package/src/__tests__/ingest.test.ts +360 -0
- package/src/__tests__/interference-detector.test.ts +203 -0
- package/src/__tests__/kg-extractor.test.ts +192 -0
- package/src/__tests__/kg-store.test.ts +300 -0
- package/src/__tests__/llm-consolidation.test.ts +226 -0
- package/src/__tests__/maintenance-locks.test.ts +174 -0
- package/src/__tests__/mcp-efficiency.test.ts +288 -0
- package/src/__tests__/mcp-related-scopes.test.ts +151 -0
- package/src/__tests__/memory-boundaries.test.ts +250 -0
- package/src/__tests__/memory-evolution-b2-b3-e1.test.ts +345 -0
- package/src/__tests__/memory-evolution.test.ts +271 -0
- package/src/__tests__/memory-health-rebalance.test.ts +144 -0
- package/src/__tests__/memory-lint.test.ts +421 -0
- package/src/__tests__/memory-output.test.ts +215 -0
- package/src/__tests__/memory-promotion.test.ts +315 -0
- package/src/__tests__/migrate-dedup-ids.test.ts +65 -0
- package/src/__tests__/multi-vector.test.ts +242 -0
- package/src/__tests__/narrative-architecture.test.ts +456 -0
- package/src/__tests__/noise-filter.test.ts +205 -0
- package/src/__tests__/obsidian-connector.test.ts +363 -0
- package/src/__tests__/omitted-reporting.test.ts +98 -0
- package/src/__tests__/pending-review.test.ts +77 -0
- package/src/__tests__/pii-redact.test.ts +314 -0
- package/src/__tests__/post-retrieval-filter.test.ts +178 -0
- package/src/__tests__/ppr-traversal.test.ts +256 -0
- package/src/__tests__/prediction-engine.test.ts +349 -0
- package/src/__tests__/predictive-prospective-memory.test.ts +416 -0
- package/src/__tests__/preference-guard-smoke.test.ts +211 -0
- package/src/__tests__/preference-matcher.test.ts +230 -0
- package/src/__tests__/preference-slots.test.ts +205 -0
- package/src/__tests__/prospective-memory.test.ts +283 -0
- package/src/__tests__/query-entity-detector.test.ts +171 -0
- package/src/__tests__/query-expander-alias.test.ts +146 -0
- package/src/__tests__/query-expander.test.ts +34 -0
- package/src/__tests__/recall-governor.test.ts +227 -0
- package/src/__tests__/recall-mode.test.ts +64 -0
- package/src/__tests__/result-synthesizer.test.ts +198 -0
- package/src/__tests__/resume-context.test.ts +4898 -0
- package/src/__tests__/retrieval-trace.test.ts +213 -0
- package/src/__tests__/retriever-boundary-weight.test.ts +136 -0
- package/src/__tests__/retroactive-boost.test.ts +223 -0
- package/src/__tests__/salience-scoring.test.ts +106 -0
- package/src/__tests__/scope-inventory.test.ts +200 -0
- package/src/__tests__/scope-policy.test.ts +69 -0
- package/src/__tests__/scope-suggester.test.ts +80 -0
- package/src/__tests__/session-checkpoint.test.ts +291 -0
- package/src/__tests__/session-distiller.test.ts +640 -0
- package/src/__tests__/session-gc.test.ts +215 -0
- package/src/__tests__/skill-engine.test.ts +552 -0
- package/src/__tests__/skill-promotion.test.ts +601 -0
- package/src/__tests__/skill-verifier.test.ts +122 -0
- package/src/__tests__/source-heartbeat.test.ts +250 -0
- package/src/__tests__/store-patch-metadata.test.ts +205 -0
- package/src/__tests__/store-scope-audit.test.ts +81 -0
- package/src/__tests__/store-upsert.test.ts +184 -0
- package/src/__tests__/supersede-chain.test.ts +154 -0
- package/src/__tests__/temporal-parser.test.ts +241 -0
- package/src/__tests__/temporal-validity.test.ts +160 -0
- package/src/__tests__/term-registry.test.ts +121 -0
- package/src/__tests__/tool-error-surfacing.test.ts +98 -0
- package/src/__tests__/tool-output-compressor.test.ts +160 -0
- package/src/__tests__/topic-tag.test.ts +113 -0
- package/src/__tests__/trusted-memory.test.ts +839 -0
- package/src/__tests__/usage-tracker.test.ts +287 -0
- package/src/__tests__/version-manager.test.ts +213 -0
- package/src/__tests__/workflow-observation-skill-outcome.test.ts +352 -0
- package/src/__tests__/workflow-observation.test.ts +433 -0
- package/src/__tests__/write-verifier.test.ts +122 -0
- package/src/access-tracker.ts +38 -32
- package/src/activity-counter.ts +46 -18
- package/src/aliases.ts +133 -0
- package/src/api-server.ts +27 -7
- package/src/audit-log.ts +4 -2
- package/src/auto-consolidation.ts +8 -3
- package/src/auto-gc.ts +85 -21
- package/src/capture-engine.ts +54 -21
- package/src/cli.ts +199 -37
- package/src/confidence-tracker.ts +36 -18
- package/src/connector-types.ts +57 -0
- package/src/consolidation-engine.ts +109 -11
- package/src/context-composer.ts +8 -1
- package/src/conversation-importer.ts +25 -1
- package/src/data-checkup.ts +126 -7
- package/src/distill-lock.ts +194 -15
- package/src/doctor.ts +38 -2
- package/src/dream-pipeline.ts +118 -34
- package/src/env-config.ts +77 -0
- package/src/error-signature.ts +153 -0
- package/src/error-taxonomy.ts +115 -0
- package/src/eval.ts +251 -6
- package/src/idempotency.ts +8 -0
- package/src/ingest.ts +234 -10
- package/src/kg-extractor.ts +6 -2
- package/src/kg-store.ts +236 -44
- package/src/llm-consolidation.ts +2 -1
- package/src/mcp-server.ts +85 -1799
- package/src/mcp-tool-deps.ts +48 -0
- package/src/mcp-tools-advanced.ts +941 -0
- package/src/mcp-tools-core.ts +633 -0
- package/src/mcp-tools-governance.ts +475 -0
- package/src/memory-boundaries.ts +3 -1
- package/src/memory-evolution.ts +73 -1
- package/src/memory-lint.ts +183 -10
- package/src/memory-output.ts +114 -3
- package/src/memory-promotion.ts +246 -0
- package/src/memory-schema.ts +25 -2
- package/src/memory-store-port.ts +43 -0
- package/src/multi-vector.ts +2 -1
- package/src/narrative-schema.ts +2 -1
- package/src/noise-filter.ts +57 -6
- package/src/obsidian-connector.ts +282 -0
- package/src/pii-detector.ts +157 -5
- package/src/query-expander.ts +90 -9
- package/src/result-synthesizer.ts +2 -1
- package/src/retention-policy.ts +2 -1
- package/src/retriever.ts +259 -139
- package/src/runtime-config.ts +8 -1
- package/src/scope-suggester.ts +113 -0
- package/src/session-distiller.ts +23 -2
- package/src/session-engine.ts +2 -1
- package/src/session-schema.ts +3 -2
- package/src/session-store.ts +5 -0
- package/src/skill-engine.ts +78 -0
- package/src/skill-promotion.ts +247 -47
- package/src/skill-schema.ts +16 -3
- package/src/skill-verifier.ts +152 -0
- package/src/source-heartbeat.ts +108 -0
- package/src/store.ts +267 -38
- package/src/tool-output-compressor.ts +3 -1
- package/src/ui-server.ts +2 -1
- package/src/usage-tracker.ts +242 -0
- package/src/version-manager.ts +3 -2
- package/src/workflow-observation-engine.ts +2 -1
- package/src/workflow-observation-schema.ts +2 -1
- package/src/workflow-observation-store.ts +6 -2
- package/.env +0 -4
- package/CLAUDE.md +0 -46
- package/config.json +0 -55
- package/daily-recall.sh +0 -110
- package/logs/daily-recall.error.log +0 -0
- package/logs/daily-recall.log +0 -30
- package/logs/ingest-2026-04-11.log +0 -110
- package/logs/ingest-2026-04-12.log +0 -90
- package/logs/launchagent-stderr.log +0 -0
- package/logs/launchagent-stdout.log +0 -0
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
microcompact,
|
|
5
|
+
summarizeSession,
|
|
6
|
+
extractAndPersist,
|
|
7
|
+
distillSession,
|
|
8
|
+
type ConversationMessage,
|
|
9
|
+
type SummaryResult,
|
|
10
|
+
} from "../session-distiller.js";
|
|
11
|
+
|
|
12
|
+
// ============================================================================
|
|
13
|
+
// Helpers
|
|
14
|
+
// ============================================================================
|
|
15
|
+
|
|
16
|
+
function msg(role: ConversationMessage["role"], content: string, tool_name?: string): ConversationMessage {
|
|
17
|
+
return { role, content, tool_name };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function toolMsg(name: string, content: string): ConversationMessage {
|
|
21
|
+
return { role: "tool", content, tool_name: name };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function createMockLLM(response: string | null = null) {
|
|
25
|
+
return {
|
|
26
|
+
chatLong: async (_system: string, _user: string, _maxTokens?: number) => response,
|
|
27
|
+
breaker: { canAttempt: () => true, recordSuccess: () => {}, recordFailure: () => {}, getState: () => "closed" as const, getFailureCount: () => 0 },
|
|
28
|
+
} as ReturnType<typeof createMockLLM> & { chatLong: (s: string, u: string, m?: number) => Promise<string | null> };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function createMockDeps() {
|
|
32
|
+
const stored: Array<Record<string, unknown>> = [];
|
|
33
|
+
let seq = 1;
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
stored,
|
|
37
|
+
deps: {
|
|
38
|
+
store: {
|
|
39
|
+
async store(entry: Record<string, unknown>) {
|
|
40
|
+
const record = {
|
|
41
|
+
...entry,
|
|
42
|
+
id: `distill-${String(seq).padStart(8, "0")}`,
|
|
43
|
+
timestamp: 1_700_000_000_000 + seq,
|
|
44
|
+
};
|
|
45
|
+
seq++;
|
|
46
|
+
stored.push(record);
|
|
47
|
+
return record;
|
|
48
|
+
},
|
|
49
|
+
async list() { return stored; },
|
|
50
|
+
async update(id: string, updates: Record<string, unknown>) {
|
|
51
|
+
const idx = stored.findIndex((e) => e.id === id);
|
|
52
|
+
if (idx < 0) return null;
|
|
53
|
+
stored[idx] = { ...stored[idx], ...updates };
|
|
54
|
+
return stored[idx];
|
|
55
|
+
},
|
|
56
|
+
async getById(id: string) {
|
|
57
|
+
return stored.find((e) => e.id === id) || null;
|
|
58
|
+
},
|
|
59
|
+
async get(id: string) {
|
|
60
|
+
return stored.find((e) => e.id === id) || null;
|
|
61
|
+
},
|
|
62
|
+
async vectorSearch() { return []; },
|
|
63
|
+
},
|
|
64
|
+
embedder: {
|
|
65
|
+
async embedPassage(_text: string) { return [0.1, 0.2, 0.3]; },
|
|
66
|
+
},
|
|
67
|
+
conflictStore: {
|
|
68
|
+
async save(record: Record<string, unknown>) { return record; },
|
|
69
|
+
async replace(record: Record<string, unknown>) { return record; },
|
|
70
|
+
async getOpenByFingerprint() { return null; },
|
|
71
|
+
async getLatestByFingerprint() { return null; },
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ============================================================================
|
|
78
|
+
// Layer 1: microcompact
|
|
79
|
+
// ============================================================================
|
|
80
|
+
|
|
81
|
+
describe("microcompact", () => {
|
|
82
|
+
it("returns empty for empty input", () => {
|
|
83
|
+
const { messages, result } = microcompact([]);
|
|
84
|
+
expect(messages).toEqual([]);
|
|
85
|
+
expect(result.tokens_freed).toBe(0);
|
|
86
|
+
expect(result.tools_cleared).toBe(0);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("preserves all messages when count <= preserveRecent", () => {
|
|
90
|
+
const input = [
|
|
91
|
+
msg("user", "hello"),
|
|
92
|
+
msg("assistant", "hi there"),
|
|
93
|
+
msg("user", "bye"),
|
|
94
|
+
];
|
|
95
|
+
const { messages, result } = microcompact(input, { preserveRecent: 6 });
|
|
96
|
+
expect(messages).toHaveLength(3);
|
|
97
|
+
expect(result.tools_cleared).toBe(0);
|
|
98
|
+
expect(messages[0].content).toBe("hello");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("clears old tool outputs beyond the preserve window", () => {
|
|
102
|
+
const longContent = "x".repeat(400);
|
|
103
|
+
const input = [
|
|
104
|
+
msg("user", "start"),
|
|
105
|
+
toolMsg("Read", longContent),
|
|
106
|
+
toolMsg("Bash", longContent),
|
|
107
|
+
toolMsg("Grep", longContent),
|
|
108
|
+
msg("assistant", "found it"),
|
|
109
|
+
msg("user", "ok"),
|
|
110
|
+
msg("assistant", "done"),
|
|
111
|
+
msg("user", "next"),
|
|
112
|
+
msg("assistant", "working"),
|
|
113
|
+
msg("user", "final"),
|
|
114
|
+
msg("assistant", "complete"),
|
|
115
|
+
];
|
|
116
|
+
// preserveRecent=4 means last 4 messages are safe (indices 7-10)
|
|
117
|
+
// keepRecentTools=1 means only 1 most recent tool result is kept
|
|
118
|
+
const { messages, result } = microcompact(input, {
|
|
119
|
+
preserveRecent: 4,
|
|
120
|
+
keepRecentTools: 1,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
expect(messages).toHaveLength(11);
|
|
124
|
+
// First two tool messages should be cleared, third kept (most recent)
|
|
125
|
+
expect(messages[1].content).toContain("[Cleared: Read");
|
|
126
|
+
expect(messages[2].content).toContain("[Cleared: Bash");
|
|
127
|
+
expect(messages[3].content).toBe(longContent); // Grep kept as most recent tool
|
|
128
|
+
expect(result.tools_cleared).toBe(2);
|
|
129
|
+
expect(result.tokens_freed).toBeGreaterThan(0);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("does not clear non-clearable tool names", () => {
|
|
133
|
+
const input = [
|
|
134
|
+
msg("user", "start"),
|
|
135
|
+
toolMsg("custom_tool", "result here"),
|
|
136
|
+
msg("assistant", "got it"),
|
|
137
|
+
msg("user", "a"),
|
|
138
|
+
msg("assistant", "b"),
|
|
139
|
+
msg("user", "c"),
|
|
140
|
+
msg("assistant", "d"),
|
|
141
|
+
msg("user", "e"),
|
|
142
|
+
msg("assistant", "f"),
|
|
143
|
+
];
|
|
144
|
+
const { messages, result } = microcompact(input, { preserveRecent: 2 });
|
|
145
|
+
expect(result.tools_cleared).toBe(0);
|
|
146
|
+
expect(messages[1].content).toBe("result here");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("estimates token count as content.length / 4", () => {
|
|
150
|
+
const content = "a".repeat(100); // 100 chars = 25 tokens
|
|
151
|
+
const input = [
|
|
152
|
+
toolMsg("Read", content),
|
|
153
|
+
msg("user", "a"),
|
|
154
|
+
msg("assistant", "b"),
|
|
155
|
+
msg("user", "c"),
|
|
156
|
+
msg("assistant", "d"),
|
|
157
|
+
msg("user", "e"),
|
|
158
|
+
msg("assistant", "f"),
|
|
159
|
+
];
|
|
160
|
+
const { messages, result } = microcompact(input, { preserveRecent: 6, keepRecentTools: 0 });
|
|
161
|
+
expect(result.tokens_freed).toBe(25);
|
|
162
|
+
expect(result.tools_cleared).toBe(1);
|
|
163
|
+
expect(messages[0].content).toContain("~25 tokens");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("keeps all clearable tools when keepRecentTools >= tool count", () => {
|
|
167
|
+
const input = [
|
|
168
|
+
toolMsg("Read", "file content"),
|
|
169
|
+
toolMsg("Bash", "command output"),
|
|
170
|
+
msg("user", "a"),
|
|
171
|
+
msg("assistant", "b"),
|
|
172
|
+
msg("user", "c"),
|
|
173
|
+
msg("assistant", "d"),
|
|
174
|
+
msg("user", "e"),
|
|
175
|
+
msg("assistant", "f"),
|
|
176
|
+
msg("user", "g"),
|
|
177
|
+
];
|
|
178
|
+
const { result } = microcompact(input, { preserveRecent: 2, keepRecentTools: 5 });
|
|
179
|
+
expect(result.tools_cleared).toBe(0);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("preserves tool_name and other fields on cleared messages", () => {
|
|
183
|
+
const input = [
|
|
184
|
+
{ role: "tool" as const, content: "long output", tool_name: "Read", timestamp: "2024-01-01" },
|
|
185
|
+
msg("user", "a"),
|
|
186
|
+
msg("assistant", "b"),
|
|
187
|
+
msg("user", "c"),
|
|
188
|
+
msg("assistant", "d"),
|
|
189
|
+
msg("user", "e"),
|
|
190
|
+
msg("assistant", "f"),
|
|
191
|
+
];
|
|
192
|
+
const { messages } = microcompact(input, { preserveRecent: 6, keepRecentTools: 0 });
|
|
193
|
+
expect(messages[0].tool_name).toBe("Read");
|
|
194
|
+
expect(messages[0].timestamp).toBe("2024-01-01");
|
|
195
|
+
expect(messages[0].content).toContain("[Cleared: Read");
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("handles all clearable tool names", () => {
|
|
199
|
+
const clearableTools = [
|
|
200
|
+
"read_file", "bash", "grep", "glob", "web_search", "web_fetch",
|
|
201
|
+
"edit_file", "write_file", "Read", "Write", "Edit", "Bash",
|
|
202
|
+
"Grep", "Glob", "WebSearch", "WebFetch",
|
|
203
|
+
];
|
|
204
|
+
const filler: ConversationMessage[] = [];
|
|
205
|
+
for (let i = 0; i < 20; i++) {
|
|
206
|
+
filler.push(msg("user", `msg ${i}`));
|
|
207
|
+
}
|
|
208
|
+
const input = [
|
|
209
|
+
...clearableTools.map((t) => toolMsg(t, "output")),
|
|
210
|
+
...filler,
|
|
211
|
+
];
|
|
212
|
+
const { result } = microcompact(input, { preserveRecent: 10, keepRecentTools: 0 });
|
|
213
|
+
expect(result.tools_cleared).toBe(clearableTools.length);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("uses default values when opts not provided", () => {
|
|
217
|
+
const input: ConversationMessage[] = [];
|
|
218
|
+
for (let i = 0; i < 10; i++) {
|
|
219
|
+
input.push(toolMsg("Read", "content"));
|
|
220
|
+
input.push(msg("user", `q${i}`));
|
|
221
|
+
}
|
|
222
|
+
// Default: preserveRecent=6, keepRecentTools=5
|
|
223
|
+
const { result } = microcompact(input);
|
|
224
|
+
// Last 6 messages are preserved, so tools in first 14 are candidates
|
|
225
|
+
// 7 tools are in the clearable zone, keepRecentTools=5 means 2 cleared
|
|
226
|
+
expect(result.tools_cleared).toBe(2);
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
// ============================================================================
|
|
231
|
+
// Layer 2: summarizeSession
|
|
232
|
+
// ============================================================================
|
|
233
|
+
|
|
234
|
+
describe("summarizeSession", () => {
|
|
235
|
+
it("returns empty result when LLM returns null", async () => {
|
|
236
|
+
const mockLLM = createMockLLM(null);
|
|
237
|
+
const result = await summarizeSession(
|
|
238
|
+
[msg("user", "hello")],
|
|
239
|
+
mockLLM as unknown as import("../llm-client.js").LLMClient,
|
|
240
|
+
);
|
|
241
|
+
expect(result.text).toBe("");
|
|
242
|
+
expect(Object.keys(result.dimensions)).toHaveLength(0);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("parses structured summary from LLM response", async () => {
|
|
246
|
+
const response = `<analysis>Thinking about the session...</analysis>
|
|
247
|
+
<summary>
|
|
248
|
+
## 1. User intent and requests
|
|
249
|
+
Wanted to build a feature
|
|
250
|
+
|
|
251
|
+
## 2. Key technical concepts
|
|
252
|
+
TypeScript, MCP protocol
|
|
253
|
+
|
|
254
|
+
## 3. Files and code segments involved
|
|
255
|
+
src/main.ts, src/utils.ts
|
|
256
|
+
|
|
257
|
+
## 4. Errors and fix records
|
|
258
|
+
N/A
|
|
259
|
+
|
|
260
|
+
## 5. Problem solving process
|
|
261
|
+
Iterative approach with testing
|
|
262
|
+
|
|
263
|
+
## 6. User original quotes preserved
|
|
264
|
+
"Make it simple and clean"
|
|
265
|
+
|
|
266
|
+
## 7. Unfinished tasks
|
|
267
|
+
Unit tests pending
|
|
268
|
+
|
|
269
|
+
## 8. Current work state
|
|
270
|
+
Implementation 80% complete
|
|
271
|
+
|
|
272
|
+
## 9. Suggested next steps
|
|
273
|
+
Write tests, review code
|
|
274
|
+
</summary>`;
|
|
275
|
+
const mockLLM = createMockLLM(response);
|
|
276
|
+
const result = await summarizeSession(
|
|
277
|
+
[msg("user", "build feature"), msg("assistant", "ok")],
|
|
278
|
+
mockLLM as unknown as import("../llm-client.js").LLMClient,
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
expect(result.text).toContain("User intent");
|
|
282
|
+
expect(result.dimensions.user_intent).toContain("build a feature");
|
|
283
|
+
expect(result.dimensions.technical_concepts).toContain("TypeScript");
|
|
284
|
+
expect(result.dimensions.files_and_code).toContain("src/main.ts");
|
|
285
|
+
expect(result.dimensions.errors_and_fixes).toBeUndefined(); // N/A is excluded
|
|
286
|
+
expect(result.dimensions.problem_solving).toContain("Iterative");
|
|
287
|
+
expect(result.dimensions.user_quotes).toContain("simple and clean");
|
|
288
|
+
expect(result.dimensions.unfinished_tasks).toContain("Unit tests");
|
|
289
|
+
expect(result.dimensions.current_state).toContain("80%");
|
|
290
|
+
expect(result.dimensions.next_steps).toContain("Write tests");
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("handles response without XML tags gracefully", async () => {
|
|
294
|
+
const response = `## 1. User intent and requests
|
|
295
|
+
Debug a crash
|
|
296
|
+
|
|
297
|
+
## 2. Key technical concepts
|
|
298
|
+
Memory management`;
|
|
299
|
+
const mockLLM = createMockLLM(response);
|
|
300
|
+
const result = await summarizeSession(
|
|
301
|
+
[msg("user", "help")],
|
|
302
|
+
mockLLM as unknown as import("../llm-client.js").LLMClient,
|
|
303
|
+
);
|
|
304
|
+
expect(result.text).toContain("User intent");
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("truncates long messages to avoid token overflow", async () => {
|
|
308
|
+
let capturedUser = "";
|
|
309
|
+
const mockLLM = {
|
|
310
|
+
chatLong: async (_system: string, user: string) => {
|
|
311
|
+
capturedUser = user;
|
|
312
|
+
return "<analysis>ok</analysis><summary>short</summary>";
|
|
313
|
+
},
|
|
314
|
+
};
|
|
315
|
+
const longMsg = msg("user", "x".repeat(10000));
|
|
316
|
+
await summarizeSession(
|
|
317
|
+
[longMsg],
|
|
318
|
+
mockLLM as unknown as import("../llm-client.js").LLMClient,
|
|
319
|
+
);
|
|
320
|
+
// Each message content is capped at 500 chars, and total at 8000
|
|
321
|
+
expect(capturedUser.length).toBeLessThanOrEqual(8000);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// ============================================================================
|
|
326
|
+
// Layer 3: extractAndPersist
|
|
327
|
+
// ============================================================================
|
|
328
|
+
|
|
329
|
+
describe("extractAndPersist", () => {
|
|
330
|
+
it("persists memories for relevant dimensions", async () => {
|
|
331
|
+
const { stored, deps } = createMockDeps();
|
|
332
|
+
const summary: SummaryResult = {
|
|
333
|
+
text: "full summary text",
|
|
334
|
+
dimensions: {
|
|
335
|
+
user_intent: "User wanted to implement a session distiller feature",
|
|
336
|
+
files_and_code: "src/session-distiller.ts, src/mcp-server.ts",
|
|
337
|
+
errors_and_fixes: "Fixed import error in llm-client.ts by adding chatLong method",
|
|
338
|
+
problem_solving: "Iterative approach: read code first, then implement layer by layer",
|
|
339
|
+
user_quotes: "User said: prefer clean code over clever code",
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const result = await extractAndPersist(summary, deps as Parameters<typeof extractAndPersist>[1], "project:test");
|
|
344
|
+
expect(result.memories_stored).toBe(5);
|
|
345
|
+
expect(result.ids).toHaveLength(5);
|
|
346
|
+
|
|
347
|
+
// Verify categories
|
|
348
|
+
const categories = stored.map((s) => s.category);
|
|
349
|
+
expect(categories).toContain("events");
|
|
350
|
+
expect(categories).toContain("entities");
|
|
351
|
+
expect(categories).toContain("cases");
|
|
352
|
+
expect(categories).toContain("patterns");
|
|
353
|
+
expect(categories).toContain("preferences");
|
|
354
|
+
|
|
355
|
+
// Verify session_distill tag is in metadata
|
|
356
|
+
for (const entry of stored) {
|
|
357
|
+
const metadata = typeof entry.metadata === "string" ? entry.metadata : JSON.stringify(entry.metadata);
|
|
358
|
+
expect(metadata).toContain("session_distill");
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it("skips dimensions with too-short content", async () => {
|
|
363
|
+
const { deps } = createMockDeps();
|
|
364
|
+
const summary: SummaryResult = {
|
|
365
|
+
text: "summary",
|
|
366
|
+
dimensions: {
|
|
367
|
+
user_intent: "short", // < 10 chars
|
|
368
|
+
errors_and_fixes: "Found and fixed a critical bug in the parser module",
|
|
369
|
+
},
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
const result = await extractAndPersist(summary, deps as Parameters<typeof extractAndPersist>[1], "project:test");
|
|
373
|
+
expect(result.memories_stored).toBe(1);
|
|
374
|
+
expect(result.ids).toHaveLength(1);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it("skips dimensions not in DIMENSION_TO_MEMORY mapping", async () => {
|
|
378
|
+
// Note: 2026-05-13 P1.2 added unfinished_tasks + next_steps to DIMENSION_TO_MEMORY.
|
|
379
|
+
// Remaining unmapped dims from DEFAULT_DIMENSION_LABELS: technical_concepts, current_state.
|
|
380
|
+
const { deps } = createMockDeps();
|
|
381
|
+
const summary: SummaryResult = {
|
|
382
|
+
text: "summary",
|
|
383
|
+
dimensions: {
|
|
384
|
+
technical_concepts: "TypeScript and Bun runtime details",
|
|
385
|
+
current_state: "Implementation complete, testing phase",
|
|
386
|
+
},
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const result = await extractAndPersist(summary, deps as Parameters<typeof extractAndPersist>[1], "project:test");
|
|
390
|
+
expect(result.memories_stored).toBe(0);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("persists unfinished_tasks (P1.2 regression 2026-05-13: dim added to DIMENSION_TO_MEMORY)", async () => {
|
|
394
|
+
const { deps } = createMockDeps();
|
|
395
|
+
const summary: SummaryResult = {
|
|
396
|
+
text: "summary",
|
|
397
|
+
dimensions: {
|
|
398
|
+
unfinished_tasks: "TODO: integrate Sentinel-Core monitoring for aws-podcast bot",
|
|
399
|
+
},
|
|
400
|
+
};
|
|
401
|
+
const result = await extractAndPersist(summary, deps as Parameters<typeof extractAndPersist>[1], "project:p1.2-unfinished");
|
|
402
|
+
// Before P1.2 fix: this dim was not in DIMENSION_TO_MEMORY → silently dropped → memories_stored=0
|
|
403
|
+
// After fix: dim mapped to cases category → persisted → memories_stored=1
|
|
404
|
+
expect(result.memories_stored).toBe(1);
|
|
405
|
+
expect(result.ids).toHaveLength(1);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it("persists next_steps (P1.2 regression 2026-05-13: dim added to DIMENSION_TO_MEMORY)", async () => {
|
|
409
|
+
const { deps } = createMockDeps();
|
|
410
|
+
const summary: SummaryResult = {
|
|
411
|
+
text: "summary",
|
|
412
|
+
dimensions: {
|
|
413
|
+
next_steps: "Next: review CodexCLI image_in workflow + write antibot Sentinel-Core spec",
|
|
414
|
+
},
|
|
415
|
+
};
|
|
416
|
+
const result = await extractAndPersist(summary, deps as Parameters<typeof extractAndPersist>[1], "project:p1.2-nextsteps");
|
|
417
|
+
expect(result.memories_stored).toBe(1);
|
|
418
|
+
expect(result.ids).toHaveLength(1);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it("returns empty result for empty dimensions", async () => {
|
|
422
|
+
const { deps } = createMockDeps();
|
|
423
|
+
const summary: SummaryResult = { text: "", dimensions: {} };
|
|
424
|
+
|
|
425
|
+
const result = await extractAndPersist(summary, deps as Parameters<typeof extractAndPersist>[1], "project:test");
|
|
426
|
+
expect(result.memories_stored).toBe(0);
|
|
427
|
+
expect(result.memories_deduped).toBe(0);
|
|
428
|
+
expect(result.memories_conflicted).toBe(0);
|
|
429
|
+
expect(result.ids).toHaveLength(0);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it("tracks disposition correctly for multiple dimensions", async () => {
|
|
433
|
+
const { deps } = createMockDeps();
|
|
434
|
+
const summary: SummaryResult = {
|
|
435
|
+
text: "summary",
|
|
436
|
+
dimensions: {
|
|
437
|
+
user_intent: "User wanted to build a distiller for sessions",
|
|
438
|
+
errors_and_fixes: "Fixed a critical error in the LLM client module",
|
|
439
|
+
},
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
const result = await extractAndPersist(summary, deps as Parameters<typeof extractAndPersist>[1], "project:test");
|
|
443
|
+
// Both should store successfully (no dedup in mock since vectorSearch returns [])
|
|
444
|
+
expect(result.memories_stored).toBe(2);
|
|
445
|
+
expect(result.ids).toHaveLength(2);
|
|
446
|
+
expect(result.memories_deduped).toBe(0);
|
|
447
|
+
expect(result.memories_conflicted).toBe(0);
|
|
448
|
+
});
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
// ============================================================================
|
|
452
|
+
// Full Pipeline: distillSession
|
|
453
|
+
// ============================================================================
|
|
454
|
+
|
|
455
|
+
describe("distillSession", () => {
|
|
456
|
+
it("runs all three layers end-to-end", async () => {
|
|
457
|
+
const { deps } = createMockDeps();
|
|
458
|
+
const llmResponse = `<analysis>Processing</analysis>
|
|
459
|
+
<summary>
|
|
460
|
+
## 1. User intent and requests
|
|
461
|
+
Implement session distiller
|
|
462
|
+
|
|
463
|
+
## 2. Key technical concepts
|
|
464
|
+
N/A
|
|
465
|
+
|
|
466
|
+
## 3. Files and code segments involved
|
|
467
|
+
src/session-distiller.ts
|
|
468
|
+
|
|
469
|
+
## 4. Errors and fix records
|
|
470
|
+
N/A
|
|
471
|
+
|
|
472
|
+
## 5. Problem solving process
|
|
473
|
+
Step by step implementation
|
|
474
|
+
|
|
475
|
+
## 6. User original quotes preserved
|
|
476
|
+
N/A
|
|
477
|
+
|
|
478
|
+
## 7. Unfinished tasks
|
|
479
|
+
N/A
|
|
480
|
+
|
|
481
|
+
## 8. Current work state
|
|
482
|
+
N/A
|
|
483
|
+
|
|
484
|
+
## 9. Suggested next steps
|
|
485
|
+
N/A
|
|
486
|
+
</summary>`;
|
|
487
|
+
|
|
488
|
+
const mockLLM = createMockLLM(llmResponse);
|
|
489
|
+
const input: ConversationMessage[] = [
|
|
490
|
+
msg("user", "implement distiller"),
|
|
491
|
+
toolMsg("Read", "long file content ".repeat(100)),
|
|
492
|
+
toolMsg("Bash", "test output ".repeat(100)),
|
|
493
|
+
msg("assistant", "done reading"),
|
|
494
|
+
msg("user", "proceed"),
|
|
495
|
+
msg("assistant", "implementing"),
|
|
496
|
+
msg("user", "looks good"),
|
|
497
|
+
msg("assistant", "finished"),
|
|
498
|
+
msg("user", "test it"),
|
|
499
|
+
msg("assistant", "all pass"),
|
|
500
|
+
];
|
|
501
|
+
|
|
502
|
+
const result = await distillSession(
|
|
503
|
+
input,
|
|
504
|
+
{ ...deps, llm: mockLLM } as Parameters<typeof distillSession>[1],
|
|
505
|
+
{ scope: "project:test", preserveRecent: 4, keepRecentTools: 0 },
|
|
506
|
+
);
|
|
507
|
+
|
|
508
|
+
// Layer 1
|
|
509
|
+
expect(result.microcompact.tools_cleared).toBe(2);
|
|
510
|
+
expect(result.microcompact.tokens_freed).toBeGreaterThan(0);
|
|
511
|
+
|
|
512
|
+
// Layer 2
|
|
513
|
+
expect(result.summary.dimensions.user_intent).toContain("session distiller");
|
|
514
|
+
|
|
515
|
+
// Layer 3
|
|
516
|
+
expect(result.persisted.memories_stored).toBeGreaterThan(0);
|
|
517
|
+
|
|
518
|
+
// Compacted messages
|
|
519
|
+
expect(result.compacted_messages).toHaveLength(10);
|
|
520
|
+
expect(result.compacted_messages[1].content).toContain("[Cleared:");
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
it("skips Layer 3 when persist=false", async () => {
|
|
524
|
+
const { deps } = createMockDeps();
|
|
525
|
+
const mockLLM = createMockLLM(
|
|
526
|
+
"<analysis>ok</analysis><summary>## 1. User intent and requests\nBuild something cool</summary>",
|
|
527
|
+
);
|
|
528
|
+
|
|
529
|
+
const input = [
|
|
530
|
+
msg("user", "hello"),
|
|
531
|
+
msg("assistant", "hi"),
|
|
532
|
+
msg("user", "build it"),
|
|
533
|
+
msg("assistant", "ok"),
|
|
534
|
+
msg("user", "done?"),
|
|
535
|
+
msg("assistant", "yes"),
|
|
536
|
+
msg("user", "great"),
|
|
537
|
+
msg("assistant", "cheers"),
|
|
538
|
+
];
|
|
539
|
+
|
|
540
|
+
const result = await distillSession(
|
|
541
|
+
input,
|
|
542
|
+
{ ...deps, llm: mockLLM } as Parameters<typeof distillSession>[1],
|
|
543
|
+
{ persist: false, preserveRecent: 2 },
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
expect(result.persisted.memories_stored).toBe(0);
|
|
547
|
+
expect(result.persisted.ids).toHaveLength(0);
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
it("handles empty conversation gracefully", async () => {
|
|
551
|
+
const { deps } = createMockDeps();
|
|
552
|
+
const mockLLM = createMockLLM(null);
|
|
553
|
+
|
|
554
|
+
const result = await distillSession(
|
|
555
|
+
[],
|
|
556
|
+
{ ...deps, llm: mockLLM } as Parameters<typeof distillSession>[1],
|
|
557
|
+
);
|
|
558
|
+
|
|
559
|
+
expect(result.microcompact.tools_cleared).toBe(0);
|
|
560
|
+
expect(result.summary.text).toBe("");
|
|
561
|
+
expect(result.persisted.memories_stored).toBe(0);
|
|
562
|
+
expect(result.compacted_messages).toHaveLength(0);
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it("uses default scope when not provided", async () => {
|
|
566
|
+
const { deps, stored } = createMockDeps();
|
|
567
|
+
const mockLLM = createMockLLM(
|
|
568
|
+
"<analysis>ok</analysis><summary>## 1. User intent and requests\nBuild a feature for the application</summary>",
|
|
569
|
+
);
|
|
570
|
+
|
|
571
|
+
const input = [
|
|
572
|
+
msg("user", "hello"),
|
|
573
|
+
msg("assistant", "hi"),
|
|
574
|
+
msg("user", "a"),
|
|
575
|
+
msg("assistant", "b"),
|
|
576
|
+
msg("user", "c"),
|
|
577
|
+
msg("assistant", "d"),
|
|
578
|
+
msg("user", "e"),
|
|
579
|
+
msg("assistant", "f"),
|
|
580
|
+
];
|
|
581
|
+
|
|
582
|
+
await distillSession(
|
|
583
|
+
input,
|
|
584
|
+
{ ...deps, llm: mockLLM } as Parameters<typeof distillSession>[1],
|
|
585
|
+
);
|
|
586
|
+
|
|
587
|
+
// Should use "default" scope
|
|
588
|
+
if (stored.length > 0) {
|
|
589
|
+
expect(stored[0].scope).toBe("default");
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
it("only summarizes old messages (before preserve window)", async () => {
|
|
594
|
+
let capturedUser = "";
|
|
595
|
+
const mockLLM = {
|
|
596
|
+
chatLong: async (_system: string, user: string) => {
|
|
597
|
+
capturedUser = user;
|
|
598
|
+
return "<analysis>ok</analysis><summary>## 1. User intent and requests\nSomething from the old messages only</summary>";
|
|
599
|
+
},
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
const { deps } = createMockDeps();
|
|
603
|
+
const input = [
|
|
604
|
+
msg("user", "old message 1"),
|
|
605
|
+
msg("assistant", "old response 1"),
|
|
606
|
+
msg("user", "recent 1"),
|
|
607
|
+
msg("assistant", "recent 2"),
|
|
608
|
+
msg("user", "recent 3"),
|
|
609
|
+
msg("assistant", "recent 4"),
|
|
610
|
+
];
|
|
611
|
+
|
|
612
|
+
await distillSession(
|
|
613
|
+
input,
|
|
614
|
+
{ ...deps, llm: mockLLM } as Parameters<typeof distillSession>[1],
|
|
615
|
+
{ preserveRecent: 4 },
|
|
616
|
+
);
|
|
617
|
+
|
|
618
|
+
expect(capturedUser).toContain("old message 1");
|
|
619
|
+
expect(capturedUser).not.toContain("recent 1");
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
it("skips Layer 2/3 when all messages are in preserve window", async () => {
|
|
623
|
+
const { deps } = createMockDeps();
|
|
624
|
+
const mockLLM = createMockLLM("should not be called");
|
|
625
|
+
|
|
626
|
+
const input = [
|
|
627
|
+
msg("user", "hello"),
|
|
628
|
+
msg("assistant", "hi"),
|
|
629
|
+
];
|
|
630
|
+
|
|
631
|
+
const result = await distillSession(
|
|
632
|
+
input,
|
|
633
|
+
{ ...deps, llm: mockLLM } as Parameters<typeof distillSession>[1],
|
|
634
|
+
{ preserveRecent: 6 },
|
|
635
|
+
);
|
|
636
|
+
|
|
637
|
+
expect(result.summary.text).toBe("");
|
|
638
|
+
expect(result.persisted.memories_stored).toBe(0);
|
|
639
|
+
});
|
|
640
|
+
});
|