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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Internal implementation for init command

pub mod config;
pub mod patterns;
pub mod validation;

use anyhow::{Context, Result};
use std::fs;
use std::path::{Path, PathBuf};

use patina::environment::Environment;
use patina::layer::Layer;

// Note: backup functions moved to 'adapter refresh' command
use self::config::{create_project_config, handle_version_manifest};
use self::patterns::copy_core_patterns_safe;
use self::validation::validate_environment;

use super::design_wizard::confirm;

/// Main execution logic for init command
pub fn execute_init(name: String, force: bool, local: bool, no_commit: bool) -> Result<()> {
    let json_output = false; // For init command, always false

    // === STEP 0: CHECK FOR HIERARCHY CONFLICTS (BEFORE ANYTHING ELSE) ===
    // Jon Gjengset principle: fail fast with clear errors
    check_hierarchy_conflicts(force)?;

    // === STEP 1: GIT SETUP (FIRST - BEFORE ANY DESTRUCTIVE CHANGES) ===
    println!("🎨 Initializing Patina...\n");

    // Check for gh CLI early (unless local mode)
    if !local {
        check_gh_cli_available()?;
    }

    // Initialize git if needed
    ensure_git_initialized()?;

    // Check if this is a re-init (already has .patina/)
    let is_reinit_early = name == "." && Path::new(".patina").exists();

    if is_reinit_early {
        // Re-init: skip branch management, just refresh files in place
        println!("🔄 Re-initializing existing Patina project...\n");
    } else {
        // First-time init: full git setup (fork detection, branch management)
        // NOTE: This checks for clean state BEFORE we modify any files
        patina::git::ensure_fork(local)?;
        println!();

        patina::git::ensure_patina_branch(force)?;
        println!("✓ On branch 'patina'\n");
    }

    // Ensure proper .gitignore exists (AFTER branch setup, so our changes go on patina branch)
    ensure_gitignore(Path::new("."))?;

    // === STEP 2: SAFE TO PROCEED WITH DESTRUCTIVE CHANGES ===

    // Note: We don't backup .claude/ here - that's handled by 'adapter refresh'
    // Init only creates skeleton (.patina/, layer/), not adapter directories
    // Devcontainer generation is handled by 'patina yolo', not init

    // Check for nested project
    if name != "." && Path::new(".patina").exists() {
        println!("⚠️  You're already in a Patina project!");
        println!(
            "   Running 'patina init {}' would create: {}",
            name,
            std::env::current_dir()?.join(&name).display()
        );
        if !confirm("Continue anyway?")? {
            println!("Initialization cancelled.");
            return Ok(());
        }
    }

    // Check if re-initializing (for non-current-dir case)
    let is_reinit = if name == "." {
        is_reinit_early
    } else {
        let path = PathBuf::from(&name);
        path.exists() && path.join(".patina").exists()
    };

    // Only print init message if we didn't already print re-init message
    if !is_reinit_early {
        if is_reinit {
            println!("🔄 Re-initializing Patina project...");
        } else {
            println!("🎨 Initializing Patina project: {name}");
        }
    }

    // Detect environment
    println!("🔍 Detecting environment...");
    let environment = Environment::detect()?;

    // Display environment info
    display_environment_info(&environment);

    // Create or determine project path
    let project_path = setup_project_path(&name)?;

    // Get project name from directory
    let project_name = if name == "." {
        project_path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("project")
            .to_string()
    } else {
        name.clone()
    };

    // Write ENVIRONMENT.toml with full captured data
    write_environment_toml(&project_path, &environment)?;

    // Initialize layer structure
    let layer_path = project_path.join("layer");
    let layer = Layer::new(&layer_path);
    layer
        .init()
        .context("Failed to initialize layer structure")?;
    println!("  ✓ Created layer structure");

    // Create UID (stable project identity)
    let uid = patina::project::create_uid_if_missing(&project_path)?;
    if !is_reinit {
        println!("  ✓ Created project UID: {}", uid);
    }

    // Create project configuration (without LLM - use 'adapter add' for that)
    create_project_config(&project_path, &name, &environment)?;

    // Handle version manifest
    handle_version_manifest(&project_path, is_reinit, json_output)?;

    // Note: LLM adapter initialization moved to 'patina adapter add'
    // Init only creates skeleton - run 'patina adapter add <claude|gemini|opencode>' for LLM support
    // Devcontainer generation moved to 'patina yolo'

    // Copy core patterns
    let patterns_copied = copy_core_patterns_safe(&project_path, &layer_path)?;
    if patterns_copied {
        println!("  ✓ Copied core patterns from Patina");
    }

    // Create initial session record (without LLM - added via 'adapter add')
    create_init_session(&layer_path, &project_name)?;

    // Initialize navigation index
    initialize_navigation(&project_path)?;

    // Note: Component updates for adapters are handled by 'adapter refresh'

    // Validate environment
    if let Some(warnings) = validate_environment(&environment)? {
        println!("\n⚠️  Environment warnings:");
        for warning in warnings {
            println!("   {warning}");
        }
    }

    // === STEP 3: COMMIT PATINA SETUP (unless --no-commit) ===
    if !no_commit && name == "." {
        // Only commit if we're initializing in current directory
        println!("\n📦 Committing Patina setup...");
        // Add specific committed files (not .patina/local/ which is gitignored)
        patina::git::add_paths(&[
            ".gitignore",
            ".patina/config.toml",
            ".patina/uid",
            ".patina/oxidize.yaml",
            ".patina/versions.json",
            "layer",
        ])?;

        let commit_msg = if is_reinit {
            "chore: update Patina configuration"
        } else {
            "chore: initialize Patina project"
        };

        patina::git::commit(commit_msg)?;
        println!("✓ Committed Patina initialization");
    }

    // Note: scrape and oxidize are now separate commands
    // Run 'patina scrape' and 'patina oxidize' after adding an adapter

    // Suggest tool installation if needed
    suggest_missing_tools(&environment)?;

    println!("\n✨ Project '{name}' initialized successfully!");
    println!("  Add an adapter: patina adapter add <claude|gemini|opencode>");

    Ok(())
}

fn display_environment_info(environment: &Environment) {
    println!("  ✓ OS: {} ({})", environment.os, environment.arch);
    for (tool, info) in &environment.tools {
        if info.available {
            println!(
                "{}: {}",
                tool,
                info.version.as_ref().unwrap_or(&"detected".to_string())
            );
        }
    }
}

/// Write ENVIRONMENT.toml with all captured environment data
fn write_environment_toml(project_path: &Path, environment: &Environment) -> Result<()> {
    let toml_path = project_path.join("ENVIRONMENT.toml");

    let content =
        toml::to_string_pretty(environment).context("Failed to serialize environment data")?;

    fs::write(&toml_path, content).context("Failed to write ENVIRONMENT.toml")?;

    println!("  ✓ Created ENVIRONMENT.toml with full environment data");
    Ok(())
}

fn setup_project_path(name: &str) -> Result<PathBuf> {
    let path = if name == "." {
        std::env::current_dir()?
    } else {
        let path = PathBuf::from(name);
        if !path.exists() {
            fs::create_dir_all(&path)?;
        }
        path.canonicalize()?
    };
    Ok(path)
}

fn create_init_session(layer_path: &Path, name: &str) -> Result<()> {
    let session_filename = format!("{}-init.md", chrono::Utc::now().format("%Y%m%d-%H%M%S"));
    let session_content = format!(
        "# {} Initialization\n\nInitialized on: {}\n\nNote: Run 'patina adapter add <name>' to add LLM support.\n",
        name,
        chrono::Utc::now().to_rfc3339(),
    );

    let sessions_path = layer_path.join("sessions");
    fs::create_dir_all(&sessions_path)?;
    fs::write(sessions_path.join(session_filename), session_content)?;

    Ok(())
}

fn initialize_navigation(project_path: &Path) -> Result<()> {
    // TODO: Fix NavigationIndexer to work with new PatternIndexer API
    // For now, just print a message
    if project_path.join("layer").exists() {
        println!("🔍 Reindexing patterns for navigation...");
        println!("  Indexing patterns... ✓ (0 patterns indexed)");
    } else {
        println!("🔍 Initializing navigation database...");
        println!("  ✓ Created navigation database");
        println!("  Indexing patterns... ✓ (0 patterns indexed)");
    }
    Ok(())
}

// Note: Component update functions moved to 'adapter refresh' command

fn suggest_missing_tools(environment: &Environment) -> Result<()> {
    use crate::commands::init::tool_installer;

    // Get list of tools we can help install
    let available_tools = tool_installer::get_available_tools();

    // Check which ones are missing from the environment
    let missing: Vec<_> = available_tools
        .iter()
        .filter(|tool| {
            !environment
                .tools
                .get(tool.name)
                .map(|info| info.available)
                .unwrap_or(false)
        })
        .collect();

    if !missing.is_empty() {
        println!("\n💡 Missing optional tools that can enhance your Patina experience:");
        for tool in &missing {
            println!("   - {}", tool.name);
        }
        println!("\n   Run 'patina init --install-tools' to install them automatically");
        println!("   (Note: --install-tools flag is not yet implemented)");
    }

    Ok(())
}

/// Check if gh CLI is available
fn check_gh_cli_available() -> Result<()> {
    use std::process::Command;

    let output = Command::new("gh").arg("--version").output();

    match output {
        Ok(output) if output.status.success() => Ok(()),
        _ => {
            eprintln!("Error: GitHub CLI (gh) is required but not found.");
            eprintln!();
            eprintln!("Please install the GitHub CLI:");
            eprintln!("  • macOS: brew install gh");
            eprintln!("  • Linux: See https://cli.github.com/manual/installation");
            eprintln!("  • Windows: winget install GitHub.cli");
            eprintln!();
            eprintln!("Or use --local flag to skip GitHub integration.");
            anyhow::bail!("GitHub CLI (gh) not found")
        }
    }
}

/// Ensure git is initialized in the current directory
fn ensure_git_initialized() -> Result<()> {
    use std::process::Command;

    // Check if we're in a git repository
    let output = Command::new("git")
        .args(["rev-parse", "--git-dir"])
        .output()
        .context("Failed to check git status")?;

    if !output.status.success() {
        // Not a git repo, initialize it
        println!("📝 No git repository found. Initializing...");

        let output = Command::new("git")
            .arg("init")
            .output()
            .context("Failed to initialize git repository")?;

        if !output.status.success() {
            anyhow::bail!("Failed to initialize git repository");
        }

        println!("✓ Initialized git repository");
    }

    Ok(())
}

/// Ensure a proper .gitignore exists with sensible defaults
pub fn ensure_gitignore(project_path: &Path) -> Result<()> {
    let gitignore_path = project_path.join(".gitignore");

    if !gitignore_path.exists() {
        // Create opinionated defaults for new projects
        create_default_gitignore(&gitignore_path)?;
    } else {
        // Ensure critical entries exist in existing .gitignore
        ensure_gitignore_entries(&gitignore_path)?;
    }

    Ok(())
}

/// Create a new .gitignore with sensible defaults
fn create_default_gitignore(gitignore_path: &Path) -> Result<()> {
    let content = r#"# Build artifacts
/target/
**/*.rs.bk
Cargo.lock

# Environment and secrets
.env
.env.*
*.pem
*.key
credentials.json
secrets.toml

# Dependencies
node_modules/
vendor/
venv/
__pycache__/
*.pyc

# Build outputs
dist/
build/
*.o
*.so
*.dylib
*.dll
*.exe

# IDE and editor files
.idea/
.vscode/
*.iml
*.swp
*.swo
*~
.DS_Store

# Patina local state (derived, not committed)
.patina/local/
ENVIRONMENT.toml

# Temporary files
*.tmp
*.bak
*.backup
*.old

# Database files
*.db
*.db-shm
*.db-wal
*.sqlite
*.sqlite3

# Logs
*.log
logs/
"#;

    fs::write(gitignore_path, content).context("Failed to create .gitignore")?;

    println!("✓ Created .gitignore with standard patterns");
    Ok(())
}

// Note: ensure_model_available() moved to oxidize command
// Models are downloaded during 'patina oxidize', not during init

/// Maximum depth to search for child patina projects
/// 6 levels covers most real project structures (monorepos go 5-6 deep)
/// TODO: Make configurable via .patina/config.toml
const CHILD_PROJECT_SEARCH_DEPTH: usize = 6;

/// Check for hierarchy conflicts before init
/// Prevents creating nested patina projects that cause duplicate slash commands
fn check_hierarchy_conflicts(force: bool) -> Result<()> {
    let current_dir = std::env::current_dir()?;

    // Check 1: Parent directories with .claude/ (would cause duplicate commands)
    let mut parent = current_dir.parent();
    let mut conflicting_parents = Vec::new();

    while let Some(p) = parent {
        let claude_dir = p.join(".claude");
        let commands_dir = claude_dir.join("commands");

        if commands_dir.exists() {
            conflicting_parents.push(p.to_path_buf());
        }

        parent = p.parent();
    }

    if !conflicting_parents.is_empty() {
        eprintln!("Error: Found .claude/commands/ in parent directory:");
        for p in &conflicting_parents {
            eprintln!("{}", p.display());
        }
        eprintln!();
        eprintln!("Claude Code walks up the directory tree and loads commands from each");
        eprintln!(".claude/commands/ it finds. This would cause duplicate slash commands.");
        eprintln!();
        eprintln!("To fix:");
        eprintln!(
            "  1. Remove the parent .claude/: rm -rf {}/.claude",
            conflicting_parents[0].display()
        );
        eprintln!("  2. Or use --force to ignore this check (not recommended)");

        if !force {
            anyhow::bail!("Hierarchy conflict: parent directory has .claude/commands/");
        }
        eprintln!();
        eprintln!("⚠️  Proceeding anyway due to --force flag...");
    }

    // Check 2: Child directories with .patina/ (would be nested projects)
    let child_projects = find_child_patina_projects(&current_dir)?;

    if !child_projects.is_empty() {
        eprintln!(
            "Error: Found Patina project(s) in subdirectories (checked {} levels):",
            CHILD_PROJECT_SEARCH_DEPTH
        );
        for p in &child_projects {
            eprintln!("{}", p.display());
        }
        eprintln!();
        eprintln!("Initializing here would create a parent project over existing ones,");
        eprintln!("causing duplicate commands and configuration conflicts.");
        eprintln!();
        eprintln!("To fix:");
        eprintln!("  1. Initialize in a different directory");
        eprintln!("  2. Or use --force to ignore this check (not recommended)");

        if !force {
            anyhow::bail!("Hierarchy conflict: child directories contain Patina projects");
        }
        eprintln!();
        eprintln!("⚠️  Proceeding anyway due to --force flag...");
    }

    Ok(())
}

/// Find child directories that already have .patina/ or .claude/commands/
/// Uses ignore crate for fast walking (respects .gitignore, skips node_modules etc.)
/// Returns on first match - we only need to find one to block init
fn find_child_patina_projects(dir: &Path) -> Result<Vec<PathBuf>> {
    use ignore::WalkBuilder;

    let walker = WalkBuilder::new(dir)
        .max_depth(Some(CHILD_PROJECT_SEARCH_DEPTH))
        .hidden(false) // Need to see .patina and .claude
        .git_ignore(true) // Skip node_modules, target, etc.
        .build();

    for entry in walker {
        let entry = match entry {
            Ok(e) => e,
            Err(_) => continue, // Skip permission errors etc.
        };

        let path = entry.path();

        // Skip the root directory itself
        if path == dir {
            continue;
        }

        // Check for patina markers
        let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");

        if file_name == ".patina"
            || (file_name == "commands"
                && path
                    .parent()
                    .map(|p| p.ends_with(".claude"))
                    .unwrap_or(false))
        {
            // Found a marker - return the project directory (parent of .patina or .claude)
            if let Some(project_dir) = path.parent() {
                let project_dir = if file_name == "commands" {
                    // .claude/commands -> go up two levels
                    project_dir.parent().unwrap_or(project_dir)
                } else {
                    project_dir
                };
                // Skip if this is the root directory itself (re-init case)
                // Finding .patina in the current dir is expected for re-init
                if project_dir == dir {
                    continue;
                }
                return Ok(vec![project_dir.to_path_buf()]);
            }
        }
    }

    Ok(vec![])
}

/// Ensure critical entries exist in an existing .gitignore
fn ensure_gitignore_entries(gitignore_path: &Path) -> Result<()> {
    let content = fs::read_to_string(gitignore_path).context("Failed to read .gitignore")?;

    // Critical entries that should always be ignored
    let must_have = [
        ("/target/", "Rust build artifacts"),
        ("node_modules/", "Node.js dependencies"),
        (".env", "Environment secrets"),
        (
            ".patina/local/",
            "Patina local state (derived, not committed)",
        ),
        ("*.db", "Database files"),
        ("*.key", "Private keys"),
        ("*.pem", "Certificates"),
    ];

    let mut added = Vec::new();
    let mut updated_content = content.clone();

    for (pattern, _description) in must_have {
        // Check if pattern already exists (accounting for variations)
        let pattern_exists = content.lines().any(|line| {
            let line = line.trim();
            line == pattern || line == pattern.trim_end_matches('/')
        });

        if !pattern_exists {
            // Add a newline if file doesn't end with one
            if !updated_content.ends_with('\n') {
                updated_content.push('\n');
            }

            // Add the pattern with a comment if we're adding multiple
            if added.is_empty() {
                updated_content.push_str("\n# Added by Patina for safety\n");
            }
            updated_content.push_str(pattern);
            updated_content.push('\n');

            added.push(pattern);
        }
    }

    if !added.is_empty() {
        fs::write(gitignore_path, updated_content).context("Failed to update .gitignore")?;
        println!("✓ Added to .gitignore: {}", added.join(", "));
    }

    Ok(())
}