context_weaver 0.1.0

A lorebook engine for LLM role-playing applications, built on weaver_lang
Documentation
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
//! Activation engine for entry matching.
//!
//! Scans chat messages for keywords and regex patterns to determine which
//! lorebook entries should activate, then optionally evaluates weaver-lang
//! condition expressions for fine-grained control.
//!
//! Activation flow:
//! 1. Constant entries are always activated.
//! 2. Messages within scan depth are searched for keyword/regex matches.
//!    Sticky entries participate in this scan — a fresh match supersedes
//!    the carry-forward and resets their countdown.
//! 3. Matched entries have their optional `condition` expression evaluated.
//! 4. Cooldown is checked — recently-fired entries are suppressed.
//! 5. Sticky entries that were NOT freshly matched are carried forward
//!    with their existing countdown.
//! 6. Results are returned as a sorted list of entry IDs.

use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

use weaver_lang::{CompiledExpr, Registry};

use crate::host::WeaverHost;
use crate::lorebook::Lorebook;
use crate::ChatMessage;

// ── Activation result ───────────────────────────────────────────────────

/// Why an entry was activated — useful for debugging and UI display.
#[derive(Debug, Clone)]
pub enum ActivationReason {
    /// Entry is marked `constant: true`.
    Constant,
    /// Matched one or more keywords.
    Keyword { matched: Vec<String> },
    /// Matched a regex pattern.
    Regex { pattern: String },
    /// Activated by a `<trigger>` in another entry's output.
    Triggered,
    /// Still active from a previous turn (sticky).
    Sticky { remaining_turns: usize },
}

/// The result of an activation scan for a single entry.
#[derive(Debug)]
pub struct ActivationResult {
    pub entry_id: String,
    pub reason: ActivationReason,
    pub priority: i32,
}

// ── Activation state (persisted between turns) ──────────────────────────

/// Tracks per-entry activation state across conversation turns.
///
/// Owned by [`ContextWeaver`](crate::ContextWeaver) and updated after
/// each assembly pass. Serializable alongside persistent state for
/// save/load support.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ActivationState {
    /// Entry ID → turn number when it was last activated.
    last_activated: HashMap<String, usize>,
    /// Entry ID → remaining sticky turns.
    sticky_remaining: HashMap<String, usize>,
    /// The current turn number, advanced by the engine.
    current_turn: usize,
}

impl ActivationState {
    pub fn new() -> Self {
        Self::default()
    }

    /// Advance to the next turn. Decrements sticky counters.
    pub fn advance_turn(&mut self) {
        self.current_turn += 1;
        self.sticky_remaining.retain(|_, turns| {
            *turns = turns.saturating_sub(1);
            *turns > 0
        });
    }

    /// Current turn number.
    pub fn current_turn(&self) -> usize {
        self.current_turn
    }

    /// Record that an entry was freshly activated on the current turn.
    ///
    /// Updates the `last_activated` timestamp (used for cooldown checks)
    /// and sets (or resets) the sticky counter.
    ///
    /// This should only be called for **fresh** activations (keyword,
    /// regex, constant, triggered) — NOT for sticky carry-forwards.
    /// The caller (assemble Phase 4) is responsible for distinguishing
    /// the two cases via [`ActivationReason`].
    ///
    /// The counter is stored as `sticky_turns + 1` because `advance_turn()`
    /// is called *before* `assemble()`. Without the +1, the first
    /// advance after activation would immediately consume one count
    /// before the entry ever gets carried forward, making
    /// `sticky_turns: N` behave as N-1 additional turns.
    pub fn record_activation(&mut self, entry_id: &str, sticky_turns: usize) {
        self.last_activated
            .insert(entry_id.to_string(), self.current_turn);
        if sticky_turns > 0 {
            self.sticky_remaining
                .insert(entry_id.to_string(), sticky_turns + 1);
        }
    }

    /// Check if an entry is on cooldown.
    pub fn is_on_cooldown(&self, entry_id: &str, cooldown: usize) -> bool {
        if cooldown == 0 {
            return false;
        }
        self.last_activated
            .get(entry_id)
            .is_some_and(|last| self.current_turn - last < cooldown)
    }

    /// Check if an entry is still sticky-active. Returns remaining turns.
    pub fn is_sticky(&self, entry_id: &str) -> Option<usize> {
        self.sticky_remaining.get(entry_id).copied()
    }

    /// Get all currently-sticky entry IDs with their remaining turns.
    pub fn sticky_entries(&self) -> impl Iterator<Item = (&str, usize)> {
        self.sticky_remaining
            .iter()
            .map(|(id, turns)| (id.as_str(), *turns))
    }
}

// ── Engine ──────────────────────────────────────────────────────────────

pub struct ActivationEngine;

impl ActivationEngine {
    /// Scan chat messages and determine which entries should activate.
    ///
    /// This is the main entry point for Tier 1 activation. It handles:
    /// - Constant entries (always active)
    /// - Keyword and regex matching against recent messages
    /// - Condition expression evaluation (Tier 1.5)
    /// - Cooldown suppression
    /// - Sticky entries carried from previous turns (as fallback)
    ///
    /// Sticky entries participate in keyword/regex scanning. If they
    /// re-match, they are treated as a fresh activation (which resets
    /// their sticky counter in Phase 4 of assembly). If they don't
    /// re-match, they are carried forward with their existing countdown.
    ///
    /// Returns entry IDs in priority order (highest first).
    pub fn scan(
        lorebook: &Lorebook,
        messages: &[ChatMessage],
        host: &mut WeaverHost,
        registry: &Registry,
        state: &ActivationState,
    ) -> Vec<ActivationResult> {
        let config = &lorebook.config;
        let case_sensitive = config.case_sensitive_keywords;

        let mut activated: Vec<ActivationResult> = Vec::new();
        let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();

        // Collect sticky entries. These will be carried forward ONLY if
        // they don't get a fresh keyword/regex/constant activation below.
        let mut sticky_carry: HashMap<String, (usize, i32)> = HashMap::new();
        for (entry_id, remaining) in state.sticky_entries() {
            if let Some(entry) = lorebook.get_entry(entry_id) {
                if entry.meta.enabled {
                    sticky_carry.insert(entry_id.to_string(), (remaining, entry.meta.priority));
                }
            }
        }

        // Scan all entries — sticky entries are NOT pre-added to `seen`,
        // so they go through the normal keyword/regex path.
        for entry in lorebook.active_entries() {
            let meta = &entry.meta;

            // Skip entries on cooldown
            if state.is_on_cooldown(&meta.id, meta.cooldown) {
                continue;
            }

            // Constant entries always activate (skip keyword/regex check)
            if meta.constant {
                if check_condition(&entry.condition, host, registry) {
                    seen.insert(meta.id.clone());
                    sticky_carry.remove(&meta.id);
                    activated.push(ActivationResult {
                        entry_id: meta.id.clone(),
                        reason: ActivationReason::Constant,
                        priority: meta.priority,
                    });
                }
                continue;
            }

            // Determine scan depth for this entry
            let scan_depth = meta.scan_depth.unwrap_or(config.default_scan_depth);

            // Get the messages within scan depth
            let scan_messages = if messages.len() > scan_depth {
                &messages[messages.len() - scan_depth..]
            } else {
                messages
            };

            // Build the text corpus to search
            let corpus: String = scan_messages
                .iter()
                .map(|m| m.content.as_str())
                .collect::<Vec<_>>()
                .join("\n");

            let corpus_normalized = if case_sensitive {
                corpus.clone()
            } else {
                corpus.to_lowercase()
            };

            // Try keyword match
            let keyword_match = check_keywords(&meta.keywords, &corpus_normalized, case_sensitive);

            // Try regex match (using pre-compiled regexes from Entry)
            let regex_match = if keyword_match.is_none() {
                check_regex_compiled(&entry.compiled_regex, &meta.regex, &corpus)
            } else {
                None
            };

            // Determine activation reason (keyword takes precedence)
            let reason = keyword_match.or(regex_match);

            if let Some(reason) = reason {
                // Check condition
                if check_condition(&entry.condition, host, registry) {
                    seen.insert(meta.id.clone());
                    sticky_carry.remove(&meta.id);
                    activated.push(ActivationResult {
                        entry_id: meta.id.clone(),
                        reason,
                        priority: meta.priority,
                    });
                }
            }
        }

        // Carry forward sticky entries that weren't freshly activated
        for (id, (remaining, priority)) in sticky_carry {
            if !seen.contains(&id) {
                activated.push(ActivationResult {
                    entry_id: id,
                    reason: ActivationReason::Sticky {
                        remaining_turns: remaining,
                    },
                    priority,
                });
            }
        }

        // Sort by priority (descending)
        activated.sort_by(|a, b| b.priority.cmp(&a.priority));

        activated
    }

    /// Filter triggered entry IDs through activation rules.
    ///
    /// Called after an evaluation pass drains trigger IDs from the host.
    /// Applies cooldown and condition checks to the triggered candidates.
    pub fn filter_triggered(
        lorebook: &Lorebook,
        triggered_ids: &[String],
        already_active: &[String],
        host: &mut WeaverHost,
        registry: &Registry,
        state: &ActivationState,
    ) -> Vec<ActivationResult> {
        let mut results = Vec::new();

        for id in triggered_ids {
            // Skip already-active entries UNLESS they are sticky
            // (triggers can refresh sticky entries)
            if already_active.contains(id) && state.is_sticky(id).is_none() {
                continue;
            }

            if let Some(entry) = lorebook.get_entry(id) {
                let meta = &entry.meta;

                if !meta.enabled {
                    continue;
                }

                if state.is_on_cooldown(&meta.id, meta.cooldown) {
                    continue;
                }

                if check_condition(&entry.condition, host, registry) {
                    results.push(ActivationResult {
                        entry_id: meta.id.clone(),
                        reason: ActivationReason::Triggered,
                        priority: meta.priority,
                    });
                }
            }
        }

        results.sort_by(|a, b| b.priority.cmp(&a.priority));
        results
    }
}

// ── Condition evaluation ────────────────────────────────────────────────

/// Evaluate an entry's optional condition expression.
///
/// Returns `true` if the condition is met (or if there is no condition).
/// Failed evaluations (type errors, missing variables, etc.) return `false`
/// — a broken condition should not silently activate an entry.
fn check_condition(
    condition: &Option<Arc<CompiledExpr>>,
    host: &mut WeaverHost,
    registry: &Registry,
) -> bool {
    match condition {
        None => true,
        Some(expr) => match expr.evaluate(host, registry) {
            Ok(val) => val.is_truthy(),
            Err(_) => false,
        },
    }
}

// ── Matching helpers ────────────────────────────────────────────────────

fn check_keywords(
    keywords: &[String],
    corpus: &str,
    case_sensitive: bool,
) -> Option<ActivationReason> {
    if keywords.is_empty() {
        return None;
    }

    let matched: Vec<String> = keywords
        .iter()
        .filter(|kw| {
            let kw_normalized = if case_sensitive {
                kw.to_string()
            } else {
                kw.to_lowercase()
            };
            corpus.contains(&kw_normalized)
        })
        .cloned()
        .collect();

    if matched.is_empty() {
        None
    } else {
        Some(ActivationReason::Keyword { matched })
    }
}

/// Check pre-compiled regex patterns against the corpus.
///
/// Uses the cached [`Regex`] objects from [`Entry`] rather than
/// recompiling from the raw pattern strings on every scan pass.
/// The raw patterns are passed alongside for the [`ActivationReason`]
/// diagnostic.
fn check_regex_compiled(
    compiled: &[Regex],
    raw_patterns: &[String],
    corpus: &str,
) -> Option<ActivationReason> {
    for (re, pattern) in compiled.iter().zip(raw_patterns.iter()) {
        if re.is_match(corpus) {
            return Some(ActivationReason::Regex {
                pattern: pattern.clone(),
            });
        }
    }
    None
}

// ── Tests ───────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_activation_state_cooldown() {
        let mut state = ActivationState::new();
        state.current_turn = 5;
        state.record_activation("entry_a", 0);

        state.current_turn = 6;
        assert!(state.is_on_cooldown("entry_a", 3));
        state.current_turn = 7;
        assert!(state.is_on_cooldown("entry_a", 3));
        state.current_turn = 8;
        assert!(!state.is_on_cooldown("entry_a", 3));
        assert!(!state.is_on_cooldown("entry_b", 3));
    }

    #[test]
    fn test_activation_state_sticky() {
        let mut state = ActivationState::new();
        // sticky_turns=3 → stored as 4 internally (3+1)
        state.record_activation("entry_a", 3);

        // Active for 3 advance_turn calls after activation
        assert_eq!(state.is_sticky("entry_a"), Some(4));
        state.advance_turn(); // turn after activation
        assert_eq!(state.is_sticky("entry_a"), Some(3));
        state.advance_turn(); // additional turn 1
        assert_eq!(state.is_sticky("entry_a"), Some(2));
        state.advance_turn(); // additional turn 2
        assert_eq!(state.is_sticky("entry_a"), Some(1));
        state.advance_turn(); // additional turn 3 → expired
        assert_eq!(state.is_sticky("entry_a"), None);
    }

    #[test]
    fn test_sticky_refresh_resets_countdown() {
        let mut state = ActivationState::new();
        // sticky_turns=2 → stored as 3 internally
        state.record_activation("entry_a", 2);
        assert_eq!(state.is_sticky("entry_a"), Some(3));

        state.advance_turn();
        assert_eq!(state.is_sticky("entry_a"), Some(2));

        state.advance_turn();
        assert_eq!(state.is_sticky("entry_a"), Some(1));

        // Fresh re-activation (e.g. keyword matched again) resets counter
        state.record_activation("entry_a", 2);
        assert_eq!(state.is_sticky("entry_a"), Some(3));

        // Full countdown from the refresh point
        state.advance_turn();
        assert_eq!(state.is_sticky("entry_a"), Some(2));
        state.advance_turn();
        assert_eq!(state.is_sticky("entry_a"), Some(1));
        state.advance_turn();
        assert_eq!(state.is_sticky("entry_a"), None);
    }

    #[test]
    fn test_activation_state_advance_tracks_turn() {
        let mut state = ActivationState::new();
        assert_eq!(state.current_turn(), 0);
        state.advance_turn();
        assert_eq!(state.current_turn(), 1);
        state.advance_turn();
        assert_eq!(state.current_turn(), 2);
    }

    #[test]
    fn test_activation_state_serialization() {
        let mut state = ActivationState::new();
        state.record_activation("entry_a", 3);
        state.advance_turn();

        let json = serde_json::to_string(&state).unwrap();
        let restored: ActivationState = serde_json::from_str(&json).unwrap();

        assert_eq!(restored.current_turn(), 1);
        assert_eq!(restored.is_sticky("entry_a"), Some(3));
    }

    #[test]
    fn test_condition_none_is_true() {
        let mut host = crate::host::WeaverHost::from_lorebook_config(
            &crate::lorebook::LorebookConfig::default(),
        );
        let registry = Registry::new();
        assert!(check_condition(&None, &mut host, &registry));
    }

    #[test]
    fn test_condition_true_expression() {
        let mut host = crate::host::WeaverHost::from_lorebook_config(
            &crate::lorebook::LorebookConfig::default(),
        );
        host.set_host_variable("state", "level", weaver_lang::Value::Number(10.0));
        let registry = Registry::new();

        let expr = Arc::new(CompiledExpr::compile("{{state:level}} > 5").unwrap());
        assert!(check_condition(&Some(expr), &mut host, &registry));
    }

    #[test]
    fn test_condition_false_expression() {
        let mut host = crate::host::WeaverHost::from_lorebook_config(
            &crate::lorebook::LorebookConfig::default(),
        );
        host.set_host_variable("state", "level", weaver_lang::Value::Number(3.0));
        let registry = Registry::new();

        let expr = Arc::new(CompiledExpr::compile("{{state:level}} > 5").unwrap());
        assert!(!check_condition(&Some(expr), &mut host, &registry));
    }

    #[test]
    fn test_condition_error_returns_false() {
        let mut host = crate::host::WeaverHost::from_lorebook_config(
            &crate::lorebook::LorebookConfig::default(),
        );
        let registry = Registry::new();

        let expr = Arc::new(CompiledExpr::compile("{{state:missing}} > 5").unwrap());
        assert!(!check_condition(&Some(expr), &mut host, &registry));
    }
}