rtk-context-engine 2.3.0

RTK — main entrypoint binary and command router CLI
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
use anyhow::Result;
use std::fs;
use std::path::Path;

const LAZY_DEV_CONTENT: &str = r#"---
description: Principi Senior Developer — YAGNI, codice minimo, modifiche mirate e diff corti per efficienza token.
alwaysApply: true
---

# Lazy Developer (YAGNI & Efficiency)

Adotta la mentalità del "miglior codice è quello che non è stato scritto" (YAGNI - You Aren't Gonna Need It) per massimizzare la chiarezza e ridurre il consumo di token.

## Regole Comportamentali

1. **Codice Minimo**: Scrivi solo le righe necessarie per implementare la feature o risolvere il bug. Evita di aggiungere astrazioni premature, classi helper o logica "per il futuro".
2. **Modifiche Mirate (Diff Corti)**: Quando modifichi file esistenti, mantieni il delta il più piccolo possibile.
   - Non riscrivere interi file o intere funzioni per modesti cambiamenti (es. ≤3 righe). Usa modifiche mirate.
   - Non formattare parti del file non correlate alla tua patch.
3. **Fiducia nella Standard Library**: Usa funzioni native del linguaggio e framework correnti. Non aggiungere nuove dipendenze o librerie esterne a meno che non sia esplicitamente richiesto o bloccante.
4. **Nessun Recap o Narrazione**: Procedi direttamente alle modifiche. Non riassumere i tuoi compiti né ripetere le istruzioni dell'utente.

## Ladder of Laziness
- Livello 1: Risolvi il problema con il minor numero di righe modificate.
- Livello 2: Usa le funzioni già esistenti nel codebase.
- Livello 3: Evita modifiche strutturali se una semplice correzione locale è sufficiente.
"#;

const TOKEN_EFFICIENCY_CONTENT: &str = r#"---
description: Token caps misurabili — BRIEF default, batch letture, no policy spam per efficienza token.
alwaysApply: true
---

# Token efficiency

## Modalità

- **BRIEF** — default operativo (patch, bugfix, routine).
- **STANDARD** — solo con **superfici multiple** o spiegazione necessaria (non audit formale).
- **AUDIT** — solo audit / security / architecture review **dichiarati**.

## Caps (operativi)

- Prima riga di ogni risposta: `Mode: BRIEF|STANDARD|AUDIT | Repo: <attivo>`.
- Max **3** file letti per batch; stop e rivaluta salvo **AUDIT** esplicito.
- Summary finale post-patch: **≤ 8 righe** (file toccati + comando test o checklist).
- **GRAPH_REPORT** / grafi: **vietato** wall of text; estratti brevi (max 10 righe).
- **Vietata** la duplicazione in chat di lunghi tratti di policy o regole.

## Skill

- **Minimizzare** stacking; una primaria + secondaria solo se a valore verificabile.
- **Non caricare** skill lunghe senza trigger chiaro.
- Planning (sparc, sequential, hermes): **no** per patch banali / one-liner.
- **codeburn**: per audit / perf / review pesante — non per ogni fix.
- **context-mode** (`skills/context-mode-routing.md`): raccomandato per molte letture.

## MCP

- I server MCP (come GitNexus / Graphify o altri indicizzatori) possono be **stale** rispetto allo stato corrente di HEAD.
- Non fare affidamento solo sulle risposte del grafo se una ricerca locale (`grep` / `git diff`) può confermare lo stato dei file.
"#;

const RTK_TOOLKIT_CONTENT: &str = r#"---
description: Guida all'uso di RTK CLI per ottimizzare il contesto e risparmiare token.
alwaysApply: true
---

# RTK CLI & Context Optimization

Questo repository utilizza **RTK CLI**, uno strumento per ottimizzare la verbosità dei comandi e archiviare i log nel database locale SQLite.

## Comandi Intercettati (Log Virtualizzati)
Quando esegui comandi standard come `git status`, `git diff`, `git log`, `cargo build`, `cargo test`, `pytest`, `ls` o `npm install`, l'output viene intercettato e filtrato per risparmiare token.
- Se l'output viene compresso, vedrai un messaggio alla fine: `[Full output cached. Access with: rtk show-log <id>]`.
- **IMPORTANTE**: Non rieseguire il comando per vedere i dettagli! Recupera il log completo usando:
  ```bash
  rtk show-log <id>
  ```

## Esplorazione Directory (`rtk pack`)
Non importare intere cartelle o leggere file multipli consecutivamente. Usa `rtk pack` per generare una rappresentazione XML compatta:
- Usa sempre `--strip` (o `-s`) per rimuovere commenti su riga singola e collassare righe vuote consecutive.
- Usa `--limit <max_tokens>` (o `-l`) per specificare un budget di token ed evitare overflow del contesto.
- Esempio:
  ```bash
  rtk pack . --strip --limit 30000
  ```

## Memoria Persistente (`rtk memory`) & Stale Context
Utilizza la memoria SQLite isolata per il progetto per salvare e recuperare informazioni importanti tra le sessioni:
- **Salva**: `rtk memory set <chiave> <valore>` (es: `rtk memory set db_port 5432`)
- **Leggi**: `rtk memory get <chiave>`
- **Elenca**: `rtk memory list` (esegui questo comando all'inizio di una nuova sessione di chat per sincronizzare il contesto!)
- **STALE CONTEXT RULE**: Se modifichi l'architettura o le dipendenze, il database FTS diventerà stantio. **DEVI** sovrascrivere in modo proattivo le vecchie regole o logiche tramite `rtk memory set <key>` per evitare allucinazioni in futuro.

## Hidden Chain of Thought (`rtk think`)
Quando risolvi bug complessi o scrivi algoritmi intricati, la "Chain of Thought" inquinerà permanentemente la Context Window.
- **NON** scrivere lunghi ragionamenti in chat.
- Invia il tuo ragionamento a `rtk think` usando una pipe:
  ```bash
  cat << 'EOF' | rtk think
  Il mio ragionamento su come risolvere questo bug...
  EOF
  ```
- Nella chat, stampa solo un breve riassunto (es: "Ho elaborato la logica tramite rtk think, applico la soluzione.").
"#;

const PONYTAIL_CONTENT: &str = include_str!("../assets/ponytail.mdc");
const CAVEMAN_SKILL: &str = include_str!("../assets/caveman/caveman-skill.md");
const CAVEMAN_COMMIT_SKILL: &str = include_str!("../assets/caveman/caveman-commit-skill.md");
const CAVEMAN_COMPRESS_SKILL: &str = include_str!("../assets/caveman/caveman-compress-skill.md");
const CAVEMAN_REVIEW_SKILL: &str = include_str!("../assets/caveman/caveman-review-skill.md");

/// Initialize the workspace by writing custom AI agent rules, custom skills, and setting up editor integration.
/// Supports Low, Medium, High, and Max savings profiles.
pub fn run_init(profile: &str) -> Result<()> {
    println!(
        "⚙️ Bootstrapping AI Efficiency rules in the current directory (Profile: {})...",
        profile.to_uppercase()
    );
    run_init_in(Path::new("."), profile)?;

    println!("✅ Created rules inside .cursor/rules/ and .agents/rules/");
    println!();

    // Automatically try to install the shell hook into Claude/Gemini settings.json
    let _ = auto_install_hook();

    // Automatically append aliases to .bashrc / .zshrc
    let aliases_installed = auto_install_aliases().unwrap_or(false);

    // Create user default config.json
    if rtk_db::config::create_default_config().is_ok() {
        if let Some(home) = std::env::var_os("USERPROFILE").or_else(|| std::env::var_os("HOME")) {
            let config_path = Path::new(&home).join(".config/rtk/config.json");
            println!(
                "🔒 Created personal guardrails configuration template in: {}",
                config_path.display()
            );
            println!();
        }
    }

    println!("==========================================================");
    println!("🎉 RTK AI Rules Bootstrapped Successfully!");
    println!("==========================================================");

    if !aliases_installed {
        println!("To complete your setup:");
        println!("1. Activate transparent terminal filtering in Claude Code by adding");
        println!("   the PreToolUse hook to your .claude/settings.json:");
        println!();
        println!("   \"hooks\": {{");
        println!("     \"PreToolUse\": [");
        println!("       {{");
        println!("         \"matcher\": \"Bash\",");
        println!("         \"hooks\": [");
        println!("           {{");
        println!("             \"type\": \"command\",");
        println!(
            "             \"command\": \"bash /path/to/rust-context-engine/hooks/rtk-rewrite.sh\","
        );
        println!("             \"timeout\": 5000");
        println!("           }}");
        println!("         ]]");
        println!("       }}");
        println!("     ]");
        println!("   }}");
        println!();
        println!("2. Add shell aliases to your ~/.bashrc or ~/.zshrc for CLI wrappers:");
        println!("   alias git=\"rtk git\"");
        println!("   alias cargo=\"rtk cargo\"");
        println!("   alias pytest=\"rtk pytest\"");
        println!("   alias ls=\"rtk ls\"");
        println!("   alias npm=\"rtk npm\"");
        println!("   alias yarn=\"rtk yarn\"");
        println!("   alias pnpm=\"rtk pnpm\"");
        println!("   alias dotnet=\"rtk dotnet\"");
        println!("==========================================================");
    }

    // Create ~/.rtk/bin wrappers
    let _ = create_path_wrappers();

    Ok(())
}

fn create_path_wrappers() -> Result<()> {
    let home = std::env::var("HOME")
        .or_else(|_| std::env::var("USERPROFILE"))
        .map_err(|_| anyhow::anyhow!("Could not find home directory"))?;

    let rtk_bin_dir = Path::new(&home).join(".rtk").join("bin");
    if !rtk_bin_dir.exists() {
        std::fs::create_dir_all(&rtk_bin_dir)?;
    }

    let commands = vec![
        "git",
        "cargo",
        "docker",
        "npm",
        "yarn",
        "pnpm",
        "pytest",
        "ls",
        "gradle",
        "go",
        "composer",
        "terraform",
        "dotnet",
    ];

    let script_template = r#"#!/bin/bash
# RTK transparent interceptor wrapper for COMMAND_NAME

IN_AGENT=0
if [ -n "$CLAUDE_CODE" ] || [ -n "$CURSOR" ] || [ -n "$AIDER" ] || [ -n "$ANTIGRAVITY" ] || [ -n "$GEMINI_AGENT" ] || [ -n "$AGENT_CONTEXT" ]; then
    IN_AGENT=1
fi

if [ "$IN_AGENT" -eq 1 ]; then
    exec rtk COMMAND_NAME "$@"
else
    CLEAN_PATH=$(echo "$PATH" | sed -E 's|[^:]*/.rtk/bin:?||g')
    REAL_BIN=$(PATH="$CLEAN_PATH" which COMMAND_NAME 2>/dev/null)
    if [ -n "$REAL_BIN" ]; then
        exec "$REAL_BIN" "$@"
    else
        exec COMMAND_NAME "$@"
    fi
fi
"#;

    for cmd in commands {
        let file_path = rtk_bin_dir.join(cmd);
        let content = script_template.replace("COMMAND_NAME", cmd);
        std::fs::write(&file_path, content)?;

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            if let Ok(meta) = std::fs::metadata(&file_path) {
                let mut perms = meta.permissions();
                perms.set_mode(0o755);
                let _ = std::fs::set_permissions(&file_path, perms);
            }
        }
    }

    println!(
        "✅ Created PATH interceptor wrappers inside: {}",
        rtk_bin_dir.display()
    );
    Ok(())
}

fn run_init_in(base: &Path, profile: &str) -> Result<()> {
    let cursor_rules_dir = base.join(".cursor").join("rules");
    let windsurf_rules_dir = base.join(".windsurf").join("rules");
    let agents_rules_dir = base.join(".agents").join("rules");
    let agents_skills_dir = base.join(".agents").join("skills");
    let github_dir = base.join(".github");

    // Create directories
    let dirs = [
        &cursor_rules_dir,
        &windsurf_rules_dir,
        &agents_rules_dir,
        &agents_skills_dir.join("caveman"),
        &agents_skills_dir.join("caveman-commit"),
        &agents_skills_dir.join("caveman-compress"),
        &agents_skills_dir.join("caveman-review"),
        &github_dir,
    ];
    for dir in &dirs {
        fs::create_dir_all(dir)?;
    }

    // Write input rules
    fs::write(cursor_rules_dir.join("lazy-dev.mdc"), LAZY_DEV_CONTENT)?;
    fs::write(
        cursor_rules_dir.join("token-efficiency.mdc"),
        TOKEN_EFFICIENCY_CONTENT,
    )?;
    fs::write(
        cursor_rules_dir.join("rtk-toolkit.mdc"),
        RTK_TOOLKIT_CONTENT,
    )?;

    fs::write(agents_rules_dir.join("lazy-dev.mdc"), LAZY_DEV_CONTENT)?;
    fs::write(
        agents_rules_dir.join("token-efficiency.mdc"),
        TOKEN_EFFICIENCY_CONTENT,
    )?;
    fs::write(
        agents_rules_dir.join("rtk-toolkit.mdc"),
        RTK_TOOLKIT_CONTENT,
    )?;

    // Write ponytail logic
    fs::write(cursor_rules_dir.join("ponytail.mdc"), PONYTAIL_CONTENT)?;
    fs::write(agents_rules_dir.join("ponytail.md"), PONYTAIL_CONTENT)?;

    // Write caveman skills
    fs::write(
        agents_skills_dir.join("caveman").join("SKILL.md"),
        CAVEMAN_SKILL,
    )?;
    fs::write(
        agents_skills_dir.join("caveman-commit").join("SKILL.md"),
        CAVEMAN_COMMIT_SKILL,
    )?;
    fs::write(
        agents_skills_dir.join("caveman-compress").join("SKILL.md"),
        CAVEMAN_COMPRESS_SKILL,
    )?;
    fs::write(
        agents_skills_dir.join("caveman-review").join("SKILL.md"),
        CAVEMAN_REVIEW_SKILL,
    )?;

    // Generate output profile rule
    let profile_content = match profile.to_lowercase().as_str() {
        "max" => {
            r#"---
description: RTK Output Autonomy Profile
alwaysApply: true
---
# RTK Output Profile: MAX

You are operating under the RTK MAX profile for maximum token efficiency.
1. Always apply the Ponytail philosophy (YAGNI, minimal code, deletion over addition).
2. You MUST auto-trigger the **caveman-ultra** skill for every response (no articles, heavy abbreviation).
3. Auto-trigger **caveman-commit** for all git commit operations.
4. Auto-trigger **caveman-review** for all code reviews.
5. Auto-trigger **caveman-compress** when writing persistent memories or documentation.
"#
        }
        "high" => {
            r#"---
description: RTK Output Autonomy Profile
alwaysApply: true
---
# RTK Output Profile: HIGH

You are operating under the RTK HIGH profile for strict token efficiency.
1. Always apply the Ponytail philosophy (YAGNI, minimal code, deletion over addition).
2. You MUST auto-trigger the **caveman-full** skill for every response (no articles, short phrasing).
3. Auto-trigger **caveman-commit** for all git commit operations.
"#
        }
        "medium" => {
            r#"---
description: RTK Output Autonomy Profile
alwaysApply: true
---
# RTK Output Profile: MEDIUM

You are operating under the RTK MEDIUM profile for balanced token efficiency.
1. Always apply the Ponytail philosophy (YAGNI, minimal code, deletion over addition).
2. You MUST auto-trigger the **caveman-lite** skill for every response (complete sentences but no filler/hedging).
"#
        }
        _ => {
            r#"---
description: RTK Output Autonomy Profile
alwaysApply: true
---
# RTK Output Profile: LOW

You are operating under the RTK LOW profile for safe efficiency.
1. Always apply the Ponytail philosophy (YAGNI, minimal code, deletion over addition).
2. Write concise, standard technical language without unnecessary conversational filler.
"#
        }
    };

    // Distribute the profile universally
    fs::write(cursor_rules_dir.join("rtk-profile.mdc"), profile_content)?;
    fs::write(windsurf_rules_dir.join("rtk-profile.md"), profile_content)?;
    fs::write(agents_rules_dir.join("AGENTS.md"), profile_content)?;
    fs::write(base.join("CLAUDE.md"), profile_content)?;

    // Append to copilot instructions
    let copilot_file = github_dir.join("copilot-instructions.md");
    let existing = fs::read_to_string(&copilot_file).unwrap_or_default();
    if !existing.contains("RTK Output Profile") {
        fs::write(
            &copilot_file,
            format!("{}\n\n{}", existing, profile_content),
        )?;
    }

    Ok(())
}

const ALIASES_BLOCK: &str = r#"
# RTK AI Token Saver Aliases
alias git="rtk git"
alias cargo="rtk cargo"
alias pytest="rtk pytest"
alias ls="rtk ls"
alias npm="rtk npm"
alias yarn="rtk yarn"
alias pnpm="rtk pnpm"
alias dotnet="rtk dotnet"
alias composer="rtk composer"
alias terraform="rtk terraform"
"#;

fn auto_install_aliases() -> Result<bool> {
    let home = std::env::var_os("USERPROFILE")
        .or_else(|| std::env::var_os("HOME"))
        .map(std::path::PathBuf::from);

    if let Some(h) = home {
        let mut installed = false;
        let shells = [".bashrc", ".zshrc", ".profile"];

        for shell in shells {
            let path = h.join(shell);
            if path.exists() {
                let content = fs::read_to_string(&path).unwrap_or_default();
                if !content.contains("RTK AI Token Saver Aliases") {
                    let mut file = fs::OpenOptions::new().append(true).open(&path)?;
                    use std::io::Write;
                    writeln!(file, "{}", ALIASES_BLOCK)?;
                    println!("🎉 Added RTK aliases to: {}", path.display());
                    installed = true;
                } else {
                    println!("ℹ️ RTK aliases already present in: {}", path.display());
                    installed = true;
                }
            }
        }
        return Ok(installed);
    }
    Ok(false)
}

fn auto_install_hook() -> Result<()> {
    let current_dir = std::env::current_dir()?;
    let hook_path = current_dir.join("hooks").join("rtk-rewrite.sh");
    if !hook_path.exists() {
        return Ok(()); // Not in the main repository, skip auto-installation
    }
    let hook_path_str = hook_path
        .canonicalize()?
        .to_string_lossy()
        .replace('\\', "/");

    let home = std::env::var_os("USERPROFILE")
        .or_else(|| std::env::var_os("HOME"))
        .map(std::path::PathBuf::from);

    if let Some(h) = home {
        let dirs = vec![h.join(".gemini").join("antigravity"), h.join(".claude")];
        for dir in dirs {
            if dir.exists() {
                let path = dir.join("settings.json");
                let mut json = if path.exists() {
                    let content = fs::read_to_string(&path).unwrap_or_default();
                    serde_json::from_str::<serde_json::Value>(&content)
                        .unwrap_or_else(|_| serde_json::Value::Object(serde_json::Map::new()))
                } else {
                    serde_json::Value::Object(serde_json::Map::new())
                };

                inject_hook_value(&mut json, &hook_path_str);

                if let Ok(pretty) = serde_json::to_string_pretty(&json) {
                    if fs::write(&path, pretty).is_ok() {
                        println!(
                            "🎉 Automatically configured Claude/Gemini settings hook in: {}",
                            path.display()
                        );
                    }
                }
            }
        }
    }
    Ok(())
}

fn inject_hook_value(json: &mut serde_json::Value, hook_path: &str) {
    if !json.is_object() {
        *json = serde_json::Value::Object(serde_json::Map::new());
    }
    let obj = json.as_object_mut().unwrap();
    if !obj.contains_key("hooks") || !obj["hooks"].is_object() {
        obj.insert(
            "hooks".to_string(),
            serde_json::Value::Object(serde_json::Map::new()),
        );
    }
    let hooks_obj = obj.get_mut("hooks").unwrap().as_object_mut().unwrap();

    if !hooks_obj.contains_key("PreToolUse") || !hooks_obj["PreToolUse"].is_array() {
        hooks_obj.insert(
            "PreToolUse".to_string(),
            serde_json::Value::Array(Vec::new()),
        );
    }
    let pre_tool_array = hooks_obj
        .get_mut("PreToolUse")
        .unwrap()
        .as_array_mut()
        .unwrap();

    let mut bash_entry_idx = None;
    for (idx, val) in pre_tool_array.iter().enumerate() {
        if let Some(val_obj) = val.as_object() {
            if val_obj.get("matcher").and_then(|m| m.as_str()) == Some("Bash") {
                bash_entry_idx = Some(idx);
                break;
            }
        }
    }

    let hook_entry = serde_json::json!({
        "type": "command",
        "command": format!("bash {}", hook_path),
        "timeout": 5000
    });

    if let Some(idx) = bash_entry_idx {
        let bash_obj = pre_tool_array[idx].as_object_mut().unwrap();
        if !bash_obj.contains_key("hooks") || !bash_obj["hooks"].is_array() {
            bash_obj.insert("hooks".to_string(), serde_json::Value::Array(Vec::new()));
        }
        let inner_hooks = bash_obj.get_mut("hooks").unwrap().as_array_mut().unwrap();

        inner_hooks.retain(|h| {
            h.get("command")
                .and_then(|c| c.as_str())
                .map(|s| !s.contains("rtk-rewrite.sh"))
                .unwrap_or(true)
        });

        inner_hooks.push(hook_entry);
    } else {
        let new_bash_entry = serde_json::json!({
            "matcher": "Bash",
            "hooks": [hook_entry]
        });
        pre_tool_array.push(new_bash_entry);
    }
}

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

    #[test]
    fn test_run_init_in() {
        let temp_dir = std::env::temp_dir().join(format!("rtk_init_test_{}", rand_suffix()));
        fs::create_dir_all(&temp_dir).unwrap();

        run_init_in(&temp_dir, "high").unwrap();

        assert!(temp_dir.join(".cursor/rules/lazy-dev.mdc").exists());
        assert!(temp_dir.join(".cursor/rules/token-efficiency.mdc").exists());
        assert!(temp_dir.join(".cursor/rules/rtk-toolkit.mdc").exists());

        assert!(temp_dir.join(".agents/rules/lazy-dev.mdc").exists());
        assert!(temp_dir.join(".agents/rules/token-efficiency.mdc").exists());
        assert!(temp_dir.join(".agents/rules/rtk-toolkit.mdc").exists());

        let lazy_dev = fs::read_to_string(temp_dir.join(".cursor/rules/lazy-dev.mdc")).unwrap();
        assert_eq!(lazy_dev, LAZY_DEV_CONTENT);

        let rtk_rules = fs::read_to_string(temp_dir.join(".agents/rules/rtk-toolkit.mdc")).unwrap();
        assert_eq!(rtk_rules, RTK_TOOLKIT_CONTENT);

        fs::remove_dir_all(temp_dir).unwrap();
    }

    #[test]
    fn test_inject_hook_value() {
        let mut json = serde_json::json!({
            "existing_setting": true,
            "hooks": {
                "PreToolUse": [
                    {
                        "matcher": "Bash",
                        "hooks": [
                            {
                                "type": "command",
                                "command": "echo old",
                                "timeout": 1000
                            }
                        ]
                    }
                ]
            }
        });

        inject_hook_value(&mut json, "/path/to/rtk-rewrite.sh");

        let pre_tool = json["hooks"]["PreToolUse"].as_array().unwrap();
        assert_eq!(pre_tool.len(), 1);
        let bash_entry = &pre_tool[0];
        assert_eq!(bash_entry["matcher"], "Bash");
        let inner_hooks = bash_entry["hooks"].as_array().unwrap();
        assert_eq!(inner_hooks.len(), 2);
        assert_eq!(inner_hooks[1]["command"], "bash /path/to/rtk-rewrite.sh");
        assert_eq!(json["existing_setting"], true);
    }

    #[test]
    fn test_create_default_config() {
        let temp_dir = std::env::temp_dir().join(format!("rtk_config_test_{}", rand_suffix()));
        fs::create_dir_all(&temp_dir).unwrap();

        // Temporarily override HOME and USERPROFILE env vars
        let original_home = std::env::var_os("HOME");
        let original_userprofile = std::env::var_os("USERPROFILE");
        std::env::set_var("HOME", &temp_dir);
        std::env::set_var("USERPROFILE", &temp_dir);

        // Call our creation helper
        let res = rtk_db::config::create_default_config();
        assert!(res.is_ok());

        // Verify that the file was created in <temp_dir>/.config/rtk/config.json
        let expected_path = temp_dir.join(".config/rtk/config.json");
        assert!(expected_path.exists());

        let content = fs::read_to_string(&expected_path).unwrap();
        assert!(content.contains("denied_commands"));
        assert!(content.contains("custom_patterns"));

        // Restore env vars
        if let Some(h) = original_home {
            std::env::set_var("HOME", h);
        } else {
            std::env::remove_var("HOME");
        }
        if let Some(up) = original_userprofile {
            std::env::set_var("USERPROFILE", up);
        } else {
            std::env::remove_var("USERPROFILE");
        }

        fs::remove_dir_all(temp_dir).unwrap();
    }

    fn rand_suffix() -> u32 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .subsec_nanos()
    }
}