patina-ai 0.23.0

Context orchestration for AI development - captures and evolves patterns over time
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
//! Context command — project patterns, beliefs, and conventions
//!
//! Shared between MCP (`context` tool) and CLI (`patina context`).
//! Returns core patterns (eternal principles), surface patterns (active architecture),
//! and epistemic beliefs.

use anyhow::Result;
use std::fs;
use std::path::Path;

use crate::commands::assay::internal::belief::search_beliefs_fts;
use crate::commands::assay::internal::search::{assay_search, SearchOptions};
use crate::retrieval::QueryEngine;
use rusqlite::Connection;

/// Get project context from the knowledge layer
///
/// Reads patterns from layer/core/ (eternal principles) and layer/surface/ (active patterns)
/// Optionally filters by topic if provided
pub fn get_project_context(topic: Option<&str>) -> Result<String> {
    let mut output = String::new();

    // Check if we're in a patina project
    let layer_path = Path::new("layer");
    if !layer_path.exists() {
        return Ok(
            "No knowledge layer found. Run 'patina init' to initialize a project.".to_string(),
        );
    }

    // Read core patterns (eternal principles)
    let core_path = layer_path.join("core");
    let core_patterns = read_patterns(&core_path, topic)?;

    // Read surface patterns (active architecture)
    let surface_path = layer_path.join("surface");
    let surface_patterns = read_patterns(&surface_path, topic)?;

    // Format output
    if !core_patterns.is_empty() {
        output.push_str("# Core Patterns (Eternal Principles)\n\n");
        for (name, content) in &core_patterns {
            output.push_str(&format!("## {}\n\n{}\n\n", name, content));
        }
    }

    if !surface_patterns.is_empty() {
        output.push_str("# Surface Patterns (Active Architecture)\n\n");
        for (name, content) in &surface_patterns {
            output.push_str(&format!("## {}\n\n{}\n\n", name, content));
        }
    }

    // Topic-specific search: factual (assay) + semantic (scry) fusion
    // Two signals, simple merge: facts first, meaning for gaps
    if let Some(t) = topic {
        output.push_str(&get_topic_search_results(t));
    }

    // Beliefs are always eligible — topic changes the query, not whether beliefs exist
    if let Some(t) = topic {
        // Topic provided: FTS5 ranking via assay belief search
        output.push_str(&get_topic_beliefs(t));
    } else {
        // No topic: aggregate stats + top beliefs by use count
        if let Ok(belief_section) = get_belief_metrics() {
            output.push_str(&belief_section);
        }
    }

    if output.is_empty() {
        if let Some(t) = topic {
            output = format!("No patterns found matching topic: '{}'", t);
        } else {
            output = "No patterns found in the knowledge layer.".to_string();
        }
    }

    // Recall directive — always appended so the LLM knows how to search
    output.push_str("## Recall Directive\n\n");
    output.push_str(
        "Project knowledge accumulates in beliefs — check them before assuming defaults.\n",
    );
    output.push_str("  Meaning:  scry(query=\"your question\") — semantic/conceptual search\n");
    output.push_str(
        "  Facts:    assay(query_type=\"search\", query=\"your question\") — keyword/factual search\n",
    );
    output.push_str(
        "  Beliefs:  scry(content_type=\"beliefs\", query=\"your question\") — belief grounding\n",
    );

    Ok(output)
}

/// Query belief metrics from the database for the context tool
pub fn get_belief_metrics() -> Result<String> {
    use rusqlite::Connection;

    const DB_PATH: &str = ".patina/local/data/patina.db";
    let conn = Connection::open(DB_PATH)?;

    // Check if beliefs table exists
    let table_exists: bool = conn
        .query_row(
            "SELECT 1 FROM sqlite_master WHERE type='table' AND name='beliefs'",
            [],
            |_| Ok(true),
        )
        .unwrap_or(false);

    if !table_exists {
        return Ok(String::new());
    }

    // Aggregate stats
    let (total, grounded, reach_files, verif_total, verif_pass, verif_fail): (
        i64,
        i64,
        i64,
        i64,
        i64,
        i64,
    ) = conn.query_row(
        "SELECT
            COUNT(*),
            SUM(CASE WHEN grounding_code_count > 0 THEN 1 ELSE 0 END),
            SUM(grounding_code_count),
            SUM(verification_total),
            SUM(verification_passed),
            SUM(verification_failed)
         FROM beliefs",
        [],
        |row| {
            Ok((
                row.get(0)?,
                row.get(1)?,
                row.get(2)?,
                row.get(3)?,
                row.get(4)?,
                row.get(5)?,
            ))
        },
    )?;

    if total == 0 {
        return Ok(String::new());
    }

    let precision = if reach_files > 0 { 100 } else { 0 }; // All reach files are source code (filtered at hop)

    let mut output = String::from("# Epistemic Beliefs\n\n");
    output.push_str(&format!(
        "**Total:** {} beliefs | **Grounded:** {}/{} ({:.0}%) | **Reach files:** {} ({}% precision)\n",
        total,
        grounded,
        total,
        if total > 0 { grounded as f64 / total as f64 * 100.0 } else { 0.0 },
        reach_files,
        precision,
    ));
    output.push_str(&format!(
        "**Verification:** {}/{} passed ({} failed)\n\n",
        verif_pass, verif_total, verif_fail,
    ));

    // Top beliefs by use count
    let mut stmt = conn.prepare(
        "SELECT id, cited_by_beliefs + cited_by_sessions + applied_in as use_count,
                entrenchment, status
         FROM beliefs
         ORDER BY use_count DESC
         LIMIT 10",
    )?;

    let top_beliefs: Vec<(String, i64, String, String)> = stmt
        .query_map([], |row| {
            Ok((
                row.get::<_, String>(0)?,
                row.get::<_, i64>(1)?,
                row.get::<_, String>(2)?,
                row.get::<_, String>(3)?,
            ))
        })?
        .filter_map(|r| r.ok())
        .collect();

    if !top_beliefs.is_empty() {
        output.push_str("**Top beliefs by use:**\n");
        for (id, use_count, entrenchment, status) in &top_beliefs {
            output.push_str(&format!(
                "- {} (use: {}, entrenchment: {}, status: {})\n",
                id, use_count, entrenchment, status,
            ));
        }
        output.push('\n');
    }

    Ok(output)
}

/// Two-signal fusion: assay (factual/keyword) + scry (semantic) search results
///
/// Called when a topic is provided. Returns factual matches first (what files,
/// commits, patterns match by keyword), then semantic matches for gaps (what's
/// conceptually related but not keyword-matched). Simple merge — no tuning.
fn get_topic_search_results(topic: &str) -> String {
    use std::collections::HashSet;

    let mut output = String::new();
    let mut seen_ids = HashSet::new();

    // 1. Factual: assay keyword search (FTS5 across code, commits, patterns)
    let search_opts = SearchOptions {
        limit: 5,
        include_issues: false,
        repo: None,
    };

    if let Ok(assay_results) = assay_search(topic, &search_opts) {
        if !assay_results.is_empty() {
            output.push_str("# Factual Matches (keyword search)\n\n");
            for r in &assay_results {
                seen_ids.insert(r.source_id.clone());
                let content = r.content.replace('\n', " ");
                let truncated: String = content.trim().chars().take(150).collect();
                let ellipsis = if content.trim().chars().count() > 150 {
                    "..."
                } else {
                    ""
                };
                output.push_str(&format!(
                    "- **{}** ({}): {}{}\n",
                    r.source_id, r.event_type, truncated, ellipsis
                ));
            }
            output.push('\n');
        }
    }

    // 2. Semantic: scry vector search (conceptually related items)
    let engine = QueryEngine::new();
    if let Ok(scry_results) = engine.query(topic, 5) {
        let novel: Vec<_> = scry_results
            .iter()
            .filter(|r| !seen_ids.contains(&r.doc_id))
            .take(5)
            .collect();

        if !novel.is_empty() {
            output.push_str("# Semantic Matches (conceptually related)\n\n");
            for r in &novel {
                let event_type = r.metadata.event_type.as_deref().unwrap_or("unknown");
                let content = r.content.replace('\n', " ");
                let truncated: String = content.trim().chars().take(150).collect();
                let ellipsis = if content.trim().chars().count() > 150 {
                    "..."
                } else {
                    ""
                };
                output.push_str(&format!(
                    "- **{}** ({}, {:.3}): {}{}\n",
                    r.doc_id, event_type, r.fused_score, truncated, ellipsis
                ));
            }
            output.push('\n');
        }
    }

    output
}

/// Query beliefs ranked by relevance to a topic
///
/// Uses FTS5 keyword search via assay's belief module to find beliefs
/// relevant to the given topic. Falls back to aggregate metrics if
/// the database is unavailable.
fn get_topic_beliefs(topic: &str) -> String {
    const DB_PATH: &str = ".patina/local/data/patina.db";

    let conn = match Connection::open(DB_PATH) {
        Ok(c) => c,
        Err(_) => return get_belief_metrics().unwrap_or_default(),
    };

    match search_beliefs_fts(&conn, topic, 5) {
        Ok(results) if !results.is_empty() => {
            let mut output = format!(
                "# Active Beliefs (ranked by relevance to \"{}\")\n\n",
                topic
            );
            for r in &results {
                output.push_str(&format!(
                    "- **{}** (score: {:.2}): {}\n",
                    r.source_id, r.score, r.content
                ));
            }
            output.push('\n');
            output
        }
        _ => get_belief_metrics().unwrap_or_default(),
    }
}

/// Read markdown patterns from a directory
fn read_patterns(dir: &Path, topic: Option<&str>) -> Result<Vec<(String, String)>> {
    let mut patterns = Vec::new();

    if !dir.exists() {
        return Ok(patterns);
    }

    // Read .md files in the directory
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        // Only process markdown files
        if path.extension().map(|e| e == "md").unwrap_or(false) {
            let name = path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("unknown")
                .to_string();

            // Skip certain files
            if name == "README" || name.starts_with('.') {
                continue;
            }

            // If topic filter provided, match against filename and title only
            // (not full body — substring on markdown bodies returns false positives)
            if let Some(t) = topic {
                let topic_lower = t.to_lowercase();
                let name_lower = name.to_lowercase();

                // Extract title from first # line without reading full content
                let content = fs::read_to_string(&path)?;
                let title = extract_title(&content);
                let title_lower = title.to_lowercase();

                if !name_lower.contains(&topic_lower) && !title_lower.contains(&topic_lower) {
                    continue;
                }

                let summary = extract_summary(&content);
                patterns.push((name, summary));
            } else {
                let content = fs::read_to_string(&path)?;
                let summary = extract_summary(&content);
                patterns.push((name, summary));
            }
        }
    }

    // Sort by name for consistent output
    patterns.sort_by(|a, b| a.0.cmp(&b.0));
    Ok(patterns)
}

/// Extract the title from markdown content (first # line after frontmatter)
fn extract_title(content: &str) -> String {
    let mut in_frontmatter = false;
    for line in content.lines() {
        if line == "---" {
            in_frontmatter = !in_frontmatter;
            continue;
        }
        if in_frontmatter {
            continue;
        }
        let trimmed = line.trim();
        if trimmed.starts_with('#') {
            return trimmed.trim_start_matches('#').trim().to_string();
        }
        // Stop after first non-empty, non-frontmatter line that isn't a title
        if !trimmed.is_empty() {
            break;
        }
    }
    String::new()
}

/// Extract a summary from markdown content (skip frontmatter, get first paragraphs)
pub fn extract_summary(content: &str) -> String {
    let mut lines: Vec<&str> = content.lines().collect();

    // Skip YAML frontmatter if present
    if lines.first().map(|l| *l == "---").unwrap_or(false) {
        if let Some(end) = lines.iter().skip(1).position(|l| *l == "---") {
            lines = lines[end + 2..].to_vec();
        }
    }

    // Skip title line (# ...)
    if lines.first().map(|l| l.starts_with('#')).unwrap_or(false) {
        lines = lines[1..].to_vec();
    }

    // Get first ~500 chars of meaningful content
    let mut summary = String::new();
    for line in lines {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            if !summary.is_empty() {
                summary.push('\n');
            }
            continue;
        }
        summary.push_str(trimmed);
        summary.push(' ');

        if summary.len() > 500 {
            // Truncate at char boundary
            let truncated: String = summary.chars().take(500).collect();
            summary = truncated;
            summary.push_str("...");
            break;
        }
    }

    summary.trim().to_string()
}

/// Execute CLI context command
pub fn execute(topic: Option<&str>) -> Result<()> {
    let output = get_project_context(topic)?;
    println!("{}", output);
    Ok(())
}