@gethmy/mcp 2.3.1 → 2.3.3

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.
Files changed (34) hide show
  1. package/dist/lib/api-client.js +2099 -648
  2. package/dist/lib/config.js +217 -201
  3. package/package.json +9 -5
  4. package/src/memory-cleanup.ts +2 -4
  5. package/dist/lib/__tests__/active-learning.test.js +0 -386
  6. package/dist/lib/__tests__/agent-performance-profiles.test.js +0 -325
  7. package/dist/lib/__tests__/auto-session.test.js +0 -661
  8. package/dist/lib/__tests__/context-assembly.test.js +0 -362
  9. package/dist/lib/__tests__/graph-expansion.test.js +0 -150
  10. package/dist/lib/__tests__/integration-memory-crud.test.js +0 -797
  11. package/dist/lib/__tests__/integration-memory-system.test.js +0 -281
  12. package/dist/lib/__tests__/lifecycle-maintenance.test.js +0 -207
  13. package/dist/lib/__tests__/pattern-detection.test.js +0 -295
  14. package/dist/lib/__tests__/prompt-builder.test.js +0 -418
  15. package/dist/lib/active-learning.js +0 -822
  16. package/dist/lib/auto-session.js +0 -214
  17. package/dist/lib/cli.js +0 -138
  18. package/dist/lib/consolidation.js +0 -303
  19. package/dist/lib/context-assembly.js +0 -884
  20. package/dist/lib/graph-expansion.js +0 -163
  21. package/dist/lib/http.js +0 -175
  22. package/dist/lib/index.js +0 -7
  23. package/dist/lib/lifecycle-maintenance.js +0 -88
  24. package/dist/lib/memory-cleanup.js +0 -455
  25. package/dist/lib/onboard.js +0 -36
  26. package/dist/lib/prompt-builder.js +0 -488
  27. package/dist/lib/remote.js +0 -166
  28. package/dist/lib/server.js +0 -3365
  29. package/dist/lib/skills.js +0 -593
  30. package/dist/lib/tui/agents.js +0 -116
  31. package/dist/lib/tui/docs.js +0 -744
  32. package/dist/lib/tui/setup.js +0 -934
  33. package/dist/lib/tui/theme.js +0 -95
  34. package/dist/lib/tui/writer.js +0 -200
@@ -1,884 +0,0 @@
1
- /**
2
- * Context Assembly Engine
3
- *
4
- * Token-budget-aware context constructor that assembles relevant memories
5
- * for a given task, producing a manifest of what was included/excluded.
6
- */
7
- import { checkPromotion, discoverRelatedContext } from "@harmony/memory";
8
- // Constants
9
- const DEFAULT_TOKEN_BUDGET = 4000;
10
- const MAX_TOKENS_PER_ENTITY = 500;
11
- const MIN_RELEVANCE_THRESHOLD = 0.15; // raised from 0.1 to filter low-signal entities
12
- // Tier weight multipliers for relevance scoring
13
- const TIER_WEIGHTS = {
14
- reference: 1.0,
15
- episode: 0.7,
16
- draft: 0.4,
17
- };
18
- // Dedicated procedure budget as a fraction of total budget
19
- const PROCEDURE_BUDGET_FRACTION = 0.15;
20
- // Tier budget allocation percentages (of remaining budget after procedure reservation)
21
- const TIER_BUDGET_ALLOCATION = {
22
- reference: 0.6,
23
- episode: 0.3,
24
- draft: 0.1,
25
- };
26
- // Minimum guaranteed slots per tier (reduced from 3 to avoid filling context with noise)
27
- const MIN_REFERENCE_SLOTS = 1;
28
- // Graph walk configuration
29
- const GRAPH_WALK_MAX_DEPTH = 1;
30
- const GRAPH_WALK_MAX_ENTITIES = 10;
31
- const GRAPH_WALK_MIN_CONFIDENCE = 0.5;
32
- const GRAPH_WALK_SEED_COUNT = 5;
33
- // Query expansion configuration
34
- const MAX_QUERY_VARIATIONS = 4;
35
- // LLM re-ranking configuration
36
- const RERANK_CLUSTER_THRESHOLD = 0.05;
37
- const RERANK_TOP_N = 10;
38
- const RERANK_MIN_CANDIDATES = 5;
39
- // Graph walk relation-type bonuses for relevance scoring
40
- const RELATION_BONUSES = {
41
- depends_on: 0.15,
42
- resolved_by: 0.2,
43
- relates_to: 0.1,
44
- implements: 0.15,
45
- blocks: 0.15,
46
- references: 0.1,
47
- extends: 0.1,
48
- caused_by: 0.15,
49
- };
50
- // Synonym map for query expansion (common dev term variations)
51
- // NOTE: Avoid circular references (auth->login, login->auth) — first synonym
52
- // is used for replacement, so each key should expand to non-overlapping terms.
53
- const QUERY_SYNONYMS = {
54
- auth: ["authentication", "authorization", "session"],
55
- authentication: ["auth", "session", "sign-in"],
56
- login: ["sign-in", "authentication", "session"],
57
- bug: ["error", "issue", "defect", "problem"],
58
- error: ["exception", "failure", "issue"],
59
- fix: ["resolve", "patch", "repair", "correct"],
60
- deploy: ["deployment", "release", "ship", "publish"],
61
- test: ["testing", "spec", "assertion", "verify"],
62
- config: ["configuration", "settings", "setup"],
63
- db: ["database", "storage", "persistence"],
64
- database: ["storage", "persistence", "data store"],
65
- api: ["endpoint", "route", "service"],
66
- ui: ["frontend", "component", "view"],
67
- perf: ["performance", "speed", "latency"],
68
- performance: ["speed", "latency", "optimization"],
69
- };
70
- /**
71
- * Estimate token count (rough: 1 token per 4 chars)
72
- */
73
- function estimateTokens(text) {
74
- return Math.ceil(text.length / 4);
75
- }
76
- /**
77
- * Content quality gate: filter out entities that waste token budget.
78
- * Returns true if the entity passes quality checks.
79
- */
80
- function passesQualityGate(entity) {
81
- const content = entity.content.trim();
82
- // Gate 1: Minimum content length — entities with <50 chars of content
83
- // are too shallow to provide value (e.g., "Resolved bug: Fix login button")
84
- if (content.length < 50)
85
- return false;
86
- // Gate 2: Title-content similarity — skip entities where content is just
87
- // the title restated. Normalize both and check if content adds anything.
88
- const normalizedTitle = entity.title
89
- .toLowerCase()
90
- .replace(/[^a-z0-9\s]/g, "")
91
- .trim();
92
- const normalizedContent = content
93
- .toLowerCase()
94
- .replace(/[^a-z0-9\s]/g, "")
95
- .trim();
96
- if (normalizedContent.length < normalizedTitle.length * 1.5) {
97
- // Content is barely longer than the title — likely just a reformulation
98
- return false;
99
- }
100
- // Gate 3: Pattern noise detection — skip "Pattern: recurring X (N instances)"
101
- // and "Consolidated from N type memories:" entities that are just catalogs
102
- if (entity.type === "pattern" &&
103
- /recurring .+ \(\d+ instances\)/i.test(entity.title)) {
104
- // Check if content is just a member list (lines starting with "- ")
105
- const lines = content.split("\n").filter((l) => l.trim().length > 0);
106
- const bulletLines = lines.filter((l) => l.trim().startsWith("- "));
107
- if (bulletLines.length > lines.length * 0.6)
108
- return false;
109
- }
110
- // Gate 4: Procedure quality — procedures must contain actual steps,
111
- // not just a card title wrapped in a template
112
- if (entity.type === "procedure") {
113
- // Count numbered steps (1. ..., 2. ..., etc.)
114
- const stepCount = (content.match(/^\d+\.\s/gm) || []).length;
115
- if (stepCount < 3)
116
- return false;
117
- }
118
- return true;
119
- }
120
- /**
121
- * Generate a unique assembly ID
122
- */
123
- function generateAssemblyId() {
124
- return `ctx_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
125
- }
126
- /**
127
- * Truncate entity content to fit within token limit.
128
- * Keeps first paragraph + bullet points if present.
129
- */
130
- function truncateContent(content, maxTokens) {
131
- const currentTokens = estimateTokens(content);
132
- if (currentTokens <= maxTokens) {
133
- return { text: content, truncated: false };
134
- }
135
- // Try to keep first paragraph
136
- const paragraphs = content.split(/\n\n+/);
137
- let result = paragraphs[0];
138
- // Add bullet points from subsequent paragraphs if they fit
139
- for (let i = 1; i < paragraphs.length; i++) {
140
- const lines = paragraphs[i]
141
- .split("\n")
142
- .filter((l) => l.startsWith("- ") || l.startsWith("* "));
143
- if (lines.length > 0) {
144
- const bulletSection = lines.join("\n");
145
- if (estimateTokens(result + "\n\n" + bulletSection) <= maxTokens) {
146
- result += "\n\n" + bulletSection;
147
- }
148
- }
149
- }
150
- // Hard truncate if still too long
151
- if (estimateTokens(result) > maxTokens) {
152
- const maxChars = maxTokens * 4;
153
- result = result.slice(0, maxChars - 3) + "...";
154
- }
155
- return { text: result, truncated: true };
156
- }
157
- /**
158
- * Escape regex metacharacters in a string for safe use in RegExp constructor.
159
- */
160
- function escapeRegex(str) {
161
- return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
162
- }
163
- /**
164
- * Expand a query into multiple search variations using synonym substitution.
165
- * Returns the original query plus up to 3 additional variations (4 total).
166
- */
167
- export function expandQuery(taskContext) {
168
- const queries = [taskContext];
169
- const lowerQueries = [taskContext.toLowerCase()];
170
- const words = taskContext
171
- .toLowerCase()
172
- .split(/\W+/)
173
- .filter((w) => w.length > 2);
174
- // Find words that have synonym expansions
175
- const expandableWords = words.filter((w) => QUERY_SYNONYMS[w]);
176
- for (const word of expandableWords) {
177
- const synonyms = QUERY_SYNONYMS[word];
178
- if (!synonyms)
179
- continue;
180
- // Create a variation by replacing the word with its first synonym
181
- const variation = taskContext.replace(new RegExp(`\\b${escapeRegex(word)}\\b`, "gi"), synonyms[0]);
182
- const lowerVariation = variation.toLowerCase();
183
- if (lowerVariation !== taskContext.toLowerCase() &&
184
- !lowerQueries.includes(lowerVariation)) {
185
- queries.push(variation);
186
- lowerQueries.push(lowerVariation);
187
- }
188
- if (queries.length >= MAX_QUERY_VARIATIONS)
189
- break;
190
- }
191
- // Also extract key noun phrases as a compact query
192
- if (words.length >= 3) {
193
- const keyPhrases = words
194
- .filter((w) => ![
195
- "the",
196
- "and",
197
- "for",
198
- "with",
199
- "this",
200
- "that",
201
- "from",
202
- "into",
203
- ].includes(w))
204
- .slice(0, 4)
205
- .join(" ");
206
- if (!lowerQueries.includes(keyPhrases)) {
207
- queries.push(keyPhrases);
208
- }
209
- }
210
- return queries.slice(0, MAX_QUERY_VARIATIONS);
211
- }
212
- /**
213
- * Compute relevance score for an entity against task context.
214
- */
215
- export function computeRelevanceScore(entity, taskContext, cardLabels, graphRelations) {
216
- const reasons = [];
217
- let score = 0;
218
- // 0. DB hybrid search signal (RRF score from FTS + vector fusion)
219
- // Scaled to 0-0.3 contribution; when present, reduces reliance on word-overlap
220
- const hasRrfScore = entity.rrf_score !== undefined && entity.rrf_score > 0;
221
- if (hasRrfScore) {
222
- // RRF scores are typically 0-0.04; normalize to 0-1 range then scale
223
- const normalizedRrf = Math.min(entity.rrf_score / 0.04, 1.0);
224
- const rrfContribution = normalizedRrf * 0.3;
225
- score += rrfContribution;
226
- reasons.push(`hybrid_search(rrf=${entity.rrf_score.toFixed(4)})`);
227
- }
228
- // 1. Text match: simple word overlap scoring (reduced weight when RRF available)
229
- const textMatchWeight = hasRrfScore ? 0.15 : 0.4;
230
- const taskWords = new Set(taskContext
231
- .toLowerCase()
232
- .split(/\W+/)
233
- .filter((w) => w.length > 2));
234
- const entityWords = new Set(`${entity.title} ${entity.content}`
235
- .toLowerCase()
236
- .split(/\W+/)
237
- .filter((w) => w.length > 2));
238
- const overlap = [...taskWords].filter((w) => entityWords.has(w));
239
- if (overlap.length > 0) {
240
- const textScore = Math.min(overlap.length / Math.max(taskWords.size, 1), 1.0) *
241
- textMatchWeight;
242
- score += textScore;
243
- reasons.push(`text_match(${overlap.length} words)`);
244
- }
245
- // 2. Tag overlap with card labels
246
- if (cardLabels.length > 0 && entity.tags.length > 0) {
247
- const labelSet = new Set(cardLabels.map((l) => l.toLowerCase()));
248
- const tagOverlap = entity.tags.filter((t) => labelSet.has(t.toLowerCase()));
249
- if (tagOverlap.length > 0) {
250
- const tagScore = (tagOverlap.length / cardLabels.length) * 0.3;
251
- score += tagScore;
252
- reasons.push(`tag_match(${tagOverlap.join(",")})`);
253
- }
254
- }
255
- // 3. Confidence as a quality signal
256
- score += entity.confidence * 0.15;
257
- if (entity.confidence >= 0.9) {
258
- reasons.push("high_confidence");
259
- }
260
- // 4. Recency: decay based on last access with tier-specific half-lives
261
- if (entity.last_accessed_at) {
262
- const daysSinceAccess = (Date.now() - new Date(entity.last_accessed_at).getTime()) /
263
- (1000 * 60 * 60 * 24);
264
- const halfLife = { draft: 7, episode: 30, reference: 180 }[entity.memory_tier];
265
- const recencyScore = 0.5 ** (daysSinceAccess / halfLife) * 0.1;
266
- score += recencyScore;
267
- if (daysSinceAccess < 7)
268
- reasons.push("recently_accessed");
269
- }
270
- // 5. Access frequency (log-scaled)
271
- if (entity.access_count > 0) {
272
- const freqScore = Math.log10(entity.access_count + 1) * 0.05;
273
- score += Math.min(freqScore, 0.1);
274
- if (entity.access_count >= 5)
275
- reasons.push(`frequently_used(${entity.access_count})`);
276
- }
277
- // 6. Usefulness score from feedback loop (0-0.15 weight)
278
- const usefulnessScore = entity.metadata?.usefulness_score ?? 0;
279
- if (usefulnessScore >= 3) {
280
- const usefulnessBoost = Math.min(usefulnessScore / 20, 0.15);
281
- score += usefulnessBoost;
282
- reasons.push(`useful(${usefulnessScore})`);
283
- }
284
- else if (usefulnessScore === 0 && entity.access_count >= 5) {
285
- // Accessed many times but never marked useful — slight penalty
286
- score -= 0.02;
287
- reasons.push("low_usefulness");
288
- }
289
- // Procedure boost: actionable step-by-step instructions are highly valuable
290
- if (entity.type === "procedure") {
291
- score += 0.1;
292
- reasons.push("procedure_boost");
293
- }
294
- // 7. Graph walk relation bonus: boost entities discovered via knowledge graph
295
- if (graphRelations && graphRelations.length > 0) {
296
- const entityRelations = graphRelations.filter((r) => r.source_id === entity.id || r.target_id === entity.id);
297
- if (entityRelations.length > 0) {
298
- // Take the highest relation bonus (don't stack all of them)
299
- let bestBonus = 0;
300
- let bestRelType = "";
301
- for (const rel of entityRelations) {
302
- const bonus = RELATION_BONUSES[rel.relation_type] ?? 0.1;
303
- if (bonus > bestBonus) {
304
- bestBonus = bonus;
305
- bestRelType = rel.relation_type;
306
- }
307
- }
308
- score += bestBonus;
309
- reasons.push(`graph_walk(${bestRelType})`);
310
- }
311
- }
312
- // Clamp raw score to 0-1 range before applying tier weight
313
- score = Math.max(0, Math.min(score, 1.0));
314
- // Apply tier weight
315
- const tierWeight = TIER_WEIGHTS[entity.memory_tier];
316
- score *= tierWeight;
317
- return { score, reasons };
318
- }
319
- /**
320
- * Assemble context from knowledge graph entities with token budget management.
321
- */
322
- export async function assembleContext(options) {
323
- const { workspaceId, projectId, taskContext, cardLabels = [], tokenBudget = DEFAULT_TOKEN_BUDGET, client, graphWalkEnabled = true, queryExpansionEnabled = true, enableLlmReranking = false, rerankFn, } = options;
324
- const assemblyId = generateAssemblyId();
325
- const manifest = {
326
- assemblyId,
327
- timestamp: new Date().toISOString(),
328
- included: [],
329
- excluded: [],
330
- budgetUsed: 0,
331
- budgetTotal: tokenBudget,
332
- tierBreakdown: {
333
- draft: { count: 0, tokens: 0 },
334
- episode: { count: 0, tokens: 0 },
335
- reference: { count: 0, tokens: 0 },
336
- },
337
- };
338
- // Fetch candidate entities: search by task context (with query expansion) + list by project
339
- const candidates = [];
340
- // P1: Query expansion — search with multiple query variations to catch synonym mismatches
341
- const queries = queryExpansionEnabled
342
- ? expandQuery(taskContext)
343
- : [taskContext];
344
- const searchResults = await Promise.allSettled(queries.map((query) => client.searchMemoryEntities(workspaceId, query, {
345
- project_id: projectId,
346
- limit: 30,
347
- })));
348
- const candidateIds = new Set();
349
- for (const result of searchResults) {
350
- if (result.status !== "fulfilled")
351
- continue;
352
- if (result.value.entities?.length > 0) {
353
- for (const raw of result.value.entities) {
354
- const entity = mapToContextEntity(raw);
355
- if (!candidateIds.has(entity.id)) {
356
- candidateIds.add(entity.id);
357
- candidates.push(entity);
358
- }
359
- }
360
- }
361
- }
362
- // Also fetch by project scope if we have few candidates
363
- if (candidates.length < 10 && projectId) {
364
- try {
365
- const listResult = await client.listMemoryEntities({
366
- workspace_id: workspaceId,
367
- project_id: projectId,
368
- limit: 30,
369
- });
370
- if (listResult.entities?.length > 0) {
371
- for (const raw of listResult.entities) {
372
- const entity = mapToContextEntity(raw);
373
- if (!candidateIds.has(entity.id)) {
374
- candidateIds.add(entity.id);
375
- candidates.push(entity);
376
- }
377
- }
378
- }
379
- }
380
- catch {
381
- // List failed, continue with what we have
382
- }
383
- }
384
- // Cross-project memory: fetch workspace-scoped entities only
385
- // This ensures shared decisions/patterns are available without leaking project-private data
386
- if (candidates.length < 20) {
387
- try {
388
- const wsResult = await client.listMemoryEntities({
389
- workspace_id: workspaceId,
390
- scope: "workspace",
391
- limit: 20,
392
- });
393
- if (wsResult.entities?.length > 0) {
394
- for (const raw of wsResult.entities) {
395
- const entity = mapToContextEntity(raw);
396
- if (!candidateIds.has(entity.id)) {
397
- candidateIds.add(entity.id);
398
- candidates.push(entity);
399
- }
400
- }
401
- }
402
- }
403
- catch {
404
- // Continue with what we have
405
- }
406
- }
407
- // P0: Graph walk enrichment — discover related entities via knowledge graph
408
- let graphRelations = [];
409
- if (graphWalkEnabled && candidates.length > 0) {
410
- try {
411
- // Take top candidates by RRF score (or first N if no RRF scores)
412
- const seedCandidates = [...candidates]
413
- .sort((a, b) => (b.rrf_score ?? 0) - (a.rrf_score ?? 0))
414
- .slice(0, GRAPH_WALK_SEED_COUNT);
415
- const seedIds = seedCandidates.map((c) => c.id);
416
- const walkResult = await discoverRelatedContext(client, seedIds, GRAPH_WALK_MAX_DEPTH, GRAPH_WALK_MAX_ENTITIES, GRAPH_WALK_MIN_CONFIDENCE);
417
- graphRelations = walkResult.relations;
418
- // Add discovered entities to candidate pool (skip those already present)
419
- const newEntityIds = walkResult.entities
420
- .filter((e) => !candidateIds.has(e.id))
421
- .map((e) => e.id);
422
- if (newEntityIds.length > 0) {
423
- // Fetch full entity data in parallel (graph walk only returns summary fields)
424
- const fetchResults = await Promise.allSettled(newEntityIds.map((id) => client.getMemoryEntity(id)));
425
- for (const result of fetchResults) {
426
- if (result.status !== "fulfilled" || !result.value.entity)
427
- continue;
428
- const mapped = mapToContextEntity(result.value.entity);
429
- candidateIds.add(mapped.id);
430
- candidates.push(mapped);
431
- }
432
- }
433
- }
434
- catch {
435
- // Graph walk failed, continue with search-only candidates
436
- }
437
- }
438
- if (candidates.length === 0) {
439
- return {
440
- context: "",
441
- manifest,
442
- memories: [],
443
- };
444
- }
445
- // Quality gate: filter out low-value entities before scoring
446
- const qualityCandidates = candidates.filter((entity) => {
447
- if (passesQualityGate(entity))
448
- return true;
449
- manifest.excluded.push({
450
- entityId: entity.id,
451
- title: entity.title,
452
- type: entity.type,
453
- tier: entity.memory_tier,
454
- relevanceScore: 0,
455
- reason: "failed_quality_gate",
456
- });
457
- return false;
458
- });
459
- if (qualityCandidates.length === 0) {
460
- return {
461
- context: "",
462
- manifest,
463
- memories: [],
464
- };
465
- }
466
- // Score all candidates (pass graph relations for relation-type bonuses)
467
- const scored = qualityCandidates.map((entity) => {
468
- const { score, reasons } = computeRelevanceScore(entity, taskContext, cardLabels, graphRelations.length > 0 ? graphRelations : undefined);
469
- return { entity, score, reasons };
470
- });
471
- // Sort by score descending
472
- scored.sort((a, b) => b.score - a.score);
473
- // P2: Optional LLM re-ranking when top scores are clustered
474
- if (enableLlmReranking &&
475
- rerankFn &&
476
- scored.length >= RERANK_MIN_CANDIDATES) {
477
- const topN = scored.slice(0, RERANK_TOP_N);
478
- const scoreRange = topN[0].score - topN[topN.length - 1].score;
479
- // Only re-rank when scores are tightly clustered
480
- if (scoreRange <= RERANK_CLUSTER_THRESHOLD) {
481
- try {
482
- const rerankCandidates = topN.map((s) => ({
483
- id: s.entity.id,
484
- title: s.entity.title,
485
- snippet: s.entity.content.slice(0, 200),
486
- }));
487
- const rerankedIds = await rerankFn(taskContext, rerankCandidates);
488
- // Reorder based on LLM ranking
489
- const idOrder = new Map(rerankedIds.map((id, i) => [id, i]));
490
- topN.sort((a, b) => {
491
- const aIdx = idOrder.get(a.entity.id) ?? 999;
492
- const bIdx = idOrder.get(b.entity.id) ?? 999;
493
- return aIdx - bIdx;
494
- });
495
- // Splice reranked items back in
496
- scored.splice(0, topN.length, ...topN);
497
- }
498
- catch {
499
- // Re-ranking failed, continue with static ordering
500
- }
501
- }
502
- }
503
- // Reserve dedicated procedure budget, allocate remaining to tiers
504
- const procedureBudget = Math.floor(tokenBudget * PROCEDURE_BUDGET_FRACTION);
505
- const remainingBudget = tokenBudget - procedureBudget;
506
- const tierBudgets = {
507
- reference: Math.floor(remainingBudget * TIER_BUDGET_ALLOCATION.reference),
508
- episode: Math.floor(remainingBudget * TIER_BUDGET_ALLOCATION.episode),
509
- draft: Math.floor(remainingBudget * TIER_BUDGET_ALLOCATION.draft),
510
- };
511
- const tierUsed = {
512
- reference: 0,
513
- episode: 0,
514
- draft: 0,
515
- };
516
- let procedureUsed = 0;
517
- const included = [];
518
- let totalUsed = 0;
519
- // First pass: guarantee minimum reference slots
520
- let referenceCount = 0;
521
- for (const item of scored) {
522
- if (item.entity.memory_tier === "reference" &&
523
- item.entity.type !== "procedure" &&
524
- referenceCount < MIN_REFERENCE_SLOTS) {
525
- const { text, truncated } = truncateContent(item.entity.content, MAX_TOKENS_PER_ENTITY);
526
- const tokens = estimateTokens(`### ${item.entity.title}\n${text}`);
527
- if (totalUsed + tokens <= tokenBudget) {
528
- included.push({ ...item, tokens, truncated });
529
- item.entity.content = text;
530
- totalUsed += tokens;
531
- tierUsed.reference += tokens;
532
- referenceCount++;
533
- }
534
- }
535
- }
536
- // Second pass: include procedure entities with dedicated budget
537
- const includedIds = new Set(included.map((i) => i.entity.id));
538
- const procedureCandidates = scored.filter((item) => item.entity.type === "procedure" && !includedIds.has(item.entity.id));
539
- for (const item of procedureCandidates) {
540
- if (item.score < MIN_RELEVANCE_THRESHOLD) {
541
- manifest.excluded.push({
542
- entityId: item.entity.id,
543
- title: item.entity.title,
544
- type: item.entity.type,
545
- tier: item.entity.memory_tier,
546
- relevanceScore: item.score,
547
- reason: "below_relevance_threshold",
548
- });
549
- continue;
550
- }
551
- const { text, truncated } = truncateContent(item.entity.content, MAX_TOKENS_PER_ENTITY);
552
- const tokens = estimateTokens(`### ${item.entity.title}\n${text}`);
553
- // Check dedicated procedure budget, allow overflow to total remaining
554
- if (procedureUsed + tokens > procedureBudget) {
555
- const totalRemaining = tokenBudget - totalUsed;
556
- if (tokens > totalRemaining) {
557
- manifest.excluded.push({
558
- entityId: item.entity.id,
559
- title: item.entity.title,
560
- type: item.entity.type,
561
- tier: item.entity.memory_tier,
562
- relevanceScore: item.score,
563
- reason: "procedure_budget_exceeded",
564
- });
565
- continue;
566
- }
567
- }
568
- if (totalUsed + tokens > tokenBudget) {
569
- manifest.excluded.push({
570
- entityId: item.entity.id,
571
- title: item.entity.title,
572
- type: item.entity.type,
573
- tier: item.entity.memory_tier,
574
- relevanceScore: item.score,
575
- reason: "total_budget_exceeded",
576
- });
577
- continue;
578
- }
579
- included.push({ ...item, tokens, truncated });
580
- item.entity.content = text;
581
- totalUsed += tokens;
582
- procedureUsed += tokens;
583
- includedIds.add(item.entity.id);
584
- }
585
- // Third pass: fill remaining budget by score (non-procedure entities)
586
- for (const item of scored) {
587
- if (includedIds.has(item.entity.id))
588
- continue;
589
- if (item.entity.type === "procedure")
590
- continue; // Already handled
591
- if (item.score < MIN_RELEVANCE_THRESHOLD) {
592
- manifest.excluded.push({
593
- entityId: item.entity.id,
594
- title: item.entity.title,
595
- type: item.entity.type,
596
- tier: item.entity.memory_tier,
597
- relevanceScore: item.score,
598
- reason: "below_relevance_threshold",
599
- });
600
- continue;
601
- }
602
- const tier = item.entity.memory_tier;
603
- const { text, truncated } = truncateContent(item.entity.content, MAX_TOKENS_PER_ENTITY);
604
- const tokens = estimateTokens(`### ${item.entity.title}\n${text}`);
605
- // Check tier budget (allow overflow to unused tiers)
606
- if (tierUsed[tier] + tokens > tierBudgets[tier]) {
607
- // Check if there's unused budget from other tiers
608
- const totalRemaining = tokenBudget - totalUsed;
609
- if (tokens > totalRemaining) {
610
- manifest.excluded.push({
611
- entityId: item.entity.id,
612
- title: item.entity.title,
613
- type: item.entity.type,
614
- tier,
615
- relevanceScore: item.score,
616
- reason: "budget_exceeded",
617
- });
618
- continue;
619
- }
620
- }
621
- if (totalUsed + tokens > tokenBudget) {
622
- manifest.excluded.push({
623
- entityId: item.entity.id,
624
- title: item.entity.title,
625
- type: item.entity.type,
626
- tier,
627
- relevanceScore: item.score,
628
- reason: "total_budget_exceeded",
629
- });
630
- continue;
631
- }
632
- included.push({ ...item, tokens, truncated });
633
- item.entity.content = text;
634
- totalUsed += tokens;
635
- tierUsed[tier] += tokens;
636
- includedIds.add(item.entity.id);
637
- }
638
- // Build manifest
639
- manifest.budgetUsed = totalUsed;
640
- const procedureItems = included.filter((i) => i.entity.type === "procedure");
641
- manifest.tierBreakdown = {
642
- reference: {
643
- count: included.filter((i) => i.entity.memory_tier === "reference" && i.entity.type !== "procedure").length,
644
- tokens: tierUsed.reference,
645
- },
646
- episode: {
647
- count: included.filter((i) => i.entity.memory_tier === "episode" && i.entity.type !== "procedure").length,
648
- tokens: tierUsed.episode,
649
- },
650
- draft: {
651
- count: included.filter((i) => i.entity.memory_tier === "draft" && i.entity.type !== "procedure").length,
652
- tokens: tierUsed.draft,
653
- },
654
- };
655
- manifest.procedureBreakdown = {
656
- count: procedureItems.length,
657
- tokens: procedureUsed,
658
- budget: procedureBudget,
659
- };
660
- for (const item of included) {
661
- manifest.included.push({
662
- entityId: item.entity.id,
663
- title: item.entity.title,
664
- type: item.entity.type,
665
- tier: item.entity.memory_tier,
666
- relevanceScore: item.score,
667
- reasons: item.reasons,
668
- tokenCount: item.tokens,
669
- truncated: item.truncated,
670
- });
671
- }
672
- // Build context string — procedures in their own section
673
- const contextSections = [];
674
- const nonProcedureItems = included.filter((i) => i.entity.type !== "procedure");
675
- if (included.length > 0) {
676
- // Procedure section first (actionable instructions)
677
- if (procedureItems.length > 0) {
678
- contextSections.push(`## Procedures (${procedureItems.length} loaded, ${procedureUsed}/${procedureBudget} tokens)`);
679
- for (const item of procedureItems) {
680
- const tags = item.entity.tags.length > 0
681
- ? ` [${item.entity.tags.join(", ")}]`
682
- : "";
683
- const tierLabel = item.entity.memory_tier !== "reference"
684
- ? ` (${item.entity.memory_tier})`
685
- : "";
686
- contextSections.push(`\n### ${item.entity.title} (confidence: ${item.entity.confidence})${tierLabel}${tags}`);
687
- contextSections.push(item.entity.content);
688
- }
689
- }
690
- // Non-procedure memories
691
- if (nonProcedureItems.length > 0) {
692
- contextSections.push(`\n## Relevant Memories (${nonProcedureItems.length} loaded, ${manifest.excluded.length} excluded)`);
693
- contextSections.push(`*Assembly: ${assemblyId} | Budget: ${totalUsed}/${tokenBudget} tokens*`);
694
- for (const item of nonProcedureItems) {
695
- const tags = item.entity.tags.length > 0
696
- ? ` [${item.entity.tags.join(", ")}]`
697
- : "";
698
- const tierLabel = item.entity.memory_tier !== "reference"
699
- ? ` (${item.entity.memory_tier})`
700
- : "";
701
- contextSections.push(`\n### ${item.entity.title} (${item.entity.type}, confidence: ${item.entity.confidence})${tierLabel}${tags}`);
702
- contextSections.push(item.entity.content);
703
- }
704
- }
705
- }
706
- // Increment access_count for included entities (fire-and-forget)
707
- incrementAccessCounts(client, included.map((i) => i.entity.id)).catch(() => { });
708
- // Auto-promote entities that cross access thresholds after the bump (fire-and-forget)
709
- promoteEligibleEntities(client, included.map((i) => i.entity)).catch(() => { });
710
- return {
711
- context: contextSections.join("\n"),
712
- manifest,
713
- memories: included.map((i) => i.entity),
714
- };
715
- }
716
- /**
717
- * Map raw API entity to ContextEntity
718
- */
719
- export function mapToContextEntity(raw) {
720
- const e = raw;
721
- return {
722
- id: e.id,
723
- type: e.type,
724
- title: e.title,
725
- content: e.content,
726
- confidence: e.confidence ?? 1.0,
727
- tags: e.tags || [],
728
- memory_tier: e.memory_tier || "reference",
729
- access_count: e.access_count || 0,
730
- last_accessed_at: e.last_accessed_at || null,
731
- created_at: e.created_at || "",
732
- updated_at: e.updated_at || "",
733
- metadata: e.metadata ?? undefined,
734
- // Hybrid search signals (present when results come from RPC)
735
- rrf_score: e.rrf_score ?? undefined,
736
- fts_rank: e.fts_rank ?? undefined,
737
- semantic_rank: e.semantic_rank ?? undefined,
738
- };
739
- }
740
- /**
741
- * Increment access counts for entities loaded into context.
742
- * Uses batch_touch_knowledge_entities RPC for a single-roundtrip update.
743
- * Falls back to individual touches if the batch endpoint is unavailable.
744
- */
745
- async function incrementAccessCounts(client, entityIds) {
746
- if (entityIds.length === 0)
747
- return;
748
- try {
749
- await client.batchTouchMemoryEntities(entityIds);
750
- }
751
- catch {
752
- // Fallback: individual touches (e.g. older server version)
753
- await Promise.allSettled(entityIds.map((id) => client.touchMemoryEntity(id)));
754
- }
755
- }
756
- /**
757
- * Check included entities for promotion eligibility after access count bump.
758
- * Uses access_count + 1 to reflect the touch that just happened.
759
- */
760
- async function promoteEligibleEntities(client, entities) {
761
- for (const entity of entities) {
762
- if (entity.memory_tier === "reference")
763
- continue;
764
- if (!entity.created_at)
765
- continue;
766
- // +1 because incrementAccessCounts just bumped it
767
- const promotion = checkPromotion(entity.memory_tier, entity.access_count + 1, entity.confidence, entity.created_at);
768
- if (promotion.eligible && promotion.targetTier) {
769
- try {
770
- await client.updateMemoryEntity(entity.id, {
771
- memory_tier: promotion.targetTier,
772
- metadata: {
773
- ...(entity.metadata || {}),
774
- promoted_at: new Date().toISOString(),
775
- promotion_reason: promotion.reason,
776
- promoted_from: entity.memory_tier,
777
- },
778
- });
779
- }
780
- catch {
781
- // Non-fatal: promotion is best-effort
782
- }
783
- }
784
- }
785
- }
786
- // In-memory manifest cache (keyed by assemblyId)
787
- const manifestCache = new Map();
788
- const MAX_CACHE_SIZE = 50;
789
- /**
790
- * Store a manifest for later retrieval.
791
- */
792
- export function cacheManifest(manifest) {
793
- if (manifestCache.size >= MAX_CACHE_SIZE) {
794
- // Remove oldest entry
795
- const firstKey = manifestCache.keys().next().value;
796
- if (firstKey)
797
- manifestCache.delete(firstKey);
798
- }
799
- manifestCache.set(manifest.assemblyId, manifest);
800
- }
801
- /**
802
- * Retrieve a cached manifest by assembly ID.
803
- */
804
- export function getCachedManifest(assemblyId) {
805
- return manifestCache.get(assemblyId);
806
- }
807
- // --- Feedback-Driven Scoring ---
808
- /** Track which assemblyId was used for which card session */
809
- const sessionAssemblyMap = new Map();
810
- const MAX_SESSION_MAP_SIZE = 100;
811
- /**
812
- * Associate an assemblyId with a card session for later feedback.
813
- * Called when context is assembled during session start or prompt generation.
814
- */
815
- export function trackSessionAssembly(cardId, assemblyId) {
816
- if (sessionAssemblyMap.size >= MAX_SESSION_MAP_SIZE) {
817
- const firstKey = sessionAssemblyMap.keys().next().value;
818
- if (firstKey)
819
- sessionAssemblyMap.delete(firstKey);
820
- }
821
- sessionAssemblyMap.set(cardId, assemblyId);
822
- }
823
- /**
824
- * Get the assemblyId associated with a card session.
825
- */
826
- export function getSessionAssemblyId(cardId) {
827
- return sessionAssemblyMap.get(cardId);
828
- }
829
- /**
830
- * Record context feedback based on session outcome.
831
- * Adjusts entity confidence based on whether the session completed successfully.
832
- *
833
- * - Completed successfully (status=completed, progress>=100): boost included entities
834
- * - Paused/blocked: neutral or slight penalty for included entities
835
- */
836
- export async function recordContextFeedback(client, cardId, sessionStatus, progressPercent, hadBlockers) {
837
- const assemblyId = sessionAssemblyMap.get(cardId);
838
- if (!assemblyId)
839
- return { adjusted: 0 };
840
- const manifest = manifestCache.get(assemblyId);
841
- if (!manifest || manifest.included.length === 0)
842
- return { adjusted: 0 };
843
- let adjusted = 0;
844
- const isSuccess = sessionStatus === "completed" && (progressPercent ?? 0) >= 100;
845
- for (const entry of manifest.included) {
846
- try {
847
- if (isSuccess) {
848
- // Boost confidence by +0.05 (max 1.0) and increment usefulness_score
849
- const { entity } = await client.getMemoryEntity(entry.entityId);
850
- const e = entity;
851
- const currentUsefulness = e.metadata?.usefulness_score ?? 0;
852
- const newConfidence = Math.min((e.confidence ?? 0.5) + 0.05, 1.0);
853
- await client.updateMemoryEntity(entry.entityId, {
854
- confidence: newConfidence,
855
- metadata: {
856
- usefulness_score: currentUsefulness + 1,
857
- last_feedback_at: new Date().toISOString(),
858
- },
859
- });
860
- adjusted++;
861
- }
862
- else if (hadBlockers) {
863
- // Slight penalty for entities included when session had blockers
864
- const { entity } = await client.getMemoryEntity(entry.entityId);
865
- const e = entity;
866
- const newConfidence = Math.max((e.confidence ?? 0.5) - 0.02, 0.1);
867
- await client.updateMemoryEntity(entry.entityId, {
868
- confidence: newConfidence,
869
- metadata: {
870
- last_feedback_at: new Date().toISOString(),
871
- },
872
- });
873
- adjusted++;
874
- }
875
- // Paused without blockers: no change (neutral signal)
876
- }
877
- catch {
878
- // Non-fatal: individual entity update failure
879
- }
880
- }
881
- // Clean up tracking
882
- sessionAssemblyMap.delete(cardId);
883
- return { adjusted };
884
- }