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
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
//! Belief audit command — show computed use/truth metrics for all beliefs
//!
//! Reads from the `beliefs` table (computed by `patina scrape`) and displays
//! real metrics instead of fabricated confidence scores.
//!
//! E4.6a: --grounding flag computes semantic grounding from usearch embeddings.

use anyhow::{Context, Result};
use clap::Subcommand;
use rusqlite::Connection;
use std::path::Path;
use usearch::{Index, IndexOptions, MetricKind, ScalarKind};

use super::scrape::database;
use super::scry::internal::enrichment::{enrich_results, SearchResults};

#[derive(Subcommand, Debug)]
pub enum BeliefCommands {
    /// Show all beliefs ranked by use/truth metrics (default)
    Audit {
        /// Sort by: "use" (default), "truth", "weak"
        #[arg(long, default_value = "use")]
        sort: String,

        /// Show only beliefs with warnings
        #[arg(long)]
        warnings_only: bool,

        /// Show semantic grounding — nearest code/commits/sessions for each belief (E4.6a)
        #[arg(long)]
        grounding: bool,
    },
}

pub fn execute(command: Option<BeliefCommands>) -> Result<()> {
    let cmd = command.unwrap_or(BeliefCommands::Audit {
        sort: "use".to_string(),
        warnings_only: false,
        grounding: false,
    });

    match cmd {
        BeliefCommands::Audit {
            sort,
            warnings_only,
            grounding,
        } => run_audit(&sort, warnings_only, grounding),
    }
}

struct BeliefRow {
    id: String,
    entrenchment: String,
    cited_by_beliefs: i32,
    cited_by_sessions: i32,
    applied_in: i32,
    evidence_count: i32,
    evidence_verified: i32,
    defeated_attacks: i32,
    verification_total: i32,
    verification_passed: i32,
    verification_failed: i32,
    verification_errored: i32,
    // E4.6a: Semantic grounding
    grounding_score: f32,
    grounding_code_count: i32,
    grounding_commit_count: i32,
    grounding_session_count: i32,
}

impl BeliefRow {
    fn total_use(&self) -> i32 {
        self.cited_by_beliefs + self.cited_by_sessions
    }

    fn v_ok_display(&self) -> String {
        if self.verification_total == 0 {
            "\u{2014}".to_string() // em dash
        } else {
            format!("{}/{}", self.verification_passed, self.verification_total)
        }
    }

    fn grounding_total(&self) -> i32 {
        self.grounding_code_count + self.grounding_commit_count + self.grounding_session_count
    }

    fn grounding_display(&self) -> String {
        if self.grounding_total() == 0 {
            "\u{2014}".to_string() // em dash
        } else {
            format!(
                "{}c{}m{}s",
                self.grounding_code_count,
                self.grounding_commit_count,
                self.grounding_session_count
            )
        }
    }

    fn health_warnings(&self) -> Vec<&'static str> {
        let mut warnings = Vec::new();
        if self.evidence_count == 0 {
            warnings.push("no-evidence");
        }
        if self.evidence_verified == 0 && self.evidence_count > 0 {
            warnings.push("unverified");
        }
        if self.total_use() == 0 {
            warnings.push("unused");
        }
        if self.applied_in == 0 {
            warnings.push("no-applications");
        }
        if self.verification_failed > 0 {
            warnings.push("verify-contested");
        }
        if self.verification_errored > 0 {
            warnings.push("verify-error");
        }
        if self.grounding_total() == 0 && self.grounding_score == 0.0 {
            warnings.push("floating");
        }
        warnings
    }
}

fn run_audit(sort_by: &str, warnings_only: bool, show_grounding: bool) -> Result<()> {
    let db_path = Path::new(database::PATINA_DB);
    if !db_path.exists() {
        anyhow::bail!("No database found. Run `patina scrape` first.");
    }

    let conn = Connection::open(db_path)?;

    // Check if metric columns exist
    let has_metrics = conn
        .prepare("SELECT cited_by_beliefs FROM beliefs LIMIT 1")
        .is_ok();

    if !has_metrics {
        anyhow::bail!(
            "Belief metrics not computed yet. Run `patina scrape --rebuild` to compute use/truth metrics."
        );
    }

    let order_clause = match sort_by {
        "truth" => "evidence_count DESC, evidence_verified DESC",
        "weak" => "(cited_by_beliefs + cited_by_sessions) ASC, evidence_count ASC",
        "grounding" => "grounding_score DESC, (grounding_code_count + grounding_commit_count + grounding_session_count) DESC",
        _ => "(cited_by_beliefs + cited_by_sessions) DESC, evidence_count DESC", // "use" default
    };

    // Check if verification columns exist (migration may not have run yet)
    let has_verification = conn
        .prepare("SELECT verification_total FROM beliefs LIMIT 1")
        .is_ok();

    // Check if grounding columns exist
    let has_grounding = conn
        .prepare("SELECT grounding_score FROM beliefs LIMIT 1")
        .is_ok();

    let sql = format!(
        "SELECT id, entrenchment, cited_by_beliefs, cited_by_sessions, applied_in,
                evidence_count, evidence_verified, defeated_attacks{}{}
         FROM beliefs
         ORDER BY {}",
        if has_verification {
            ", verification_total, verification_passed, verification_failed, verification_errored"
        } else {
            ""
        },
        if has_grounding {
            ", grounding_score, grounding_code_count, grounding_commit_count, grounding_session_count"
        } else {
            ""
        },
        order_clause
    );

    let mut stmt = conn.prepare(&sql)?;
    let rows: Vec<BeliefRow> = stmt
        .query_map([], |row| {
            let base_idx = 8; // 0-7 are always present
            let v_offset = base_idx;
            let g_offset = if has_verification {
                v_offset + 4
            } else {
                v_offset
            };

            Ok(BeliefRow {
                id: row.get(0)?,
                entrenchment: row.get(1)?,
                cited_by_beliefs: row.get(2)?,
                cited_by_sessions: row.get(3)?,
                applied_in: row.get(4)?,
                evidence_count: row.get(5)?,
                evidence_verified: row.get(6)?,
                defeated_attacks: row.get(7)?,
                verification_total: if has_verification {
                    row.get(v_offset)?
                } else {
                    0
                },
                verification_passed: if has_verification {
                    row.get(v_offset + 1)?
                } else {
                    0
                },
                verification_failed: if has_verification {
                    row.get(v_offset + 2)?
                } else {
                    0
                },
                verification_errored: if has_verification {
                    row.get(v_offset + 3)?
                } else {
                    0
                },
                grounding_score: if has_grounding {
                    row.get(g_offset)?
                } else {
                    0.0
                },
                grounding_code_count: if has_grounding {
                    row.get(g_offset + 1)?
                } else {
                    0
                },
                grounding_commit_count: if has_grounding {
                    row.get(g_offset + 2)?
                } else {
                    0
                },
                grounding_session_count: if has_grounding {
                    row.get(g_offset + 3)?
                } else {
                    0
                },
            })
        })?
        .filter_map(|r| r.ok())
        .collect();

    if rows.is_empty() {
        println!("No beliefs found. Create beliefs in layer/surface/epistemic/beliefs/");
        return Ok(());
    }

    // Filter if warnings_only
    let display_rows: Vec<&BeliefRow> = if warnings_only {
        rows.iter()
            .filter(|r| !r.health_warnings().is_empty())
            .collect()
    } else {
        rows.iter().collect()
    };

    // Print header
    println!(
        "\n  Belief Audit — {} beliefs (sorted by {})\n",
        rows.len(),
        sort_by
    );
    println!(
        "  {:<36} {:>5} {:>5} {:>4} {:>4} {:>4} {:>4} {:>5} {:>9} {:>7} WARNINGS",
        "BELIEF", "B-USE", "S-USE", "EVID", "VERI", "DEFT", "APPL", "V-OK", "ENTRENCH", "GROUND"
    );
    println!(
        "  {:<36} {:>5} {:>5} {:>4} {:>4} {:>4} {:>4} {:>5} {:>9} {:>7} ────────",
        "──────", "─────", "─────", "────", "────", "────", "────", "─────", "─────────", "───────"
    );

    let mut warning_count = 0;
    for row in &display_rows {
        let warnings = row.health_warnings();
        if !warnings.is_empty() {
            warning_count += 1;
        }
        let warning_str = if warnings.is_empty() {
            String::new()
        } else {
            warnings.join(", ")
        };

        // Truncate ID for display
        let display_id = if row.id.len() > 35 {
            format!("{}", &row.id[..34])
        } else {
            row.id.clone()
        };

        println!(
            "  {:<36} {:>5} {:>5} {:>4} {:>4} {:>4} {:>4} {:>5} {:>9} {:>7} {}",
            display_id,
            row.cited_by_beliefs,
            row.cited_by_sessions,
            row.evidence_count,
            row.evidence_verified,
            row.defeated_attacks,
            row.applied_in,
            row.v_ok_display(),
            row.entrenchment,
            row.grounding_display(),
            warning_str,
        );
    }

    // Summary
    let total_use: i32 = rows.iter().map(|r| r.total_use()).sum();
    let total_evidence: i32 = rows.iter().map(|r| r.evidence_count).sum();
    let total_verified: i32 = rows.iter().map(|r| r.evidence_verified).sum();
    let with_no_evidence: usize = rows.iter().filter(|r| r.evidence_count == 0).count();
    let with_unverified: usize = rows
        .iter()
        .filter(|r| r.evidence_verified == 0 && r.evidence_count > 0)
        .count();
    let unused: usize = rows.iter().filter(|r| r.total_use() == 0).count();

    // Verification stats
    let beliefs_with_queries: usize = rows.iter().filter(|r| r.verification_total > 0).count();
    let total_queries: i32 = rows.iter().map(|r| r.verification_total).sum();
    let total_passed: i32 = rows.iter().map(|r| r.verification_passed).sum();
    let total_failed: i32 = rows.iter().map(|r| r.verification_failed).sum();
    let total_errored: i32 = rows.iter().map(|r| r.verification_errored).sum();

    // Grounding stats
    let grounded: usize = rows.iter().filter(|r| r.grounding_total() > 0).count();
    let floating: usize = rows.len() - grounded;

    println!("\n  ── Summary ──");
    println!("  Total beliefs: {}", rows.len());
    println!(
        "  Total citations: {} ({} by beliefs, {} by sessions)",
        total_use,
        rows.iter().map(|r| r.cited_by_beliefs).sum::<i32>(),
        rows.iter().map(|r| r.cited_by_sessions).sum::<i32>()
    );
    println!(
        "  Evidence: {} total, {} verified ({:.0}%)",
        total_evidence,
        total_verified,
        if total_evidence > 0 {
            total_verified as f64 / total_evidence as f64 * 100.0
        } else {
            0.0
        }
    );
    if total_queries > 0 {
        println!(
            "  Verification: {} queries across {} beliefs ({} passed, {} contested, {} errors)",
            total_queries, beliefs_with_queries, total_passed, total_failed, total_errored
        );
    }
    if grounded > 0 || floating > 0 {
        println!("  Grounding: {} grounded, {} floating", grounded, floating);
    }
    if warning_count > 0 {
        println!("\n  Warnings: {}", warning_count);
        if with_no_evidence > 0 {
            println!("    {} beliefs with no evidence", with_no_evidence);
        }
        if with_unverified > 0 {
            println!("    {} beliefs with unverified evidence", with_unverified);
        }
        if unused > 0 {
            println!("    {} beliefs with no citations", unused);
        }
        if floating > 0 {
            println!(
                "    {} beliefs floating (no code/commit/session grounding)",
                floating
            );
        }
        if total_failed > 0 {
            println!("    {} beliefs with contested verification", total_failed);
        }
        if total_errored > 0 {
            println!("    {} beliefs with verification errors", total_errored);
        }
    }
    println!();

    // E4.6a: Semantic grounding report
    if show_grounding {
        run_grounding_report(&conn, &rows)?;
    }

    Ok(())
}

/// Compute and display semantic grounding for each belief (E4.6a)
///
/// Uses the usearch semantic index to find each belief's nearest neighbors
/// across all content types. Shows what code, commits, and sessions each
/// belief is semantically connected to.
fn run_grounding_report(conn: &Connection, rows: &[BeliefRow]) -> Result<()> {
    // Get embeddings path
    let model = crate::commands::scry::internal::search::get_embedding_model();
    let index_path = format!(
        ".patina/local/data/embeddings/{}/projections/semantic.usearch",
        model
    );

    if !Path::new(&index_path).exists() {
        println!("  Grounding: semantic index not found. Run `patina oxidize` first.\n");
        return Ok(());
    }

    // Load usearch index
    let index_options = IndexOptions {
        dimensions: 256,
        metric: MetricKind::Cos,
        quantization: ScalarKind::F32,
        ..Default::default()
    };

    let index = Index::new(&index_options).context("Failed to create index")?;
    index
        .load(&index_path)
        .context("Failed to load semantic index")?;

    const BELIEF_ID_OFFSET: i64 = 4_000_000_000;
    const CODE_ID_OFFSET: i64 = 1_000_000_000;
    const PATTERN_ID_OFFSET: i64 = 2_000_000_000;
    const COMMIT_ID_OFFSET: i64 = 3_000_000_000;
    const GROUNDING_LIMIT: usize = 20; // Search this many neighbors
    const DISPLAY_LIMIT: usize = 3; // Show top 3 per type

    println!("  ── Semantic Grounding (E4.6a) ──\n");

    let mut grounded_count = 0;
    let mut floating_count = 0;

    for row in rows {
        // Look up belief's rowid
        let rowid: Result<i64, _> =
            conn.query_row("SELECT rowid FROM beliefs WHERE id = ?", [&row.id], |r| {
                r.get(0)
            });

        let rowid = match rowid {
            Ok(r) => r,
            Err(_) => continue,
        };

        let belief_key = (BELIEF_ID_OFFSET + rowid) as u64;

        // Get belief's vector
        let mut vector = vec![0.0_f32; 256];
        if index.get(belief_key, &mut vector).is_err() {
            continue;
        }

        // Check for zero vector (not in index)
        let magnitude: f32 = vector.iter().map(|v| v * v).sum::<f32>().sqrt();
        if magnitude < 0.001 {
            continue;
        }

        // Search for neighbors
        let matches = match index.search(&vector, GROUNDING_LIMIT + 2) {
            Ok(m) => m,
            Err(_) => continue,
        };

        let results = SearchResults {
            keys: matches.keys,
            distances: matches.distances,
        };

        let enriched = match enrich_results(conn, &results, "semantic", 0.0) {
            Ok(r) => r,
            Err(_) => continue,
        };

        // Filter out self entries and categorize
        let mut code_results = Vec::new();
        let mut commit_results = Vec::new();
        let mut session_results = Vec::new();

        for r in &enriched {
            // Skip self — belief appears as both belief.surface and pattern.surface
            // Pattern source_id is now file_path, so check contains for pattern match
            if r.event_type == "belief.surface" && r.source_id == row.id {
                continue;
            }
            if r.event_type.starts_with("pattern.") && r.source_id.contains(&row.id) {
                continue;
            }

            let key = r.id;
            if (CODE_ID_OFFSET..PATTERN_ID_OFFSET).contains(&key) {
                code_results.push(r);
            } else if (COMMIT_ID_OFFSET..BELIEF_ID_OFFSET).contains(&key) {
                commit_results.push(r);
            } else if key < CODE_ID_OFFSET {
                session_results.push(r);
            }
        }

        let has_grounding =
            !code_results.is_empty() || !commit_results.is_empty() || !session_results.is_empty();

        if has_grounding {
            grounded_count += 1;
        } else {
            floating_count += 1;
        }

        // Display
        let display_id = if row.id.len() > 35 {
            format!("{}", &row.id[..34])
        } else {
            row.id.clone()
        };

        println!(
            "  {} ({}c {}m {}s)",
            display_id,
            code_results.len(),
            commit_results.len(),
            session_results.len()
        );

        // Show top code neighbors
        for r in code_results.iter().take(DISPLAY_LIMIT) {
            println!("    code  {:.3}  {}", r.score, truncate(&r.source_id, 60));
        }
        for r in commit_results.iter().take(DISPLAY_LIMIT) {
            println!("    commit {:.3}  {}", r.score, truncate(&r.content, 60));
        }
        for r in session_results.iter().take(DISPLAY_LIMIT) {
            println!("    session {:.3} {}", r.score, truncate(&r.content, 55));
        }

        if has_grounding {
            println!();
        } else {
            println!("    (floating — no code/commit/session neighbors)\n");
        }
    }

    println!(
        "  ── Grounding Summary: {} grounded, {} floating ──\n",
        grounded_count, floating_count
    );

    Ok(())
}

fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        return s.to_string();
    }
    let truncated: String = s.chars().take(max - 1).collect();
    format!("{}", truncated)
}