aprender-shell 0.34.0

AI-powered shell completion trained on your history
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

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::Train {
            history,
            output,
            ngram,
            memory_limit,
            password,
        } => {
            cmd_train(history, &output, ngram, memory_limit, password);
        }
        Commands::Update {
            history,
            model,
            quiet,
            password,
        } => {
            cmd_update(history, &model, quiet, password);
        }
        Commands::Suggest {
            prefix,
            model,
            count,
            memory_limit,
            password,
        } => {
            cmd_suggest(&prefix, &model, count, memory_limit, password);
        }
        Commands::Stats {
            model,
            memory_limit,
            password,
        } => {
            cmd_stats(&model, memory_limit, password);
        }
        Commands::Export { model, output } => {
            cmd_export(&model, &output);
        }
        Commands::Import { input, output } => {
            cmd_import(&input, &output);
        }
        Commands::ZshWidget => {
            cmd_zsh_widget();
        }
        Commands::BashWidget => {
            cmd_bash_widget();
        }
        Commands::FishWidget => {
            cmd_fish_widget();
        }
        Commands::Uninstall {
            zsh,
            bash,
            fish,
            keep_model,
            dry_run,
        } => {
            cmd_uninstall(zsh, bash, fish, keep_model, dry_run);
        }
        Commands::Validate {
            history,
            ngram,
            ratio,
        } => {
            cmd_validate(history, ngram, ratio);
        }
        Commands::Augment {
            history,
            output,
            ngram,
            augmentation_ratio,
            quality_threshold,
            monitor_diversity,
            use_code_eda,
        } => {
            cmd_augment(
                history,
                &output,
                ngram,
                augmentation_ratio,
                quality_threshold,
                monitor_diversity,
                use_code_eda,
            );
        }
        Commands::Analyze { history, top } => {
            cmd_analyze(history, top);
        }
        Commands::Tune {
            history,
            trials,
            ratio,
        } => {
            cmd_tune(history, trials, ratio);
        }
        Commands::Inspect {
            model,
            format,
            password,
        } => {
            cmd_inspect(&model, &format, password);
        }
        Commands::Publish {
            model,
            repo,
            commit,
            create,
            private,
        } => {
            cmd_publish(&model, &repo, &commit, create, private);
        }
        Commands::Stream {
            model,
            count,
            format,
            password,
        } => {
            cmd_stream(&model, count, &format, password);
        }
        Commands::Daemon {
            model,
            socket,
            count,
            password,
            foreground,
        } => {
            cmd_daemon(&model, &socket, count, password, foreground);
        }
        Commands::DaemonStop { socket } => {
            cmd_daemon_stop(&socket);
        }
        Commands::DaemonStatus { socket } => {
            cmd_daemon_status(&socket);
        }
    }
}

/// Helper: Find and validate history file with graceful error handling (QA 2.4, 8.3)
fn find_history_file_graceful(history_path: Option<PathBuf>) -> PathBuf {
    match history_path {
        Some(path) => {
            if !path.exists() {
                eprintln!("❌ History file not found: {}", path.display());
                eprintln!("   Hint: Check the path or use -f to specify a different file");
                std::process::exit(1);
            }
            path
        }
        None => match HistoryParser::find_history_file() {
            Some(path) => path,
            None => {
                eprintln!("❌ Could not find shell history file");
                eprintln!("   Hint: Use -f to specify a history file manually");
                std::process::exit(1);
            }
        },
    }
}

/// Helper: Parse history file with graceful error handling (QA 8.3)
fn parse_history_graceful(history_file: &PathBuf) -> Vec<String> {
    let parser = HistoryParser::new();
    match parser.parse_file(history_file) {
        Ok(cmds) => cmds,
        Err(e) => {
            eprintln!("❌ Failed to read history file: {e}");
            if e.to_string().contains("ermission") {
                eprintln!(
                    "   Hint: Check file permissions with 'ls -la {}'",
                    history_file.display()
                );
            }
            std::process::exit(1);
        }
    }
}

fn validate_ngram(n: usize) {
    if !(2..=5).contains(&n) {
        eprintln!("❌ Error: N-gram size must be between 2 and 5 (got {})", n);
        std::process::exit(1);
    }
}

fn cmd_train(
    history_path: Option<PathBuf>,
    output: &str,
    ngram: usize,
    memory_limit: Option<usize>,
    use_password: bool,
) {
    validate_ngram(ngram);
    print_train_header(use_password, memory_limit.is_some());

    let commands = load_history_commands(history_path);
    let password = get_train_password(use_password);
    let output_path = expand_path(output);

    if let Some(mem_mb) = memory_limit {
        train_paged_model(&commands, &output_path, ngram, mem_mb, use_password);
    } else {
        train_standard_model(&commands, &output_path, ngram, password.as_deref());
    }
}

fn print_train_header(use_password: bool, paged: bool) {
    let encrypted_str = if use_password { " encrypted" } else { "" };
    let mode_str = if paged { "paged" } else { "standard" };
    println!("🚀 aprender-shell: Training{encrypted_str} {mode_str} model...\n");
}

fn load_history_commands(history_path: Option<PathBuf>) -> Vec<String> {
    let history_file = find_history_file_graceful(history_path);
    println!("📂 History file: {}", history_file.display());

    let commands = parse_history_graceful(&history_file);
    println!("📊 Commands loaded: {}", commands.len());

    if commands.is_empty() {
        eprintln!("❌ No commands found in history file");
        std::process::exit(1);
    }
    commands
}

fn get_train_password(use_password: bool) -> Option<String> {
    if !use_password {
        return None;
    }
    println!("🔐 Encrypting model with AES-256-GCM");
    let pwd = prompt_password_or_exit("   Enter password: ");
    let confirm = prompt_password_or_exit("   Confirm password: ");
    if pwd != confirm {
        eprintln!("❌ Passwords do not match");
        std::process::exit(1);
    }
    if pwd.len() < 8 {
        eprintln!("❌ Password must be at least 8 characters");
        std::process::exit(1);
    }
    Some(pwd)
}

fn prompt_password_or_exit(prompt: &str) -> String {
    rpassword::prompt_password(prompt).unwrap_or_else(|e| {
        eprintln!("❌ Failed to read password: {e}");
        std::process::exit(1);
    })
}

fn train_paged_model(
    commands: &[String],
    output_path: &Path,
    ngram: usize,
    mem_mb: usize,
    use_password: bool,
) {
    if use_password {
        eprintln!("⚠️  Encryption not yet supported for paged models. Creating unencrypted model.");
    }
    let output_path = output_path.with_extension("apbundle");
    print!(
        "🧠 Training {ngram}-gram paged model ({}MB limit)... ",
        mem_mb
    );
    let mut model = PagedMarkovModel::new(ngram, mem_mb);
    model.train(commands);
    println!("done!");

    save_model_or_exit(|| model.save(&output_path), &output_path, "paged model");

    let stats = model.stats();
    println!("\n✅ Paged model saved to: {}", output_path.display());
    println!("\n📈 Model Statistics:");
    println!("   Segments:        {}", stats.total_segments);
    println!("   Vocabulary size: {}", stats.vocab_size);
    println!("   Memory limit:    {} MB", mem_mb);
    println!("\n💡 Next steps:");
    println!("   1. Test: aprender-shell suggest \"git \" --memory-limit {mem_mb}");
    println!("   2. Stats: aprender-shell stats --memory-limit {mem_mb}");
}

fn train_standard_model(
    commands: &[String],
    output_path: &Path,
    ngram: usize,
    password: Option<&str>,
) {
    print!("🧠 Training {ngram}-gram model... ");
    let mut model = MarkovModel::new(ngram);
    model.train(commands);
    println!("done!");

    if let Some(pwd) = password {
        save_model_or_exit(
            || model.save_encrypted(output_path, pwd),
            output_path,
            "encrypted model",
        );
        println!("\n🔒 Encrypted model saved to: {}", output_path.display());
    } else {
        save_model_or_exit(|| model.save(output_path), output_path, "model");
        println!("\n✅ Model saved to: {}", output_path.display());
    }

    print_standard_model_stats(&model, password.is_some());
}

fn save_model_or_exit<F, E: std::fmt::Display>(save_fn: F, path: &Path, model_type: &str)
where
    F: FnOnce() -> Result<(), E>,
{
    if let Err(e) = save_fn() {
        eprintln!("❌ Failed to save {model_type}: {e}");
        if e.to_string().contains("ermission") {
            eprintln!("   Hint: Check write permissions for '{}'", path.display());
        }
        std::process::exit(1);
    }
}

fn print_standard_model_stats(model: &MarkovModel, encrypted: bool) {
    println!("\n📈 Model Statistics:");
    println!("   Unique n-grams: {}", model.ngram_count());
    println!("   Vocabulary size: {}", model.vocab_size());
    println!(
        "   Model size: {:.1} KB",
        model.size_bytes() as f64 / 1024.0
    );
    if encrypted {
        println!("   Encryption: AES-256-GCM (Argon2id KDF)");
    }

    println!("\n💡 Next steps:");
    if encrypted {
        println!("   1. Test: aprender-shell suggest \"git \" --password");
        println!("   2. Stats: aprender-shell stats --password");
    } else {
        println!("   1. Test: aprender-shell suggest \"git \"");
        println!("   2. Install: aprender-shell zsh-widget >> ~/.zshrc");
    }
}

/// Load an existing model from disk, handling encryption and error diagnostics.
fn load_existing_model(path: &std::path::Path, password: Option<&str>) -> MarkovModel {
    if let Some(pwd) = password {
        return MarkovModel::load_encrypted(path, pwd).unwrap_or_else(|e| {
            eprintln!("❌ Failed to load encrypted model: {e}");
            eprintln!("   Hint: Check password or try without --password flag");
            std::process::exit(1);
        });
    }
    match MarkovModel::load(path) {
        Ok(m) => m,
        Err(e) => {
            eprintln!("❌ Failed to load model '{}': {e}", path.display());
            print_model_load_hint(&e);
            std::process::exit(1);
        }
    }
}

/// Print a diagnostic hint based on the model load error.
fn print_model_load_hint(e: &std::io::Error) {
    let msg = e.to_string();
    if msg.contains("Checksum mismatch") {
        eprintln!("   Hint: The model file may be corrupted. Run 'aprender-shell train' to rebuild.");
    } else if msg.contains("magic") || msg.contains("invalid") {
        eprintln!("   Hint: The file may not be a valid aprender model.");
    }
}

/// Save a model to disk, preserving encryption status.
fn save_model_to_disk(model: &MarkovModel, path: &std::path::Path, password: Option<&str>) {
    if let Some(pwd) = password {
        if let Err(e) = model.save_encrypted(path, pwd) {
            eprintln!("❌ Failed to save encrypted model: {e}");
            std::process::exit(1);
        }
    } else if let Err(e) = model.save(path) {
        eprintln!("❌ Failed to save model: {e}");
        std::process::exit(1);
    }
}

fn cmd_update(history_path: Option<PathBuf>, model_path: &str, quiet: bool, use_password: bool) {
    let path = expand_path(model_path);

    let password = if use_password {
        Some(
            rpassword::prompt_password("Enter password: ").unwrap_or_else(|e| {
                eprintln!("❌ Failed to read password: {e}");
                std::process::exit(1);
            }),
        )
    } else {
        None
    };

    let mut model = if path.exists() {
        load_existing_model(&path, password.as_deref())
    } else {
        if !quiet {
            println!("📝 No existing model, creating new one...");
        }
        MarkovModel::new(3)
    };

    let history_file = find_history_file_graceful(history_path);
    let all_commands = parse_history_graceful(&history_file);

    let last_pos = model.last_trained_position();
    let new_commands: Vec<String> = all_commands.into_iter().skip(last_pos).collect();

    if new_commands.is_empty() {
        if !quiet {
            println!("✓ Model is up to date (no new commands)");
        }
        return;
    }

    if !quiet {
        println!("📊 Found {} new commands", new_commands.len());
    }

    model.train_incremental(&new_commands);
    save_model_to_disk(&model, &path, password.as_deref());

    if !quiet {
        println!(
            "✅ Model updated ({} total commands)",
            model.total_commands()
        );
    }
}

/// Get password from environment or prompt.
fn get_password_or_prompt(use_password: bool, error_prefix: &str) -> Option<String> {
    if !use_password {
        return None;
    }
    std::env::var("APRENDER_PASSWORD").ok().or_else(|| {
        Some(
            rpassword::prompt_password("Enter password: ").unwrap_or_else(|e| {
                eprintln!("{error_prefix}Failed to read password: {e}");
                std::process::exit(1);
            }),
        )
    })
}