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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Remote and multi-repo routing for scry
//!
//! Handles routing queries to mother daemon and cross-repo searches.
//! Graph routing is the sole cross-repo strategy (D4).

use std::path::Path;

use anyhow::Result;

use patina::mother::{self, EdgeType, Graph};

use crate::commands::persona;

use super::super::{ScryOptions, ScryResult};
use super::enrichment::truncate_content;
use super::logging::{log_scry_query_with_routing, EdgeInfo, RoutedResult, RoutingContext};
use super::search::scry_text;

/// Execute scry via mother daemon
pub fn execute_via_mother(query: Option<&str>, options: &ScryOptions) -> Result<()> {
    let address = mother::get_address().unwrap_or_else(|| "unknown".to_string());
    println!("🔮 Scry - Querying mother at {}\n", address);

    // File-based queries not supported via mother yet
    if options.file.is_some() {
        anyhow::bail!("File-based queries (--file) not supported via mother. Run locally.");
    }

    let query = query.ok_or_else(|| anyhow::anyhow!("Query text required"))?;
    println!("Query: \"{}\"\n", query);

    // Build request
    let request = mother::ScryRequest {
        query: query.to_string(),
        dimension: options.dimension.clone(),
        repo: options.repo.clone(),
        all_repos: options.all_repos,
        include_issues: options.include_issues,
        include_persona: options.include_persona,
        limit: options.limit,
        min_score: options.min_score,
    };

    // Execute query
    let response = mother::scry(request)?;

    if response.results.is_empty() {
        println!("No results found.");
        return Ok(());
    }

    println!("Found {} results:\n", response.count);
    println!("{}", "".repeat(60));

    for (i, result) in response.results.iter().enumerate() {
        let timestamp_display = if result.timestamp.is_empty() {
            String::new()
        } else {
            format!(" | {}", result.timestamp)
        };
        println!(
            "\n[{}] Score: {:.3} | {} | {}{}",
            i + 1,
            result.score,
            result.event_type,
            result.source_id,
            timestamp_display
        );
        println!("    {}", truncate_content(&result.content, 200));
    }

    println!("\n{}", "".repeat(60));

    Ok(())
}

/// Execute query using graph-based routing (sole cross-repo strategy)
///
/// Smart routing flow:
/// 1. Detect current project from graph
/// 2. Query graph for related nodes (USES, TESTS_WITH, LEARNS_FROM)
/// 3. Filter by domain match if query contains domain terms
/// 4. Execute federated search on project + related repos
/// 5. Weight results by relationship strength
pub fn execute_graph_routing(query: Option<&str>, options: &ScryOptions) -> Result<()> {
    let query = query.ok_or_else(|| anyhow::anyhow!("Query required for graph routing"))?;

    println!("Mode: Graph Routing (smart cross-project search)\n");
    println!("Query: \"{}\"\n", query);

    // 1. Open graph and detect current project
    let graph = Graph::open()?;
    let current_project = detect_current_project(&graph)?;

    println!("📍 Current project: {}", current_project);

    // 2. Get related nodes from graph
    let edge_types = [EdgeType::Uses, EdgeType::TestsWith, EdgeType::LearnsFrom];
    let related_nodes = graph.get_related(&current_project, &edge_types)?;

    if related_nodes.is_empty() {
        println!("⚠️  No related repos in graph. Falling back to current project only.");
        println!("   Tip: Use 'patina mother link' to add relationships.\n");
    } else {
        println!(
            "🔗 Related repos: {}",
            related_nodes
                .iter()
                .map(|n| n.id.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        );
    }

    // 3. Filter by domain match (optional - check if query terms match node domains)
    let query_lower = query.to_lowercase();
    let filtered_nodes: Vec<_> = if should_filter_by_domain(&query_lower) {
        let filtered: Vec<_> = related_nodes
            .iter()
            .filter(|n| node_matches_query_domain(n, &query_lower))
            .collect();

        if !filtered.is_empty() && filtered.len() < related_nodes.len() {
            println!(
                "🎯 Domain filter: {} (matched {} of {} related)",
                filtered
                    .iter()
                    .map(|n| n.id.as_str())
                    .collect::<Vec<_>>()
                    .join(", "),
                filtered.len(),
                related_nodes.len()
            );
            filtered.into_iter().cloned().collect()
        } else {
            related_nodes.clone()
        }
    } else {
        related_nodes.clone()
    };

    // Build list of repos to search (for routing context)
    let repos_to_search: Vec<String> = filtered_nodes.iter().map(|n| n.id.clone()).collect();

    // Get edges for weighting
    let edges = graph.get_edges_from(&current_project)?;

    // Build EdgeInfo for routing context (G2.5)
    let edges_used: Vec<EdgeInfo> = edges
        .iter()
        .filter(|e| repos_to_search.contains(&e.to_node))
        .map(|e| EdgeInfo {
            id: e.id,
            from_node: e.from_node.clone(),
            to_node: e.to_node.clone(),
            edge_type: e.edge_type.as_str().to_string(),
            weight: e.weight,
        })
        .collect();

    // Track whether domain filtering was applied
    let domain_filter_applied = filtered_nodes.len() < related_nodes.len();

    println!();

    // 4. Execute federated search
    // Tuple: (source_label, repo_id, weight, result)
    let mut all_results: Vec<(String, String, f32, ScryResult)> = Vec::new();

    // Search current project
    let in_project = Path::new(".patina/local/data/patina.db").exists();
    if in_project {
        println!("📂 Searching current project...");
        let project_options = ScryOptions {
            repo: None,
            all_repos: false,
            ..options.clone()
        };
        match scry_text(query, &project_options) {
            Ok(results) => {
                println!("   Found {} results", results.len());
                for r in results {
                    // Current project gets weight 1.0 (baseline)
                    all_results.push(("[PROJECT]".to_string(), current_project.clone(), 1.0, r));
                }
            }
            Err(e) => {
                eprintln!("   ⚠️  Project search failed: {}", e);
            }
        }
    }

    // Search related repos
    let repos_searched = repos_to_search.len();

    for repo_id in &repos_to_search {
        println!("📚 Searching {}...", repo_id);
        let repo_options = ScryOptions {
            repo: Some(repo_id.clone()),
            all_repos: false,
            ..options.clone()
        };
        match scry_text(query, &repo_options) {
            Ok(results) => {
                println!("   Found {} results", results.len());

                // 5. Apply relationship weighting
                let weight = get_relationship_weight(&edges, repo_id);

                for r in results {
                    all_results.push((
                        format!("[{}]", repo_id.to_uppercase()),
                        repo_id.clone(),
                        weight,
                        r,
                    ));
                }
            }
            Err(e) => {
                eprintln!("   ⚠️  {} search failed: {}", repo_id, e);
            }
        }
    }

    // Query persona if enabled
    if options.include_persona {
        println!("🧠 Searching persona...");
        if let Ok(persona_results) = persona::query(query, options.limit, options.min_score, None) {
            println!("   Found {} results", persona_results.len());
            for p in persona_results {
                all_results.push((
                    "[PERSONA]".to_string(),
                    "persona".to_string(), // Persona is a special source
                    1.0,                   // Persona gets baseline weight
                    ScryResult {
                        id: 0,
                        content: p.content,
                        score: p.score,
                        event_type: p.source.clone(),
                        source_id: p.domains.join(", "),
                        timestamp: p.timestamp,
                    },
                ));
            }
        }
    }

    // Sort by weighted score and take top limit
    // Tuple: (source_label, repo_id, weight, result)
    all_results.sort_by(|a, b| {
        let weighted_a = a.3.score * a.2;
        let weighted_b = b.3.score * b.2;
        weighted_b
            .partial_cmp(&weighted_a)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    all_results.truncate(options.limit);

    // Build routing context for logging (G2.5)
    let total_repos = crate::commands::repo::list().map(|r| r.len()).unwrap_or(0);
    let routing_context = RoutingContext {
        strategy: "graph".to_string(),
        source_project: current_project.clone(),
        edges_used,
        repos_searched: repos_to_search.clone(),
        repos_available: total_repos + 1, // +1 for current project
        domain_filter_applied,
    };

    // Convert to RoutedResult for logging
    let routed_results: Vec<RoutedResult> = all_results
        .iter()
        .map(|(_, repo_id, weight, result)| RoutedResult {
            source_repo: repo_id.clone(),
            weight: *weight,
            result: result.clone(),
        })
        .collect();

    // Log query with routing context (G2.5)
    let query_id = log_scry_query_with_routing(query, &routed_results, &routing_context);

    // Record edge usage for each edge that contributed (G2.5)
    if let Some(ref qid) = query_id {
        for edge in &routing_context.edges_used {
            // Find best rank for this edge's target repo in results
            let best_rank = routed_results
                .iter()
                .enumerate()
                .find(|(_, r)| r.source_repo == edge.to_node)
                .map(|(i, _)| i + 1);

            // Record usage (best-effort, don't fail on error)
            let _ = graph.record_edge_usage(edge.id, qid, &edge.to_node, best_rank);
        }
    }

    println!();

    if all_results.is_empty() {
        println!("No results found.");
        return Ok(());
    }

    // Report routing efficiency
    println!(
        "Found {} results (searched {} of {} repos):\n",
        all_results.len(),
        repos_searched + 1, // +1 for current project
        total_repos + 1
    );
    println!("{}", "".repeat(60));

    for (i, (source, _repo_id, weight, result)) in all_results.iter().enumerate() {
        let timestamp_display = if result.timestamp.is_empty() {
            String::new()
        } else {
            format!(" | {}", result.timestamp)
        };
        let weight_display = if (*weight - 1.0).abs() > 0.01 {
            format!(" (w={:.2})", weight)
        } else {
            String::new()
        };
        println!(
            "\n[{}] {} Score: {:.3}{} | {} | {}{}",
            i + 1,
            source,
            result.score,
            weight_display,
            result.event_type,
            result.source_id,
            timestamp_display
        );
        println!("    {}", truncate_content(&result.content, 200));
    }

    println!("\n{}", "".repeat(60));

    // Show query_id for feedback commands
    if let Some(ref qid) = query_id {
        println!("\nQuery ID: {} (use with 'scry open/copy/feedback')", qid);
    }

    Ok(())
}

/// Detect current project from graph
///
/// Looks up the current working directory in the graph nodes.
/// Falls back to directory name if not found.
fn detect_current_project(graph: &Graph) -> Result<String> {
    let cwd = std::env::current_dir()?;

    // Try to find a node matching the current directory
    let nodes = graph.list_nodes()?;
    for node in &nodes {
        if node.path == cwd {
            return Ok(node.id.clone());
        }
    }

    // Fallback: use directory name
    let dir_name = cwd
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("unknown");

    // Check if this name exists in graph
    if graph.get_node(dir_name)?.is_some() {
        return Ok(dir_name.to_string());
    }

    // Return directory name even if not in graph (will have no edges)
    Ok(dir_name.to_string())
}

/// Check if query should trigger domain filtering
///
/// Returns true if query contains domain-specific terms that could narrow results.
fn should_filter_by_domain(query: &str) -> bool {
    // Domain keywords that suggest filtering would help
    let domain_hints = [
        "cairo",
        "rust",
        "typescript",
        "javascript",
        "python",
        "go",
        "java",
        "c++",
        "cpp",
        "solidity",
        "prolog",
        "dojo",
        "starknet",
        "mcp",
        "ecs",
        "vector",
        "embedding",
    ];

    domain_hints.iter().any(|hint| query.contains(hint))
}

/// Check if a node's domains match query terms
fn node_matches_query_domain(node: &mother::Node, query: &str) -> bool {
    // Check if any domain matches query
    for domain in &node.domains {
        if query.contains(&domain.to_lowercase()) {
            return true;
        }
    }

    // Check if node ID matches query (e.g., "dojo" in "how does dojo handle...")
    if query.contains(&node.id.to_lowercase()) {
        return true;
    }

    false
}

/// Get relationship weight for a repo based on edge type
///
/// Weighting rationale (from G0 error analysis):
/// - TESTS_WITH: High relevance for benchmark/testing queries
/// - LEARNS_FROM: Medium relevance for pattern/implementation queries
/// - USES: Medium relevance for dependency queries
/// - Default: Baseline weight
fn get_relationship_weight(edges: &[mother::Edge], repo_id: &str) -> f32 {
    for edge in edges {
        if edge.to_node == repo_id {
            return match edge.edge_type {
                EdgeType::TestsWith => 1.2,  // Boost test subjects
                EdgeType::LearnsFrom => 1.1, // Slight boost for learning sources
                EdgeType::Uses => 1.1,       // Slight boost for dependencies
                EdgeType::Sibling => 1.0,    // Baseline for siblings
                EdgeType::Domain => 1.0,     // Baseline for domain connections
            };
        }
    }
    1.0 // Default weight
}