|
| 1 | +// altimate_change - Training insights: self-improvement recommendations |
| 2 | +// Inspired by OpenClaw's crystallization pattern — surfaces actionable |
| 3 | +// recommendations based on training usage patterns. |
| 4 | +import { TrainingStore, type TrainingEntry } from "./store" |
| 5 | +import { TRAINING_MAX_PATTERNS_PER_KIND, type TrainingKind } from "./types" |
| 6 | + |
| 7 | +export interface TrainingInsight { |
| 8 | + type: "stale" | "high-value" | "near-limit" | "budget-warning" | "consolidation" |
| 9 | + severity: "info" | "warning" |
| 10 | + message: string |
| 11 | + entries?: string[] |
| 12 | +} |
| 13 | + |
| 14 | +export namespace TrainingInsights { |
| 15 | + /** |
| 16 | + * Analyze training entries and return actionable insights. |
| 17 | + * Lightweight — reads from disk only, no LLM calls. |
| 18 | + */ |
| 19 | + export async function analyze(): Promise<TrainingInsight[]> { |
| 20 | + const entries = await TrainingStore.list() |
| 21 | + if (entries.length === 0) return [] |
| 22 | + |
| 23 | + const insights: TrainingInsight[] = [] |
| 24 | + |
| 25 | + // 1. Stale entries: saved but never applied after being injected multiple sessions |
| 26 | + const stale = entries.filter((e) => e.meta.applied === 0 && isOlderThanDays(e.created, 7)) |
| 27 | + if (stale.length > 0) { |
| 28 | + insights.push({ |
| 29 | + type: "stale", |
| 30 | + severity: "info", |
| 31 | + message: `${stale.length} training entry/entries saved 7+ days ago but never applied. Consider reviewing or removing.`, |
| 32 | + entries: stale.map((e) => `${e.kind}/${e.name}`), |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + // 2. High-value entries: frequently applied, worth highlighting |
| 37 | + const highValue = entries.filter((e) => e.meta.applied >= 5).sort((a, b) => b.meta.applied - a.meta.applied) |
| 38 | + if (highValue.length > 0) { |
| 39 | + insights.push({ |
| 40 | + type: "high-value", |
| 41 | + severity: "info", |
| 42 | + message: `${highValue.length} high-value entry/entries (applied 5+ times). These are your most impactful training.`, |
| 43 | + entries: highValue.slice(0, 5).map((e) => `${e.kind}/${e.name} (${e.meta.applied}x)`), |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + // 3. Near-limit warnings per kind |
| 48 | + const counts = await TrainingStore.count() |
| 49 | + for (const [kind, count] of Object.entries(counts)) { |
| 50 | + if (count >= TRAINING_MAX_PATTERNS_PER_KIND - 2 && count < TRAINING_MAX_PATTERNS_PER_KIND) { |
| 51 | + insights.push({ |
| 52 | + type: "near-limit", |
| 53 | + severity: "warning", |
| 54 | + message: `${kind} entries near limit: ${count}/${TRAINING_MAX_PATTERNS_PER_KIND}. Consider consolidating before adding more.`, |
| 55 | + }) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // 4. Consolidation opportunities: multiple entries of same kind with similar names |
| 60 | + const byKind = new Map<TrainingKind, TrainingEntry[]>() |
| 61 | + for (const e of entries) { |
| 62 | + const list = byKind.get(e.kind) ?? [] |
| 63 | + list.push(e) |
| 64 | + byKind.set(e.kind, list) |
| 65 | + } |
| 66 | + for (const [kind, items] of byKind) { |
| 67 | + if (items.length < 2) continue |
| 68 | + // Find entries whose names share a common prefix (3+ chars) |
| 69 | + const groups = findRelatedEntries(items) |
| 70 | + for (const group of groups) { |
| 71 | + if (group.length >= 3) { |
| 72 | + insights.push({ |
| 73 | + type: "consolidation", |
| 74 | + severity: "info", |
| 75 | + message: `${group.length} related ${kind} entries could potentially be consolidated into one.`, |
| 76 | + entries: group.map((e) => e.name), |
| 77 | + }) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return insights |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Format insights for display in training_list output. |
| 87 | + */ |
| 88 | + export function format(insights: TrainingInsight[]): string { |
| 89 | + if (insights.length === 0) return "" |
| 90 | + const lines = ["\n### Insights"] |
| 91 | + for (const insight of insights) { |
| 92 | + const icon = insight.severity === "warning" ? "!" : "-" |
| 93 | + lines.push(`${icon} ${insight.message}`) |
| 94 | + if (insight.entries && insight.entries.length > 0) { |
| 95 | + for (const e of insight.entries.slice(0, 5)) { |
| 96 | + lines.push(` - \`${e}\``) |
| 97 | + } |
| 98 | + if (insight.entries.length > 5) { |
| 99 | + lines.push(` - ...and ${insight.entries.length - 5} more`) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + return lines.join("\n") |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function isOlderThanDays(dateStr: string, days: number): boolean { |
| 108 | + const created = new Date(dateStr) |
| 109 | + const cutoff = new Date() |
| 110 | + cutoff.setDate(cutoff.getDate() - days) |
| 111 | + return created < cutoff |
| 112 | +} |
| 113 | + |
| 114 | +function findRelatedEntries(entries: TrainingEntry[]): TrainingEntry[][] { |
| 115 | + // Group entries that share a common prefix of 3+ characters |
| 116 | + const groups: TrainingEntry[][] = [] |
| 117 | + const used = new Set<string>() |
| 118 | + |
| 119 | + for (let i = 0; i < entries.length; i++) { |
| 120 | + if (used.has(entries[i].name)) continue |
| 121 | + const group = [entries[i]] |
| 122 | + const prefix = entries[i].name.split("-")[0] |
| 123 | + if (prefix.length < 3) continue |
| 124 | + |
| 125 | + for (let j = i + 1; j < entries.length; j++) { |
| 126 | + if (used.has(entries[j].name)) continue |
| 127 | + if (entries[j].name.startsWith(prefix)) { |
| 128 | + group.push(entries[j]) |
| 129 | + used.add(entries[j].name) |
| 130 | + } |
| 131 | + } |
| 132 | + if (group.length >= 2) { |
| 133 | + used.add(entries[i].name) |
| 134 | + groups.push(group) |
| 135 | + } |
| 136 | + } |
| 137 | + return groups |
| 138 | +} |
0 commit comments