@gethmy/mcp 2.2.4 → 2.3.1
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/dist/cli.js +637 -335
- package/dist/index.js +637 -335
- package/dist/lib/active-learning.js +73 -129
- package/dist/lib/api-client.js +6 -0
- package/dist/lib/consolidation.js +71 -11
- package/dist/lib/context-assembly.js +69 -4
- package/dist/lib/memory-cleanup.js +455 -0
- package/dist/lib/prompt-builder.js +5 -1
- package/dist/lib/server.js +77 -0
- package/package.json +1 -1
- package/src/active-learning.ts +83 -145
- package/src/api-client.ts +37 -1
- package/src/consolidation.ts +81 -12
- package/src/context-assembly.ts +75 -4
- package/src/memory-cleanup.ts +658 -0
- package/src/prompt-builder.ts +13 -1
- package/src/server.ts +89 -0
|
@@ -0,0 +1,658 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified Memory Cleanup
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates a 5-stage cleanup pipeline: prune stale drafts, consolidate
|
|
5
|
+
* similar memories, detect orphans, detect duplicates, and backfill embeddings.
|
|
6
|
+
*
|
|
7
|
+
* All stages are non-fatal — individual failures are collected but never block
|
|
8
|
+
* the remaining stages. Defaults to dry-run mode (preview only).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { evaluateLifecycle } from "@harmony/memory";
|
|
12
|
+
import type { HarmonyApiClient } from "./api-client.js";
|
|
13
|
+
import {
|
|
14
|
+
type ConsolidationResult,
|
|
15
|
+
consolidateMemories,
|
|
16
|
+
} from "./consolidation.js";
|
|
17
|
+
import { findSimilarEntities } from "./graph-expansion.js";
|
|
18
|
+
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Types
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
interface MemoryEntity {
|
|
24
|
+
id: string;
|
|
25
|
+
type: string;
|
|
26
|
+
title: string;
|
|
27
|
+
content: string;
|
|
28
|
+
confidence: number;
|
|
29
|
+
memory_tier: "draft" | "episode" | "reference";
|
|
30
|
+
access_count: number;
|
|
31
|
+
last_accessed_at: string | null;
|
|
32
|
+
created_at: string;
|
|
33
|
+
updated_at?: string;
|
|
34
|
+
tags?: string[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type CleanupStep =
|
|
38
|
+
| "prune"
|
|
39
|
+
| "consolidate"
|
|
40
|
+
| "orphans"
|
|
41
|
+
| "duplicates"
|
|
42
|
+
| "backfill";
|
|
43
|
+
|
|
44
|
+
export interface CleanupOptions {
|
|
45
|
+
dryRun?: boolean;
|
|
46
|
+
steps?: CleanupStep[];
|
|
47
|
+
maxAgeDays?: number;
|
|
48
|
+
minClusterSize?: number;
|
|
49
|
+
orphanAgeDays?: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface PruneStepResult {
|
|
53
|
+
staleDraftsFound: number;
|
|
54
|
+
pruned: number;
|
|
55
|
+
items: Array<{
|
|
56
|
+
id: string;
|
|
57
|
+
title: string;
|
|
58
|
+
ageDays: number;
|
|
59
|
+
decayScore: number;
|
|
60
|
+
}>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface ConsolidateStepResult {
|
|
64
|
+
clustersFound: number;
|
|
65
|
+
entitiesProcessed: number;
|
|
66
|
+
consolidated: number;
|
|
67
|
+
details: ConsolidationResult["details"];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface OrphanStepResult {
|
|
71
|
+
orphansFound: number;
|
|
72
|
+
removed: number;
|
|
73
|
+
items: Array<{
|
|
74
|
+
id: string;
|
|
75
|
+
title: string;
|
|
76
|
+
type: string;
|
|
77
|
+
tier: string;
|
|
78
|
+
ageDays: number;
|
|
79
|
+
accessCount: number;
|
|
80
|
+
}>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface DuplicateStepResult {
|
|
84
|
+
duplicatePairsFound: number;
|
|
85
|
+
resolved: number;
|
|
86
|
+
pairs: Array<{
|
|
87
|
+
keepId: string;
|
|
88
|
+
keepTitle: string;
|
|
89
|
+
removeId: string;
|
|
90
|
+
removeTitle: string;
|
|
91
|
+
similarity: number;
|
|
92
|
+
}>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface BackfillStepResult {
|
|
96
|
+
processed: number;
|
|
97
|
+
remaining: number;
|
|
98
|
+
errors: Array<{ entity_id: string; error: string }>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface CleanupReport {
|
|
102
|
+
success: boolean;
|
|
103
|
+
dryRun: boolean;
|
|
104
|
+
timestamp: string;
|
|
105
|
+
workspace: { id: string; projectId?: string };
|
|
106
|
+
|
|
107
|
+
summary: {
|
|
108
|
+
totalEntities: number;
|
|
109
|
+
issuesFound: number;
|
|
110
|
+
actionsTaken: number;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
steps: {
|
|
114
|
+
prune?: PruneStepResult;
|
|
115
|
+
consolidate?: ConsolidateStepResult;
|
|
116
|
+
orphans?: OrphanStepResult;
|
|
117
|
+
duplicates?: DuplicateStepResult;
|
|
118
|
+
backfill?: BackfillStepResult;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
errors: Array<{ step: string; message: string }>;
|
|
122
|
+
healthReport: string;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const ALL_STEPS: CleanupStep[] = [
|
|
126
|
+
"prune",
|
|
127
|
+
"consolidate",
|
|
128
|
+
"orphans",
|
|
129
|
+
"duplicates",
|
|
130
|
+
"backfill",
|
|
131
|
+
];
|
|
132
|
+
|
|
133
|
+
const MS_PER_DAY = 1000 * 60 * 60 * 24;
|
|
134
|
+
const MAX_ENTITIES_FETCH = 200;
|
|
135
|
+
const DUPLICATE_SIMILARITY_THRESHOLD = 0.85;
|
|
136
|
+
const CONCURRENCY_LIMIT = 5;
|
|
137
|
+
|
|
138
|
+
// ---------------------------------------------------------------------------
|
|
139
|
+
// Main orchestrator
|
|
140
|
+
// ---------------------------------------------------------------------------
|
|
141
|
+
|
|
142
|
+
export async function runMemoryCleanup(
|
|
143
|
+
client: HarmonyApiClient,
|
|
144
|
+
workspaceId: string,
|
|
145
|
+
projectId?: string,
|
|
146
|
+
options?: CleanupOptions,
|
|
147
|
+
): Promise<CleanupReport> {
|
|
148
|
+
const dryRun = options?.dryRun !== false;
|
|
149
|
+
const steps = options?.steps ?? ALL_STEPS;
|
|
150
|
+
const maxAgeDays = options?.maxAgeDays ?? 30;
|
|
151
|
+
const minClusterSize = options?.minClusterSize ?? 3;
|
|
152
|
+
const orphanAgeDays = options?.orphanAgeDays ?? 14;
|
|
153
|
+
|
|
154
|
+
const report: CleanupReport = {
|
|
155
|
+
success: true,
|
|
156
|
+
dryRun,
|
|
157
|
+
timestamp: new Date().toISOString(),
|
|
158
|
+
workspace: { id: workspaceId, projectId },
|
|
159
|
+
summary: { totalEntities: 0, issuesFound: 0, actionsTaken: 0 },
|
|
160
|
+
steps: {},
|
|
161
|
+
errors: [],
|
|
162
|
+
healthReport: "",
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// Fetch all entities once (shared across steps)
|
|
166
|
+
let entities: MemoryEntity[] = [];
|
|
167
|
+
try {
|
|
168
|
+
const listResult = await client.listMemoryEntities({
|
|
169
|
+
workspace_id: workspaceId,
|
|
170
|
+
project_id: projectId,
|
|
171
|
+
limit: MAX_ENTITIES_FETCH,
|
|
172
|
+
});
|
|
173
|
+
entities = (listResult.entities || []) as MemoryEntity[];
|
|
174
|
+
report.summary.totalEntities = entities.length;
|
|
175
|
+
} catch (err) {
|
|
176
|
+
report.errors.push({
|
|
177
|
+
step: "init",
|
|
178
|
+
message: `Failed to fetch entities: ${(err as Error).message}`,
|
|
179
|
+
});
|
|
180
|
+
report.success = false;
|
|
181
|
+
report.healthReport = generateHealthReport(report);
|
|
182
|
+
return report;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Stage 1: Prune stale drafts
|
|
186
|
+
if (steps.includes("prune")) {
|
|
187
|
+
try {
|
|
188
|
+
report.steps.prune = runPruneStep(entities, maxAgeDays);
|
|
189
|
+
if (!dryRun) {
|
|
190
|
+
for (const item of report.steps.prune.items) {
|
|
191
|
+
try {
|
|
192
|
+
await client.deleteMemoryEntity(item.id);
|
|
193
|
+
report.steps.prune.pruned++;
|
|
194
|
+
} catch (err) {
|
|
195
|
+
report.errors.push({
|
|
196
|
+
step: "prune",
|
|
197
|
+
message: `Failed to delete ${item.id}: ${(err as Error).message}`,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
report.summary.actionsTaken += report.steps.prune.pruned;
|
|
202
|
+
}
|
|
203
|
+
report.summary.issuesFound += report.steps.prune.staleDraftsFound;
|
|
204
|
+
} catch (err) {
|
|
205
|
+
report.errors.push({
|
|
206
|
+
step: "prune",
|
|
207
|
+
message: (err as Error).message,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Stage 2: Consolidate similar memories
|
|
213
|
+
if (steps.includes("consolidate")) {
|
|
214
|
+
try {
|
|
215
|
+
const result = await consolidateMemories(client, workspaceId, projectId, {
|
|
216
|
+
dryRun,
|
|
217
|
+
minClusterSize,
|
|
218
|
+
});
|
|
219
|
+
report.steps.consolidate = {
|
|
220
|
+
clustersFound: result.clustersFound,
|
|
221
|
+
entitiesProcessed: result.entitiesProcessed,
|
|
222
|
+
consolidated: result.consolidated,
|
|
223
|
+
details: result.details,
|
|
224
|
+
};
|
|
225
|
+
report.summary.issuesFound += result.clustersFound;
|
|
226
|
+
if (!dryRun) report.summary.actionsTaken += result.consolidated;
|
|
227
|
+
} catch (err) {
|
|
228
|
+
report.errors.push({
|
|
229
|
+
step: "consolidate",
|
|
230
|
+
message: (err as Error).message,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Stage 3: Detect orphans
|
|
236
|
+
if (steps.includes("orphans")) {
|
|
237
|
+
try {
|
|
238
|
+
report.steps.orphans = await runOrphanStep(
|
|
239
|
+
client,
|
|
240
|
+
entities,
|
|
241
|
+
orphanAgeDays,
|
|
242
|
+
);
|
|
243
|
+
if (!dryRun) {
|
|
244
|
+
for (const item of report.steps.orphans.items) {
|
|
245
|
+
try {
|
|
246
|
+
await client.deleteMemoryEntity(item.id);
|
|
247
|
+
report.steps.orphans.removed++;
|
|
248
|
+
} catch (err) {
|
|
249
|
+
report.errors.push({
|
|
250
|
+
step: "orphans",
|
|
251
|
+
message: `Failed to delete ${item.id}: ${(err as Error).message}`,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
report.summary.actionsTaken += report.steps.orphans.removed;
|
|
256
|
+
}
|
|
257
|
+
report.summary.issuesFound += report.steps.orphans.orphansFound;
|
|
258
|
+
} catch (err) {
|
|
259
|
+
report.errors.push({
|
|
260
|
+
step: "orphans",
|
|
261
|
+
message: (err as Error).message,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Stage 4: Detect duplicates
|
|
267
|
+
if (steps.includes("duplicates")) {
|
|
268
|
+
try {
|
|
269
|
+
report.steps.duplicates = await runDuplicateStep(
|
|
270
|
+
client,
|
|
271
|
+
entities,
|
|
272
|
+
workspaceId,
|
|
273
|
+
projectId,
|
|
274
|
+
);
|
|
275
|
+
if (!dryRun) {
|
|
276
|
+
for (const pair of report.steps.duplicates.pairs) {
|
|
277
|
+
try {
|
|
278
|
+
await client.deleteMemoryEntity(pair.removeId);
|
|
279
|
+
report.steps.duplicates.resolved++;
|
|
280
|
+
} catch (err) {
|
|
281
|
+
report.errors.push({
|
|
282
|
+
step: "duplicates",
|
|
283
|
+
message: `Failed to delete ${pair.removeId}: ${(err as Error).message}`,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
report.summary.actionsTaken += report.steps.duplicates.resolved;
|
|
288
|
+
}
|
|
289
|
+
report.summary.issuesFound += report.steps.duplicates.duplicatePairsFound;
|
|
290
|
+
} catch (err) {
|
|
291
|
+
report.errors.push({
|
|
292
|
+
step: "duplicates",
|
|
293
|
+
message: (err as Error).message,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Stage 5: Backfill embeddings
|
|
299
|
+
if (steps.includes("backfill")) {
|
|
300
|
+
try {
|
|
301
|
+
if (dryRun) {
|
|
302
|
+
// In dry-run, just report that backfill would run
|
|
303
|
+
report.steps.backfill = {
|
|
304
|
+
processed: 0,
|
|
305
|
+
remaining: -1,
|
|
306
|
+
errors: [],
|
|
307
|
+
};
|
|
308
|
+
} else {
|
|
309
|
+
const result = await client.backfillEmbeddings(workspaceId);
|
|
310
|
+
report.steps.backfill = {
|
|
311
|
+
processed: result.processed,
|
|
312
|
+
remaining: result.remaining,
|
|
313
|
+
errors: result.errors || [],
|
|
314
|
+
};
|
|
315
|
+
report.summary.actionsTaken += result.processed;
|
|
316
|
+
}
|
|
317
|
+
} catch (err) {
|
|
318
|
+
report.errors.push({
|
|
319
|
+
step: "backfill",
|
|
320
|
+
message: (err as Error).message,
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
report.healthReport = generateHealthReport(report);
|
|
326
|
+
return report;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// ---------------------------------------------------------------------------
|
|
330
|
+
// Step implementations
|
|
331
|
+
// ---------------------------------------------------------------------------
|
|
332
|
+
|
|
333
|
+
function runPruneStep(
|
|
334
|
+
entities: MemoryEntity[],
|
|
335
|
+
maxAgeDays: number,
|
|
336
|
+
): PruneStepResult {
|
|
337
|
+
const now = Date.now();
|
|
338
|
+
const drafts = entities.filter((e) => e.memory_tier === "draft");
|
|
339
|
+
const stale: PruneStepResult["items"] = [];
|
|
340
|
+
|
|
341
|
+
for (const entity of drafts) {
|
|
342
|
+
const ageDays =
|
|
343
|
+
(now - new Date(entity.created_at).getTime()) / MS_PER_DAY;
|
|
344
|
+
if (ageDays < maxAgeDays) continue;
|
|
345
|
+
|
|
346
|
+
const lifecycle = evaluateLifecycle(entity);
|
|
347
|
+
stale.push({
|
|
348
|
+
id: entity.id,
|
|
349
|
+
title: entity.title,
|
|
350
|
+
ageDays: Math.round(ageDays),
|
|
351
|
+
decayScore: Math.round(lifecycle.decay.score * 100) / 100,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return { staleDraftsFound: stale.length, pruned: 0, items: stale };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
async function runOrphanStep(
|
|
359
|
+
client: HarmonyApiClient,
|
|
360
|
+
entities: MemoryEntity[],
|
|
361
|
+
orphanAgeDays: number,
|
|
362
|
+
): Promise<OrphanStepResult> {
|
|
363
|
+
const now = Date.now();
|
|
364
|
+
const result: OrphanStepResult = { orphansFound: 0, removed: 0, items: [] };
|
|
365
|
+
|
|
366
|
+
// Pre-filter: only check entities that look like orphan candidates
|
|
367
|
+
const candidates = entities.filter((e) => {
|
|
368
|
+
if (e.memory_tier === "reference") return false;
|
|
369
|
+
if (e.access_count >= 2) return false;
|
|
370
|
+
const ageDays =
|
|
371
|
+
(now - new Date(e.created_at).getTime()) / MS_PER_DAY;
|
|
372
|
+
return ageDays >= orphanAgeDays;
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// Check relations in concurrent batches
|
|
376
|
+
for (let i = 0; i < candidates.length; i += CONCURRENCY_LIMIT) {
|
|
377
|
+
const batch = candidates.slice(i, i + CONCURRENCY_LIMIT);
|
|
378
|
+
const results = await Promise.allSettled(
|
|
379
|
+
batch.map(async (entity) => {
|
|
380
|
+
const related = await client.getRelatedEntities(entity.id);
|
|
381
|
+
const totalRelations =
|
|
382
|
+
(related.outgoing?.length || 0) + (related.incoming?.length || 0);
|
|
383
|
+
if (totalRelations > 0) return null;
|
|
384
|
+
|
|
385
|
+
const ageDays =
|
|
386
|
+
(now - new Date(entity.created_at).getTime()) / MS_PER_DAY;
|
|
387
|
+
return {
|
|
388
|
+
id: entity.id,
|
|
389
|
+
title: entity.title,
|
|
390
|
+
type: entity.type,
|
|
391
|
+
tier: entity.memory_tier,
|
|
392
|
+
ageDays: Math.round(ageDays),
|
|
393
|
+
accessCount: entity.access_count,
|
|
394
|
+
};
|
|
395
|
+
}),
|
|
396
|
+
);
|
|
397
|
+
|
|
398
|
+
for (const r of results) {
|
|
399
|
+
if (r.status === "fulfilled" && r.value) {
|
|
400
|
+
result.items.push(r.value);
|
|
401
|
+
result.orphansFound++;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return result;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
async function runDuplicateStep(
|
|
410
|
+
client: HarmonyApiClient,
|
|
411
|
+
entities: MemoryEntity[],
|
|
412
|
+
workspaceId: string,
|
|
413
|
+
projectId?: string,
|
|
414
|
+
): Promise<DuplicateStepResult> {
|
|
415
|
+
const result: DuplicateStepResult = {
|
|
416
|
+
duplicatePairsFound: 0,
|
|
417
|
+
resolved: 0,
|
|
418
|
+
pairs: [],
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
const seenPairs = new Set<string>();
|
|
422
|
+
const flaggedForRemoval = new Set<string>();
|
|
423
|
+
const entityMap = new Map(entities.map((e) => [e.id, e]));
|
|
424
|
+
|
|
425
|
+
// Pre-fetch similarities in concurrent batches
|
|
426
|
+
type SimilarMatch = {
|
|
427
|
+
id: string;
|
|
428
|
+
type: string;
|
|
429
|
+
title: string;
|
|
430
|
+
content: string;
|
|
431
|
+
confidence: number;
|
|
432
|
+
};
|
|
433
|
+
const similarityMap = new Map<string, SimilarMatch[]>();
|
|
434
|
+
for (let i = 0; i < entities.length; i += CONCURRENCY_LIMIT) {
|
|
435
|
+
const batch = entities.slice(i, i + CONCURRENCY_LIMIT);
|
|
436
|
+
const results = await Promise.allSettled(
|
|
437
|
+
batch.map(async (entity) => {
|
|
438
|
+
const similar = await findSimilarEntities(
|
|
439
|
+
client,
|
|
440
|
+
entity.title,
|
|
441
|
+
entity.content,
|
|
442
|
+
workspaceId,
|
|
443
|
+
{ projectId, limit: 5, minRrfScore: 0.05, excludeIds: [entity.id] },
|
|
444
|
+
);
|
|
445
|
+
return { entityId: entity.id, similar };
|
|
446
|
+
}),
|
|
447
|
+
);
|
|
448
|
+
for (const r of results) {
|
|
449
|
+
if (r.status === "fulfilled") {
|
|
450
|
+
similarityMap.set(r.value.entityId, r.value.similar);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Process pairs sequentially (flaggedForRemoval creates dependencies)
|
|
456
|
+
for (const entity of entities) {
|
|
457
|
+
if (flaggedForRemoval.has(entity.id)) continue;
|
|
458
|
+
const similar = similarityMap.get(entity.id) || [];
|
|
459
|
+
|
|
460
|
+
for (const match of similar) {
|
|
461
|
+
if (flaggedForRemoval.has(match.id)) continue;
|
|
462
|
+
|
|
463
|
+
const pairKey = [entity.id, match.id].sort().join(":");
|
|
464
|
+
if (seenPairs.has(pairKey)) continue;
|
|
465
|
+
seenPairs.add(pairKey);
|
|
466
|
+
|
|
467
|
+
const sim = titleSimilarity(entity.title, match.title);
|
|
468
|
+
if (sim < DUPLICATE_SIMILARITY_THRESHOLD) continue;
|
|
469
|
+
|
|
470
|
+
// Keep the one with higher confidence, more accesses, or higher tier
|
|
471
|
+
const entityScore = entityQualityScore(entity);
|
|
472
|
+
const matchEntity = entityMap.get(match.id);
|
|
473
|
+
const matchScore = matchEntity
|
|
474
|
+
? entityQualityScore(matchEntity)
|
|
475
|
+
: match.confidence;
|
|
476
|
+
|
|
477
|
+
const [keep, remove] =
|
|
478
|
+
entityScore >= matchScore
|
|
479
|
+
? [entity, { id: match.id, title: match.title }]
|
|
480
|
+
: [{ id: match.id, title: match.title }, entity];
|
|
481
|
+
|
|
482
|
+
flaggedForRemoval.add(remove.id);
|
|
483
|
+
result.pairs.push({
|
|
484
|
+
keepId: keep.id,
|
|
485
|
+
keepTitle: keep.title,
|
|
486
|
+
removeId: remove.id,
|
|
487
|
+
removeTitle: remove.title,
|
|
488
|
+
similarity: Math.round(sim * 100) / 100,
|
|
489
|
+
});
|
|
490
|
+
result.duplicatePairsFound++;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return result;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// ---------------------------------------------------------------------------
|
|
498
|
+
// Helpers
|
|
499
|
+
// ---------------------------------------------------------------------------
|
|
500
|
+
|
|
501
|
+
const TIER_WEIGHTS: Record<string, number> = {
|
|
502
|
+
reference: 3,
|
|
503
|
+
episode: 2,
|
|
504
|
+
draft: 1,
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
function entityQualityScore(entity: MemoryEntity): number {
|
|
508
|
+
return (
|
|
509
|
+
entity.confidence +
|
|
510
|
+
(TIER_WEIGHTS[entity.memory_tier] || 0) +
|
|
511
|
+
Math.min(entity.access_count, 10) * 0.1
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function titleSimilarity(a: string, b: string): number {
|
|
516
|
+
const na = a.toLowerCase().trim();
|
|
517
|
+
const nb = b.toLowerCase().trim();
|
|
518
|
+
if (na === nb) return 1;
|
|
519
|
+
|
|
520
|
+
const wordsA = new Set(na.split(/\W+/).filter(Boolean));
|
|
521
|
+
const wordsB = new Set(nb.split(/\W+/).filter(Boolean));
|
|
522
|
+
if (wordsA.size === 0 || wordsB.size === 0) return 0;
|
|
523
|
+
|
|
524
|
+
let intersection = 0;
|
|
525
|
+
for (const w of wordsA) {
|
|
526
|
+
if (wordsB.has(w)) intersection++;
|
|
527
|
+
}
|
|
528
|
+
// Jaccard similarity
|
|
529
|
+
const union = wordsA.size + wordsB.size - intersection;
|
|
530
|
+
return union > 0 ? intersection / union : 0;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// ---------------------------------------------------------------------------
|
|
534
|
+
// Health report renderer
|
|
535
|
+
// ---------------------------------------------------------------------------
|
|
536
|
+
|
|
537
|
+
function generateHealthReport(report: CleanupReport): string {
|
|
538
|
+
const mode = report.dryRun ? "Dry Run (preview)" : "Executed";
|
|
539
|
+
const lines: string[] = [
|
|
540
|
+
"# Memory Health Report\n",
|
|
541
|
+
`**Mode:** ${mode} | **Entities:** ${report.summary.totalEntities} | **Issues:** ${report.summary.issuesFound} | **Actions:** ${report.summary.actionsTaken}`,
|
|
542
|
+
"",
|
|
543
|
+
];
|
|
544
|
+
|
|
545
|
+
if (report.summary.totalEntities >= MAX_ENTITIES_FETCH) {
|
|
546
|
+
lines.push(
|
|
547
|
+
`> **Note:** Entity count hit the ${MAX_ENTITIES_FETCH} fetch limit. Some entities may not have been analyzed.\n`,
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// Prune
|
|
552
|
+
if (report.steps.prune) {
|
|
553
|
+
const p = report.steps.prune;
|
|
554
|
+
lines.push("## Stale Drafts");
|
|
555
|
+
if (p.staleDraftsFound === 0) {
|
|
556
|
+
lines.push("No stale drafts found.\n");
|
|
557
|
+
} else {
|
|
558
|
+
lines.push(
|
|
559
|
+
`Found **${p.staleDraftsFound}** stale drafts${!report.dryRun ? ` (pruned ${p.pruned})` : ""}:`,
|
|
560
|
+
);
|
|
561
|
+
lines.push("| Title | Age | Decay |");
|
|
562
|
+
lines.push("|-------|-----|-------|");
|
|
563
|
+
for (const item of p.items.slice(0, 20)) {
|
|
564
|
+
lines.push(`| ${item.title} | ${item.ageDays}d | ${item.decayScore} |`);
|
|
565
|
+
}
|
|
566
|
+
lines.push("");
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// Consolidate
|
|
571
|
+
if (report.steps.consolidate) {
|
|
572
|
+
const c = report.steps.consolidate;
|
|
573
|
+
lines.push("## Consolidation");
|
|
574
|
+
if (c.clustersFound === 0) {
|
|
575
|
+
lines.push(
|
|
576
|
+
`Scanned ${c.entitiesProcessed} draft/episode entities — no clusters found.\n`,
|
|
577
|
+
);
|
|
578
|
+
} else {
|
|
579
|
+
lines.push(
|
|
580
|
+
`Found **${c.clustersFound}** clusters across ${c.entitiesProcessed} entities:`,
|
|
581
|
+
);
|
|
582
|
+
for (const d of c.details.slice(0, 10)) {
|
|
583
|
+
lines.push(`- **${d.mergedTitle}** — ${d.clusterSize} entities`);
|
|
584
|
+
}
|
|
585
|
+
lines.push("");
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// Orphans
|
|
590
|
+
if (report.steps.orphans) {
|
|
591
|
+
const o = report.steps.orphans;
|
|
592
|
+
lines.push("## Orphaned Entities");
|
|
593
|
+
if (o.orphansFound === 0) {
|
|
594
|
+
lines.push("No orphans found.\n");
|
|
595
|
+
} else {
|
|
596
|
+
lines.push(
|
|
597
|
+
`Found **${o.orphansFound}** orphans${!report.dryRun ? ` (removed ${o.removed})` : ""}:`,
|
|
598
|
+
);
|
|
599
|
+
lines.push("| Title | Type | Tier | Age | Accesses |");
|
|
600
|
+
lines.push("|-------|------|------|-----|----------|");
|
|
601
|
+
for (const item of o.items.slice(0, 20)) {
|
|
602
|
+
lines.push(
|
|
603
|
+
`| ${item.title} | ${item.type} | ${item.tier} | ${item.ageDays}d | ${item.accessCount} |`,
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
lines.push("");
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// Duplicates
|
|
611
|
+
if (report.steps.duplicates) {
|
|
612
|
+
const d = report.steps.duplicates;
|
|
613
|
+
lines.push("## Near-Duplicates");
|
|
614
|
+
if (d.duplicatePairsFound === 0) {
|
|
615
|
+
lines.push("No duplicates found.\n");
|
|
616
|
+
} else {
|
|
617
|
+
lines.push(
|
|
618
|
+
`Found **${d.duplicatePairsFound}** duplicate pairs${!report.dryRun ? ` (resolved ${d.resolved})` : ""}:`,
|
|
619
|
+
);
|
|
620
|
+
for (const pair of d.pairs.slice(0, 20)) {
|
|
621
|
+
lines.push(
|
|
622
|
+
`- "${pair.keepTitle}" ~ "${pair.removeTitle}" (${Math.round(pair.similarity * 100)}% similar, keep first)`,
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
lines.push("");
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// Backfill
|
|
630
|
+
if (report.steps.backfill) {
|
|
631
|
+
const b = report.steps.backfill;
|
|
632
|
+
lines.push("## Embedding Coverage");
|
|
633
|
+
if (report.dryRun) {
|
|
634
|
+
lines.push("Backfill will run when executed with `dryRun: false`.\n");
|
|
635
|
+
} else if (b.remaining === 0) {
|
|
636
|
+
lines.push(`All embeddings up to date (processed ${b.processed}).\n`);
|
|
637
|
+
} else {
|
|
638
|
+
lines.push(
|
|
639
|
+
`Processed ${b.processed} entities. ${b.remaining} still need embeddings.\n`,
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// Errors
|
|
645
|
+
if (report.errors.length > 0) {
|
|
646
|
+
lines.push("## Errors");
|
|
647
|
+
for (const e of report.errors) {
|
|
648
|
+
lines.push(`- **${e.step}:** ${e.message}`);
|
|
649
|
+
}
|
|
650
|
+
lines.push("");
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
if (report.dryRun) {
|
|
654
|
+
lines.push("---\n*Run with `dryRun: false` to execute cleanup.*");
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
return lines.join("\n");
|
|
658
|
+
}
|
package/src/prompt-builder.ts
CHANGED
|
@@ -439,7 +439,19 @@ export function generatePrompt(
|
|
|
439
439
|
sections.push(`- ${f}`);
|
|
440
440
|
});
|
|
441
441
|
sections.push(
|
|
442
|
-
`- **Memory:**
|
|
442
|
+
`- **Memory:** Store reusable knowledge via \`harmony_remember\`. Only store what a future agent couldn't easily discover from the code itself, applies beyond this specific card, and includes a "because" (not just what, but why).`,
|
|
443
|
+
);
|
|
444
|
+
sections.push(
|
|
445
|
+
` - GOOD: "BoardContext card state must use moveCard action, never direct setState — optimistic updates depend on action ordering"`,
|
|
446
|
+
);
|
|
447
|
+
sections.push(
|
|
448
|
+
` - GOOD: "Mobile bottom bar is 64px, overlaps fixed-position drawers — always add pb-16 to drawer content"`,
|
|
449
|
+
);
|
|
450
|
+
sections.push(
|
|
451
|
+
` - BAD: "Fixed the login button" (no reusable knowledge — the fix is in the code)`,
|
|
452
|
+
);
|
|
453
|
+
sections.push(
|
|
454
|
+
` - BAD: "Completed card #42" (ephemeral, auto-tracked by session)`,
|
|
443
455
|
);
|
|
444
456
|
|
|
445
457
|
// Output suggestions
|