forked from yunus-0x/meridian
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlessons.js
More file actions
765 lines (671 loc) · 28.6 KB
/
Copy pathlessons.js
File metadata and controls
765 lines (671 loc) · 28.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/**
* Agent learning system.
*
* After each position closes, performance is analyzed and lessons are
* derived. These lessons are injected into the system prompt so the
* agent avoids repeating mistakes and doubles down on what works.
*/
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { log } from "./logger.js";
import { getSharedLessonsForPrompt, pushHiveLesson, pushHivePerformanceEvent } from "./hivemind.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const USER_CONFIG_PATH = path.join(__dirname, "user-config.json");
const LESSONS_FILE = "./lessons.json";
const MIN_EVOLVE_POSITIONS = 5; // don't evolve until we have real data
const MAX_CHANGE_PER_STEP = 0.20; // never shift a threshold more than 20% at once
const PERFORMANCE_SIGNAL_FIELDS = [
"organic_score",
"fee_tvl_ratio",
"volume",
"mcap",
"holder_count",
"smart_wallets_present",
"narrative_quality",
"study_win_rate",
"hive_consensus",
"volatility",
];
const MAX_MANUAL_LESSON_LENGTH = 400;
function sanitizeLessonText(text, maxLen = MAX_MANUAL_LESSON_LENGTH) {
if (text == null) return null;
const cleaned = String(text)
.replace(/[\r\n\t]+/g, " ")
.replace(/\s+/g, " ")
.replace(/[<>`]/g, "")
.trim()
.slice(0, maxLen);
return cleaned || null;
}
function load() {
if (!fs.existsSync(LESSONS_FILE)) {
return { lessons: [], performance: [] };
}
try {
return JSON.parse(fs.readFileSync(LESSONS_FILE, "utf8"));
} catch {
return { lessons: [], performance: [] };
}
}
function save(data) {
fs.writeFileSync(LESSONS_FILE, JSON.stringify(data, null, 2));
}
function buildSignalSnapshot(perf) {
const snapshot = { ...(perf.signal_snapshot || {}) };
if (perf.base_mint && snapshot.base_mint == null) snapshot.base_mint = perf.base_mint;
for (const field of PERFORMANCE_SIGNAL_FIELDS) {
if (snapshot[field] == null && perf[field] != null) {
snapshot[field] = perf[field];
}
}
return Object.values(snapshot).some((value) => value != null) ? snapshot : null;
}
// ─── Record Position Performance ──────────────────────────────
/**
* Call this when a position closes. Captures performance data and
* derives a lesson if the outcome was notably good or bad.
*
* @param {Object} perf
* @param {string} perf.position - Position address
* @param {string} perf.pool - Pool address
* @param {string} perf.pool_name - Pool name (e.g. "Mustard-SOL")
* @param {string} perf.strategy - "spot" | "curve" | "bid_ask"
* @param {number} perf.bin_range - Bin range used
* @param {number} perf.bin_step - Pool bin step
* @param {number} perf.volatility - Pool volatility at deploy time
* @param {number} perf.fee_tvl_ratio - fee/TVL ratio at deploy time
* @param {number} perf.organic_score - Token organic score at deploy time
* @param {number} perf.amount_sol - Amount deployed
* @param {number} perf.fees_earned_usd - Total fees earned
* @param {number} perf.final_value_usd - Value when closed
* @param {number} perf.initial_value_usd - Value when opened
* @param {number} perf.minutes_in_range - Total minutes position was in range
* @param {number} perf.minutes_held - Total minutes position was held
* @param {string} perf.close_reason - Why it was closed
*/
export async function recordPerformance(perf) {
const data = load();
// Guard against unit-mixed records where a SOL-sized final value is
// accidentally written into a USD field (e.g. final_value_usd = 2 for a 2 SOL close).
const suspiciousUnitMix =
Number.isFinite(perf.initial_value_usd) &&
Number.isFinite(perf.final_value_usd) &&
Number.isFinite(perf.amount_sol) &&
perf.initial_value_usd >= 20 &&
perf.amount_sol >= 0.25 &&
perf.final_value_usd > 0 &&
perf.final_value_usd <= perf.amount_sol * 2;
if (suspiciousUnitMix) {
log("lessons_warn", `Skipped suspicious performance record for ${perf.pool_name || perf.pool}: initial=${perf.initial_value_usd}, final=${perf.final_value_usd}, amount_sol=${perf.amount_sol}`);
return;
}
const pnl_usd = (perf.final_value_usd + perf.fees_earned_usd) - perf.initial_value_usd;
const pnl_pct = perf.initial_value_usd > 0
? (pnl_usd / perf.initial_value_usd) * 100
: 0;
const range_efficiency = perf.minutes_held > 0
? (perf.minutes_in_range / perf.minutes_held) * 100
: 0;
const closeReasonText = String(perf.close_reason || "").toLowerCase();
const suspiciousAbsurdClosedPnl =
Number.isFinite(pnl_pct) &&
perf.initial_value_usd >= 20 &&
pnl_pct <= -90 &&
!closeReasonText.includes("stop loss");
if (suspiciousAbsurdClosedPnl) {
log("lessons_warn", `Skipped absurd closed PnL record for ${perf.pool_name || perf.pool}: pnl_pct=${pnl_pct.toFixed(2)} reason=${perf.close_reason}`);
return;
}
const signalSnapshot = buildSignalSnapshot(perf);
const entry = {
...perf,
signal_snapshot: signalSnapshot,
pnl_usd: Math.round(pnl_usd * 100) / 100,
pnl_pct: Math.round(pnl_pct * 100) / 100,
range_efficiency: Math.round(range_efficiency * 10) / 10,
recorded_at: new Date().toISOString(),
};
data.performance.push(entry);
// Derive and store a lesson
const lesson = derivLesson(entry);
if (lesson) {
data.lessons.push(lesson);
log("lessons", `New lesson: ${lesson.rule}`);
}
save(data);
if (lesson) {
void pushHiveLesson(lesson);
}
// Update pool-level memory
if (perf.pool) {
const { recordPoolDeploy } = await import("./pool-memory.js");
recordPoolDeploy(perf.pool, {
pool_name: perf.pool_name,
base_mint: perf.base_mint,
deployed_at: perf.deployed_at,
closed_at: entry.recorded_at,
pnl_pct: entry.pnl_pct,
pnl_usd: entry.pnl_usd,
range_efficiency: entry.range_efficiency,
minutes_held: perf.minutes_held,
fees_earned_usd: perf.fees_earned_usd,
fees_earned_sol: perf.fees_earned_sol,
fee_earned_pct: perf.initial_value_usd > 0 ? ((perf.fees_earned_usd || 0) / perf.initial_value_usd) * 100 : null,
close_reason: perf.close_reason,
strategy: perf.strategy,
volatility: perf.volatility,
});
}
// Evolve thresholds every 5 closed positions
if (data.performance.length % MIN_EVOLVE_POSITIONS === 0) {
const { config, reloadScreeningThresholds } = await import("./config.js");
const result = evolveThresholds(data.performance, config);
if (result?.changes && Object.keys(result.changes).length > 0) {
reloadScreeningThresholds();
log("evolve", `Auto-evolved thresholds: ${JSON.stringify(result.changes)}`);
}
// Darwinian signal weight recalculation
if (config.darwin?.enabled) {
const { recalculateWeights } = await import("./signal-weights.js");
const wResult = recalculateWeights(data.performance, config);
if (wResult.changes.length > 0) {
log("evolve", `Darwin: adjusted ${wResult.changes.length} signal weight(s)`);
}
}
}
void pushHivePerformanceEvent({
...entry,
base_mint: perf.base_mint || null,
fees_earned_sol: perf.fees_earned_sol || 0,
eventId: `close:${perf.position}:${entry.recorded_at}`,
});
}
/**
* Derive a lesson from a closed position's performance.
* Only generates a lesson if the outcome was clearly good or bad.
*/
function derivLesson(perf) {
const tags = [];
const feeYieldPct = perf.initial_value_usd > 0
? ((perf.fees_earned_usd || 0) / perf.initial_value_usd) * 100
: 0;
// Categorize outcome
const outcome = perf.pnl_pct >= 5 ? "good"
: (perf.pnl_pct >= 0 && feeYieldPct >= 2) ? "good"
: perf.pnl_pct >= 0 ? "neutral"
: perf.pnl_pct >= -5 ? "poor"
: "bad";
if (outcome === "neutral") return null; // nothing interesting to learn
// Build context description
const context = [
`${perf.pool_name}`,
`strategy=${perf.strategy}`,
`bin_step=${perf.bin_step}`,
`volatility=${perf.volatility}`,
`fee_tvl_ratio=${perf.fee_tvl_ratio}`,
`organic=${perf.organic_score}`,
`bin_range=${typeof perf.bin_range === 'object' ? JSON.stringify(perf.bin_range) : perf.bin_range}`,
].join(", ");
let rule = "";
if (outcome === "good" || outcome === "bad") {
if (perf.range_efficiency < 30 && outcome === "bad") {
rule = `AVOID: ${perf.pool_name}-type pools (volatility=${perf.volatility}, bin_step=${perf.bin_step}) with strategy="${perf.strategy}" — went OOR ${100 - perf.range_efficiency}% of the time. Consider wider bin_range or bid_ask strategy.`;
tags.push("oor", perf.strategy, `volatility_${Math.round(perf.volatility)}`);
} else if (perf.range_efficiency > 80 && outcome === "good") {
rule = `PREFER: ${perf.pool_name}-type pools (volatility=${perf.volatility}, bin_step=${perf.bin_step}) with strategy="${perf.strategy}" — ${perf.range_efficiency}% in-range efficiency, PnL +${perf.pnl_pct}%.`;
tags.push("efficient", perf.strategy);
} else if (outcome === "bad" && perf.close_reason?.includes("volume")) {
rule = `AVOID: Pools with fee_tvl_ratio=${perf.fee_tvl_ratio} that showed volume collapse — fees evaporated quickly. Minimum sustained volume check needed before deploying.`;
tags.push("volume_collapse");
} else if (outcome === "good") {
rule = `WORKED: ${context} → PnL +${perf.pnl_pct}%, range efficiency ${perf.range_efficiency}%.`;
tags.push("worked");
} else {
rule = `FAILED: ${context} → PnL ${perf.pnl_pct}%, range efficiency ${perf.range_efficiency}%. Reason: ${perf.close_reason}.`;
tags.push("failed");
}
}
if (!rule) return null;
const closeReasonText = String(perf.close_reason || "").toLowerCase();
const positiveEvidence =
feeYieldPct >= 1 ||
(perf.fees_earned_usd || 0) >= 3 ||
perf.pnl_pct >= 3;
const negativeEvidence =
perf.pnl_pct <= -5 ||
perf.range_efficiency <= 30 ||
closeReasonText.includes("out of range") ||
closeReasonText.includes("oor") ||
closeReasonText.includes("low yield") ||
closeReasonText.includes("volume");
let confidence = 0.35;
if (outcome === "good") {
confidence = positiveEvidence ? 0.82 : 0.22;
} else if (outcome === "bad") {
confidence = negativeEvidence ? 0.88 : 0.45;
} else if (outcome === "poor") {
confidence = negativeEvidence ? 0.68 : 0.32;
}
return {
id: Date.now(),
rule,
tags,
outcome,
sourceType: "performance",
confidence: Math.round(confidence * 100) / 100,
context,
pnl_pct: perf.pnl_pct,
fees_earned_usd: perf.fees_earned_usd,
initial_value_usd: perf.initial_value_usd,
range_efficiency: perf.range_efficiency,
close_reason: perf.close_reason,
pool: perf.pool,
created_at: new Date().toISOString(),
};
}
// ─── Adaptive Threshold Evolution ──────────────────────────────
/**
* Analyze closed position performance and evolve screening thresholds.
* Writes changes to user-config.json and returns a summary.
*
* @param {Array} perfData - Array of performance records (from lessons.json)
* @param {Object} config - Live config object (mutated in place)
* @returns {{ changes: Object, rationale: Object } | null}
*/
export function evolveThresholds(perfData, config) {
if (!perfData || perfData.length < MIN_EVOLVE_POSITIONS) return null;
const winners = perfData.filter((p) => p.pnl_pct > 0);
const losers = perfData.filter((p) => p.pnl_pct < -5);
// Need at least some signal in both directions before adjusting
const hasSignal = winners.length >= 2 || losers.length >= 2;
if (!hasSignal) return null;
const changes = {};
const rationale = {};
// ── 1. maxVolatility ─────────────────────────────────────────
// If losers tend to cluster at higher volatility → tighten the ceiling.
// If winners span higher volatility safely → we can loosen a bit.
{
const winnerVols = winners.map((p) => p.volatility).filter(isFiniteNum);
const loserVols = losers.map((p) => p.volatility).filter(isFiniteNum);
const current = config.screening.maxVolatility;
if (loserVols.length >= 2) {
// 25th percentile of loser volatilities — this is where things start going wrong
const loserP25 = percentile(loserVols, 25);
if (loserP25 < current) {
// Tighten: new ceiling = loserP25 + a small buffer
const target = loserP25 * 1.15;
const newVal = clamp(nudge(current, target, MAX_CHANGE_PER_STEP), 1.0, 20.0);
const rounded = Number(newVal.toFixed(1));
if (rounded < current) {
changes.maxVolatility = rounded;
rationale.maxVolatility = `Losers clustered at volatility ~${loserP25.toFixed(1)} — tightened from ${current} → ${rounded}`;
}
}
} else if (winnerVols.length >= 3 && losers.length === 0) {
// All winners so far — loosen conservatively so we don't miss good pools
const winnerP75 = percentile(winnerVols, 75);
if (winnerP75 > current * 1.1) {
const target = winnerP75 * 1.1;
const newVal = clamp(nudge(current, target, MAX_CHANGE_PER_STEP), 1.0, 20.0);
const rounded = Number(newVal.toFixed(1));
if (rounded > current) {
changes.maxVolatility = rounded;
rationale.maxVolatility = `All ${winners.length} positions profitable — loosened from ${current} → ${rounded}`;
}
}
}
}
// ── 2. minFeeTvlRatio ─────────────────────────────────────────
// Raise the floor if low-fee pools consistently underperform.
{
const winnerFees = winners.map((p) => p.fee_tvl_ratio).filter(isFiniteNum);
const loserFees = losers.map((p) => p.fee_tvl_ratio).filter(isFiniteNum);
const current = config.screening.minFeeTvlRatio;
if (winnerFees.length >= 2) {
// Minimum fee/TVL among winners — we know pools below this don't work for us
const minWinnerFee = Math.min(...winnerFees);
if (minWinnerFee > current * 1.2) {
const target = minWinnerFee * 0.85; // stay slightly below min winner
const newVal = clamp(nudge(current, target, MAX_CHANGE_PER_STEP), 0.05, 10.0);
const rounded = Number(newVal.toFixed(2));
if (rounded > current) {
changes.minFeeTvlRatio = rounded;
rationale.minFeeTvlRatio = `Lowest winner fee_tvl=${minWinnerFee.toFixed(2)} — raised floor from ${current} → ${rounded}`;
}
}
}
if (loserFees.length >= 2) {
// If losers all had high fee/TVL, that's noise (pumps then crash) — don't raise min
// But if losers had low fee/TVL, raise min
const maxLoserFee = Math.max(...loserFees);
if (maxLoserFee < current * 1.5 && winnerFees.length > 0) {
const minWinnerFee = Math.min(...winnerFees);
if (minWinnerFee > maxLoserFee) {
const target = maxLoserFee * 1.2;
const newVal = clamp(nudge(current, target, MAX_CHANGE_PER_STEP), 0.05, 10.0);
const rounded = Number(newVal.toFixed(2));
if (rounded > current && !changes.minFeeTvlRatio) {
changes.minFeeTvlRatio = rounded;
rationale.minFeeTvlRatio = `Losers had fee_tvl<=${maxLoserFee.toFixed(2)}, winners higher — raised floor from ${current} → ${rounded}`;
}
}
}
}
}
// ── 3. minOrganic ─────────────────────────────────────────────
// Raise organic floor if low-organic tokens consistently failed.
{
const loserOrganics = losers.map((p) => p.organic_score).filter(isFiniteNum);
const winnerOrganics = winners.map((p) => p.organic_score).filter(isFiniteNum);
const current = config.screening.minOrganic;
if (loserOrganics.length >= 2 && winnerOrganics.length >= 1) {
const avgLoserOrganic = avg(loserOrganics);
const avgWinnerOrganic = avg(winnerOrganics);
// Only raise if there's a clear gap (winners consistently more organic)
if (avgWinnerOrganic - avgLoserOrganic >= 10) {
// Set floor just below worst winner
const minWinnerOrganic = Math.min(...winnerOrganics);
const target = Math.max(minWinnerOrganic - 3, current);
const newVal = clamp(Math.round(nudge(current, target, MAX_CHANGE_PER_STEP)), 60, 90);
if (newVal > current) {
changes.minOrganic = newVal;
rationale.minOrganic = `Winner avg organic ${avgWinnerOrganic.toFixed(0)} vs loser avg ${avgLoserOrganic.toFixed(0)} — raised from ${current} → ${newVal}`;
}
}
}
}
if (Object.keys(changes).length === 0) return { changes: {}, rationale: {} };
// ── Persist changes to user-config.json ───────────────────────
let userConfig = {};
if (fs.existsSync(USER_CONFIG_PATH)) {
try { userConfig = JSON.parse(fs.readFileSync(USER_CONFIG_PATH, "utf8")); } catch { /* ignore */ }
}
Object.assign(userConfig, changes);
userConfig._lastEvolved = new Date().toISOString();
userConfig._positionsAtEvolution = perfData.length;
fs.writeFileSync(USER_CONFIG_PATH, JSON.stringify(userConfig, null, 2));
// Apply to live config object immediately
const s = config.screening;
if (changes.maxVolatility != null) s.maxVolatility = changes.maxVolatility;
if (changes.minFeeTvlRatio != null) s.minFeeTvlRatio = changes.minFeeTvlRatio;
if (changes.minOrganic != null) s.minOrganic = changes.minOrganic;
// Log a lesson summarizing the evolution
const data = load();
data.lessons.push({
id: Date.now(),
rule: `[AUTO-EVOLVED @ ${perfData.length} positions] ${Object.entries(changes).map(([k, v]) => `${k}=${v}`).join(", ")} — ${Object.values(rationale).join("; ")}`,
tags: ["evolution", "config_change"],
outcome: "manual",
created_at: new Date().toISOString(),
});
save(data);
return { changes, rationale };
}
// ─── Helpers ───────────────────────────────────────────────────
function isFiniteNum(n) {
return typeof n === "number" && isFinite(n);
}
function avg(arr) {
return arr.reduce((s, x) => s + x, 0) / arr.length;
}
function percentile(arr, p) {
const sorted = [...arr].sort((a, b) => a - b);
const idx = (p / 100) * (sorted.length - 1);
const lo = Math.floor(idx);
const hi = Math.ceil(idx);
return sorted[lo] + (sorted[hi] - sorted[lo]) * (idx - lo);
}
function clamp(val, min, max) {
return Math.max(min, Math.min(max, val));
}
/** Move current toward target by at most maxChange fraction. */
function nudge(current, target, maxChange) {
const delta = target - current;
const maxDelta = current * maxChange;
if (Math.abs(delta) <= maxDelta) return target;
return current + Math.sign(delta) * maxDelta;
}
// ─── Manual Lessons ────────────────────────────────────────────
/**
* Add a manual lesson (e.g. from operator observation).
*
* @param {string} rule
* @param {string[]} tags
* @param {Object} opts
* @param {boolean} opts.pinned - Always inject regardless of cap
* @param {string} opts.role - "SCREENER" | "MANAGER" | "GENERAL" | null (all roles)
*/
export function addLesson(rule, tags = [], { pinned = false, role = null } = {}) {
const safeRule = sanitizeLessonText(rule);
if (!safeRule) return;
const data = load();
const lesson = {
id: Date.now(),
rule: safeRule,
tags,
outcome: "manual",
sourceType: tags.includes("self_tune") || tags.includes("config_change") ? "config_change" : "manual",
pinned: !!pinned,
role: role || null,
created_at: new Date().toISOString(),
};
data.lessons.push(lesson);
save(data);
log("lessons", `Manual lesson added${pinned ? " [PINNED]" : ""}${role ? ` [${role}]` : ""}: ${safeRule}`);
void pushHiveLesson(lesson);
}
/**
* Pin a lesson by ID — pinned lessons are always injected regardless of cap.
*/
export function pinLesson(id) {
const data = load();
const lesson = data.lessons.find((l) => l.id === id);
if (!lesson) return { found: false };
lesson.pinned = true;
save(data);
log("lessons", `Pinned lesson ${id}: ${lesson.rule.slice(0, 60)}`);
return { found: true, pinned: true, id, rule: lesson.rule };
}
/**
* Unpin a lesson by ID.
*/
export function unpinLesson(id) {
const data = load();
const lesson = data.lessons.find((l) => l.id === id);
if (!lesson) return { found: false };
lesson.pinned = false;
save(data);
return { found: true, pinned: false, id, rule: lesson.rule };
}
/**
* List lessons with optional filters — for agent browsing via Telegram.
*/
export function listLessons({ role = null, pinned = null, tag = null, limit = 30 } = {}) {
const data = load();
let lessons = [...data.lessons];
if (pinned !== null) lessons = lessons.filter((l) => !!l.pinned === pinned);
if (role) lessons = lessons.filter((l) => !l.role || l.role === role);
if (tag) lessons = lessons.filter((l) => l.tags?.includes(tag));
return {
total: lessons.length,
lessons: lessons.slice(-limit).map((l) => ({
id: l.id,
rule: l.rule.slice(0, 120),
tags: l.tags,
outcome: l.outcome,
pinned: !!l.pinned,
role: l.role || "all",
created_at: l.created_at?.slice(0, 10),
})),
};
}
/**
* Remove lessons matching a keyword in their rule text (case-insensitive).
*/
export function removeLessonsByKeyword(keyword) {
const data = load();
const before = data.lessons.length;
const kw = keyword.toLowerCase();
data.lessons = data.lessons.filter((l) => !l.rule.toLowerCase().includes(kw));
save(data);
return before - data.lessons.length;
}
/**
* Clear ALL lessons (keeps performance data).
*/
export function clearAllLessons() {
const data = load();
const count = data.lessons.length;
data.lessons = [];
save(data);
return count;
}
/**
* Clear ALL performance records.
*/
export function clearPerformance() {
const data = load();
const count = data.performance.length;
data.performance = [];
save(data);
return count;
}
// ─── Lesson Retrieval ──────────────────────────────────────────
// Tags that map to each agent role — used for role-aware lesson injection
const ROLE_TAGS = {
SCREENER: ["screening", "narrative", "strategy", "deployment", "token", "volume", "entry", "bundler", "holders", "organic"],
MANAGER: ["management", "risk", "oor", "fees", "position", "hold", "close", "pnl", "rebalance", "claim"],
GENERAL: [], // all lessons
};
/**
* Get lessons formatted for injection into the system prompt.
* Structured injection with three tiers:
* 1. Pinned — always injected, up to PINNED_CAP
* 2. Role-matched — lessons tagged for this agentType, up to ROLE_CAP
* 3. Recent — fill remaining slots up to RECENT_CAP
*
* @param {Object} opts
* @param {string} [opts.agentType] - "SCREENER" | "MANAGER" | "GENERAL"
* @param {number} [opts.maxLessons] - Override total cap (default 35)
*/
export function getLessonsForPrompt(opts = {}) {
// Support legacy call signature: getLessonsForPrompt(20)
if (typeof opts === "number") opts = { maxLessons: opts };
const { agentType = "GENERAL", maxLessons } = opts;
const data = load();
if (data.lessons.length === 0) return null;
// Smaller caps for automated cycles — they don't need the full lesson history
const isAutoCycle = agentType === "SCREENER" || agentType === "MANAGER";
const PINNED_CAP = isAutoCycle ? 5 : 10;
const ROLE_CAP = isAutoCycle ? 6 : 15;
const RECENT_CAP = maxLessons ?? (isAutoCycle ? 10 : 35);
const outcomePriority = { bad: 0, poor: 1, failed: 1, good: 2, worked: 2, manual: 1, neutral: 3, evolution: 2 };
const byPriority = (a, b) => (outcomePriority[a.outcome] ?? 3) - (outcomePriority[b.outcome] ?? 3);
// ── Tier 1: Pinned ──────────────────────────────────────────────
// Respect role even for pinned lessons — a pinned SCREENER lesson shouldn't pollute MANAGER
const pinned = data.lessons
.filter((l) => l.pinned && (!l.role || l.role === agentType || agentType === "GENERAL"))
.sort(byPriority)
.slice(0, PINNED_CAP);
const usedIds = new Set(pinned.map((l) => l.id));
// ── Tier 2: Role-matched ────────────────────────────────────────
const roleTags = ROLE_TAGS[agentType] || [];
const roleMatched = data.lessons
.filter((l) => {
if (usedIds.has(l.id)) return false;
// Include if: lesson has no role restriction OR matches this role
const roleOk = !l.role || l.role === agentType || agentType === "GENERAL";
// Include if: lesson has role-relevant tags OR no tags (general)
const tagOk = roleTags.length === 0 || !l.tags?.length || l.tags.some((t) => roleTags.includes(t));
return roleOk && tagOk;
})
.sort(byPriority)
.slice(0, ROLE_CAP);
roleMatched.forEach((l) => usedIds.add(l.id));
// ── Tier 3: Recent fill ─────────────────────────────────────────
const remainingBudget = RECENT_CAP - pinned.length - roleMatched.length;
const recent = remainingBudget > 0
? data.lessons
.filter((l) => !usedIds.has(l.id))
.sort((a, b) => (b.created_at || "").localeCompare(a.created_at || ""))
.slice(0, remainingBudget)
: [];
const selected = [...pinned, ...roleMatched, ...recent];
const shared = getSharedLessonsForPrompt({
agentType,
maxLessons: isAutoCycle ? 4 : 6,
});
if (selected.length === 0 && !shared) return null;
const sections = [];
if (pinned.length) sections.push(`── PINNED (${pinned.length}) ──\n` + fmt(pinned));
if (roleMatched.length) sections.push(`── ${agentType} (${roleMatched.length}) ──\n` + fmt(roleMatched));
if (recent.length) sections.push(`── RECENT (${recent.length}) ──\n` + fmt(recent));
if (shared) sections.push(`── HIVEMIND ──\n${shared}`);
return sections.join("\n\n");
}
function fmt(lessons) {
return lessons.map((l) => {
const date = l.created_at ? l.created_at.slice(0, 16).replace("T", " ") : "unknown";
const pin = l.pinned ? "📌 " : "";
return `${pin}[${l.outcome.toUpperCase()}] [${date}] ${l.rule}`;
}).join("\n");
}
/**
* Get individual performance records filtered by time window.
* Tool handler: get_performance_history
*
* @param {Object} opts
* @param {number} [opts.hours=24] - How many hours back to look
* @param {number} [opts.limit=50] - Max records to return
*/
export function getPerformanceHistory({ hours = 24, limit = 50 } = {}) {
const data = load();
const p = data.performance;
if (p.length === 0) return { positions: [], count: 0, hours };
const cutoff = new Date(Date.now() - hours * 60 * 60 * 1000).toISOString();
const filtered = p
.filter((r) => r.recorded_at >= cutoff)
.slice(-limit)
.map((r) => ({
pool_name: r.pool_name,
pool: r.pool,
strategy: r.strategy,
pnl_usd: r.pnl_usd,
pnl_pct: r.pnl_pct,
fees_earned_usd: r.fees_earned_usd,
range_efficiency: r.range_efficiency,
minutes_held: r.minutes_held,
close_reason: r.close_reason,
closed_at: r.recorded_at,
}));
const totalPnl = filtered.reduce((s, r) => s + (r.pnl_usd ?? 0), 0);
const wins = filtered.filter((r) => r.pnl_usd > 0).length;
return {
hours,
count: filtered.length,
total_pnl_usd: Math.round(totalPnl * 100) / 100,
win_rate_pct: filtered.length > 0 ? Math.round((wins / filtered.length) * 100) : null,
positions: filtered,
};
}
/**
* Get performance stats summary.
*/
export function getPerformanceSummary() {
const data = load();
const p = data.performance;
if (p.length === 0) return null;
const totalPnl = p.reduce((s, x) => s + x.pnl_usd, 0);
const avgPnlPct = p.reduce((s, x) => s + x.pnl_pct, 0) / p.length;
const avgRangeEfficiency = p.reduce((s, x) => s + x.range_efficiency, 0) / p.length;
const wins = p.filter((x) => x.pnl_usd > 0).length;
return {
total_positions_closed: p.length,
total_pnl_usd: Math.round(totalPnl * 100) / 100,
avg_pnl_pct: Math.round(avgPnlPct * 100) / 100,
avg_range_efficiency_pct: Math.round(avgRangeEfficiency * 10) / 10,
win_rate_pct: Math.round((wins / p.length) * 100),
total_lessons: data.lessons.length,
};
}