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
//! Self-evolution config: tunable thresholds for memory crystallization.
//!
//! Resolved at gateway startup from `config.ext.evolution` and stashed in a
//! process-wide `OnceLock`. Readers in `memory.rs`, `crystallizer.rs`, and
//! `meditation.rs` look up the live values without paying lock cost on the
//! hot path.
use std::sync::OnceLock;
// ---------------------------------------------------------------------------
// Resolved config (no Option<> — defaults applied)
// ---------------------------------------------------------------------------
#[derive(Debug, Clone)]
pub struct EvolutionConfig {
/// Master kill-switch. When false, no crystallization runs.
pub enabled: bool,
pub cluster: ClusterConfig,
pub promotion: PromotionConfig,
pub meditation: MeditationParams,
pub workflow: WorkflowConfig,
}
#[derive(Debug, Clone)]
pub struct WorkflowConfig {
/// Master kill-switch for workflow crystallization. Off by default —
/// users must explicitly opt in.
pub enabled: bool,
/// Difficulty score threshold (0.0-1.0) above which a turn is eligible.
pub score_threshold: f32,
/// Minimum tool calls — easier turns don't qualify even with high score.
pub min_tool_calls: usize,
/// Minimum tool errors — 0 allows clean-but-complex turns; 1+ requires
/// the agent had to recover from a failure (the "stepped on a landmine"
/// signal).
pub min_errors: usize,
/// Cap on workflow distillations per hour, process-wide.
pub max_per_hour: usize,
}
#[derive(Debug, Clone)]
pub struct ClusterConfig {
/// Minimum related Core docs needed to attempt distillation.
pub min_size: usize,
/// Cosine similarity threshold for "related".
pub similarity_threshold: f32,
}
#[derive(Debug, Clone)]
pub struct PromotionConfig {
/// Path 1: access_count alone wins promotion.
pub access_only: i64,
/// Path 2: importance alone wins promotion.
pub importance_only: f32,
/// Path 3a: access_count threshold when combined with importance.
pub both_access: i64,
/// Path 3b: importance threshold when combined with access_count.
pub both_importance: f32,
}
#[derive(Debug, Clone)]
pub struct MeditationParams {
/// Max clusters processed per meditation crystallize phase.
pub max_per_cycle: usize,
/// Cosine similarity threshold for dedup phase.
pub dedup_threshold: f32,
/// Days after crystallization before demoting source memories.
pub crystallized_ttl_days: u32,
}
impl Default for EvolutionConfig {
fn default() -> Self {
Self {
enabled: true,
cluster: ClusterConfig {
min_size: 3,
similarity_threshold: 0.75,
},
promotion: PromotionConfig {
access_only: 15,
importance_only: 0.9,
both_access: 5,
both_importance: 0.8,
},
meditation: MeditationParams {
max_per_cycle: 5,
dedup_threshold: 0.92,
crystallized_ttl_days: 7,
},
workflow: WorkflowConfig {
enabled: false,
score_threshold: 0.7,
min_tool_calls: 5,
min_errors: 1,
max_per_hour: 3,
},
}
}
}
impl EvolutionConfig {
/// Looser thresholds for testing — chat 2-3 turns on a topic and the
/// pipeline fires. NOT for production.
pub fn test_preset() -> Self {
Self {
enabled: true,
cluster: ClusterConfig {
min_size: 2,
similarity_threshold: 0.5,
},
promotion: PromotionConfig {
access_only: 3,
importance_only: 0.6,
both_access: 2,
both_importance: 0.5,
},
meditation: MeditationParams {
max_per_cycle: 1,
dedup_threshold: 0.92,
crystallized_ttl_days: 7,
},
workflow: WorkflowConfig {
enabled: true,
score_threshold: 0.4,
min_tool_calls: 2,
min_errors: 0,
max_per_hour: 5,
},
}
}
/// Build from raw schema. Preset (if any) sets the base; individual
/// fields override on top of that base.
pub fn from_raw(raw: Option<&crate::config::schema::EvolutionConfig>) -> Self {
let raw = match raw {
Some(r) => r,
None => return Self::default(),
};
let mut cfg = match raw.preset.as_deref() {
Some("test") => Self::test_preset(),
_ => Self::default(),
};
if let Some(v) = raw.enabled {
cfg.enabled = v;
}
if let Some(c) = &raw.cluster {
if let Some(v) = c.min_size {
cfg.cluster.min_size = v;
}
if let Some(v) = c.similarity_threshold {
cfg.cluster.similarity_threshold = v;
}
}
if let Some(p) = &raw.promotion {
if let Some(v) = p.access_only {
cfg.promotion.access_only = v;
}
if let Some(v) = p.importance_only {
cfg.promotion.importance_only = v;
}
if let Some(v) = p.both_access {
cfg.promotion.both_access = v;
}
if let Some(v) = p.both_importance {
cfg.promotion.both_importance = v;
}
}
if let Some(m) = &raw.meditation {
if let Some(v) = m.max_per_cycle {
cfg.meditation.max_per_cycle = v;
}
if let Some(v) = m.dedup_threshold {
cfg.meditation.dedup_threshold = v;
}
if let Some(v) = m.crystallized_ttl_days {
cfg.meditation.crystallized_ttl_days = v;
}
}
if let Some(w) = &raw.workflow {
if let Some(v) = w.enabled {
cfg.workflow.enabled = v;
}
if let Some(v) = w.score_threshold {
cfg.workflow.score_threshold = v;
}
if let Some(v) = w.min_tool_calls {
cfg.workflow.min_tool_calls = v;
}
if let Some(v) = w.min_errors {
cfg.workflow.min_errors = v;
}
if let Some(v) = w.max_per_hour {
cfg.workflow.max_per_hour = v;
}
}
cfg
}
}
// ---------------------------------------------------------------------------
// Process-wide singleton
// ---------------------------------------------------------------------------
static EVO: OnceLock<EvolutionConfig> = OnceLock::new();
/// Initialize the global evolution config. Called once during gateway
/// startup. Subsequent calls are ignored — this is intentional to keep
/// readers lock-free.
pub fn init_evolution_config(cfg: EvolutionConfig) {
let _ = EVO.set(cfg);
}
/// Get the current evolution config. Returns the default if not yet
/// initialized (early-boot safety).
pub fn evolution_config() -> &'static EvolutionConfig {
EVO.get_or_init(EvolutionConfig::default)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_match_pre_config_constants() {
let cfg = EvolutionConfig::default();
assert!(cfg.enabled);
assert_eq!(cfg.cluster.min_size, 3);
assert!((cfg.cluster.similarity_threshold - 0.75).abs() < 0.001);
assert_eq!(cfg.promotion.access_only, 15);
assert!((cfg.promotion.importance_only - 0.9).abs() < 0.001);
assert_eq!(cfg.promotion.both_access, 5);
assert!((cfg.promotion.both_importance - 0.8).abs() < 0.001);
assert_eq!(cfg.meditation.max_per_cycle, 5);
assert!((cfg.meditation.dedup_threshold - 0.92).abs() < 0.001);
assert_eq!(cfg.meditation.crystallized_ttl_days, 7);
}
#[test]
fn test_preset_is_looser() {
let d = EvolutionConfig::default();
let t = EvolutionConfig::test_preset();
assert!(t.cluster.min_size < d.cluster.min_size);
assert!(t.cluster.similarity_threshold < d.cluster.similarity_threshold);
assert!(t.promotion.access_only < d.promotion.access_only);
assert!(t.promotion.importance_only < d.promotion.importance_only);
}
#[test]
fn from_raw_none_returns_default() {
let cfg = EvolutionConfig::from_raw(None);
assert_eq!(cfg.cluster.min_size, 3);
}
#[test]
fn from_raw_preset_then_override() {
// Build a raw config with preset = "test" and an explicit override
// on min_size to verify the layering order.
let raw = crate::config::schema::EvolutionConfig {
enabled: None,
preset: Some("test".to_owned()),
cluster: Some(crate::config::schema::EvolutionClusterConfig {
min_size: Some(7), // override even after preset
similarity_threshold: None, // keep preset's 0.5
}),
promotion: None,
meditation: None,
workflow: None,
};
let cfg = EvolutionConfig::from_raw(Some(&raw));
assert_eq!(cfg.cluster.min_size, 7); // explicit override wins
assert!((cfg.cluster.similarity_threshold - 0.5).abs() < 0.001); // from preset
assert_eq!(cfg.promotion.access_only, 3); // from preset
}
}