every-other-token 4.1.2

A real-time LLM stream interceptor for token-level interaction research
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
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
//! # Self-Documentation (Task 2.5)
//!
//! Every time the system modifies itself, auto-generate:
//! - Changelog entries with what changed and why
//! - Updated architecture diagrams (Mermaid)
//! - Performance delta reports
//! - Dependency impact analysis
//!
//! Output is written to `docs/self_modifications/` as markdown files.
//! The system can explain its own evolution through these records.

use std::{
    collections::HashMap,
    sync::{Arc, Mutex},
    time::{SystemTime, UNIX_EPOCH},
};

use serde::{Deserialize, Serialize};

// ─── Error ────────────────────────────────────────────────────────────────────

/// Errors from the documentation generator.
#[derive(Debug)]
pub enum DocsError {
    /// Lock poisoned.
    LockPoisoned,
    /// I/O error writing a document.
    Io(String),
}

impl std::fmt::Display for DocsError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DocsError::LockPoisoned => write!(f, "docs lock poisoned"),
            DocsError::Io(msg) => write!(f, "I/O error: {msg}"),
        }
    }
}

impl std::error::Error for DocsError {}

// ─── Document types ───────────────────────────────────────────────────────────

/// A metric value before and after a modification.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricDelta {
    /// Metric name (e.g., `"p95_latency_ms"`)
    pub name: String,
    /// Value before the change.
    pub before: f64,
    /// Value after the change.
    pub after: f64,
    /// Computed delta (`after - before`).
    pub delta: f64,
    /// Whether a lower value is better for this metric.
    pub lower_is_better: bool,
}

impl MetricDelta {
    /// Create a new delta record.
    pub fn new(name: impl Into<String>, before: f64, after: f64, lower_is_better: bool) -> Self {
        let delta = after - before;
        Self {
            name: name.into(),
            before,
            after,
            delta,
            lower_is_better,
        }
    }

    /// Return true if the change was an improvement.
    pub fn is_improvement(&self) -> bool {
        if self.lower_is_better {
            self.delta < 0.0
        } else {
            self.delta > 0.0
        }
    }

    /// Return a human-readable improvement percentage.
    pub fn improvement_pct(&self) -> f64 {
        if self.before == 0.0 {
            return 0.0;
        }
        (self.delta / self.before.abs()) * 100.0
    }
}

/// A single self-modification changelog entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangelogEntry {
    /// Modification record ID.
    pub modification_id: String,
    /// Short summary of what changed.
    pub title: String,
    /// Explanation of *why* the change was made (problem being solved).
    pub rationale: String,
    /// Files that were changed.
    pub files_changed: Vec<String>,
    /// Metric deltas observed after deployment.
    pub metric_deltas: Vec<MetricDelta>,
    /// Who/what initiated this change.
    pub initiated_by: String,
    /// Unix timestamp.
    pub timestamp_secs: u64,
    /// Whether the change was ultimately kept (vs rolled back).
    pub kept: bool,
}

impl ChangelogEntry {
    /// Render this entry as a markdown section.
    pub fn to_markdown(&self) -> String {
        let ts = self.timestamp_secs;
        let kept_str = if self.kept { "Deployed" } else { "Rolled back" };
        let mut md = format!(
            "## [{title}](#{id}) -- {kept}\n\n\
             - **ID**: `{id}`\n\
             - **When**: Unix timestamp `{ts}`\n\
             - **By**: {by}\n\
             - **Status**: {kept}\n\n\
             ### Why\n\n{rationale}\n\n\
             ### Files changed\n\n",
            title = self.title,
            id = self.modification_id,
            kept = kept_str,
            ts = ts,
            by = self.initiated_by,
            rationale = self.rationale,
        );

        for f in &self.files_changed {
            md.push_str(&format!("- `{f}`\n"));
        }

        if !self.metric_deltas.is_empty() {
            md.push_str("\n### Performance impact\n\n");
            md.push_str("| Metric | Before | After | Delta | Result |\n");
            md.push_str("|--------|--------|-------|-------|--------|\n");
            for d in &self.metric_deltas {
                let result = if d.is_improvement() {
                    "Improved"
                } else {
                    "Degraded"
                };
                md.push_str(&format!(
                    "| {} | {:.3} | {:.3} | {:+.3} ({:+.1}%) | {} |\n",
                    d.name,
                    d.before,
                    d.after,
                    d.delta,
                    d.improvement_pct(),
                    result,
                ));
            }
        }

        md.push('\n');
        md
    }
}

/// A Mermaid diagram description for the current pipeline architecture.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArchitectureDiagram {
    /// Mermaid flowchart source.
    pub mermaid_source: String,
    /// Timestamp when generated.
    pub generated_at_secs: u64,
    /// Version or modification ID this diagram reflects.
    pub version: String,
}

impl ArchitectureDiagram {
    /// Render as a markdown code block.
    pub fn to_markdown(&self) -> String {
        format!(
            "```mermaid\n{}\n```\n\n_Generated at Unix `{}` (version `{}`)_\n",
            self.mermaid_source, self.generated_at_secs, self.version
        )
    }
}

/// A dependency impact analysis for a proposed change.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DependencyImpact {
    /// Files directly changed.
    pub changed_files: Vec<String>,
    /// Modules that transitively depend on the changed files.
    pub transitive_dependents: Vec<String>,
    /// Risk assessment: how many modules are affected.
    pub blast_radius: usize,
    /// Whether the change touches any public API surface.
    pub touches_public_api: bool,
}

impl DependencyImpact {
    /// Compute from a list of changed files and a simple module map.
    pub fn compute(
        changed_files: Vec<String>,
        dependency_map: &HashMap<String, Vec<String>>,
    ) -> Self {
        let mut transitive: Vec<String> = Vec::new();
        let mut visited = std::collections::HashSet::new();

        for file in &changed_files {
            collect_dependents(file, dependency_map, &mut transitive, &mut visited);
        }

        let blast_radius = transitive.len();
        let touches_public_api = changed_files
            .iter()
            .any(|f| f.contains("lib.rs") || f.contains("mod.rs") || f.contains("pub "));

        DependencyImpact {
            changed_files,
            transitive_dependents: transitive,
            blast_radius,
            touches_public_api,
        }
    }

    /// Render as markdown.
    pub fn to_markdown(&self) -> String {
        let api_warning = if self.touches_public_api {
            "**This change touches public API surface -- review carefully.**\n\n"
        } else {
            ""
        };

        let mut md = format!(
            "### Dependency impact\n\n{api_warning}\
             - **Blast radius**: {blast} modules affected\n\
             - **Changed files**: {count}\n\n",
            api_warning = api_warning,
            blast = self.blast_radius,
            count = self.changed_files.len(),
        );

        if !self.transitive_dependents.is_empty() {
            md.push_str("**Transitively affected modules:**\n\n");
            for dep in &self.transitive_dependents {
                md.push_str(&format!("- `{dep}`\n"));
            }
        }

        md
    }
}

fn collect_dependents(
    module: &str,
    map: &HashMap<String, Vec<String>>,
    result: &mut Vec<String>,
    visited: &mut std::collections::HashSet<String>,
) {
    for (parent, deps) in map {
        if deps.iter().any(|d| d == module) && !visited.contains(parent) {
            visited.insert(parent.clone());
            result.push(parent.clone());
            collect_dependents(parent, map, result, visited);
        }
    }
}

// ─── Documentation generator ─────────────────────────────────────────────────

struct DocsInner {
    changelog: Vec<ChangelogEntry>,
    diagrams: Vec<ArchitectureDiagram>,
    output_dir: String,
    max_changelog_entries: usize,
}

/// Generates and stores self-modification documentation.
#[derive(Clone)]
pub struct SelfDocGenerator {
    inner: Arc<Mutex<DocsInner>>,
}

impl SelfDocGenerator {
    /// Create a new generator.
    pub fn new(output_dir: impl Into<String>) -> Self {
        Self {
            inner: Arc::new(Mutex::new(DocsInner {
                changelog: Vec::new(),
                diagrams: Vec::new(),
                output_dir: output_dir.into(),
                max_changelog_entries: 1000,
            })),
        }
    }

    /// Record a changelog entry.
    pub fn record_change(&self, entry: ChangelogEntry) -> Result<(), DocsError> {
        let mut inner = self.inner.lock().map_err(|_| DocsError::LockPoisoned)?;
        if inner.changelog.len() >= inner.max_changelog_entries {
            inner.changelog.remove(0);
        }
        inner.changelog.push(entry);
        Ok(())
    }

    /// Record an architecture diagram snapshot.
    pub fn record_diagram(&self, diagram: ArchitectureDiagram) -> Result<(), DocsError> {
        let mut inner = self.inner.lock().map_err(|_| DocsError::LockPoisoned)?;
        inner.diagrams.push(diagram);
        Ok(())
    }

    /// Return the full changelog (newest first).
    pub fn changelog(&self) -> Vec<ChangelogEntry> {
        self.inner
            .lock()
            .map(|inner| {
                let mut v = inner.changelog.clone();
                v.reverse();
                v
            })
            .unwrap_or_default()
    }

    /// Render the full changelog as a markdown document.
    pub fn render_changelog_md(&self) -> String {
        let entries = self.changelog();
        let mut md = "# Self-Modification Changelog\n\n".to_string();
        md.push_str(&format!("_Last updated: Unix `{}`_\n\n---\n\n", unix_now()));

        if entries.is_empty() {
            md.push_str("_No modifications recorded yet._\n");
        } else {
            for entry in &entries {
                md.push_str(&entry.to_markdown());
                md.push_str("---\n\n");
            }
        }
        md
    }

    /// Render the latest architecture diagram as markdown.
    pub fn render_latest_diagram_md(&self) -> Option<String> {
        self.inner
            .lock()
            .ok()
            .and_then(|inner| inner.diagrams.last().cloned())
            .map(|d| d.to_markdown())
    }

    /// Generate the standard EOT pipeline architecture Mermaid diagram.
    pub fn generate_pipeline_diagram(&self, version: &str) -> ArchitectureDiagram {
        let source = "flowchart LR\n\
            A[TokenStream] --> B[Interceptor]\n\
            B --> C[Modifier]\n\
            C --> D[Writer]\n\
            B -.-> E[(TelemetryBus)]\n\
            subgraph self-tune\n\
              E --> F[AnomalyDetector]\n\
              E --> G[Controller]\n\
              E --> H[Orchestrator]\n\
            end\n\
            subgraph self-modify\n\
              H --> I[TaskGenerator]\n\
              I --> J[ValidationGate]\n\
              J --> K[Deployment]\n\
              K --> L[AgentMemory]\n\
            end"
        .to_string();

        ArchitectureDiagram {
            mermaid_source: source,
            generated_at_secs: unix_now(),
            version: version.to_string(),
        }
    }

    /// Return the output directory for documentation files.
    pub fn output_dir(&self) -> String {
        self.inner
            .lock()
            .map(|inner| inner.output_dir.clone())
            .unwrap_or_default()
    }

    /// Return the number of recorded changelog entries.
    pub fn entry_count(&self) -> usize {
        self.inner
            .lock()
            .map(|inner| inner.changelog.len())
            .unwrap_or(0)
    }
}

fn unix_now() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

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

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

    fn make_entry(id: &str) -> ChangelogEntry {
        ChangelogEntry {
            modification_id: id.to_string(),
            title: format!("Fix {id}"),
            rationale: "Latency was too high".to_string(),
            files_changed: vec!["src/interceptor.rs".to_string()],
            metric_deltas: vec![MetricDelta::new("p95_latency_ms", 10.0, 7.0, true)],
            initiated_by: "controller".to_string(),
            timestamp_secs: 1_700_000_000,
            kept: true,
        }
    }

    #[test]
    fn test_metric_delta_improvement_lower_is_better() {
        let d = MetricDelta::new("latency", 10.0, 7.0, true);
        assert!(d.is_improvement());
        assert!(d.delta < 0.0);
    }

    #[test]
    fn test_metric_delta_degradation_lower_is_better() {
        let d = MetricDelta::new("latency", 7.0, 10.0, true);
        assert!(!d.is_improvement());
    }

    #[test]
    fn test_metric_delta_improvement_higher_is_better() {
        let d = MetricDelta::new("throughput", 100.0, 120.0, false);
        assert!(d.is_improvement());
    }

    #[test]
    fn test_metric_delta_improvement_pct() {
        let d = MetricDelta::new("latency", 100.0, 80.0, true);
        assert!((d.improvement_pct() - (-20.0)).abs() < 0.01);
    }

    #[test]
    fn test_metric_delta_zero_before() {
        let d = MetricDelta::new("m", 0.0, 5.0, false);
        assert_eq!(d.improvement_pct(), 0.0);
    }

    #[test]
    fn test_changelog_entry_to_markdown_contains_title() {
        let e = make_entry("mod-1");
        let md = e.to_markdown();
        assert!(md.contains("Fix mod-1"));
    }

    #[test]
    fn test_changelog_entry_to_markdown_shows_metrics() {
        let e = make_entry("mod-1");
        let md = e.to_markdown();
        assert!(md.contains("p95_latency_ms"));
    }

    #[test]
    fn test_architecture_diagram_to_markdown() {
        let diag = ArchitectureDiagram {
            mermaid_source: "flowchart LR\n  A --> B".to_string(),
            generated_at_secs: 0,
            version: "v1".to_string(),
        };
        let md = diag.to_markdown();
        assert!(md.contains("```mermaid"));
        assert!(md.contains("A --> B"));
    }

    #[test]
    fn test_dependency_impact_blast_radius() {
        let mut map: HashMap<String, Vec<String>> = HashMap::new();
        map.insert("src/a.rs".into(), vec!["src/interceptor.rs".into()]);
        map.insert("src/b.rs".into(), vec!["src/a.rs".into()]);
        let impact = DependencyImpact::compute(vec!["src/interceptor.rs".into()], &map);
        assert!(impact.blast_radius >= 1);
    }

    #[test]
    fn test_dependency_impact_no_dependents() {
        let map: HashMap<String, Vec<String>> = HashMap::new();
        let impact = DependencyImpact::compute(vec!["src/isolated.rs".into()], &map);
        assert_eq!(impact.blast_radius, 0);
    }

    #[test]
    fn test_dependency_impact_to_markdown() {
        let map: HashMap<String, Vec<String>> = HashMap::new();
        let impact = DependencyImpact::compute(vec!["src/lib.rs".into()], &map);
        let md = impact.to_markdown();
        assert!(md.contains("Dependency impact"));
        assert!(md.contains("public API"));
    }

    #[test]
    fn test_generator_record_and_retrieve_changelog() {
        let gen = SelfDocGenerator::new("docs/self_modifications");
        gen.record_change(make_entry("m1")).unwrap();
        assert_eq!(gen.entry_count(), 1);
    }

    #[test]
    fn test_changelog_newest_first() {
        let gen = SelfDocGenerator::new("docs");
        gen.record_change(make_entry("a")).unwrap();
        gen.record_change(make_entry("b")).unwrap();
        let log = gen.changelog();
        assert_eq!(log[0].modification_id, "b");
    }

    #[test]
    fn test_render_changelog_md_empty() {
        let gen = SelfDocGenerator::new("docs");
        let md = gen.render_changelog_md();
        assert!(md.contains("No modifications recorded yet"));
    }

    #[test]
    fn test_render_changelog_md_with_entry() {
        let gen = SelfDocGenerator::new("docs");
        gen.record_change(make_entry("m1")).unwrap();
        let md = gen.render_changelog_md();
        assert!(md.contains("Fix m1"));
    }

    #[test]
    fn test_generate_pipeline_diagram() {
        let gen = SelfDocGenerator::new("docs");
        let diag = gen.generate_pipeline_diagram("v1.2.3");
        assert!(diag.mermaid_source.contains("flowchart"));
        assert_eq!(diag.version, "v1.2.3");
    }

    #[test]
    fn test_record_and_render_latest_diagram() {
        let gen = SelfDocGenerator::new("docs");
        let diag = gen.generate_pipeline_diagram("v1");
        gen.record_diagram(diag).unwrap();
        let md = gen.render_latest_diagram_md();
        assert!(md.is_some());
        assert!(md.unwrap().contains("mermaid"));
    }

    #[test]
    fn test_output_dir_returned() {
        let gen = SelfDocGenerator::new("/tmp/docs");
        assert_eq!(gen.output_dir(), "/tmp/docs");
    }

    #[test]
    fn test_generator_clone_shares_state() {
        let gen = SelfDocGenerator::new("docs");
        let gen2 = gen.clone();
        gen.record_change(make_entry("x")).unwrap();
        assert_eq!(gen2.entry_count(), 1);
    }

    #[test]
    fn test_kept_false_shows_rolled_back() {
        let mut entry = make_entry("m2");
        entry.kept = false;
        let md = entry.to_markdown();
        assert!(md.contains("Rolled back"));
    }

    #[test]
    fn test_max_entries_cap() {
        let gen = SelfDocGenerator::new("docs");
        {
            let mut inner = gen.inner.lock().unwrap();
            inner.max_changelog_entries = 3;
        }
        for i in 0..5 {
            gen.record_change(make_entry(&format!("m{i}"))).unwrap();
        }
        assert_eq!(gen.entry_count(), 3);
    }

    #[test]
    fn test_docs_error_display() {
        let e = DocsError::LockPoisoned;
        assert!(e.to_string().contains("lock poisoned"));
        let e2 = DocsError::Io("disk full".to_string());
        assert!(e2.to_string().contains("disk full"));
    }

    #[test]
    fn test_metric_delta_serde_roundtrip() {
        let d = MetricDelta::new("lat", 10.0, 7.0, true);
        let json = serde_json::to_string(&d).unwrap();
        let back: MetricDelta = serde_json::from_str(&json).unwrap();
        assert_eq!(back.name, "lat");
        assert!((back.delta - d.delta).abs() < f64::EPSILON);
    }

    #[test]
    fn test_changelog_entry_serde_roundtrip() {
        let e = make_entry("serde-test");
        let json = serde_json::to_string(&e).unwrap();
        let back: ChangelogEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(back.modification_id, "serde-test");
    }

    #[test]
    fn test_architecture_diagram_serde_roundtrip() {
        let d = ArchitectureDiagram {
            mermaid_source: "flowchart LR".to_string(),
            generated_at_secs: 123,
            version: "v1".to_string(),
        };
        let json = serde_json::to_string(&d).unwrap();
        let back: ArchitectureDiagram = serde_json::from_str(&json).unwrap();
        assert_eq!(back.version, "v1");
    }

    #[test]
    fn test_dependency_impact_serde_roundtrip() {
        let impact = DependencyImpact {
            changed_files: vec!["a.rs".into()],
            transitive_dependents: vec!["b.rs".into()],
            blast_radius: 1,
            touches_public_api: false,
        };
        let json = serde_json::to_string(&impact).unwrap();
        let back: DependencyImpact = serde_json::from_str(&json).unwrap();
        assert_eq!(back.blast_radius, 1);
    }
}