ripmap 0.1.0

Ultra-fast codebase cartography for LLMs
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
//! Promptgram: treating prompts as structured programs.
//!
//! A promptgram is not a blob of text but a structured program with sections
//! that can be independently evolved by the outer loop.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

/// A promptgram: a structured prompt treated as a program.
///
/// Each section serves a specific role and can be independently modified
/// by the outer loop optimizer.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Promptgram {
    /// Unique identifier for this promptgram
    pub id: String,

    /// Parent promptgram ID (if this was derived from another)
    #[serde(default)]
    pub parent_id: Option<String>,

    /// Version number (increments with each edit)
    pub version: usize,

    /// The structured sections of the prompt
    pub sections: HashMap<String, PromptSection>,

    /// Metadata about this promptgram
    pub metadata: PromptgramMetadata,
}

/// A section of a promptgram.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptSection {
    /// Section name (Role, Policy, Heuristics, etc.)
    pub name: String,

    /// Section content (markdown/text)
    pub content: String,

    /// Is this section immutable (cannot be edited by L2)?
    #[serde(default)]
    pub immutable: bool,

    /// Tags for categorization
    #[serde(default)]
    pub tags: Vec<String>,
}

/// Metadata about a promptgram.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PromptgramMetadata {
    /// When this promptgram was created
    pub created_at: i64,

    /// Last modified timestamp
    pub modified_at: i64,

    /// Best NDCG achieved with this promptgram
    pub best_ndcg: f64,

    /// Number of inner runs using this promptgram
    pub run_count: usize,

    /// Human-readable description
    pub description: String,

    /// Lineage: IDs of ancestor promptgrams
    #[serde(default)]
    pub lineage: Vec<String>,
}

/// Standard section names for inner promptgrams (L1).
pub mod sections {
    pub const ROLE: &str = "Role";
    pub const API_CONTRACT: &str = "API_contract";
    pub const POLICY: &str = "Policy";
    pub const HEURISTICS: &str = "Heuristics";
    pub const CURRICULUM: &str = "Curriculum";
    pub const OUTPUT_SCHEMA: &str = "Output_schema";
    pub const STYLE: &str = "Style";
}

impl Promptgram {
    /// Create a new promptgram with the given ID.
    pub fn new(id: impl Into<String>) -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);

        Promptgram {
            id: id.into(),
            parent_id: None,
            version: 1,
            sections: HashMap::new(),
            metadata: PromptgramMetadata {
                created_at: now,
                modified_at: now,
                ..Default::default()
            },
        }
    }

    /// Add or update a section.
    pub fn with_section(mut self, name: &str, content: &str, immutable: bool) -> Self {
        self.sections.insert(
            name.to_string(),
            PromptSection {
                name: name.to_string(),
                content: content.to_string(),
                immutable,
                tags: vec![],
            },
        );
        self
    }

    /// Get a section by name.
    pub fn get_section(&self, name: &str) -> Option<&PromptSection> {
        self.sections.get(name)
    }

    /// Render the promptgram to a single prompt string.
    ///
    /// Sections are rendered in a canonical order with markdown headers.
    pub fn render(&self) -> String {
        let section_order = [
            sections::ROLE,
            sections::API_CONTRACT,
            sections::POLICY,
            sections::HEURISTICS,
            sections::CURRICULUM,
            sections::OUTPUT_SCHEMA,
            sections::STYLE,
        ];

        let mut output = String::new();

        // Render sections in canonical order
        for section_name in &section_order {
            if let Some(section) = self.sections.get(*section_name) {
                output.push_str(&format!("## {}\n\n", section.name));
                output.push_str(&section.content);
                output.push_str("\n\n");
            }
        }

        // Render any additional sections not in canonical order
        for (name, section) in &self.sections {
            if !section_order.contains(&name.as_str()) {
                output.push_str(&format!("## {}\n\n", section.name));
                output.push_str(&section.content);
                output.push_str("\n\n");
            }
        }

        output.trim().to_string()
    }

    /// Create a child promptgram (fork with new ID, linked lineage).
    pub fn fork(&self, new_id: impl Into<String>) -> Self {
        let new_id = new_id.into();
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);

        let mut lineage = self.metadata.lineage.clone();
        lineage.push(self.id.clone());

        Promptgram {
            id: new_id,
            parent_id: Some(self.id.clone()),
            version: 1,
            sections: self.sections.clone(),
            metadata: PromptgramMetadata {
                created_at: now,
                modified_at: now,
                best_ndcg: 0.0,
                run_count: 0,
                description: format!("Fork of {}", self.id),
                lineage,
            },
        }
    }

    /// Apply an edit to a section.
    ///
    /// Returns Err if the section is immutable or doesn't exist (for replace/delete).
    pub fn apply_edit(&mut self, edit: &super::PromptEdit) -> Result<(), String> {
        let section = self
            .sections
            .get_mut(&edit.section)
            .ok_or_else(|| format!("Section '{}' not found", edit.section))?;

        if section.immutable {
            return Err(format!("Section '{}' is immutable", edit.section));
        }

        match edit.edit_type.as_str() {
            "append" => {
                section.content.push_str("\n\n");
                section.content.push_str(&edit.content);
            }
            "replace" => {
                let target = edit.target.as_deref().unwrap_or("");
                if target.is_empty() {
                    // Replace entire section
                    section.content = edit.content.clone();
                } else {
                    // Replace specific target text
                    section.content = section.content.replace(target, &edit.content);
                }
            }
            "delete" => {
                let target = edit.target.as_deref().unwrap_or("");
                section.content = section.content.replace(target, "");
            }
            _ => return Err(format!("Unknown edit type: {}", edit.edit_type)),
        }

        self.version += 1;
        self.metadata.modified_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);

        Ok(())
    }

    /// Load a promptgram from a TOML file.
    pub fn load(path: impl AsRef<Path>) -> Result<Self, String> {
        let content = std::fs::read_to_string(path.as_ref())
            .map_err(|e| format!("Failed to read promptgram: {}", e))?;
        toml::from_str(&content).map_err(|e| format!("Failed to parse promptgram: {}", e))
    }

    /// Save the promptgram to a TOML file.
    pub fn save(&self, path: impl AsRef<Path>) -> Result<(), String> {
        let content = toml::to_string_pretty(self)
            .map_err(|e| format!("Failed to serialize promptgram: {}", e))?;
        std::fs::write(path.as_ref(), content)
            .map_err(|e| format!("Failed to write promptgram: {}", e))
    }

    /// Load from markdown with section headers.
    ///
    /// Format:
    /// ```markdown
    /// ## Role
    /// Content here...
    ///
    /// ## Policy
    /// More content...
    /// ```
    ///
    /// Architecture note: For inner promptgrams (L1), only "Role" is immutable.
    /// Protocol sections (API_contract, Output_schema) are injected at runtime
    /// by reasoning.rs and should not be in the markdown files that L2 evolves.
    pub fn from_markdown(id: &str, content: &str) -> Self {
        let mut promptgram = Self::new(id);
        let mut current_section: Option<String> = None;
        let mut current_content = String::new();

        for line in content.lines() {
            if line.starts_with("## ") {
                // Save previous section if any
                if let Some(section_name) = current_section.take() {
                    // For inner promptgrams (L1): only Role is immutable
                    // Protocol sections (API_contract, Output_schema) are injected at runtime
                    // and kept immutable to prevent L2 from modifying the protocol itself
                    let immutable = matches!(
                        section_name.as_str(),
                        "Role" | "API_contract" | "Output_schema"
                    );
                    promptgram =
                        promptgram.with_section(&section_name, current_content.trim(), immutable);
                    current_content.clear();
                }

                // Start new section
                current_section = Some(line[3..].trim().to_string());
            } else if current_section.is_some() {
                current_content.push_str(line);
                current_content.push('\n');
            }
        }

        // Save last section
        if let Some(section_name) = current_section {
            let immutable = matches!(
                section_name.as_str(),
                "Role" | "API_contract" | "Output_schema"
            );
            promptgram = promptgram.with_section(&section_name, current_content.trim(), immutable);
        }

        promptgram
    }
}

/// Diff two promptgrams section by section.
///
/// Returns a list of differences for L2 to understand what changed.
/// This helps L2 reason about which mutations were effective.
pub fn diff_prompts(a: &Promptgram, b: &Promptgram) -> Vec<PromptDiff> {
    let mut diffs = Vec::new();

    // Check all sections in A
    for (name, section_a) in &a.sections {
        match b.sections.get(name) {
            Some(section_b) => {
                if section_a.content != section_b.content {
                    diffs.push(PromptDiff {
                        section: name.clone(),
                        diff_type: DiffType::Modified,
                        before: Some(section_a.content.clone()),
                        after: Some(section_b.content.clone()),
                        lines_added: count_lines(&section_b.content)
                            .saturating_sub(count_lines(&section_a.content)),
                        lines_removed: count_lines(&section_a.content)
                            .saturating_sub(count_lines(&section_b.content)),
                    });
                }
            }
            None => {
                diffs.push(PromptDiff {
                    section: name.clone(),
                    diff_type: DiffType::Removed,
                    before: Some(section_a.content.clone()),
                    after: None,
                    lines_added: 0,
                    lines_removed: count_lines(&section_a.content),
                });
            }
        }
    }

    // Check for sections in B but not in A
    for (name, section_b) in &b.sections {
        if !a.sections.contains_key(name) {
            diffs.push(PromptDiff {
                section: name.clone(),
                diff_type: DiffType::Added,
                before: None,
                after: Some(section_b.content.clone()),
                lines_added: count_lines(&section_b.content),
                lines_removed: 0,
            });
        }
    }

    diffs
}

/// A difference between two promptgram sections.
#[derive(Debug, Clone)]
pub struct PromptDiff {
    pub section: String,
    pub diff_type: DiffType,
    pub before: Option<String>,
    pub after: Option<String>,
    pub lines_added: usize,
    pub lines_removed: usize,
}

impl PromptDiff {
    /// Format as a compact summary string.
    pub fn summary(&self) -> String {
        match self.diff_type {
            DiffType::Added => format!(
                "[+{}] {} (+{} lines)",
                self.section, "added", self.lines_added
            ),
            DiffType::Removed => format!(
                "[-{}] {} (-{} lines)",
                self.section, "removed", self.lines_removed
            ),
            DiffType::Modified => format!(
                "[~{}] modified (+{}/-{})",
                self.section, self.lines_added, self.lines_removed
            ),
        }
    }
}

/// Type of difference between sections.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiffType {
    Added,
    Removed,
    Modified,
}

fn count_lines(s: &str) -> usize {
    s.lines().count()
}

/// Create baseline inner promptgram (L1 v001).
///
/// This is the seed promptgram - L2 will mutate it over time.
/// Version numbers track lineage, not archetypes.
///
/// NOTE: Protocol sections (API_contract, Output_schema) are now injected
/// at runtime by reasoning.rs from training/prompts/protocol/inner_output_schema.md.
/// The baseline only contains the policy sections that L2 can evolve:
/// Role, Policy, Heuristics, and Style.
pub fn baseline_promptgram() -> Promptgram {
    Promptgram::new("inner_v001")
        // Role: immutable - defines the agent's fundamental identity
        .with_section(
            sections::ROLE,
            r#"You approximate the gradient in concept space.
Given failures and trajectory, propose parameter changes."#,
            true,
        )
        // Policy: mutable - high-level approach to the problem
        .with_section(
            sections::POLICY,
            r#"Analyze trajectory state:
- Improving: continue direction
- Degrading: revert or reverse
- Plateaued: orthogonal move

Analyze failures:
- Missing signal vs overwhelming signal
- Parameter interactions"#,
            false,
        )
        // Heuristics: mutable - specific rules and patterns discovered
        .with_section(
            sections::HEURISTICS,
            r#"- NDCG drop >5% = collapse signal
- Temporal and structural signals compete
- High boosts cause tunnel vision
- Low alpha localizes, high alpha globalizes
- Depth penalties break monorepos"#,
            false,
        )
        // Style: mutable - tone and presentation guidance
        .with_section(
            sections::STYLE,
            r#"Analytical. Specific. Reference concrete failures."#,
            false,
        )
}

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

    #[test]
    fn test_promptgram_render() {
        let pg = Promptgram::new("test")
            .with_section(sections::ROLE, "You are a test", true)
            .with_section(sections::POLICY, "Do the thing", false);

        let rendered = pg.render();
        assert!(rendered.contains("## Role"));
        assert!(rendered.contains("You are a test"));
        assert!(rendered.contains("## Policy"));
    }

    #[test]
    fn test_promptgram_fork() {
        let parent = Promptgram::new("parent").with_section(sections::POLICY, "Original", false);

        let child = parent.fork("child");

        assert_eq!(child.parent_id, Some("parent".to_string()));
        assert_eq!(child.metadata.lineage, vec!["parent"]);
        assert!(child.get_section(sections::POLICY).is_some());
    }

    #[test]
    fn test_promptgram_edit_immutable() {
        let mut pg = Promptgram::new("test").with_section(sections::ROLE, "Original", true);

        let edit = super::super::PromptEdit {
            section: sections::ROLE.to_string(),
            edit_type: "replace".to_string(),
            target: Some(String::new()),
            content: "Modified".to_string(),
            rationale: "test".to_string(),
        };

        assert!(pg.apply_edit(&edit).is_err());
    }

    #[test]
    fn test_from_markdown() {
        let md = r#"## Role
You are a test optimizer.

## Policy
Do smart things.
Make good choices.

## Style
Be brief.
"#;

        let pg = Promptgram::from_markdown("test", md);
        assert!(pg.get_section("Role").is_some());
        assert!(pg.get_section("Policy").is_some());
        assert!(pg.get_section("Style").is_some());
    }
}