claudette 0.8.3

Local-first AI personal secretary for Ollama. Telegram bot, voice, persistent scheduler, Gmail and Calendar. Single-binary Rust.
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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
//! Code-generation group — 2 tools (generate_code, spawn_agent) plus
//! the reference-file extraction infrastructure that generate_code
//! depends on.
//!
//! **Reference-file extraction** (Sprint 13 brownfield fix): when the
//! user asks generate_code to write tests/code that references an
//! existing file, we extract every path-like token from three sources
//! (priority order):
//!   1. Per-turn stash — paths the entry points pre-extracted from the
//!      raw user prompt (most reliable: bypasses the brain entirely).
//!   2. `reference_files` — the explicit schema param.
//!   3. `description` — fallback scan of the free-form description.
//!
//! The collector is conservative: it surfaces only files that (a)
//! syntactically look like a path, and (b) actually exist on disk
//! under the read policy. Size caps keep the coder prompt under ~70 KB.
//!
//! `set_current_turn_paths` and `extract_user_prompt_paths` stay `pub`
//! and are re-exported from the `tools` module — REPL / single-shot /
//! Telegram / TUI entry points call them before each turn.

use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};

use serde_json::{json, Value};

use super::{ensure_dir, file_url_for, files_dir, validate_read_path, validate_write_path};

/// File extensions we'll include as reference context.
const REF_EXTENSIONS: &[&str] = &[
    "py", "rs", "js", "mjs", "cjs", "jsx", "ts", "tsx", "html", "htm", "css", "json", "toml",
    "yaml", "yml", "md", "txt", "sh", "bash", "go", "java", "c", "cpp", "cc", "cxx", "h", "hpp",
    "rb", "php", "sql", "xml", "ini", "cfg", "conf",
];

/// Max files, per-file byte cap, and total byte cap. Keeps the coder prompt
/// below ~70 KB even when the user references several modules.
const REF_MAX_FILES: usize = 4;
const REF_MAX_BYTES_PER_FILE: usize = 16 * 1024;
const REF_MAX_BYTES_TOTAL: usize = 64 * 1024;

// ────────────────────────────────────────────────────────────────────────────
// Per-turn user-prompt path stash (Sprint 13.2 — bypass-the-brain brownfield)
//
// The brain summarises the user prompt before constructing tool calls and
// regularly drops file paths. Even with the explicit `reference_files` schema
// param, the 4b brain rarely populates it. Solution: extract paths from the
// raw user prompt at the entry point (REPL / single-shot / Telegram / TUI),
// stash them here, and merge in `collect_reference_files`. Bypasses the brain
// entirely. Each entry point overwrites the stash before submitting the turn.
// ────────────────────────────────────────────────────────────────────────────

static CURRENT_TURN_PATHS: OnceLock<Mutex<Vec<String>>> = OnceLock::new();

fn current_turn_paths_mu() -> &'static Mutex<Vec<String>> {
    CURRENT_TURN_PATHS.get_or_init(|| Mutex::new(Vec::new()))
}

/// Replace the per-turn path list. Called from each entry point with the paths
/// extracted from the raw user prompt. An empty Vec clears the stash, which is
/// the right thing to do for non-brownfield prompts (no leakage between turns).
pub fn set_current_turn_paths(paths: Vec<String>) {
    if let Ok(mut g) = current_turn_paths_mu().lock() {
        *g = paths;
    }
}

/// Read the current stash. Returns an empty Vec if poisoned (defensive — we'd
/// rather degrade to "no refs" than panic the agent loop).
pub(crate) fn current_turn_paths() -> Vec<String> {
    current_turn_paths_mu()
        .lock()
        .map(|g| g.clone())
        .unwrap_or_default()
}

/// Scan the raw user prompt for path tokens and keep only those that resolve
/// to an existing file under the read policy. Used by entry points to populate
/// the per-turn stash.
#[must_use]
pub fn extract_user_prompt_paths(prompt: &str) -> Vec<String> {
    extract_path_candidates(prompt)
        .into_iter()
        .filter(|t| resolve_reference(t).is_some())
        .collect()
}

/// Collect reference files for the coder prompt. Three sources, in priority order:
///   1. **Per-turn stash** — paths the system pre-extracted from the raw user
///      prompt (Sprint 13.2). Most reliable: bypasses the brain entirely.
///   2. `explicit` — paths the brain passed via the `reference_files` tool param.
///      Useful when the brain follows the schema instruction.
///   3. `description` — fallback path-scan for when the brain forgets BOTH the
///      param AND the path didn't make it into the user message verbatim.
///
/// All three go through the same `validate_read_path` policy and size caps,
/// and dedup by absolute path so a path hit on multiple sources only loads once.
pub(crate) fn collect_reference_files(
    explicit: &[&str],
    description: &str,
) -> Vec<crate::codet::ReferenceFile> {
    let mut out: Vec<crate::codet::ReferenceFile> = Vec::new();
    let mut seen_abs: std::collections::HashSet<PathBuf> = std::collections::HashSet::new();
    let mut total_bytes: usize = 0;

    let stash_iter = current_turn_paths().into_iter();
    let explicit_iter = explicit.iter().map(|s| (*s).to_string());
    let scanner_iter = extract_path_candidates(description).into_iter();
    for token in stash_iter.chain(explicit_iter).chain(scanner_iter) {
        if out.len() >= REF_MAX_FILES {
            break;
        }
        let Some(resolved) = resolve_reference(&token) else {
            continue;
        };
        if !seen_abs.insert(resolved.clone()) {
            continue;
        }
        let Ok(content) = fs::read_to_string(&resolved) else {
            continue;
        };
        let trimmed = truncate_content(content);
        if total_bytes.saturating_add(trimmed.len()) > REF_MAX_BYTES_TOTAL {
            break;
        }
        total_bytes += trimmed.len();
        out.push(crate::codet::ReferenceFile {
            path: token,
            content: trimmed,
        });
    }
    out
}

fn truncate_content(mut content: String) -> String {
    if content.len() > REF_MAX_BYTES_PER_FILE {
        // Truncate at a char boundary, then annotate.
        let mut cut = REF_MAX_BYTES_PER_FILE;
        while cut > 0 && !content.is_char_boundary(cut) {
            cut -= 1;
        }
        content.truncate(cut);
        content.push_str("\n... [truncated — file continues]\n");
    }
    content
}

/// Break a free-form description into path-shaped candidate tokens, stripping
/// surrounding quotes/brackets/trailing punctuation. Does NOT check the
/// filesystem — `resolve_reference` does that.
fn extract_path_candidates(text: &str) -> Vec<String> {
    let mut raw: Vec<String> = Vec::new();
    let mut buf = String::new();
    for c in text.chars() {
        if c.is_whitespace()
            || matches!(
                c,
                ',' | ';' | '(' | ')' | '[' | ']' | '{' | '}' | '"' | '\'' | '`' | '<' | '>'
            )
        {
            if !buf.is_empty() {
                raw.push(std::mem::take(&mut buf));
            }
        } else {
            buf.push(c);
        }
    }
    if !buf.is_empty() {
        raw.push(buf);
    }

    raw.into_iter()
        .filter_map(|t| {
            // Strip trailing sentence punctuation (em-dash, en-dash, etc).
            let trimmed = t
                .trim_end_matches(|c: char| {
                    matches!(c, '.' | ',' | ';' | ':' | '!' | '?' | '' | '' | ')')
                })
                .to_string();
            if trimmed.is_empty() {
                return None;
            }
            // URLs look like paths-with-extensions but aren't reachable via
            // the filesystem — drop them before they trip resolve_reference.
            if trimmed.contains("://") {
                return None;
            }
            if looks_like_path(&trimmed) || has_code_extension(&trimmed) {
                Some(trimmed)
            } else {
                None
            }
        })
        .collect()
}

/// `true` iff the token uses explicit path syntax (tilde, absolute, dotted
/// relative, or a Windows drive letter). URLs are excluded.
fn looks_like_path(s: &str) -> bool {
    if s.contains("://") {
        return false;
    }
    if s.starts_with("~/") || s.starts_with("~\\") {
        return true;
    }
    if s.starts_with("./") || s.starts_with(".\\") || s.starts_with("../") || s.starts_with("..\\")
    {
        return true;
    }
    if s.starts_with('/') || s.starts_with('\\') {
        return true;
    }
    let bytes = s.as_bytes();
    bytes.len() >= 3
        && bytes[0].is_ascii_alphabetic()
        && bytes[1] == b':'
        && (bytes[2] == b'\\' || bytes[2] == b'/')
}

fn has_code_extension(s: &str) -> bool {
    Path::new(s)
        .extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| {
            let lower = e.to_ascii_lowercase();
            REF_EXTENSIONS.contains(&lower.as_str())
        })
}

/// Resolve a token to an absolute path on disk, or `None` if no readable
/// file exists under $HOME, the scratch dir, or the current working dir.
fn resolve_reference(token: &str) -> Option<PathBuf> {
    // Explicit path: use the same read-policy as read_file.
    if looks_like_path(token) {
        return validate_read_path(token).ok().filter(|p| p.is_file());
    }
    // Bare filename with a code extension: try scratch dir then cwd.
    if !has_code_extension(token) {
        return None;
    }
    for dir in [
        files_dir(),
        std::env::current_dir().unwrap_or_else(|_| files_dir()),
    ] {
        let candidate = dir.join(token);
        if candidate.is_file() {
            let as_string = candidate.to_string_lossy().to_string();
            if let Ok(validated) = validate_read_path(&as_string) {
                return Some(validated);
            }
        }
    }
    None
}

pub(super) fn schemas() -> Vec<Value> {
    vec![
        json!({
            "type": "function",
            "function": {
                "name": "generate_code",
                "description": "Write code to a file via the specialised coder model (auto-validates syntax + tests). Use instead of write_file for any code file. Reply with path + one sentence — never paste the generated code. For brownfield edits, pass the existing file in reference_files so the coder reads the real API.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "description":     { "type": "string", "description": "What code to write" },
                        "filename":        { "type": "string", "description": "Filename; extension sets the language (calc.py, lib.rs, app.ts)" },
                        "reference_files": { "type": "array", "items": { "type": "string" }, "description": "Paths the coder must read first (up to 4). Required for brownfield." }
                    },
                    "required": ["description", "filename"]
                }
            }
        }),
        json!({
            "type": "function",
            "function": {
                "name": "spawn_agent",
                "description": "Delegate a task to a specialized agent. 'researcher' for web/file/code research, 'gitops' for git workflows, 'reviewer' for code review.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "agent_type": { "type": "string", "enum": ["researcher", "gitops", "reviewer"], "description": "Agent type" },
                        "task":       { "type": "string", "description": "Task description for the agent" },
                        "auto":       { "type": "boolean", "description": "Skip confirmation prompts for dangerous tools (default false)" }
                    },
                    "required": ["agent_type", "task"]
                }
            }
        }),
    ]
}

pub(super) fn dispatch(name: &str, input: &str) -> Option<Result<String, String>> {
    let result = match name {
        "generate_code" => run_generate_code(input),
        "spawn_agent" => run_spawn_agent(input),
        _ => return None,
    };
    Some(result)
}

fn run_generate_code(input: &str) -> Result<String, String> {
    let v: Value = serde_json::from_str(input)
        .map_err(|e| format!("generate_code: invalid JSON ({e}): {input}"))?;
    let description = v
        .get("description")
        .and_then(Value::as_str)
        .ok_or("generate_code: missing 'description'")?;
    let filename = v
        .get("filename")
        .and_then(Value::as_str)
        .ok_or("generate_code: missing 'filename'")?;

    // Infer language from extension.
    let ext = Path::new(filename)
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("text");
    let language = match ext {
        "py" => "Python",
        "rs" => "Rust",
        "js" => "JavaScript",
        "ts" => "TypeScript",
        "php" => "PHP",
        "rb" => "Ruby",
        "go" => "Go",
        "java" => "Java",
        "c" | "h" => "C",
        "cpp" | "hpp" => "C++",
        "sh" | "bash" => "Bash",
        other => other,
    };

    // Collect reference files for the coder. Two signals:
    //   - `reference_files`: explicit array the brain passed (deterministic).
    //   - `description`: free-form scan for path tokens the brain mentioned in prose.
    // The explicit param is the contract; the scanner stays as a fallback so
    // brains that forget the param still get partial coverage.
    // Brownfield fix v2 (Sprint 13.1, 2026-04-18) — see project_sprint13_brownfield.
    let explicit_refs: Vec<&str> = v
        .get("reference_files")
        .and_then(Value::as_array)
        .map(|arr| arr.iter().filter_map(Value::as_str).collect())
        .unwrap_or_default();
    let references = collect_reference_files(&explicit_refs, description);

    // Generate code via the coder model.
    let code = crate::codet::generate_code(description, language, &references)
        .ok_or("generate_code: coder model returned no usable output")?;

    // Write via the same sandbox logic as write_file. Bare relative paths
    // resolve against, in priority order: the active mission tree → the
    // user's explicit workspace CWD (daily-driver: "create helpers.py here"
    // means the project) → the scratch sandbox (~/.claudette/files/).
    let resolved_input = if Path::new(filename).is_absolute()
        || filename.starts_with("~/")
        || filename.starts_with("~\\")
    {
        filename.to_string()
    } else {
        let base = if crate::missions::active_mission().is_some() {
            crate::missions::active_cwd()
        } else if let Some(ws_cwd) = super::workspace_cwd() {
            ws_cwd
        } else {
            files_dir()
        };
        base.join(filename).display().to_string()
    };
    let path = validate_write_path(&resolved_input)?;

    if let Some(parent) = path.parent() {
        ensure_dir(parent)?;
    }
    fs::write(&path, &code)
        .map_err(|e| format!("generate_code: write {} failed: {e}", path.display()))?;

    let mut result = json!({
        "ok": true,
        "path": path.display().to_string(),
        "file_url": file_url_for(&path),
        "bytes": code.len(),
        "language": language,
        "generated_by": crate::codet::coder_model(),
        // Strong hint for the model: the file is on disk, do not paste
        "reply_hint": "File written. Reply with: file path + 1-sentence \
                       summary. DO NOT include the code in your response. \
                       If the user wants it opened in a browser, pass `path` \
                       (or `file_url`) verbatim to open_url — do not \
                       reconstruct file:// URLs by hand.",
    });

    // Run Codet validation (same as write_file post-write hook). Pass the
    // references so the fix-loop also sees the real API when repairing tests.
    if let Some(validation) = crate::codet::validate_code_file(&path, &references) {
        result["validation"] = validation.to_json();

        if let crate::codet::CodetStatus::CouldNotFix { ref last_error } = validation.status {
            let short_err: String = last_error.lines().take(3).collect::<Vec<_>>().join(" | ");
            eprintln!(
                "{} {}",
                crate::theme::warn(crate::theme::WARN_GLYPH),
                crate::theme::warn(&format!(
                    "codet: {} failed validation after {} attempt(s), {} landed — {}",
                    path.display(),
                    validation.attempts_made,
                    validation.fixes_applied,
                    short_err,
                ))
            );
        }
    }

    Ok(result.to_string())
}

fn run_spawn_agent(input: &str) -> Result<String, String> {
    let v: Value = serde_json::from_str(input)
        .map_err(|e| format!("spawn_agent: invalid JSON ({e}): {input}"))?;
    let type_str = v
        .get("agent_type")
        .and_then(Value::as_str)
        .ok_or("spawn_agent: missing 'agent_type'")?;
    let agent_type = crate::agents::AgentType::parse(type_str).ok_or_else(|| {
        format!("spawn_agent: unknown agent type '{type_str}'. Use 'researcher' or 'gitops'.")
    })?;
    let task = v
        .get("task")
        .and_then(Value::as_str)
        .ok_or("spawn_agent: missing 'task'")?;
    let auto_mode = v.get("auto").and_then(Value::as_bool).unwrap_or(false);

    crate::agents::spawn_agent(agent_type, task, auto_mode)
}

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

    /// Serializer for any test that reads or writes `CURRENT_TURN_PATHS`.
    /// Cargo runs tests in parallel; without this guard, a stash-setting test
    /// can leak state into a stash-reading test running concurrently.
    static STASH_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    fn lock_stash() -> std::sync::MutexGuard<'static, ()> {
        // Recover from poisoning — a panic in one test must not block the rest.
        STASH_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    #[test]
    fn looks_like_path_recognises_common_shapes() {
        assert!(looks_like_path("~/foo/bar.py"));
        assert!(looks_like_path("~\\foo\\bar.py"));
        assert!(looks_like_path("./foo"));
        assert!(looks_like_path("../foo"));
        assert!(looks_like_path("/abs/path"));
        assert!(looks_like_path("C:\\Users\\me\\x.py"));
        assert!(looks_like_path("D:/dev/claudette/x.py"));
        assert!(!looks_like_path("plainword"));
        assert!(!looks_like_path("file.py")); // bare filename — not a path per se
        assert!(!looks_like_path("https://example.com/x.py"));
        assert!(!looks_like_path("http://example.com/x.py"));
    }

    #[test]
    fn has_code_extension_recognises_code_files() {
        assert!(has_code_extension("calculator.py"));
        assert!(has_code_extension("lib.RS")); // case-insensitive
        assert!(has_code_extension("path/to/file.ts"));
        assert!(!has_code_extension("no-extension"));
        assert!(!has_code_extension("readme"));
        // Extensions we don't include shouldn't leak in.
        assert!(!has_code_extension("archive.zip"));
    }

    #[test]
    fn extract_path_candidates_strips_punctuation_and_brackets() {
        let text = "Read the file ~/.claudette/files/calculator.py — it's a module.";
        let cands = extract_path_candidates(text);
        assert!(
            cands
                .iter()
                .any(|t| t == "~/.claudette/files/calculator.py"),
            "missing tilde path, got: {cands:?}",
        );
    }

    #[test]
    fn extract_path_candidates_keeps_bare_code_filename() {
        let cands = extract_path_candidates("Please read calculator.py carefully.");
        assert!(
            cands.iter().any(|t| t == "calculator.py"),
            "missing bare filename, got: {cands:?}",
        );
    }

    #[test]
    fn extract_path_candidates_ignores_urls_and_prose() {
        let cands =
            extract_path_candidates("Visit https://example.com/x.py then write a greeting.");
        // No URL, no plain prose words.
        assert!(
            !cands.iter().any(|t| t.contains("example.com")),
            "leaked URL: {cands:?}",
        );
        assert!(
            !cands.iter().any(|t| t == "greeting"),
            "kept prose word: {cands:?}",
        );
    }

    #[test]
    fn collect_reference_files_reads_tilde_path() {
        let _g = lock_stash();
        set_current_turn_paths(vec![]); // start clean
                                        // Write a fixture under the user's home so validate_read_path accepts it.
        let dir = user_home().join(".claudette").join("files");
        fs::create_dir_all(&dir).unwrap();
        let fixture = dir.join("refsprint_fixture.py");
        let body = "class RefFixture:\n    def hello(self):\n        return 'hi'\n";
        fs::write(&fixture, body).unwrap();

        let desc =
            "Read the file ~/.claudette/files/refsprint_fixture.py and write tests for its API."
                .to_string();
        let refs = collect_reference_files(&[], &desc);

        // Cleanup before asserting so we don't leak fixtures on failure.
        let _ = fs::remove_file(&fixture);

        assert_eq!(refs.len(), 1, "expected 1 reference, got {}", refs.len());
        assert!(
            refs[0].content.contains("class RefFixture"),
            "content missing, got: {:?}",
            refs[0].content
        );
        assert_eq!(refs[0].path, "~/.claudette/files/refsprint_fixture.py");
    }

    #[test]
    fn collect_reference_files_ignores_missing_and_non_code() {
        let _g = lock_stash();
        set_current_turn_paths(vec![]);
        // A description with a URL, a word, and a nonexistent filename.
        let desc = "Write a function. No file here. See http://example.com/foo.py and ghost.py.";
        let refs = collect_reference_files(&[], desc);
        assert!(
            refs.is_empty(),
            "expected no refs for missing files, got {refs:?}",
        );
    }

    #[test]
    fn collect_reference_files_caps_file_size() {
        let _g = lock_stash();
        set_current_turn_paths(vec![]);
        let dir = user_home().join(".claudette").join("files");
        fs::create_dir_all(&dir).unwrap();
        let fixture = dir.join("refsprint_big_fixture.py");
        // 20 KB of Python, over the 16 KB per-file cap.
        let body: String = "x = 1\n".repeat(20 * 1024 / 6 + 1);
        fs::write(&fixture, &body).unwrap();

        let desc = "See ~/.claudette/files/refsprint_big_fixture.py".to_string();
        let refs = collect_reference_files(&[], &desc);

        let _ = fs::remove_file(&fixture);

        assert_eq!(refs.len(), 1);
        assert!(
            refs[0].content.contains("[truncated — file continues]"),
            "missing truncation marker",
        );
        assert!(
            refs[0].content.len() <= 16 * 1024 + 100,
            "content not truncated: {} bytes",
            refs[0].content.len()
        );
    }

    // ─── Sprint 13.1 — explicit reference_files param ────────────────

    #[test]
    fn collect_reference_files_uses_explicit_param() {
        let _g = lock_stash();
        set_current_turn_paths(vec![]);
        let dir = user_home().join(".claudette").join("files");
        fs::create_dir_all(&dir).unwrap();
        let fixture = dir.join("refsprint_explicit_fixture.py");
        let body = "def explicit_marker():\n    return 'from explicit param'\n";
        fs::write(&fixture, body).unwrap();

        // Description has NO path tokens — only the explicit param does.
        let desc = "Write tests for the helper module.";
        let explicit = ["~/.claudette/files/refsprint_explicit_fixture.py"];
        let refs = collect_reference_files(&explicit, desc);

        let _ = fs::remove_file(&fixture);

        assert_eq!(refs.len(), 1, "expected 1 reference, got {}", refs.len());
        assert!(
            refs[0].content.contains("explicit_marker"),
            "content missing, got: {:?}",
            refs[0].content
        );
    }

    #[test]
    fn collect_reference_files_dedups_explicit_and_scanner() {
        let _g = lock_stash();
        set_current_turn_paths(vec![]);
        let dir = user_home().join(".claudette").join("files");
        fs::create_dir_all(&dir).unwrap();
        let fixture = dir.join("refsprint_dedup_fixture.py");
        fs::write(&fixture, "x = 1\n").unwrap();

        // Same path appears in BOTH the explicit param and the description text.
        let desc = "Read ~/.claudette/files/refsprint_dedup_fixture.py and tests.";
        let explicit = ["~/.claudette/files/refsprint_dedup_fixture.py"];
        let refs = collect_reference_files(&explicit, desc);

        let _ = fs::remove_file(&fixture);

        assert_eq!(refs.len(), 1, "duplicate not collapsed: {refs:?}");
    }

    #[test]
    fn collect_reference_files_silently_drops_invalid_explicit_paths() {
        let _g = lock_stash();
        set_current_turn_paths(vec![]);
        // Explicit paths that don't exist on disk are filtered out, not erroring.
        let explicit = ["/this/path/does/not/exist.py", "~/no_such_file.py"];
        let refs = collect_reference_files(&explicit, "irrelevant description");
        assert!(refs.is_empty(), "expected empty, got {refs:?}");
    }

    // ─── Sprint 13.2 — per-turn user-prompt path stash ───────────────

    #[test]
    fn extract_user_prompt_paths_keeps_existing_files_only() {
        let dir = user_home().join(".claudette").join("files");
        fs::create_dir_all(&dir).unwrap();
        let fixture = dir.join("refsprint_stash_real.py");
        fs::write(&fixture, "x = 1\n").unwrap();

        let prompt = "Add tests for ~/.claudette/files/refsprint_stash_real.py \
                      and also for ~/.claudette/files/refsprint_stash_ghost.py";
        let paths = extract_user_prompt_paths(prompt);
        let _ = fs::remove_file(&fixture);

        assert!(
            paths.iter().any(|p| p.contains("refsprint_stash_real.py")),
            "real path missing: {paths:?}"
        );
        assert!(
            !paths.iter().any(|p| p.contains("refsprint_stash_ghost.py")),
            "ghost path leaked: {paths:?}"
        );
    }

    #[test]
    fn collect_reference_files_honours_turn_stash() {
        let _g = lock_stash();
        let dir = user_home().join(".claudette").join("files");
        fs::create_dir_all(&dir).unwrap();
        let fixture = dir.join("refsprint_stash_fixture.py");
        let body = "def stash_marker():\n    return 'from turn stash'\n";
        fs::write(&fixture, body).unwrap();

        // Stash one path; pass empty explicit, irrelevant description.
        set_current_turn_paths(vec![
            "~/.claudette/files/refsprint_stash_fixture.py".to_string()
        ]);
        let refs = collect_reference_files(&[], "Write tests for the helper.");

        // Always clear the stash so other tests aren't affected.
        set_current_turn_paths(vec![]);
        let _ = fs::remove_file(&fixture);

        assert_eq!(refs.len(), 1, "stash not honoured: {refs:?}");
        assert!(
            refs[0].content.contains("stash_marker"),
            "wrong content: {:?}",
            refs[0].content
        );
    }

    #[test]
    fn set_current_turn_paths_overwrites_previous_stash() {
        let _g = lock_stash();
        set_current_turn_paths(vec!["a.py".to_string(), "b.py".to_string()]);
        assert_eq!(current_turn_paths().len(), 2);
        set_current_turn_paths(vec!["c.py".to_string()]);
        assert_eq!(current_turn_paths(), vec!["c.py".to_string()]);
        set_current_turn_paths(vec![]);
        assert!(current_turn_paths().is_empty());
    }

    #[test]
    fn collect_reference_files_explicit_respects_max_files() {
        let _g = lock_stash();
        set_current_turn_paths(vec![]);
        let dir = user_home().join(".claudette").join("files");
        fs::create_dir_all(&dir).unwrap();
        let mut fixtures = Vec::new();
        let mut explicit_paths = Vec::new();
        for i in 0..6 {
            let p = dir.join(format!("refsprint_cap_fixture_{i}.py"));
            fs::write(&p, format!("# fixture {i}\nx = {i}\n")).unwrap();
            fixtures.push(p);
            explicit_paths.push(format!("~/.claudette/files/refsprint_cap_fixture_{i}.py"));
        }
        let explicit_refs: Vec<&str> = explicit_paths.iter().map(String::as_str).collect();

        let refs = collect_reference_files(&explicit_refs, "");

        for f in &fixtures {
            let _ = fs::remove_file(f);
        }

        assert_eq!(
            refs.len(),
            REF_MAX_FILES,
            "expected cap, got {}",
            refs.len()
        );
    }

    #[test]
    fn schemas_lists_two_tools() {
        let schemas = schemas();
        assert_eq!(schemas.len(), 2);
        let names: Vec<&str> = schemas
            .iter()
            .filter_map(|v| v.pointer("/function/name").and_then(Value::as_str))
            .collect();
        assert_eq!(names, ["generate_code", "spawn_agent"]);
    }
}