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
// Shared utilities for all scrape subcommands

pub mod beliefs;
pub mod code;
pub mod database;
pub mod forge;
pub mod git;
pub mod layer;
pub mod sessions;

use anyhow::{bail, Result};
use std::path::PathBuf;

use patina::paths;

/// Common configuration for all scrapers
pub struct ScrapeConfig {
    pub db_path: String,
    pub force: bool,
}

impl ScrapeConfig {
    pub fn new(force: bool) -> Self {
        Self {
            db_path: database::PATINA_DB.to_string(),
            force,
        }
    }
}

/// Common stats that all scrapers return
#[derive(Debug)]
pub struct ScrapeStats {
    pub items_processed: usize,
    pub time_elapsed: std::time::Duration,
    pub database_size_kb: u64,
}

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

    #[test]
    fn test_scrape_stats_creation() {
        let stats = ScrapeStats {
            items_processed: 100,
            time_elapsed: Duration::from_secs(5),
            database_size_kb: 1024,
        };
        assert_eq!(stats.items_processed, 100);
        assert_eq!(stats.time_elapsed.as_secs(), 5);
        assert_eq!(stats.database_size_kb, 1024);
    }
}

/// Run all scrapers in sequence (code, git, layer, beliefs)
///
/// This is the default when running `patina scrape` with no subcommand.
/// Layer scraper handles both patterns and sessions (unified in v0.12.0).
pub fn execute_all() -> Result<()> {
    // Ensure UID exists (migration for projects without one)
    patina::project::create_uid_if_missing(&std::env::current_dir()?)?;

    println!("🔄 Running all scrapers...\n");

    println!("📊 [1/4] Scraping code...");
    execute_code(false, false)?;

    println!("\n📊 [2/4] Scraping git...");
    let git_stats = git::run(false)?;
    println!("{} commits", git_stats.items_processed);

    println!("\n📜 [3/4] Scraping layer (patterns + sessions)...");
    let layer_stats = layer::run(false)?;
    println!("{} items", layer_stats.items_processed);

    println!("\n🧠 [4/4] Scraping beliefs...");
    let belief_stats = beliefs::run(false)?;
    println!("{} beliefs", belief_stats.items_processed);

    println!("\n✅ All scrapers complete!");
    Ok(())
}

/// Rebuild database from scratch.
///
/// For ref repos: removes old eventlog bloat (git/code events) and rebuilds
/// with lean storage pattern. Includes forge data re-fetch.
///
/// See: layer/surface/build/spec-ref-repo-storage.md
pub fn execute_rebuild() -> Result<()> {
    // Ensure UID exists (migration for projects without one)
    patina::project::create_uid_if_missing(&std::env::current_dir()?)?;

    let db_path = PathBuf::from(database::PATINA_DB);
    let is_ref = database::is_ref_repo(&db_path);

    // Get old size if exists
    let old_size_kb = std::fs::metadata(&db_path)
        .map(|m| m.len() / 1024)
        .unwrap_or(0);

    if is_ref {
        println!("🔧 Rebuilding ref repo database (lean storage)...");
        println!("   Old size: {} KB", old_size_kb);
    } else {
        println!("🔧 Rebuilding project database...");
    }

    // Delete existing database
    if db_path.exists() {
        std::fs::remove_file(&db_path)?;
        println!("   Deleted old database");
    }

    // Run all scrapers fresh (they will use lean storage for ref repos)
    println!("\n🔄 Running all scrapers...\n");

    println!("📊 [1/5] Scraping code...");
    execute_code(false, false)?;

    println!("\n📊 [2/5] Scraping git...");
    let git_stats = git::run(false)?;
    println!("{} commits", git_stats.items_processed);

    println!("\n📜 [3/5] Scraping layer (patterns + sessions)...");
    let layer_stats = layer::run(false)?;
    println!("{} items", layer_stats.items_processed);

    println!("\n🧠 [4/5] Scraping beliefs...");
    let belief_stats = beliefs::run(false)?;
    println!("{} beliefs", belief_stats.items_processed);

    // For ref repos, also rebuild forge data (this is the expensive cached data we preserve)
    if is_ref {
        println!("\n🔗 [5/5] Scraping forge (issues/PRs)...");
        // Use full=true to force complete re-fetch since we deleted the database
        execute_forge(true, false, false, false, None, None)?;
    } else {
        println!("\n📝 [5/5] Skipping forge (run 'patina scrape forge' separately)");
    }

    // Report new size
    let new_size_kb = std::fs::metadata(&db_path)
        .map(|m| m.len() / 1024)
        .unwrap_or(0);

    println!("\n✅ Rebuild complete!");
    println!("   New size: {} KB", new_size_kb);

    if is_ref && old_size_kb > 0 {
        let reduction = if old_size_kb > new_size_kb {
            ((old_size_kb - new_size_kb) * 100) / old_size_kb
        } else {
            0
        };
        println!(
            "   Reduction: {} KB → {} KB ({}% smaller)",
            old_size_kb, new_size_kb, reduction
        );
    }

    Ok(())
}

/// Execute code scraper for current directory
///
/// For external repos, use `patina repo update <name>` instead.
pub fn execute_code(init: bool, force: bool) -> Result<()> {
    let config = ScrapeConfig::new(force);

    if init {
        code::initialize(&config)?;
    } else {
        let stats = code::run(config)?;

        println!("\n📊 Code Extraction Summary:");
        println!("  • Items processed: {}", stats.items_processed);
        println!("  • Time elapsed: {:?}", stats.time_elapsed);
        println!("  • Database size: {} KB", stats.database_size_kb);
    }

    Ok(())
}

/// Execute git scraper with summary output
pub fn execute_git(full: bool) -> Result<()> {
    let stats = git::run(full)?;
    println!("\n📊 Git Scrape Summary:");
    println!("  • Commits processed: {}", stats.items_processed);
    println!("  • Time elapsed: {:?}", stats.time_elapsed);
    println!("  • Database size: {} KB", stats.database_size_kb);
    Ok(())
}

/// Execute sessions scraper with summary output (deprecated)
pub fn execute_sessions(full: bool) -> Result<()> {
    eprintln!("WARNING: `scrape sessions` is deprecated. Use `scrape layer` instead.");
    eprintln!("         Sessions are part of layer/ and scraped automatically.\n");
    let stats = sessions::run(full)?;
    println!("\n📊 Sessions Scrape Summary:");
    println!("  • Sessions processed: {}", stats.items_processed);
    println!("  • Time elapsed: {:?}", stats.time_elapsed);
    println!("  • Database size: {} KB", stats.database_size_kb);
    Ok(())
}

/// Execute unified layer scraper (patterns + sessions)
pub fn execute_layer(full: bool) -> Result<()> {
    let stats = layer::run(full)?;
    println!("\n📊 Layer Scrape Summary:");
    println!(
        "  • Items processed: {} (patterns + sessions)",
        stats.items_processed
    );
    println!("  • Time elapsed: {:?}", stats.time_elapsed);
    println!("  • Database size: {} KB", stats.database_size_kb);
    Ok(())
}

/// Resolve ref repo name to path.
fn resolve_repo_path(name: &str) -> Result<PathBuf> {
    let repo_path = paths::repos::cache_dir().join(name);
    if !repo_path.exists() {
        bail!(
            "Repository '{}' not found. Use 'patina repo list' to see registered repos.",
            name
        );
    }
    Ok(repo_path)
}

/// Execute forge scraper (issues and PRs from GitHub/Gitea)
pub fn execute_forge(
    full: bool,
    status: bool,
    sync: bool,
    log: bool,
    limit: Option<usize>,
    repo: Option<String>,
) -> Result<()> {
    // Resolve working directory if --repo provided
    let working_dir = match &repo {
        Some(name) => Some(resolve_repo_path(name)?),
        None => None,
    };

    // Get repo spec early - needed for status, sync, log
    let repo_spec = get_repo_spec(working_dir.as_ref())?;

    // Handle --log: tail the sync log file
    if log {
        return execute_forge_log(&repo_spec);
    }

    // Handle --status: show sync status
    if status {
        return execute_forge_status(working_dir.as_ref(), &repo_spec);
    }

    // Handle --sync: fork to background
    if sync {
        return execute_forge_background(working_dir.as_ref(), &repo_spec);
    }

    // Handle --limit: foreground sync with cap
    if let Some(limit_val) = limit {
        return execute_forge_limited(working_dir.as_ref(), &repo_spec, limit_val);
    }

    // Default: discovery only (instant)
    let config = forge::ForgeScrapeConfig {
        force: full,
        working_dir,
        ..Default::default()
    };
    let stats = forge::run(config)?;
    println!("\n📊 Forge Scrape Summary:");
    println!("  • Items processed: {}", stats.items_processed);
    println!("  • Time elapsed: {:?}", stats.time_elapsed);
    println!("  • Database size: {} KB", stats.database_size_kb);
    Ok(())
}

/// Get repo spec (owner/repo) from git remote.
fn get_repo_spec(working_dir: Option<&PathBuf>) -> Result<String> {
    use std::process::Command;

    let mut cmd = Command::new("git");
    cmd.args(["remote", "get-url", "origin"]);
    if let Some(dir) = working_dir {
        cmd.current_dir(dir);
    }
    let output = cmd.output()?;

    if !output.status.success() {
        bail!("No git remote configured.");
    }

    let remote_url = String::from_utf8_lossy(&output.stdout).trim().to_string();
    let detected = patina::forge::detect(&remote_url);

    if detected.owner.is_empty() {
        bail!("Could not detect forge from remote URL.");
    }

    Ok(format!("{}/{}", detected.owner, detected.repo))
}

/// Get db path based on working directory.
fn get_db_path(working_dir: Option<&PathBuf>) -> PathBuf {
    match working_dir {
        Some(dir) => dir.join(".patina/local/data/patina.db"),
        None => PathBuf::from(database::PATINA_DB),
    }
}

/// Show forge sync status without making changes.
fn execute_forge_status(working_dir: Option<&PathBuf>, repo_spec: &str) -> Result<()> {
    let db_path = get_db_path(working_dir);

    if !db_path.exists() {
        println!("No patina.db found. Run `patina scrape` first.");
        return Ok(());
    }

    // Check if sync is running
    let running_pid = patina::forge::sync::is_running(repo_spec);

    let conn = database::initialize(&db_path)?;
    let stats = patina::forge::sync::status(&conn, repo_spec)?;

    println!("📊 Forge Sync Status for {}:", repo_spec);

    if let Some(pid) = running_pid {
        println!("  • Status: Syncing (PID {})", pid);
    } else {
        println!("  • Status: Idle");
    }

    println!("  • Resolved: {}", stats.resolved);
    println!("  • Pending: {}", stats.pending);
    println!("  • Errors: {}", stats.errors);

    if stats.pending > 0 {
        // At 750ms per ref, 50 refs/batch = ~37.5 seconds per batch
        let total_time_secs = (stats.pending as f64) * 0.75;
        let hours = (total_time_secs / 3600.0).floor() as usize;
        let minutes = ((total_time_secs % 3600.0) / 60.0).ceil() as usize;

        if hours > 0 {
            println!("\n  ETA: ~{}h {}m remaining", hours, minutes);
        } else {
            println!("\n  ETA: ~{}m remaining", minutes);
        }

        println!("  Rate: ~48 refs/min (750ms pacing)");
    }

    Ok(())
}

/// Tail the sync log file.
fn execute_forge_log(repo_spec: &str) -> Result<()> {
    use std::process::Command;

    let log_path = patina::forge::sync::log_path(repo_spec);

    if !log_path.exists() {
        println!("No log file found at: {}", log_path.display());
        println!("Run `patina scrape forge --sync` first.");
        return Ok(());
    }

    println!("📄 Tailing: {}", log_path.display());
    println!("   (Ctrl+C to stop)\n");

    // Use tail -f to follow the log
    let status = Command::new("tail")
        .args(["-f", log_path.to_str().unwrap_or("")])
        .status()?;

    if !status.success() {
        bail!("tail command failed");
    }

    Ok(())
}

/// Start background sync.
fn execute_forge_background(working_dir: Option<&PathBuf>, repo_spec: &str) -> Result<()> {
    use std::process::Command;

    let db_path = get_db_path(working_dir);

    if !db_path.exists() {
        bail!("No patina.db found. Run `patina scrape` first.");
    }

    // Get detected forge info
    let mut cmd = Command::new("git");
    cmd.args(["remote", "get-url", "origin"]);
    if let Some(dir) = working_dir {
        cmd.current_dir(dir);
    }
    let output = cmd.output()?;
    let remote_url = String::from_utf8_lossy(&output.stdout).trim().to_string();
    let detected = patina::forge::detect(&remote_url);

    // Start background sync
    let pid = patina::forge::sync::start_background(&db_path, repo_spec, &detected)?;

    let log_path = patina::forge::sync::log_path(repo_spec);

    println!("🔄 Syncing in background (PID {})", pid);
    println!("   Log: {}", log_path.display());
    println!("   Check: patina scrape forge --status");

    Ok(())
}

/// Foreground sync with limit.
fn execute_forge_limited(
    working_dir: Option<&PathBuf>,
    repo_spec: &str,
    limit: usize,
) -> Result<()> {
    use std::process::Command;

    let db_path = get_db_path(working_dir);

    if !db_path.exists() {
        bail!("No patina.db found. Run `patina scrape` first.");
    }

    // Get detected forge info
    let mut cmd = Command::new("git");
    cmd.args(["remote", "get-url", "origin"]);
    if let Some(dir) = working_dir {
        cmd.current_dir(dir);
    }
    let output = cmd.output()?;
    let remote_url = String::from_utf8_lossy(&output.stdout).trim().to_string();
    let detected = patina::forge::detect(&remote_url);

    println!("🔄 Syncing up to {} refs in foreground...", limit);

    let conn = database::initialize(&db_path)?;
    let reader = patina::forge::reader(&detected);
    let stats = patina::forge::sync::sync_limited(&conn, reader.as_ref(), repo_spec, limit)?;

    println!("\n📊 Forge Sync Summary:");
    println!("  • Discovered: {}", stats.discovered);
    println!("  • Resolved: {}", stats.resolved);
    println!("  • Pending: {}", stats.pending);
    if stats.cache_hits > 0 {
        println!("  • Cache hits: {}", stats.cache_hits);
    }
    if stats.errors > 0 {
        println!("  • Errors: {}", stats.errors);
    }

    Ok(())
}