agentcache 0.2.4 → 0.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.
@@ -0,0 +1,684 @@
1
+ import {
2
+ getDataDir,
3
+ getGitContext
4
+ } from "./chunk-WHP4Z32Z.js";
5
+
6
+ // src/knowledge/passes/3-canonicalizer.ts
7
+ import { createHash } from "crypto";
8
+ var STOP_WORDS = /* @__PURE__ */ new Set([
9
+ "a",
10
+ "an",
11
+ "the",
12
+ "is",
13
+ "are",
14
+ "was",
15
+ "were",
16
+ "be",
17
+ "been",
18
+ "being",
19
+ "have",
20
+ "has",
21
+ "had",
22
+ "do",
23
+ "does",
24
+ "did",
25
+ "will",
26
+ "would",
27
+ "could",
28
+ "should",
29
+ "may",
30
+ "might",
31
+ "shall",
32
+ "can",
33
+ "need",
34
+ "must",
35
+ "to",
36
+ "of",
37
+ "in",
38
+ "for",
39
+ "on",
40
+ "with",
41
+ "at",
42
+ "by",
43
+ "from",
44
+ "as",
45
+ "into",
46
+ "through",
47
+ "during",
48
+ "before",
49
+ "after",
50
+ "above",
51
+ "below",
52
+ "this",
53
+ "that",
54
+ "these",
55
+ "those",
56
+ "it",
57
+ "its",
58
+ "and",
59
+ "but",
60
+ "or",
61
+ "nor",
62
+ "not",
63
+ "so",
64
+ "yet",
65
+ "all",
66
+ "each",
67
+ "every",
68
+ "both",
69
+ "few",
70
+ "more",
71
+ "most",
72
+ "i",
73
+ "we",
74
+ "you",
75
+ "they",
76
+ "he",
77
+ "she"
78
+ ]);
79
+ var ANTONYM_MAP = [
80
+ [/\bnever\b/g, "forbidden"],
81
+ [/\bdon'?t\b/g, "forbidden"],
82
+ [/\bavoid\b/g, "forbidden"],
83
+ [/\bprohibit(ed)?\b/g, "forbidden"],
84
+ [/\balways\b/g, "required"],
85
+ [/\bmust\b/g, "required"],
86
+ [/\brequire(d)?\b/g, "required"],
87
+ [/\buse\b/g, "use"],
88
+ [/\bprefer\b/g, "use"]
89
+ ];
90
+ function canonicalize(observations, existingCanonicalKeys) {
91
+ const canonicalized = observations.map((obs) => ({
92
+ ...obs,
93
+ canonicalKey: computeCanonicalKey(obs.content)
94
+ }));
95
+ const existingSet = new Set(existingCanonicalKeys || []);
96
+ const autoReinforced = [];
97
+ const needsClustering = [];
98
+ for (const obs of canonicalized) {
99
+ if (existingSet.has(obs.canonicalKey)) {
100
+ autoReinforced.push(obs);
101
+ } else {
102
+ needsClustering.push(obs);
103
+ }
104
+ }
105
+ return { observations: canonicalized, autoReinforced, needsClustering };
106
+ }
107
+ function computeCanonicalKey(content) {
108
+ let text = content.toLowerCase().trim();
109
+ for (const [pattern, replacement] of ANTONYM_MAP) {
110
+ text = text.replace(pattern, replacement);
111
+ }
112
+ text = text.replace(/[^\w\s]/g, " ");
113
+ const tokens = text.split(/\s+/).filter((t) => !STOP_WORDS.has(t) && t.length > 1).sort();
114
+ return tokens.join(" ");
115
+ }
116
+ function computeCanonicalHash(content) {
117
+ const key = computeCanonicalKey(content);
118
+ return createHash("sha256").update(key).digest("hex").slice(0, 16);
119
+ }
120
+
121
+ // src/knowledge/compiler.ts
122
+ import { randomUUID as randomUUID3 } from "crypto";
123
+
124
+ // src/knowledge/passes/1-extractor.ts
125
+ import { randomUUID } from "crypto";
126
+ var EXTRACT_PROMPT_VERSION = "extract-v1";
127
+ function buildExtractionPrompt(events) {
128
+ const transcript = events.filter((e) => e.content || e.tool_name).map((e) => {
129
+ if (e.role) return `[${e.role}]: ${e.content}`;
130
+ if (e.tool_name) return `[tool:${e.tool_name}]: ${JSON.stringify(e.tool_input).slice(0, 500)}`;
131
+ return "";
132
+ }).filter(Boolean).join("\n");
133
+ return `You are a knowledge extraction engine. Analyze this coding session transcript and extract distinct learnings.
134
+
135
+ Extract into four types:
136
+ - rule: a standing instruction or constraint the developer expressed
137
+ - lesson: a mistake made and what fixed it
138
+ - decision: an architectural or design choice with rationale
139
+ - context: current task state, open threads, what was left in progress
140
+
141
+ Return ONLY valid JSON: { "observations": [{ "type": "rule"|"lesson"|"decision"|"context", "content": "...", "sourceQuote": "...", "confidence": "high"|"medium" }] }
142
+
143
+ Only return high and medium confidence items. Ignore conversational noise, tool outputs, and implementation details that aren't generalizable.
144
+
145
+ <transcript>
146
+ ${transcript}
147
+ </transcript>`;
148
+ }
149
+ function parseExtractionResponse(text, sessionId, project) {
150
+ const jsonMatch = text.match(/\{[\s\S]*\}/);
151
+ if (!jsonMatch) return [];
152
+ const parsed = JSON.parse(jsonMatch[0]);
153
+ if (!parsed.observations || !Array.isArray(parsed.observations)) return [];
154
+ const now = Date.now();
155
+ return parsed.observations.filter((o) => o.type && o.content && o.confidence).filter((o) => ["high", "medium"].includes(o.confidence)).map((o) => ({
156
+ id: `obs_${randomUUID().slice(0, 8)}`,
157
+ sessionId,
158
+ timestamp: now,
159
+ type: o.type,
160
+ content: o.content,
161
+ sourceQuote: o.sourceQuote || "",
162
+ confidence: o.confidence,
163
+ project,
164
+ scope: o.scope || (o.type === "rule" || o.type === "lesson" ? "global" : "project")
165
+ }));
166
+ }
167
+
168
+ // src/knowledge/passes/2-normalizer.ts
169
+ var FILLER_PATTERNS = [
170
+ /^i (noticed|realized|learned|found|discovered|think) that /i,
171
+ /^it (seems|appears|looks) (like|that) /i,
172
+ /^we should /i,
173
+ /^you should /i,
174
+ /^basically,? /i,
175
+ /^essentially,? /i,
176
+ /^actually,? /i
177
+ ];
178
+ var IMPERATIVE_RULES = [
179
+ [/^you should never /i, "Never "],
180
+ [/^we should never /i, "Never "],
181
+ [/^don't ever /i, "Never "],
182
+ [/^never /i, "Never "],
183
+ [/^you should always /i, "Always "],
184
+ [/^we should always /i, "Always "],
185
+ [/^always /i, "Always "]
186
+ ];
187
+ function normalize(observations) {
188
+ const normalized = observations.map((obs) => ({
189
+ ...obs,
190
+ content: normalizeContent(obs.content, obs.type)
191
+ }));
192
+ const seen = /* @__PURE__ */ new Set();
193
+ return normalized.filter((obs) => {
194
+ const key = obs.content.toLowerCase().trim();
195
+ if (seen.has(key)) return false;
196
+ seen.add(key);
197
+ return true;
198
+ });
199
+ }
200
+ function normalizeContent(content, type) {
201
+ let text = content.trim();
202
+ for (const pattern of FILLER_PATTERNS) {
203
+ text = text.replace(pattern, "");
204
+ }
205
+ if (type === "rule") {
206
+ for (const [pattern, replacement] of IMPERATIVE_RULES) {
207
+ if (pattern.test(text)) {
208
+ text = text.replace(pattern, replacement);
209
+ break;
210
+ }
211
+ }
212
+ }
213
+ text = text.charAt(0).toUpperCase() + text.slice(1);
214
+ const firstSentenceEnd = text.search(/\. [A-Z]/);
215
+ if (firstSentenceEnd > 0) {
216
+ text = text.slice(0, firstSentenceEnd + 1);
217
+ }
218
+ return text;
219
+ }
220
+
221
+ // src/knowledge/passes/4-clusterer.ts
222
+ var CLUSTER_PROMPT_VERSION = "cluster-v1";
223
+ function buildClusteringPrompt(observations, existingItems) {
224
+ const obsJson = observations.map((o) => ({
225
+ id: o.id,
226
+ type: o.type,
227
+ content: o.content,
228
+ canonicalKey: o.canonicalKey
229
+ }));
230
+ const itemsJson = existingItems.filter((i) => i.status === "active").map((i) => ({
231
+ id: i.id,
232
+ type: i.type,
233
+ content: i.content,
234
+ canonicalHash: i.canonicalHash
235
+ }));
236
+ return `You are a knowledge clustering engine. Determine whether new observations create new knowledge or relate to existing items. Be conservative.
237
+
238
+ For each observation, assign an action:
239
+ CREATE \u2014 genuinely new knowledge, no existing item covers it
240
+ REINFORCE \u2014 confirms an existing item (provide targetKnowledgeItemId)
241
+ SUPERSEDE \u2014 replaces/corrects an existing item (provide targetKnowledgeItemId)
242
+ DEPRECATE \u2014 makes an existing item irrelevant (provide targetKnowledgeItemId)
243
+ IGNORE \u2014 duplicate, trivial, or too vague to keep
244
+
245
+ New observations:
246
+ ${JSON.stringify(obsJson, null, 2)}
247
+
248
+ Existing knowledge items:
249
+ ${JSON.stringify(itemsJson, null, 2)}
250
+
251
+ Return ONLY valid JSON: { "clusters": [{ "observationId": "...", "action": "CREATE"|"REINFORCE"|"SUPERSEDE"|"DEPRECATE"|"IGNORE", "targetKnowledgeItemId": "..." (only if action targets an existing item), "reasoning": "..." }] }`;
252
+ }
253
+ function parseClusteringResponse(text, observations) {
254
+ const jsonMatch = text.match(/\{[\s\S]*\}/);
255
+ if (!jsonMatch) {
256
+ return observations.map((o) => ({ observationId: o.id, action: "CREATE", reasoning: "Parse failure \u2014 defaulting to CREATE" }));
257
+ }
258
+ const parsed = JSON.parse(jsonMatch[0]);
259
+ if (!parsed.clusters || !Array.isArray(parsed.clusters)) {
260
+ return observations.map((o) => ({ observationId: o.id, action: "CREATE", reasoning: "Parse failure \u2014 defaulting to CREATE" }));
261
+ }
262
+ return parsed.clusters.map((c) => ({
263
+ observationId: c.observationId,
264
+ action: c.action || "CREATE",
265
+ targetKnowledgeItemId: c.targetKnowledgeItemId || void 0,
266
+ reasoning: c.reasoning || ""
267
+ }));
268
+ }
269
+
270
+ // src/knowledge/passes/5-contradiction.ts
271
+ var CONTRADICTION_PROMPT_VERSION = "contradiction-v1";
272
+
273
+ // src/knowledge/passes/6-compile.ts
274
+ import { randomUUID as randomUUID2 } from "crypto";
275
+ function calculateConfidence(count) {
276
+ if (count >= 7) return "high";
277
+ if (count >= 3) return "medium";
278
+ return "low";
279
+ }
280
+ function compileKnowledge(clusters, existingItems, observations, project, now) {
281
+ const itemMap = new Map(existingItems.map((i) => [i.id, { ...i }]));
282
+ const obsMap = new Map(observations.map((o) => [o.id, o]));
283
+ const result = {
284
+ created: [],
285
+ reinforced: [],
286
+ superseded: [],
287
+ deprecated: [],
288
+ ignored: 0
289
+ };
290
+ for (const cluster of clusters) {
291
+ const obs = obsMap.get(cluster.observationId);
292
+ if (!obs) continue;
293
+ switch (cluster.action) {
294
+ case "CREATE": {
295
+ const newItem = {
296
+ id: `ki_${randomUUID2().slice(0, 8)}`,
297
+ canonicalHash: computeCanonicalHash(obs.content),
298
+ type: obs.type,
299
+ title: obs.content.slice(0, 80),
300
+ content: obs.content,
301
+ confidence: "low",
302
+ observationCount: 1,
303
+ authority: "AUTO",
304
+ status: "active",
305
+ supersededById: void 0,
306
+ enforce: false,
307
+ project,
308
+ scope: obs.scope || (obs.type === "rule" || obs.type === "lesson" ? "global" : "project"),
309
+ createdAt: now,
310
+ updatedAt: now,
311
+ lastSeenAt: now,
312
+ metadata: {}
313
+ };
314
+ result.created.push(newItem);
315
+ break;
316
+ }
317
+ case "REINFORCE": {
318
+ const target = itemMap.get(cluster.targetKnowledgeItemId);
319
+ if (!target) break;
320
+ target.observationCount += 1;
321
+ target.lastSeenAt = now;
322
+ target.updatedAt = now;
323
+ target.confidence = calculateConfidence(target.observationCount);
324
+ result.reinforced.push(target);
325
+ break;
326
+ }
327
+ case "SUPERSEDE": {
328
+ const target = itemMap.get(cluster.targetKnowledgeItemId);
329
+ if (target) {
330
+ const newItem = {
331
+ id: `ki_${randomUUID2().slice(0, 8)}`,
332
+ canonicalHash: computeCanonicalHash(obs.content),
333
+ type: obs.type,
334
+ title: obs.content.slice(0, 80),
335
+ content: obs.content,
336
+ confidence: "low",
337
+ observationCount: 1,
338
+ authority: "AUTO",
339
+ status: "active",
340
+ supersededById: void 0,
341
+ enforce: false,
342
+ project,
343
+ scope: obs.scope || (obs.type === "rule" || obs.type === "lesson" ? "global" : "project"),
344
+ createdAt: now,
345
+ updatedAt: now,
346
+ lastSeenAt: now,
347
+ metadata: {}
348
+ };
349
+ target.status = "superseded";
350
+ target.updatedAt = now;
351
+ target.supersededById = newItem.id;
352
+ result.superseded.push(target);
353
+ result.created.push(newItem);
354
+ }
355
+ break;
356
+ }
357
+ case "DEPRECATE": {
358
+ const target = itemMap.get(cluster.targetKnowledgeItemId);
359
+ if (target) {
360
+ target.status = "deprecated";
361
+ target.updatedAt = now;
362
+ result.deprecated.push(target);
363
+ }
364
+ break;
365
+ }
366
+ case "IGNORE":
367
+ result.ignored += 1;
368
+ break;
369
+ }
370
+ }
371
+ return result;
372
+ }
373
+
374
+ // src/knowledge/passes/7-projector.ts
375
+ import { mkdirSync, writeFileSync } from "fs";
376
+ function projectToMarkdown(items, generatedDir, compilerVersion) {
377
+ mkdirSync(generatedDir, { recursive: true });
378
+ const active = items.filter((i) => i.status === "active");
379
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
380
+ const header = (title) => `<!-- AUTO-GENERATED BY AGENTCACHE v${compilerVersion} \u2014 DO NOT EDIT -->
381
+ <!-- Source of truth: .agentcache/agentcache.db -->
382
+ <!-- Last compiled: ${timestamp} | ${active.length} active items -->
383
+
384
+ # ${title}
385
+
386
+ `;
387
+ const rules = active.filter((i) => i.type === "rule").sort((a, b) => confidenceOrder(b) - confidenceOrder(a));
388
+ const lessons = active.filter((i) => i.type === "lesson").sort((a, b) => confidenceOrder(b) - confidenceOrder(a));
389
+ const decisions = active.filter((i) => i.type === "decision").sort((a, b) => confidenceOrder(b) - confidenceOrder(a));
390
+ const context = active.filter((i) => i.type === "context").sort((a, b) => b.lastSeenAt - a.lastSeenAt);
391
+ writeFileSync(
392
+ `${generatedDir}/RULES.md`,
393
+ header("Rules") + formatItems(rules),
394
+ "utf-8"
395
+ );
396
+ writeFileSync(
397
+ `${generatedDir}/LESSONS.md`,
398
+ header("Lessons") + formatItems(lessons),
399
+ "utf-8"
400
+ );
401
+ writeFileSync(
402
+ `${generatedDir}/DECISIONS.md`,
403
+ header("Decisions") + formatItems(decisions),
404
+ "utf-8"
405
+ );
406
+ writeFileSync(
407
+ `${generatedDir}/CONTEXT.md`,
408
+ header("Context") + formatItems(context),
409
+ "utf-8"
410
+ );
411
+ }
412
+ function confidenceOrder(item) {
413
+ switch (item.confidence) {
414
+ case "high":
415
+ return 3;
416
+ case "medium":
417
+ return 2;
418
+ case "low":
419
+ return 1;
420
+ }
421
+ }
422
+ function formatItems(items) {
423
+ if (items.length === 0) return "_No items yet._\n";
424
+ return items.map((i) => {
425
+ const badge = i.enforce ? " \u{1F6E1}\uFE0F" : "";
426
+ const conf = `(${i.confidence}, ${i.observationCount}\xD7)`;
427
+ return `- ${i.content}${badge} ${conf}`;
428
+ }).join("\n") + "\n";
429
+ }
430
+
431
+ // src/knowledge/passes/7b-skill-projector.ts
432
+ import { mkdirSync as mkdirSync2, writeFileSync as writeFileSync2 } from "fs";
433
+ import { join } from "path";
434
+ import { homedir } from "os";
435
+ var MAX_SKILL_TOKENS = 5e3;
436
+ var AVG_CHARS_PER_TOKEN = 4;
437
+ var MAX_SKILL_CHARS = MAX_SKILL_TOKENS * AVG_CHARS_PER_TOKEN;
438
+ function projectToSkills(items, projectRoot) {
439
+ const active = items.filter((i) => i.status === "active");
440
+ const globalItems = active.filter((i) => i.scope === "global");
441
+ const projectItems = active.filter((i) => i.scope === "project");
442
+ writeGlobalSkill(globalItems);
443
+ writeProjectSkill(projectItems, projectRoot);
444
+ }
445
+ function writeGlobalSkill(items) {
446
+ const skillDir = join(homedir(), ".agentcache", "skills", "developer-knowledge");
447
+ mkdirSync2(skillDir, { recursive: true });
448
+ const rules = items.filter((i) => i.type === "rule").sort(byConfidence);
449
+ const lessons = items.filter((i) => i.type === "lesson").sort(byConfidence);
450
+ const body = buildSkillBody(rules, lessons, [], []);
451
+ const content = buildSkillFile(
452
+ "developer-knowledge",
453
+ "Engineering rules and lessons learned across all projects \u2014 compiled automatically from coding sessions by AgentCache",
454
+ body
455
+ );
456
+ writeFileSync2(join(skillDir, "SKILL.md"), truncateToLimit(content), "utf-8");
457
+ }
458
+ function writeProjectSkill(items, projectRoot) {
459
+ if (!projectRoot || projectRoot === process.cwd()) return;
460
+ if (items.length === 0) return;
461
+ const skillDir = join(projectRoot, ".agentcache", "skills", "project-knowledge");
462
+ mkdirSync2(skillDir, { recursive: true });
463
+ const rules = items.filter((i) => i.type === "rule").sort(byConfidence);
464
+ const lessons = items.filter((i) => i.type === "lesson").sort(byConfidence);
465
+ const decisions = items.filter((i) => i.type === "decision").sort(byConfidence);
466
+ const context = items.filter((i) => i.type === "context").sort((a, b) => b.lastSeenAt - a.lastSeenAt);
467
+ const body = buildSkillBody(rules, lessons, decisions, context);
468
+ const content = buildSkillFile(
469
+ "project-knowledge",
470
+ "Project-specific decisions, rules, context, and lessons \u2014 compiled automatically from coding sessions by AgentCache",
471
+ body
472
+ );
473
+ writeFileSync2(join(skillDir, "SKILL.md"), truncateToLimit(content), "utf-8");
474
+ }
475
+ function buildSkillFile(name, description, body) {
476
+ return `---
477
+ name: ${name}
478
+ description: "${description}"
479
+ ---
480
+
481
+ ${body}`;
482
+ }
483
+ function buildSkillBody(rules, lessons, decisions, context) {
484
+ let out = "";
485
+ if (rules.length > 0) {
486
+ out += "## Rules\n\nFollow these without exception:\n\n";
487
+ out += rules.map((r) => `- ${r.content}${enforceTag(r)}`).join("\n") + "\n\n";
488
+ }
489
+ if (lessons.length > 0) {
490
+ out += "## Lessons\n\nPitfalls learned from past sessions:\n\n";
491
+ out += lessons.map((l) => `- ${l.content}`).join("\n") + "\n\n";
492
+ }
493
+ if (decisions.length > 0) {
494
+ out += "## Decisions\n\nArchitectural choices in effect \u2014 do not contradict:\n\n";
495
+ out += decisions.map((d) => `- ${d.content}`).join("\n") + "\n\n";
496
+ }
497
+ if (context.length > 0) {
498
+ out += "## Current Context\n\nActive project state (may be temporal):\n\n";
499
+ out += context.map((c) => `- ${c.content}`).join("\n") + "\n\n";
500
+ }
501
+ return out.trimEnd() + "\n";
502
+ }
503
+ function enforceTag(item) {
504
+ return item.enforce ? " [ENFORCED]" : "";
505
+ }
506
+ function byConfidence(a, b) {
507
+ const order = { high: 3, medium: 2, low: 1 };
508
+ return (order[b.confidence] || 0) - (order[a.confidence] || 0);
509
+ }
510
+ function truncateToLimit(content) {
511
+ if (content.length <= MAX_SKILL_CHARS) return content;
512
+ const lines = content.split("\n");
513
+ let result = "";
514
+ for (const line of lines) {
515
+ if ((result + line + "\n").length > MAX_SKILL_CHARS - 50) break;
516
+ result += line + "\n";
517
+ }
518
+ result += "\n<!-- Truncated to stay within 5000 token skill budget -->\n";
519
+ return result;
520
+ }
521
+
522
+ // src/knowledge/compiler.ts
523
+ var COMPILER_VERSION = "0.1.0";
524
+ function startCompile(events, sessionId, project, projectRoot, repo, transcriptPath) {
525
+ const git = getGitContext(projectRoot);
526
+ const session = {
527
+ id: sessionId,
528
+ project,
529
+ startedAt: Date.now() - 6e4,
530
+ endedAt: Date.now(),
531
+ gitBranch: git.branch,
532
+ gitCommit: git.commit,
533
+ provider: "agent",
534
+ model: "host-agent",
535
+ transcriptPath: transcriptPath || "",
536
+ observationCount: 0
537
+ };
538
+ repo.saveSession(session);
539
+ const prompt = buildExtractionPrompt(events);
540
+ return { sessionId, project, projectRoot, prompt };
541
+ }
542
+ function processExtraction(repo, responseText, sessionId, project, projectRoot) {
543
+ const rawObservations = parseExtractionResponse(responseText, sessionId, project);
544
+ const normalized = normalize(rawObservations);
545
+ const existingItems = repo.getKnowledgeItems(project, { status: "active" });
546
+ const existingKeys = existingItems.map((i) => computeCanonicalKey(i.content));
547
+ const canonicalized = canonicalize(normalized, existingKeys);
548
+ for (const obs of canonicalized.autoReinforced) {
549
+ const matchingItem = existingItems.find(
550
+ (item) => computeCanonicalKey(item.content) === obs.canonicalKey
551
+ );
552
+ if (matchingItem) {
553
+ const newCount = matchingItem.observationCount + 1;
554
+ const confidence = newCount >= 7 ? "high" : newCount >= 3 ? "medium" : "low";
555
+ repo.updateKnowledgeItem(matchingItem.id, {
556
+ observationCount: newCount,
557
+ lastSeenAt: Date.now(),
558
+ updatedAt: Date.now(),
559
+ confidence
560
+ });
561
+ }
562
+ }
563
+ repo.saveObservations(normalized);
564
+ if (canonicalized.needsClustering.length === 0) {
565
+ saveCompileRun(repo, sessionId, project, normalized.length, canonicalized.autoReinforced.length, 0, 0, 0, 0, 0, Date.now());
566
+ const activeItems = repo.getKnowledgeItems(project, { status: "active" });
567
+ projectToMarkdown(activeItems, getDataDir(), COMPILER_VERSION);
568
+ projectToSkills(activeItems, projectRoot);
569
+ return {
570
+ status: "complete",
571
+ diagnostics: formatDiagnostics(normalized.length, canonicalized.autoReinforced.length, 0, 0, 0, 0, 0, project, sessionId)
572
+ };
573
+ }
574
+ const clusteringPrompt = buildClusteringPrompt(canonicalized.needsClustering, existingItems);
575
+ return {
576
+ status: "needs_clustering",
577
+ clusteringPrompt,
578
+ sessionId
579
+ };
580
+ }
581
+ function processClustering(repo, responseText, sessionId, project, projectRoot) {
582
+ const startedAt = Date.now();
583
+ const existingItems = repo.getKnowledgeItems(project, { status: "active" });
584
+ const observations = repo.getObservations(project);
585
+ const sessionObs = observations.filter((o) => o.sessionId === sessionId);
586
+ const canonicalized = canonicalize(sessionObs);
587
+ const needsClustering = canonicalized.needsClustering;
588
+ const clusters = parseClusteringResponse(responseText, needsClustering);
589
+ const contradictions = [];
590
+ const supersedeActions = clusters.filter((c) => c.action === "SUPERSEDE");
591
+ for (const s of supersedeActions) {
592
+ if (s.targetKnowledgeItemId) {
593
+ const target = existingItems.find((i) => i.id === s.targetKnowledgeItemId);
594
+ if (target) {
595
+ contradictions.push({
596
+ id: `con_${randomUUID3().slice(0, 8)}`,
597
+ project,
598
+ itemAId: target.id,
599
+ itemBId: s.observationId,
600
+ topic: target.title.slice(0, 50),
601
+ description: `"${target.content}" superseded by new observation`,
602
+ recommendation: "keep_newer",
603
+ resolved: false,
604
+ createdAt: Date.now()
605
+ });
606
+ }
607
+ }
608
+ }
609
+ for (const c of contradictions) {
610
+ repo.saveContradiction(c);
611
+ }
612
+ const now = Date.now();
613
+ const compiled = compileKnowledge(clusters, existingItems, needsClustering, project, now);
614
+ for (const item of compiled.created) repo.saveKnowledgeItem(item);
615
+ for (const item of compiled.reinforced) {
616
+ repo.updateKnowledgeItem(item.id, {
617
+ observationCount: item.observationCount,
618
+ lastSeenAt: item.lastSeenAt,
619
+ updatedAt: item.updatedAt,
620
+ confidence: item.confidence
621
+ });
622
+ }
623
+ for (const item of compiled.superseded) {
624
+ repo.updateKnowledgeItem(item.id, {
625
+ status: item.status,
626
+ updatedAt: item.updatedAt,
627
+ supersededById: item.supersededById
628
+ });
629
+ }
630
+ for (const item of compiled.deprecated) {
631
+ repo.updateKnowledgeItem(item.id, { status: item.status, updatedAt: item.updatedAt });
632
+ }
633
+ const totalObs = sessionObs.length;
634
+ saveCompileRun(repo, sessionId, project, totalObs, 0, compiled.created.length, compiled.reinforced.length, compiled.superseded.length, compiled.deprecated.length, compiled.ignored, startedAt);
635
+ const activeItems = repo.getKnowledgeItems(project, { status: "active" });
636
+ projectToMarkdown(activeItems, getDataDir(), COMPILER_VERSION);
637
+ projectToSkills(activeItems, projectRoot);
638
+ return {
639
+ status: "complete",
640
+ diagnostics: formatDiagnostics(totalObs, 0, compiled.created.length, compiled.reinforced.length, compiled.superseded.length, compiled.deprecated.length, compiled.ignored, project, sessionId)
641
+ };
642
+ }
643
+ function saveCompileRun(repo, sessionId, project, observationsProcessed, autoReinforced, created, reinforced, superseded, deprecated, ignored, startedAt) {
644
+ const endedAt = Date.now();
645
+ const run = {
646
+ id: `cr_${randomUUID3().slice(0, 8)}`,
647
+ project,
648
+ sessionId,
649
+ compilerVersion: COMPILER_VERSION,
650
+ promptVersions: { extract: EXTRACT_PROMPT_VERSION, cluster: CLUSTER_PROMPT_VERSION, contradiction: CONTRADICTION_PROMPT_VERSION },
651
+ startedAt,
652
+ endedAt,
653
+ durationMs: endedAt - startedAt,
654
+ observationsProcessed,
655
+ knowledgeCreated: created,
656
+ knowledgeReinforced: reinforced + autoReinforced,
657
+ knowledgeDeprecated: deprecated,
658
+ knowledgeSuperseded: superseded,
659
+ knowledgeIgnored: ignored,
660
+ contradictionsDetected: 0,
661
+ diagnostics: ""
662
+ };
663
+ repo.saveCompileRun(run);
664
+ }
665
+ function formatDiagnostics(extracted, autoReinforced, created, reinforced, superseded, deprecated, ignored, project, sessionId) {
666
+ return [
667
+ `AgentCache Compiler v${COMPILER_VERSION}`,
668
+ `Project: ${project} | Session: ${sessionId}`,
669
+ ` ${extracted} observations processed`,
670
+ autoReinforced > 0 ? ` ${autoReinforced} auto-reinforced (no LLM needed)` : "",
671
+ ` ${created} new knowledge items`,
672
+ ` ${reinforced} reinforced`,
673
+ superseded > 0 ? ` ${superseded} superseded` : "",
674
+ deprecated > 0 ? ` ${deprecated} deprecated` : "",
675
+ ignored > 0 ? ` ${ignored} ignored` : ""
676
+ ].filter(Boolean).join("\n");
677
+ }
678
+
679
+ export {
680
+ computeCanonicalHash,
681
+ startCompile,
682
+ processExtraction,
683
+ processClustering
684
+ };