@gethmy/mcp 2.4.6 → 2.5.0

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.
@@ -1,1165 +0,0 @@
1
- /**
2
- * Active Learning Loop
3
- *
4
- * Auto-extracts memories from agent work sessions.
5
- * Triggered on harmony_end_agent_session and harmony_update_agent_progress.
6
- */
7
-
8
- import type { HarmonyApiClient } from "./api-client.js";
9
- import { getActiveProjectId, getActiveWorkspaceId } from "./config.js";
10
- import {
11
- autoExpandGraph,
12
- findSimilarEntities,
13
- linkCrossTypeNeighbors,
14
- } from "./graph-expansion.js";
15
-
16
- // Entity types that can participate in `contradicts` relations (per schema.ts)
17
- const CONTRADICTION_TYPES = new Set([
18
- "decision",
19
- "pattern",
20
- "preference",
21
- "lesson",
22
- ]);
23
-
24
- export interface SessionContext {
25
- cardId: string;
26
- cardTitle: string;
27
- cardLabels: string[];
28
- agentIdentifier: string;
29
- agentName: string;
30
- status: "completed" | "paused";
31
- progressPercent?: number;
32
- blockers?: string[];
33
- currentTask?: string;
34
- sessionDurationMs?: number;
35
- cardDescription?: string;
36
- cardSubtasks?: Array<{ title: string; done: boolean }>;
37
- }
38
-
39
- export interface ExtractedLearning {
40
- title: string;
41
- content: string;
42
- type: string;
43
- tier: string;
44
- confidence: number;
45
- tags: string[];
46
- metadata: Record<string, unknown>;
47
- }
48
-
49
- // --- Mid-Session Learning ---
50
-
51
- /** Track previous task per session for change detection */
52
- const sessionTaskHistory = new Map<
53
- string,
54
- {
55
- lastTask: string;
56
- lastExtractionAt: number;
57
- steps: Array<{ task: string; progress: number; timestamp: number }>;
58
- }
59
- >();
60
-
61
- /** Rate limit: max 1 extraction per 2 minutes per session */
62
- const MID_SESSION_RATE_LIMIT_MS = 120_000;
63
-
64
- /**
65
- * Simple Levenshtein similarity (0-1 range, 1 = identical).
66
- */
67
- function levenshteinSimilarity(a: string, b: string): number {
68
- if (a === b) return 1;
69
- if (a.length === 0 || b.length === 0) return 0;
70
-
71
- // Use shorter strings for performance (truncate to 200 chars)
72
- const sa = a.slice(0, 200).toLowerCase();
73
- const sb = b.slice(0, 200).toLowerCase();
74
-
75
- const matrix: number[][] = [];
76
- for (let i = 0; i <= sa.length; i++) {
77
- matrix[i] = [i];
78
- }
79
- for (let j = 0; j <= sb.length; j++) {
80
- matrix[0][j] = j;
81
- }
82
-
83
- for (let i = 1; i <= sa.length; i++) {
84
- for (let j = 1; j <= sb.length; j++) {
85
- const cost = sa[i - 1] === sb[j - 1] ? 0 : 1;
86
- matrix[i][j] = Math.min(
87
- matrix[i - 1][j] + 1,
88
- matrix[i][j - 1] + 1,
89
- matrix[i - 1][j - 1] + cost,
90
- );
91
- }
92
- }
93
-
94
- const maxLen = Math.max(sa.length, sb.length);
95
- return 1 - matrix[sa.length][sb.length] / maxLen;
96
- }
97
-
98
- export interface MidSessionContext {
99
- cardId: string;
100
- cardTitle: string;
101
- agentIdentifier: string;
102
- agentName: string;
103
- currentTask?: string;
104
- status?: string;
105
- blockers?: string[];
106
- progressPercent?: number;
107
- }
108
-
109
- /**
110
- * Extract learnings from mid-session progress updates.
111
- * Called from harmony_update_agent_progress.
112
- */
113
- export async function extractMidSessionLearnings(
114
- _client: HarmonyApiClient,
115
- ctx: MidSessionContext,
116
- ): Promise<{ count: number; entityIds: string[] }> {
117
- const workspaceId = getActiveWorkspaceId();
118
- if (!workspaceId) return { count: 0, entityIds: [] };
119
-
120
- const _projectId = getActiveProjectId() || undefined;
121
- const now = Date.now();
122
- const _entityIds: string[] = [];
123
-
124
- const history = sessionTaskHistory.get(ctx.cardId);
125
-
126
- // Always track step history regardless of rate limit
127
- if (ctx.currentTask) {
128
- const previousTask = history?.lastTask || "";
129
- const similarity = levenshteinSimilarity(previousTask, ctx.currentTask);
130
- const existingSteps = history?.steps || [];
131
-
132
- if (
133
- existingSteps.length === 0 ||
134
- (similarity < 0.6 && previousTask.length > 0)
135
- ) {
136
- const newStep = {
137
- task: ctx.currentTask,
138
- progress: ctx.progressPercent ?? 0,
139
- timestamp: now,
140
- };
141
-
142
- if (existingSteps.length === 0 && previousTask.length > 0) {
143
- existingSteps.push({
144
- task: previousTask,
145
- progress: 0,
146
- timestamp: history?.lastExtractionAt ?? now,
147
- });
148
- }
149
- existingSteps.push(newStep);
150
-
151
- sessionTaskHistory.set(ctx.cardId, {
152
- lastTask: ctx.currentTask,
153
- lastExtractionAt: history?.lastExtractionAt ?? 0,
154
- steps: existingSteps,
155
- });
156
- }
157
- }
158
-
159
- // Rate limit check (only gates memory entity creation, not step tracking)
160
- if (history && now - history.lastExtractionAt < MID_SESSION_RATE_LIMIT_MS) {
161
- // Still within rate limit, but always extract blockers immediately
162
- if (ctx.status !== "blocked" || !ctx.blockers?.length) {
163
- return { count: 0, entityIds: [] };
164
- }
165
- }
166
-
167
- // Rule 1: Status transitions to "blocked" → track but DON'T create mid-session entities.
168
- // Blockers are captured at session end with full context — mid-session entities are
169
- // low-confidence duplicates that add noise to the knowledge graph.
170
- if (ctx.status === "blocked" && ctx.blockers?.length) {
171
- sessionTaskHistory.set(ctx.cardId, {
172
- lastTask: ctx.currentTask || "",
173
- lastExtractionAt: now,
174
- steps: history?.steps || [],
175
- });
176
- return { count: 0, entityIds: [] };
177
- }
178
-
179
- // Rule 2: Task transitions are tracked in step history (above) but no longer
180
- // create separate context entities. The step history feeds procedure extraction
181
- // at session end, which is more valuable than individual transition snapshots.
182
- if (ctx.currentTask) {
183
- const currentHistory = sessionTaskHistory.get(ctx.cardId);
184
- sessionTaskHistory.set(ctx.cardId, {
185
- lastTask: ctx.currentTask,
186
- lastExtractionAt: currentHistory?.lastExtractionAt ?? 0,
187
- steps: currentHistory?.steps || [],
188
- });
189
- }
190
-
191
- return { count: 0, entityIds: [] };
192
- }
193
-
194
- /**
195
- * Clean up mid-session tracking for a card (call on session end).
196
- */
197
- export function clearMidSessionTracking(cardId: string): void {
198
- sessionTaskHistory.delete(cardId);
199
- }
200
-
201
- // --- Procedure Extraction ---
202
-
203
- interface StepEntry {
204
- task: string;
205
- progress: number;
206
- timestamp: number;
207
- }
208
-
209
- interface EnrichedStep {
210
- task: string;
211
- progress: number;
212
- progressDelta: number;
213
- durationMs: number;
214
- isKeyDecision: boolean;
215
- }
216
-
217
- /**
218
- * Enrich raw steps with progress deltas and key decision detection.
219
- * A step is a "key decision" if it represents a >20% progress jump.
220
- */
221
- function enrichSteps(steps: StepEntry[]): EnrichedStep[] {
222
- return steps.map((step, i) => {
223
- const prevProgress = i > 0 ? steps[i - 1].progress : 0;
224
- const prevTimestamp = i > 0 ? steps[i - 1].timestamp : step.timestamp;
225
- const progressDelta = step.progress - prevProgress;
226
- return {
227
- task: step.task,
228
- progress: step.progress,
229
- progressDelta,
230
- durationMs: step.timestamp - prevTimestamp,
231
- isKeyDecision: progressDelta >= 20,
232
- };
233
- });
234
- }
235
-
236
- /**
237
- * Build structured procedure content from session data and enriched steps.
238
- */
239
- function buildProcedureContent(
240
- session: SessionContext,
241
- enrichedSteps: EnrichedStep[],
242
- wikiLinksLine: string,
243
- ): string {
244
- const triggerLabels =
245
- session.cardLabels.length > 0
246
- ? `Labels: ${session.cardLabels.join(", ")}`
247
- : "";
248
-
249
- const stepsMarkdown = enrichedSteps
250
- .map((s, i) => {
251
- const marker = s.isKeyDecision ? " **[key step]**" : "";
252
- const duration =
253
- s.durationMs > 0 ? ` (~${Math.round(s.durationMs / 60000)}min)` : "";
254
- return `${i + 1}. ${s.task} (${s.progress}%, +${s.progressDelta}%)${marker}${duration}`;
255
- })
256
- .join("\n");
257
-
258
- const subtaskSection =
259
- session.cardSubtasks && session.cardSubtasks.length > 0
260
- ? [
261
- "",
262
- "## Subtasks Completed",
263
- ...session.cardSubtasks.map(
264
- (s) => `- [${s.done ? "x" : " "}] ${s.title}`,
265
- ),
266
- ].join("\n")
267
- : "";
268
-
269
- const durationInfo = session.sessionDurationMs
270
- ? `\nDuration: ${Math.round(session.sessionDurationMs / 60000)} minutes`
271
- : "";
272
-
273
- return [
274
- "## Trigger",
275
- `When working on: "${session.cardTitle}"`,
276
- triggerLabels,
277
- "",
278
- "## Steps",
279
- stepsMarkdown,
280
- subtaskSection,
281
- "",
282
- "## Outcome",
283
- `Completed at ${session.progressPercent ?? "unknown"}%`,
284
- session.currentTask ? `Final state: ${session.currentTask}` : "",
285
- `Agent: ${session.agentName}`,
286
- durationInfo,
287
- wikiLinksLine,
288
- ]
289
- .filter((line) => line !== undefined)
290
- .join("\n");
291
- }
292
-
293
- /**
294
- * Extract a new procedure or reinforce an existing similar one.
295
- *
296
- * Deduplication: searches for existing procedure entities with similar titles.
297
- * If found, merges new steps and bumps confidence. Otherwise creates a new one.
298
- */
299
- async function extractOrReinforceProcedure(
300
- client: HarmonyApiClient,
301
- session: SessionContext,
302
- steps: StepEntry[],
303
- workspaceId: string,
304
- projectId?: string,
305
- wikiLinksLine = "",
306
- ): Promise<
307
- | { mode: "created"; learning: ExtractedLearning }
308
- | { mode: "reinforced"; entityId: string }
309
- | null
310
- > {
311
- const enrichedSteps = enrichSteps(steps);
312
- const newContent = buildProcedureContent(
313
- session,
314
- enrichedSteps,
315
- wikiLinksLine,
316
- );
317
- const tags = [
318
- "auto-extracted",
319
- "procedure",
320
- ...session.cardLabels.slice(0, 3),
321
- ];
322
-
323
- // Try to find an existing similar procedure to reinforce
324
- try {
325
- const similar = await findSimilarEntities(
326
- client,
327
- `Procedure: ${session.cardTitle}`,
328
- newContent,
329
- workspaceId,
330
- { projectId, limit: 5, minRrfScore: 0.03 },
331
- );
332
-
333
- const matchingProcedure = similar.find(
334
- (e) => e.type === "procedure" && (e.rrf_score ?? 0) >= 0.05,
335
- );
336
-
337
- if (matchingProcedure) {
338
- // Fetch the full entity to get metadata and memory_tier
339
- const { entity: rawEntity } = await client.getMemoryEntity(
340
- matchingProcedure.id,
341
- );
342
- const fullEntity = rawEntity as {
343
- id: string;
344
- content?: string;
345
- confidence?: number;
346
- memory_tier?: string;
347
- metadata?: Record<string, unknown>;
348
- };
349
-
350
- const currentMeta = fullEntity.metadata || {};
351
- const sourceCards = (
352
- (currentMeta.source_cards as string[]) || []
353
- ).slice();
354
- if (!sourceCards.includes(session.cardId)) {
355
- sourceCards.push(session.cardId);
356
- }
357
- const reuseCount = ((currentMeta.reuse_count as number) || 0) + 1;
358
- const currentConfidence = fullEntity.confidence ?? 0.7;
359
- // Bump confidence by 0.05 per reinforcement, max 0.95
360
- const newConfidence = Math.min(0.95, currentConfidence + 0.05);
361
-
362
- // Append new steps as an additional execution record
363
- const stepsAppendix = enrichedSteps
364
- .map((s, i) => `${i + 1}. ${s.task} (${s.progress}%)`)
365
- .join("\n");
366
- const appendix = `\n\n---\n### Execution ${reuseCount + 1}: ${session.cardTitle}\n${stepsAppendix}\nAgent: ${session.agentName} | ${new Date().toISOString().split("T")[0]}`;
367
-
368
- const updatedMeta = {
369
- ...currentMeta,
370
- reuse_count: reuseCount,
371
- source_cards: sourceCards,
372
- last_reinforced_at: new Date().toISOString(),
373
- step_count: Math.max(
374
- (currentMeta.step_count as number) || 0,
375
- steps.length,
376
- ),
377
- };
378
-
379
- // Auto-promote to reference tier if reinforced enough (≥3 executions)
380
- const shouldPromote =
381
- reuseCount >= 2 && fullEntity.memory_tier !== "reference";
382
-
383
- await client.updateMemoryEntity(fullEntity.id, {
384
- content: (fullEntity.content || "") + appendix,
385
- confidence: newConfidence,
386
- metadata: {
387
- ...updatedMeta,
388
- ...(shouldPromote
389
- ? {
390
- promoted_reason: `Reinforced by ${reuseCount + 1} successful sessions`,
391
- promoted_at: new Date().toISOString(),
392
- }
393
- : {}),
394
- },
395
- ...(shouldPromote ? { memory_tier: "reference" } : {}),
396
- });
397
-
398
- return { mode: "reinforced", entityId: fullEntity.id };
399
- }
400
- } catch {
401
- // Similarity search failed, fall through to create new
402
- }
403
-
404
- // No existing procedure found — create a new one
405
- return {
406
- mode: "created",
407
- learning: {
408
- title: `Procedure: ${session.cardTitle}`,
409
- content: newContent,
410
- type: "procedure",
411
- tier: "episode",
412
- confidence: 0.7,
413
- tags,
414
- metadata: {
415
- source: "active_learning",
416
- card_id: session.cardId,
417
- source_cards: [session.cardId],
418
- step_count: steps.length,
419
- key_step_count: enrichedSteps.filter((s) => s.isKeyDecision).length,
420
- reuse_count: 0,
421
- },
422
- },
423
- };
424
- }
425
-
426
- // --- Same-Session Causal Linking ---
427
-
428
- /** Causal pairing rules for entities created within the same session */
429
- const SESSION_CAUSAL_RULES: Array<{
430
- sourceType: string;
431
- targetType: string;
432
- relation: string;
433
- confidence: number;
434
- }> = [
435
- {
436
- sourceType: "error",
437
- targetType: "solution",
438
- relation: "resolved_by",
439
- confidence: 0.8,
440
- },
441
- {
442
- sourceType: "lesson",
443
- targetType: "error",
444
- relation: "learned_from",
445
- confidence: 0.75,
446
- },
447
- ];
448
-
449
- /**
450
- * Link entities created within the same session using causal relations.
451
- *
452
- * For example, if a session produces both an `error` and a `solution`,
453
- * creates an `error -[resolved_by]-> solution` relation.
454
- *
455
- * Non-fatal: all errors are caught silently.
456
- */
457
- export async function linkSessionEntities(
458
- client: HarmonyApiClient,
459
- createdPairs: Array<{ id: string; learning: ExtractedLearning }>,
460
- _workspaceId: string,
461
- _projectId?: string,
462
- ): Promise<{ relationsCreated: number }> {
463
- let relationsCreated = 0;
464
-
465
- for (const rule of SESSION_CAUSAL_RULES) {
466
- const sources = createdPairs.filter(
467
- (p) => p.learning.type === rule.sourceType,
468
- );
469
- const targets = createdPairs.filter(
470
- (p) => p.learning.type === rule.targetType,
471
- );
472
-
473
- for (const source of sources) {
474
- for (const target of targets) {
475
- try {
476
- await client.createMemoryRelation({
477
- source_id: source.id,
478
- target_id: target.id,
479
- relation_type: rule.relation,
480
- confidence: rule.confidence,
481
- });
482
- relationsCreated++;
483
- } catch {
484
- // Skip duplicate/failed relations silently
485
- }
486
- }
487
- }
488
- }
489
-
490
- return { relationsCreated };
491
- }
492
-
493
- // --- Session-End Learning ---
494
-
495
- /**
496
- * Extract learnings from a completed agent session.
497
- * Returns the number of memories created.
498
- */
499
- export async function extractLearnings(
500
- client: HarmonyApiClient,
501
- session: SessionContext,
502
- ): Promise<{ count: number; entityIds: string[] }> {
503
- const workspaceId = getActiveWorkspaceId();
504
- if (!workspaceId) {
505
- return { count: 0, entityIds: [] };
506
- }
507
-
508
- const projectId = getActiveProjectId() || undefined;
509
- const learnings: ExtractedLearning[] = [];
510
-
511
- // Search for related entities to enrich summaries with wiki-links
512
- let relatedEntityTitles: string[] = [];
513
- if (workspaceId) {
514
- try {
515
- const searchQuery = [
516
- session.cardTitle,
517
- session.currentTask || "",
518
- ...session.cardLabels,
519
- ]
520
- .filter(Boolean)
521
- .join(" ");
522
- const similar = await findSimilarEntities(
523
- client,
524
- searchQuery,
525
- session.currentTask || session.cardTitle,
526
- workspaceId,
527
- { projectId, limit: 5, minRrfScore: 0.01 },
528
- );
529
- relatedEntityTitles = similar.slice(0, 3).map((e) => e.title);
530
- } catch {
531
- /* non-fatal */
532
- }
533
- }
534
-
535
- const wikiLinksLine =
536
- relatedEntityTitles.length > 0
537
- ? `\nRelated: ${relatedEntityTitles.map((t) => `[[${t}]]`).join(", ")}`
538
- : "";
539
-
540
- // Rule 1: Session had blockers → create error entity (only for substantial blockers)
541
- // Skip trivial blocker strings — only store if the blocker text contains
542
- // enough detail to be useful to a future agent (>80 chars).
543
- if (session.blockers && session.blockers.length > 0) {
544
- for (const blocker of session.blockers) {
545
- if (blocker.length < 80) continue; // Skip trivial blockers like "stuck" or "waiting on API"
546
-
547
- // Dedup: check if a similar error entity already exists
548
- let isDuplicate = false;
549
- try {
550
- const similar = await findSimilarEntities(
551
- client,
552
- blocker.slice(0, 200),
553
- blocker,
554
- workspaceId,
555
- { projectId, limit: 3, minRrfScore: 0.05 },
556
- );
557
- isDuplicate = similar.some(
558
- (e) => e.type === "error" && (e.rrf_score ?? 0) >= 0.06,
559
- );
560
- } catch {
561
- /* non-fatal */
562
- }
563
-
564
- if (!isDuplicate) {
565
- learnings.push({
566
- title: `Blocker: ${blocker.slice(0, 100)}`,
567
- content: `Encountered while working on "${session.cardTitle}":\n\n${blocker}\n\nAgent: ${session.agentName}\nSession status: ${session.status}`,
568
- type: "error",
569
- tier: "episode",
570
- confidence: 0.6,
571
- tags: [
572
- "auto-extracted",
573
- "blocker",
574
- ...session.cardLabels.slice(0, 3),
575
- ],
576
- metadata: {
577
- source: "active_learning",
578
- card_id: session.cardId,
579
- },
580
- });
581
- }
582
- }
583
- }
584
-
585
- // Rule 2: Session paused with blockers → create lesson (paused only, not clean completions).
586
- // Clean completions produce no reusable knowledge — the work is in the code/PR.
587
- // Only create a lesson when the session was interrupted (paused with blockers),
588
- // so a future agent can understand what was left unfinished and why.
589
- if (session.status === "paused" && (session.blockers?.length ?? 0) > 0) {
590
- const durationInfo = session.sessionDurationMs
591
- ? `\nDuration: ${Math.round(session.sessionDurationMs / 60000)} minutes`
592
- : "";
593
-
594
- learnings.push({
595
- title: `Paused: ${session.cardTitle}`,
596
- content: [
597
- `Paused work on "${session.cardTitle}".`,
598
- session.currentTask ? `Last task: ${session.currentTask}` : "",
599
- session.progressPercent !== undefined
600
- ? `Progress: ${session.progressPercent}%`
601
- : "",
602
- durationInfo,
603
- session.blockers?.length
604
- ? `Blockers: ${session.blockers.join("; ")}`
605
- : "",
606
- `\nAgent: ${session.agentName}`,
607
- wikiLinksLine,
608
- ]
609
- .filter(Boolean)
610
- .join("\n"),
611
- type: "lesson",
612
- tier: "draft",
613
- confidence: 0.6,
614
- tags: [
615
- "auto-extracted",
616
- "session-paused",
617
- ...session.cardLabels.slice(0, 3),
618
- ],
619
- metadata: {
620
- source: "active_learning",
621
- card_id: session.cardId,
622
- },
623
- });
624
- }
625
-
626
- // Rule 3: Bug solution — REMOVED.
627
- // Storing "Resolved bug: {card title}" with no detail about the actual fix
628
- // adds zero value. The real solution is in the code diff / PR. Agents should
629
- // use `harmony_remember` to store non-obvious root cause details manually.
630
-
631
- // Store learnings, tracking entity ID → learning for graph expansion
632
- const entityIds: string[] = [];
633
-
634
- // Rule 4: Successful session with tracked steps → create or reinforce procedure entity
635
- // Thresholds raised: require 5+ distinct steps AND 10+ minute duration to avoid
636
- // creating "procedures" from trivial tasks (e.g., a 2-step "investigate → fix" session).
637
- const stepHistory = sessionTaskHistory.get(session.cardId);
638
- const MIN_PROCEDURE_STEPS = 5;
639
- const MIN_PROCEDURE_DURATION_MS = 10 * 60 * 1000; // 10 minutes
640
- const hasEnoughSteps =
641
- stepHistory && stepHistory.steps.length >= MIN_PROCEDURE_STEPS;
642
- const hasMinDuration =
643
- (session.sessionDurationMs ?? 0) >= MIN_PROCEDURE_DURATION_MS;
644
- const isSuccessful =
645
- session.status === "completed" &&
646
- (session.progressPercent === undefined || session.progressPercent >= 85) &&
647
- !session.blockers?.length;
648
-
649
- if (isSuccessful && hasEnoughSteps && hasMinDuration) {
650
- const procedureResult = await extractOrReinforceProcedure(
651
- client,
652
- session,
653
- stepHistory.steps,
654
- workspaceId,
655
- projectId,
656
- wikiLinksLine,
657
- );
658
- if (procedureResult) {
659
- if (procedureResult.mode === "created") {
660
- learnings.push(procedureResult.learning);
661
- } else {
662
- // Reinforced existing procedure — already saved via API, just track the ID
663
- entityIds.push(procedureResult.entityId);
664
- }
665
- }
666
- }
667
- const createdPairs: Array<{ id: string; learning: ExtractedLearning }> = [];
668
- for (const learning of learnings) {
669
- try {
670
- const metadata = {
671
- ...learning.metadata,
672
- };
673
-
674
- const result = await client.createMemoryEntity({
675
- workspace_id: workspaceId,
676
- project_id: projectId,
677
- type: learning.type,
678
- scope: "project",
679
- memory_tier: learning.tier,
680
- title: learning.title,
681
- content: learning.content,
682
- confidence: learning.confidence,
683
- tags: learning.tags,
684
- metadata,
685
- agent_identifier: session.agentIdentifier,
686
- });
687
-
688
- const entity = result.entity as { id: string };
689
- if (entity?.id) {
690
- entityIds.push(entity.id);
691
- createdPairs.push({ id: entity.id, learning });
692
- }
693
- } catch {
694
- // Non-fatal: individual learning extraction failure shouldn't block others
695
- }
696
- }
697
-
698
- // Auto-expand the knowledge graph and detect contradictions for each newly created entity (fire-and-forget)
699
- for (const { id, learning } of createdPairs) {
700
- autoExpandGraph(
701
- client,
702
- id,
703
- learning.title,
704
- learning.content,
705
- learning.tags,
706
- workspaceId,
707
- projectId,
708
- ).catch(() => {});
709
-
710
- detectContradictions(
711
- client,
712
- id,
713
- learning.type,
714
- learning.title,
715
- learning.content,
716
- learning.tags,
717
- workspaceId,
718
- projectId,
719
- ).catch(() => {});
720
-
721
- // Cross-type causal linking (e.g., new error → existing solutions)
722
- linkCrossTypeNeighbors(
723
- client,
724
- id,
725
- learning.type,
726
- learning.title,
727
- learning.content,
728
- workspaceId,
729
- projectId,
730
- ).catch(() => {});
731
- }
732
-
733
- // Same-session causal linking (e.g., error + solution in same session → resolved_by)
734
- if (createdPairs.length >= 2) {
735
- linkSessionEntities(client, createdPairs, workspaceId, projectId).catch(
736
- () => {},
737
- );
738
- }
739
-
740
- // Pattern detection DISABLED — these create noise entities like
741
- // "Pattern: recurring procedure (N instances)" that are just catalogs of
742
- // entity titles, eating token budget with zero actionable content.
743
- // The consolidation tool (harmony_consolidate_memories) serves a similar
744
- // purpose and can be improved separately with LLM synthesis.
745
- // See: https://github.com/getharmony/getharmony/issues/memory-quality
746
-
747
- // Clean up mid-session tracking
748
- clearMidSessionTracking(session.cardId);
749
-
750
- return { count: entityIds.length, entityIds };
751
- }
752
-
753
- const PATTERN_THRESHOLD = 3;
754
-
755
- /**
756
- * Detect recurring patterns across sessions.
757
- *
758
- * Uses embedding-based similarity (via hybrid search) to find related entities
759
- * instead of naive tag matching. When ≥3 similar entities cluster, creates or
760
- * updates a `pattern` entity with `part_of` relations from members.
761
- */
762
- export async function detectAndCreatePatterns(
763
- client: HarmonyApiClient,
764
- newEntityIds: string[],
765
- session: SessionContext,
766
- workspaceId: string,
767
- projectId?: string,
768
- ): Promise<string[]> {
769
- const patternEntityIds: string[] = [];
770
-
771
- for (const newEntityId of newEntityIds) {
772
- try {
773
- // Fetch the newly created entity to get its type, title, and content
774
- const { entity: rawEntity } = await client.getMemoryEntity(newEntityId);
775
- const entity = rawEntity as {
776
- id: string;
777
- type: string;
778
- title: string;
779
- content: string;
780
- tags?: string[];
781
- };
782
- if (!entity?.type) continue;
783
-
784
- // Use embedding-based search with title + content snippet as query
785
- const similar = await findSimilarEntities(
786
- client,
787
- entity.title,
788
- entity.content,
789
- workspaceId,
790
- { projectId, limit: 30, minRrfScore: 0.01 },
791
- );
792
-
793
- // Exclude newly created entities
794
- const existing = similar.filter(
795
- (c) => !newEntityIds.includes(c.id) && c.type === entity.type,
796
- );
797
-
798
- if (existing.length < PATTERN_THRESHOLD) continue;
799
-
800
- // Build a descriptive pattern title from member entity titles
801
- const memberTitles = [
802
- entity.title,
803
- ...existing.slice(0, 4).map((e) => e.title),
804
- ];
805
- const patternTitle = `Pattern: recurring ${entity.type} (${existing.length + 1} instances)`;
806
-
807
- // Check if a pattern entity for this type already exists
808
- const { entities: existingPatterns } = await client.listMemoryEntities({
809
- workspace_id: workspaceId,
810
- project_id: projectId,
811
- type: "pattern",
812
- limit: 10,
813
- });
814
-
815
- // Find a pattern that covers this entity type
816
- const matchingPattern = (
817
- existingPatterns as Array<{
818
- id: string;
819
- metadata?: Record<string, unknown>;
820
- }>
821
- ).find((p) => p.metadata?.pattern_type === entity.type);
822
-
823
- let patternId: string | null = null;
824
-
825
- if (matchingPattern) {
826
- patternId = matchingPattern.id;
827
- await client.updateMemoryEntity(patternId, {
828
- content: `Recurring pattern: ${entity.type} entities appearing ${existing.length + 1} times.\n\nMembers:\n${memberTitles.map((t) => `- ${t}`).join("\n")}\n\nLast updated: ${new Date().toISOString()}`,
829
- metadata: {
830
- pattern_count: existing.length + 1,
831
- pattern_type: entity.type,
832
- last_updated: new Date().toISOString(),
833
- },
834
- });
835
- } else {
836
- const result = await client.createMemoryEntity({
837
- workspace_id: workspaceId,
838
- project_id: projectId,
839
- type: "pattern",
840
- scope: "project",
841
- memory_tier: "reference",
842
- title: patternTitle,
843
- content: `Recurring pattern: ${entity.type} entities detected ${existing.length + 1} times.\n\nMembers:\n${memberTitles.map((t) => `- ${t}`).join("\n")}`,
844
- confidence: 0.75,
845
- tags: ["auto-extracted", "pattern", entity.type],
846
- metadata: {
847
- source: "pattern_detection",
848
- pattern_type: entity.type,
849
- pattern_count: existing.length + 1,
850
- },
851
- agent_identifier: session.agentIdentifier,
852
- });
853
-
854
- const created = result.entity as { id: string };
855
- if (created?.id) {
856
- patternId = created.id;
857
- patternEntityIds.push(patternId);
858
- }
859
- }
860
-
861
- if (!patternId) continue;
862
-
863
- // Link members → pattern using part_of relations
864
- const toLink = [newEntityId, ...existing.slice(0, 4).map((e) => e.id)];
865
- for (const sourceId of toLink) {
866
- try {
867
- await client.createMemoryRelation({
868
- source_id: sourceId,
869
- target_id: patternId,
870
- relation_type: "part_of",
871
- confidence: 0.75,
872
- });
873
- } catch {
874
- // Skip duplicate/failed relations silently
875
- }
876
- }
877
- } catch {
878
- // Non-fatal: pattern detection failure should not block anything
879
- }
880
- }
881
-
882
- return patternEntityIds;
883
- }
884
-
885
- // --- Cross-Session Causal Pattern Detection ---
886
-
887
- const CAUSAL_PATTERN_THRESHOLD = 3;
888
-
889
- /**
890
- * Detect recurring error→solution chains across sessions.
891
- *
892
- * When a session produces both error and solution entities, searches for
893
- * similar errors from other sessions that also have `resolved_by` relations.
894
- * If ≥3 such pairs exist, creates a `pattern` entity to capture the
895
- * recurring causal chain.
896
- */
897
- export async function detectCausalPatterns(
898
- client: HarmonyApiClient,
899
- createdPairs: Array<{ id: string; learning: ExtractedLearning }>,
900
- session: SessionContext,
901
- workspaceId: string,
902
- projectId?: string,
903
- ): Promise<string[]> {
904
- const patternIds: string[] = [];
905
-
906
- // Find error+solution pairs from this session
907
- const errors = createdPairs.filter((p) => p.learning.type === "error");
908
- const solutions = createdPairs.filter((p) => p.learning.type === "solution");
909
- if (errors.length === 0 || solutions.length === 0) return patternIds;
910
-
911
- for (const errorPair of errors) {
912
- try {
913
- // Search for similar errors from other sessions
914
- const similarErrors = await findSimilarEntities(
915
- client,
916
- errorPair.learning.title,
917
- errorPair.learning.content,
918
- workspaceId,
919
- {
920
- projectId,
921
- limit: 20,
922
- minRrfScore: 0.03,
923
- excludeIds: createdPairs.map((p) => p.id),
924
- type: "error",
925
- },
926
- );
927
-
928
- // Filter to errors that have resolved_by relations
929
- const resolvedErrors: Array<{
930
- errorId: string;
931
- errorTitle: string;
932
- solutionTitle: string;
933
- }> = [];
934
- for (const similar of similarErrors.slice(0, 10)) {
935
- try {
936
- const { outgoing } = await client.getRelatedEntities(similar.id);
937
- const resolvedByRel = (
938
- outgoing as Array<{ relation_type: string; target_title?: string }>
939
- ).find((r) => r.relation_type === "resolved_by");
940
- if (resolvedByRel) {
941
- resolvedErrors.push({
942
- errorId: similar.id,
943
- errorTitle: similar.title,
944
- solutionTitle:
945
- (resolvedByRel as { target_title?: string }).target_title ||
946
- "unknown",
947
- });
948
- }
949
- } catch {
950
- // Non-fatal: skip entities where relation lookup fails
951
- }
952
- }
953
-
954
- // Need at least CAUSAL_PATTERN_THRESHOLD similar resolved errors (including current)
955
- if (resolvedErrors.length + 1 < CAUSAL_PATTERN_THRESHOLD) continue;
956
-
957
- // Check if a causal pattern already exists for this domain
958
- const { entities: existingPatterns } = await client.listMemoryEntities({
959
- workspace_id: workspaceId,
960
- project_id: projectId,
961
- type: "pattern",
962
- limit: 10,
963
- });
964
-
965
- const matchingPattern = (
966
- existingPatterns as Array<{
967
- id: string;
968
- metadata?: Record<string, unknown>;
969
- }>
970
- ).find(
971
- (p) => p.metadata?.pattern_chain_type === "error_resolved_by_solution",
972
- );
973
-
974
- if (matchingPattern) {
975
- // Update existing causal pattern
976
- await client.updateMemoryEntity(matchingPattern.id, {
977
- content: [
978
- `Recurring error→solution chain detected (${resolvedErrors.length + 1} instances).`,
979
- "",
980
- "## Error→Solution Pairs",
981
- `- ${errorPair.learning.title} → ${solutions[0].learning.title}`,
982
- ...resolvedErrors
983
- .slice(0, 5)
984
- .map((r) => `- ${r.errorTitle} → ${r.solutionTitle}`),
985
- "",
986
- `Last updated: ${new Date().toISOString()}`,
987
- ].join("\n"),
988
- metadata: {
989
- pattern_chain_type: "error_resolved_by_solution",
990
- pattern_count: resolvedErrors.length + 1,
991
- last_updated: new Date().toISOString(),
992
- },
993
- });
994
-
995
- // Link current error+solution to the pattern
996
- for (const pair of [errorPair, solutions[0]]) {
997
- try {
998
- await client.createMemoryRelation({
999
- source_id: pair.id,
1000
- target_id: matchingPattern.id,
1001
- relation_type: "part_of",
1002
- confidence: 0.75,
1003
- });
1004
- } catch {
1005
- // Skip duplicate relations
1006
- }
1007
- }
1008
- } else {
1009
- // Create new causal pattern
1010
- const result = await client.createMemoryEntity({
1011
- workspace_id: workspaceId,
1012
- project_id: projectId,
1013
- type: "pattern",
1014
- scope: "project",
1015
- memory_tier: "reference",
1016
- title: `Pattern: recurring error→solution chain (${resolvedErrors.length + 1} instances)`,
1017
- content: [
1018
- `Recurring error→solution chain detected across ${resolvedErrors.length + 1} sessions.`,
1019
- "",
1020
- "## Error→Solution Pairs",
1021
- `- ${errorPair.learning.title} → ${solutions[0].learning.title}`,
1022
- ...resolvedErrors
1023
- .slice(0, 5)
1024
- .map((r) => `- ${r.errorTitle} → ${r.solutionTitle}`),
1025
- ].join("\n"),
1026
- confidence: 0.8,
1027
- tags: ["auto-extracted", "pattern", "causal-chain"],
1028
- metadata: {
1029
- source: "causal_pattern_detection",
1030
- pattern_chain_type: "error_resolved_by_solution",
1031
- pattern_count: resolvedErrors.length + 1,
1032
- },
1033
- agent_identifier: session.agentIdentifier,
1034
- });
1035
-
1036
- const created = result.entity as { id: string };
1037
- if (created?.id) {
1038
- patternIds.push(created.id);
1039
-
1040
- // Link members to the pattern
1041
- const memberIds = [
1042
- errorPair.id,
1043
- solutions[0].id,
1044
- ...resolvedErrors.slice(0, 4).map((r) => r.errorId),
1045
- ];
1046
- for (const memberId of memberIds) {
1047
- try {
1048
- await client.createMemoryRelation({
1049
- source_id: memberId,
1050
- target_id: created.id,
1051
- relation_type: "part_of",
1052
- confidence: 0.75,
1053
- });
1054
- } catch {
1055
- // Skip duplicate relations
1056
- }
1057
- }
1058
- }
1059
- }
1060
- } catch {
1061
- // Non-fatal: causal pattern detection failure should not block anything
1062
- }
1063
- }
1064
-
1065
- return patternIds;
1066
- }
1067
-
1068
- // --- Contradiction Detection ---
1069
-
1070
- export interface ContradictionResult {
1071
- entityId: string;
1072
- title: string;
1073
- type: string;
1074
- tags: string[];
1075
- }
1076
-
1077
- /**
1078
- * Detect potential contradictions for a newly created entity using semantic search.
1079
- *
1080
- * Uses hybrid FTS+vector search to find same-type entities with similar content,
1081
- * then creates `contradicts` relations. Only applies to entity types allowed by
1082
- * the schema: decision, pattern, preference, lesson.
1083
- *
1084
- * Returns the list of contradicting entities found (for inclusion in tool response).
1085
- */
1086
- export async function detectContradictions(
1087
- client: HarmonyApiClient,
1088
- entityId: string,
1089
- entityType: string,
1090
- title: string,
1091
- content: string,
1092
- tags: string[],
1093
- workspaceId: string,
1094
- projectId?: string,
1095
- ): Promise<ContradictionResult[]> {
1096
- // Only types that can participate in contradicts relations
1097
- if (!CONTRADICTION_TYPES.has(entityType)) return [];
1098
-
1099
- // Need at least a title to search
1100
- if (!title.trim()) return [];
1101
-
1102
- try {
1103
- const similar = await findSimilarEntities(
1104
- client,
1105
- title,
1106
- content,
1107
- workspaceId,
1108
- {
1109
- projectId,
1110
- limit: 10,
1111
- excludeIds: [entityId],
1112
- },
1113
- );
1114
-
1115
- // Filter to same type only (contradictions are within the same type)
1116
- const candidates = similar.filter(
1117
- (e) => e.type === entityType && CONTRADICTION_TYPES.has(e.type),
1118
- );
1119
-
1120
- if (candidates.length === 0) return [];
1121
-
1122
- // Check for actual content divergence: similar topic but different stance.
1123
- // Entities that are very similar (high RRF) are likely supporting, not contradicting.
1124
- // We want entities that share topic keywords but have meaningful differences.
1125
- const contradictions: ContradictionResult[] = [];
1126
-
1127
- for (const candidate of candidates) {
1128
- // Skip entities with very high similarity — they're duplicates/supporting, not contradictions
1129
- if ((candidate.rrf_score ?? 0) > 0.8) continue;
1130
-
1131
- // Skip entities with very low similarity — they're unrelated
1132
- if ((candidate.rrf_score ?? 0) < 0.02) continue;
1133
-
1134
- // Check tag overlap: need at least 1 shared tag to indicate same domain
1135
- const sharedTags = tags.filter((t) => candidate.tags?.includes(t));
1136
- if (tags.length > 0 && sharedTags.length === 0) continue;
1137
-
1138
- contradictions.push({
1139
- entityId: candidate.id,
1140
- title: candidate.title,
1141
- type: candidate.type,
1142
- tags: candidate.tags || [],
1143
- });
1144
- }
1145
-
1146
- // Create contradicts relations (fire-and-forget style, but await for response)
1147
- for (const c of contradictions) {
1148
- try {
1149
- await client.createMemoryRelation({
1150
- source_id: entityId,
1151
- target_id: c.entityId,
1152
- relation_type: "contradicts",
1153
- confidence: 0.5,
1154
- });
1155
- } catch {
1156
- // Skip duplicate/failed relations silently (409 Conflict is expected)
1157
- }
1158
- }
1159
-
1160
- return contradictions;
1161
- } catch {
1162
- // Non-fatal: contradiction detection should never block entity creation
1163
- return [];
1164
- }
1165
- }