parecode 0.1.1

A terminal coding agent built for token efficiency and local model reliability
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
use anyhow::Result;
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::mpsc;

use crate::budget::{Budget, LoopDetector};
use crate::cache::FileCache;
use crate::client::{Client, ContentPart, Message, MessageContent, Tool, ToolCall};
use crate::history::History;
use crate::hooks::{self, HookConfig};
use crate::mcp::McpClient;
use crate::tools;
use crate::tui::UiEvent;

const MAX_TOOL_CALLS: usize = 40;

const SYSTEM_PROMPT_BASE: &str = r#"You are PareCode, a focused coding assistant. You help with software engineering tasks by using the available tools.

Guidelines:
- Be direct and efficient — use the minimum tool calls needed
- Read files before editing them
- NEVER use write_file on a file that already exists — always use edit_file to modify existing files
- write_file is ONLY for creating brand-new files that do not exist yet
- When adding tests, functions, or code to an existing file: use edit_file to append or insert — never rewrite the whole file
- After editing source files, verify the change compiles before declaring done
- For replacement tasks (e.g. "replace X with Y"), use search to confirm no instances of X remain before declaring done
- When a task is complete, say so clearly and stop calling tools
- edit_file returns a fresh excerpt of the file around the edit site after every successful edit — use those hashes directly for follow-up edits; do NOT call read_file again to verify an edit you just made
- IMPORTANT: Only make ONE edit_file or patch_file call per file per response. After editing a file, wait for the result before planning the next edit — the file's line numbers and hashes change after every edit, so batching multiple edits to the same file will fail.
- Use patch_file (unified diff) when making changes to multiple separate locations in the same file, or when the changes are large and structured. Use edit_file for single-location changes. Both tools are equally valid — choose whichever uses fewer tokens for the task.
- For large files: use read_file with symbols=true to get a function/class index first, then read_file with line_range=[start,end] to fetch only the section you need
- read_file output lines are prefixed `N [hash] | content` — the 4-char hash in brackets is the anchor for edit_file. Example: from `  42 [a3f2] | fn foo()`, pass anchor="a3f2" (just the 4 chars, no brackets, no line number). This prevents stale-line errors if the file changed between read and edit. Symbol index output also includes hashes — use them the same way.
- append=true adds content after the LAST LINE of the file. Only use it when the file has no relevant closing block yet (e.g. creating the very first test module in a file that has none at all). If you can see a test block, a class, or any closing brace at the end of the file — use old_str to insert inside it, not append=true.
- To add to an existing block: use old_str matching the closing brace of that block (e.g. the final `}` plus the line before it) and replace it with the new content plus the closing brace.
- In plan mode, the "Completed steps" preamble describes what changed but its line numbers are STALE. Always read anchors and line positions from the pre-loaded file content shown in the attached files section — never from the completed steps summary.
- Tool outputs are summarised in history to save context. Use the recall tool to retrieve the full output of any previous tool call when you need it.
- Do not ask for permission mid-task. If something is clearly required (adding a dependency, creating a file, running a command), do it and report what you did. Only stop to ask if there are genuinely multiple valid approaches that change the outcome significantly."#;

/// Build a compact project file map to inject into the system prompt.
/// Walks depth-2, ignores noise dirs, caps at 80 paths.
/// Returns None if cwd doesn't look like a code project.
fn build_project_map() -> Option<String> {
    use std::path::Path;

    // Only inject if there's a recognisable project root marker
    let markers = [
        "Cargo.toml", "package.json", "pyproject.toml", "go.mod",
        "Makefile", "CMakeLists.txt", ".parecode", "src",
    ];
    if !markers.iter().any(|m| Path::new(m).exists()) {
        return None;
    }

    const MAX_ENTRIES: usize = 80;
    const IGNORED: &[&str] = &[
        "node_modules", ".git", "target", ".next", "dist", "build",
        "__pycache__", ".venv", "venv", ".cache", "coverage", ".idea",
    ];

    let mut paths: Vec<String> = Vec::new();
    collect_paths(Path::new("."), 0, 2, IGNORED, &mut paths, MAX_ENTRIES);

    if paths.is_empty() {
        return None;
    }

    let map = paths.join("\n");
    Some(format!("\n\n# Project layout\n\n{map}"))
}

fn collect_paths(
    dir: &std::path::Path,
    depth: usize,
    max_depth: usize,
    ignored: &[&str],
    out: &mut Vec<String>,
    cap: usize,
) {
    if out.len() >= cap {
        return;
    }
    let Ok(entries) = std::fs::read_dir(dir) else { return };
    let mut entries: Vec<_> = entries.filter_map(|e| e.ok()).collect();
    entries.sort_by_key(|e| {
        let is_file = e.file_type().map(|t| t.is_file()).unwrap_or(false);
        (is_file as u8, e.file_name())
    });

    for entry in entries {
        if out.len() >= cap {
            break;
        }
        let name = entry.file_name();
        let name_str = name.to_string_lossy();
        // Skip hidden files/dirs and ignored dirs
        if name_str.starts_with('.') && name_str != ".parecode" {
            continue;
        }
        let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
        let path = entry.path();
        let display = path.to_string_lossy().trim_start_matches("./").to_string();

        if is_dir {
            if ignored.contains(&name_str.as_ref()) {
                continue;
            }
            out.push(format!("{display}/"));
            if depth < max_depth {
                collect_paths(&path, depth + 1, max_depth, ignored, out, cap);
            }
        } else {
            out.push(display);
        }
    }
}

/// Load project conventions from AGENTS.md, CLAUDE.md, or .parecode/conventions.md.
/// Returns None if no conventions file is found.
fn load_conventions() -> Option<String> {
    let candidates = [
        "AGENTS.md",
        "CLAUDE.md",
        ".parecode/conventions.md",
    ];
    for path in &candidates {
        if let Ok(content) = std::fs::read_to_string(path) {
            let trimmed = content.trim().to_string();
            if !trimmed.is_empty() {
                return Some(format!("\n\n# Project conventions ({path})\n\n{trimmed}"));
            }
        }
    }
    None
}

pub struct AgentConfig {
    pub verbose: bool,
    pub dry_run: bool,
    pub context_tokens: u32,
    pub _profile_name: String,
    pub _model: String,
    pub _show_timestamps: bool,
    pub mcp: Arc<McpClient>,
    /// Resolved hook commands (may be from explicit config or auto-detected).
    pub hooks: Arc<HookConfig>,
    /// When false, all hooks are suppressed for this run (set by `/hooks off`).
    pub hooks_enabled: bool,
    /// Auto-commit all changes after successful task completion.
    pub auto_commit: bool,
    /// Prefix for auto-commit messages (e.g. "parecode: ").
    pub auto_commit_prefix: String,
    /// Enable git integration: checkpoint before task, git status in system prompt, diff after.
    pub git_context: bool,
}

/// Run agent, emitting UiEvents to a ratatui TUI over `ui_tx`.
/// `attached` is a list of (path, content) pairs pre-loaded by the user via @.
/// `prior_context` is an optional preamble summarising earlier turns in this session.
pub async fn run_tui(
    task: &str,
    client: &Client,
    config: &AgentConfig,
    attached: Vec<(String, String)>,
    prior_context: Option<String>,
    ui_tx: mpsc::UnboundedSender<UiEvent>,
) -> Result<()> {
    let task_start = std::time::Instant::now();
    let task_cwd = std::env::current_dir()
        .ok()
        .and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
        .unwrap_or_else(|| "unknown".to_string());
    // Merge native tools + MCP-discovered tools into one list for the model
    let mut tools = tools::all_definitions();
    let mcp_tools = config.mcp.all_tools().await;
    for mt in &mcp_tools {
        tools.push(Tool {
            name: mt.qualified_name.clone(),
            description: mt.description.clone(),
            parameters: mt.input_schema.clone(),
        });
    }
    let mut messages: Vec<Message> = Vec::new();
    let mut total_input_tokens = 0u32;
    let mut total_output_tokens = 0u32;
    let mut tool_call_count = 0usize;

    let budget = Budget::new(config.context_tokens);
    let mut history = History::default();
    let mut cache = FileCache::default();
    let mut loop_detector = LoopDetector::default();

    // Build system prompt: base + optional project map + optional conventions
    let mut system_prompt = SYSTEM_PROMPT_BASE.to_string();
    if let Some(map) = build_project_map() {
        system_prompt.push_str(&map);
    }
    if let Some(conventions) = load_conventions() {
        system_prompt.push_str(&conventions);
    }
    // ── Git status injection ──────────────────────────────────────────────────
    // Inject `git status --short` so the model knows what files are uncommitted.
    // Skips silently if not in a git repo or git_context is disabled.
    if config.git_context {
        if let Some(status) = std::env::current_dir()
            .ok()
            .and_then(|cwd| crate::git::GitRepo::open(&cwd))
            .and_then(|repo| repo.status_short().ok())
            .filter(|s| !s.trim().is_empty())
        {
            system_prompt.push_str(&format!(
                "\n\n# Git status\n\n```\n{}\n```",
                status.trim()
            ));
        }
    }
    let system_prompt = system_prompt.as_str();
    let system_tokens = crate::budget::estimate_tokens(system_prompt);

    // Build the first user message:
    //   1. Prior session context (summaries of earlier turns, if any)
    //   2. Attached file contents (files pinned via @ in TUI)
    //   3. The task itself
    let user_content = {
        let mut s = String::new();
        if let Some(ctx) = prior_context {
            s.push_str(&ctx);
        }
        if !attached.is_empty() {
            s.push_str("The following files have been attached for context:\n\n");
            for (path, content) in &attached {
                s.push_str(&format!("[{path}]\n{content}\n\n"));
            }
            s.push_str("---\n\n");
        }
        s.push_str(task);
        s
    };

    // ── Git checkpoint ────────────────────────────────────────────────────────
    // Create a checkpoint before the task starts. If the tree is dirty, this
    // commits all pending changes as a WIP checkpoint so /undo can restore them.
    // Skips silently if not in a git repo or git_context is disabled.
    let checkpoint_hash: Option<String> = if config.git_context {
        std::env::current_dir().ok().and_then(|cwd| {
            crate::git::GitRepo::open(&cwd).and_then(|repo| {
                let summary: String = task
                    .lines()
                    .next()
                    .unwrap_or(task)
                    .chars()
                    .take(60)
                    .collect();
                repo.checkpoint(&summary).ok()
            })
        })
    } else {
        None
    };

    messages.push(Message {
        role: "user".to_string(),
        content: MessageContent::from(user_content),
    });

    loop {
        cache.next_turn();

        // ── Hard tool-call budget ─────────────────────────────────────────────
        if tool_call_count >= MAX_TOOL_CALLS {
            let _ = ui_tx.send(UiEvent::ToolBudgetHit { limit: MAX_TOOL_CALLS });
            break;
        }

        // ── Proactive token budget enforcement ────────────────────────────────
        let (est, compressed) = budget.enforce(&mut messages, system_tokens);
        if compressed {
            let _ = ui_tx.send(UiEvent::BudgetWarning);
        }
        let _ = ui_tx.send(UiEvent::ContextUpdate {
            used: est,
            total: budget.total_context(),
            compressed,
        });

        // ── Call the model ────────────────────────────────────────────────────
        // Split <think>...</think> tokens into ThinkingChunk events so the TUI
        // can render model reasoning separately from the actual response text.
        let tx_clone = ui_tx.clone();
        let in_think = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let think_buf = std::sync::Arc::new(std::sync::Mutex::new(String::new()));
        let in_think_c = in_think.clone();
        let think_buf_c = think_buf.clone();
        let response = client
            .chat(system_prompt, &messages, &tools, move |chunk| {
                // Accumulate into a small lookahead buffer to handle tags split across chunks
                think_buf_c.lock().unwrap().push_str(chunk);
                loop {
                    let buf = think_buf_c.lock().unwrap().clone();
                    if in_think_c.load(std::sync::atomic::Ordering::Relaxed) {
                        // Looking for </think>
                        if let Some(pos) = buf.find("</think>") {
                            let thinking = &buf[..pos];
                            if !thinking.is_empty() {
                                let _ = tx_clone.send(UiEvent::ThinkingChunk(thinking.to_string()));
                            }
                            *think_buf_c.lock().unwrap() = buf[pos + 8..].to_string();
                            in_think_c.store(false, std::sync::atomic::Ordering::Relaxed);
                        } else {
                            // No close tag yet — flush all but last 8 chars (tag might be split)
                            let keep = buf.len().saturating_sub(8);
                            if keep > 0 {
                                let _ = tx_clone.send(UiEvent::ThinkingChunk(buf[..keep].to_string()));
                                *think_buf_c.lock().unwrap() = buf[keep..].to_string();
                            }
                            break;
                        }
                    } else {
                        // Looking for <think>
                        if let Some(pos) = buf.find("<think>") {
                            let before = &buf[..pos];
                            if !before.is_empty() {
                                let _ = tx_clone.send(UiEvent::Chunk(before.to_string()));
                            }
                            *think_buf_c.lock().unwrap() = buf[pos + 7..].to_string();
                            in_think_c.store(true, std::sync::atomic::Ordering::Relaxed);
                        } else {
                            // No open tag — flush all but last 7 chars.
                            // saturating_sub gives a byte offset; step back to a
                            // char boundary so multi-byte chars (e.g. em-dash) don't panic.
                            let keep_bytes = buf.len().saturating_sub(7);
                            let keep = buf.floor_char_boundary(keep_bytes);
                            if keep > 0 {
                                let _ = tx_clone.send(UiEvent::Chunk(buf[..keep].to_string()));
                                *think_buf_c.lock().unwrap() = buf[keep..].to_string();
                            }
                            break;
                        }
                    }
                }
            })
            .await?;
        // Flush any remaining buffer content
        {
            let remainder = think_buf.lock().unwrap().clone();
            if !remainder.is_empty() {
                if in_think.load(std::sync::atomic::Ordering::Relaxed) {
                    let _ = ui_tx.send(UiEvent::ThinkingChunk(remainder));
                } else {
                    let _ = ui_tx.send(UiEvent::Chunk(remainder));
                }
            }
        }

        total_input_tokens += response.input_tokens;
        total_output_tokens += response.output_tokens;

        if config.verbose && (response.input_tokens > 0 || response.output_tokens > 0) {
            let _ = ui_tx.send(UiEvent::TokenStats {
                input: response.input_tokens,
                output: response.output_tokens,
                total_input: total_input_tokens,
                total_output: total_output_tokens,
            });
        }

        messages.push(Message {
            role: "assistant".to_string(),
            content: MessageContent::from(response.text.clone()),
        });

        // No tool calls → done
        if response.tool_calls.is_empty() {
            break;
        }

        // ── Execute tool calls ────────────────────────────────────────────────
        // All tool calls from a single response are executed and all results
        // returned together (required by the OpenAI API spec).
        //
        // Dependency guard: if the model batches multiple mutating calls
        // targeting the same file, only the first is executed. The rest get a
        // stub result telling the model to re-plan after seeing that result.
        // This prevents speculative chaining (e.g. append then edit the
        // just-appended content with stale anchors).
        //
        // Read-only calls (read_file, search, list_files, bash) are always
        // executed regardless — they don't mutate state so batching is safe.
        let mut tool_results: Vec<ContentPart> = Vec::new();
        // Track which files have been mutated in this batch
        let mut mutated_files: std::collections::HashSet<String> = std::collections::HashSet::new();

        for tc in &response.tool_calls {
            tool_call_count += 1;

            // Extract the target path for mutation-detection (edit/write/append ops)
            let is_mutating = matches!(tc.name.as_str(), "edit_file" | "write_file" | "patch_file");
            let target_path = if is_mutating {
                serde_json::from_str::<serde_json::Value>(&tc.arguments)
                    .ok()
                    .and_then(|v| v["path"].as_str().map(|s| s.to_string()))
            } else {
                None
            };

            // Stub out dependent mutations: same file already mutated this batch
            let content = if let Some(ref path) = target_path {
                if mutated_files.contains(path) {
                    let stub = format!(
                        "[Not executed: '{}' was already modified by an earlier call in this \
                         batch. Re-plan this edit after seeing that result — use fresh line \
                         numbers and hashes from the post-edit context above.]",
                        path
                    );
                    let _ = ui_tx.send(UiEvent::ToolResult {
                        summary: format!("⚠ skipped dependent edit on {path}"),
                    });
                    tool_results.push(ContentPart::ToolResult {
                        tool_use_id: tc.id.clone(),
                        content: stub,
                    });
                    continue;
                }
            } else { () };
            let _ = content; // suppress unused warning

            let mut result_content = if loop_detector.record(&tc.name, &tc.arguments) {
                let _ = ui_tx.send(UiEvent::LoopWarning { tool_name: tc.name.clone() });
                format!(
                    "[Loop detected: {} called with identical arguments. \
                     Try a different approach or more specific arguments.]",
                    tc.name
                )
            } else {
                let raw_output = execute_tool(tc, config, &mut cache, &history, &ui_tx, &config.mcp).await;

                // Bash commands may mutate files — invalidate any cached reads
                // for paths that appear in the command string.
                if tc.name == "bash" {
                    if let Some(cmd) = serde_json::from_str::<serde_json::Value>(&tc.arguments)
                        .ok()
                        .and_then(|v| v["command"].as_str().map(|s| s.to_string()))
                    {
                        cache.invalidate_if_mentioned(&cmd);
                    }
                }

                let (model_output, display_summary) = if config.dry_run {
                    (raw_output.clone(), raw_output.clone())
                } else {
                    let (full, display) = history.record(&tc.id, &tc.name, &raw_output);
                    (full, display)
                };

                let _ = ui_tx.send(UiEvent::ToolResult { summary: display_summary });

                if config.verbose {
                    let extra: Vec<&str> = model_output.lines().skip(1).take(4).collect();
                    for line in extra {
                        let _ = ui_tx.send(UiEvent::ToolResult { summary: format!("  {line}") });
                    }
                }

                model_output
            };

            // ── on_edit hooks ─────────────────────────────────────────────────
            // Run after each successful mutating call. Output is appended
            // directly into the tool result so the model sees compile/lint
            // errors and can self-correct immediately.
            if is_mutating && config.hooks_enabled && !config.hooks.on_edit.is_empty() {
                for cmd in &config.hooks.on_edit {
                    let hr = hooks::run_hook(cmd).await;
                    let success = hr.exit_code == 0;
                    let hook_line = if success && hr.output.trim().is_empty() {
                        format!("\n\n⚙ `{cmd}` ✓")
                    } else {
                        format!("\n\n⚙ `{cmd}` (exit {}):\n{}", hr.exit_code, hr.output)
                    };
                    result_content.push_str(&hook_line);
                    let _ = ui_tx.send(UiEvent::HookOutput {
                        event: "on_edit".to_string(),
                        output: hr.output,
                        exit_code: hr.exit_code,
                    });
                }
            }

            // Record mutation after successful execution and compress stale reads
            if let Some(path) = target_path {
                // Compress old read_file results for this path — their hashes
                // and line numbers are now stale, wasting context budget.
                if !result_content.contains("[Tool error") {
                    history.compress_reads_for(&path);
                }
                mutated_files.insert(path);
            }

            tool_results.push(ContentPart::ToolResult {
                tool_use_id: tc.id.clone(),
                content: result_content,
            });
        }

        messages.push(Message {
            role: "tool".to_string(),
            content: MessageContent::Parts(tool_results),
        });
    }

    // ── on_task_done hooks ────────────────────────────────────────────────────
    // Run after the agent loop. Output goes to TUI only — not into context.
    if config.hooks_enabled && !config.hooks.on_task_done.is_empty() {
        for cmd in &config.hooks.on_task_done {
            let hr = hooks::run_hook(cmd).await;
            let _ = ui_tx.send(UiEvent::HookOutput {
                event: "on_task_done".to_string(),
                output: hr.output,
                exit_code: hr.exit_code,
            });
        }
    }

    // ── Git post-task ─────────────────────────────────────────────────────────
    // Emit a diff notification and optionally auto-commit.
    if config.git_context {
        if let Some(cwd) = std::env::current_dir().ok() {
            if let Some(repo) = crate::git::GitRepo::open(&cwd) {
                let ref_pt = checkpoint_hash.as_deref().unwrap_or("HEAD");
                if let Ok(stat) = repo.diff_stat_from(ref_pt) {
                    if !stat.trim().is_empty() {
                        // Count lines that describe a changed file (contain '|')
                        let files_changed = stat.lines().filter(|l| l.contains('|')).count();
                        let _ = ui_tx.send(UiEvent::GitChanges {
                            stat: stat.trim().to_string(),
                            checkpoint_hash: checkpoint_hash.clone(),
                            files_changed,
                        });
                    }
                }
                if config.auto_commit {
                    let summary: String = task
                        .lines()
                        .next()
                        .unwrap_or(task)
                        .chars()
                        .take(72)
                        .collect();
                    let msg = format!("{}{}", config.auto_commit_prefix, summary);
                    match repo.auto_commit(&msg) {
                        Ok(()) => {
                            let _ = ui_tx.send(UiEvent::GitAutoCommit { message: msg });
                        }
                        Err(e) => {
                            let _ = ui_tx.send(UiEvent::GitError(format!("auto-commit: {e}")));
                        }
                    }
                }
            }
        }
    }

    // ── Task complete ─────────────────────────────────────────────────────────
    // Send a final context update so the status bar reflects post-task size
    let final_est = crate::budget::estimate_messages(&messages) + system_tokens;
    let _ = ui_tx.send(UiEvent::ContextUpdate {
        used: final_est,
        total: budget.total_context(),
        compressed: false,
    });
    let _ = ui_tx.send(UiEvent::AgentDone {
        input_tokens: total_input_tokens,
        output_tokens: total_output_tokens,
        tool_calls: tool_call_count,
        compressed_count: history.compressed_count(),
        duration_secs: task_start.elapsed().as_secs() as u32,
        cwd: task_cwd,
    });

    Ok(())
}

/// Quick mode — single API call, no multi-turn loop, minimal context.
/// Targets < 2k tokens total. No file loading, no session history.
/// Allows at most 1 tool call before returning (edit_file, search, bash read-only).
pub async fn run_quick(
    task: &str,
    client: &Client,
    config: &AgentConfig,
    ui_tx: mpsc::UnboundedSender<UiEvent>,
) -> Result<()> {
    let task_start = std::time::Instant::now();
    let task_cwd = std::env::current_dir()
        .ok()
        .and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
        .unwrap_or_else(|| "unknown".to_string());
    const QUICK_SYSTEM: &str = "You are PareCode in quick mode. Answer concisely in one response. \
If a tool call is needed, make exactly one — prefer edit_file or search. \
Do not read files unless strictly necessary. Keep responses short.";

    // Lean tool list — only the tools that make sense for quick tasks
    let quick_tools: Vec<crate::client::Tool> = tools::all_definitions()
        .into_iter()
        .filter(|t| matches!(t.name.as_str(), "edit_file" | "search" | "read_file" | "bash"))
        .collect();

    let messages = vec![Message {
        role: "user".to_string(),
        content: MessageContent::from(task.to_string()),
    }];

    let tx_clone = ui_tx.clone();
    let response = client
        .chat(QUICK_SYSTEM, &messages, &quick_tools, move |chunk| {
            let _ = tx_clone.send(UiEvent::Chunk(chunk.to_string()));
        })
        .await?;

    let total_input = response.input_tokens;
    let total_output = response.output_tokens;

    // Execute at most one tool call
    if let Some(tc) = response.tool_calls.first() {
        let mut cache = FileCache::default();
        let history = History::default();
        let raw = execute_tool(tc, config, &mut cache, &history, &ui_tx, &config.mcp).await;
        let _ = ui_tx.send(UiEvent::ToolResult {
            summary: raw.lines().take(30).collect::<Vec<_>>().join("\n"),
        });
    }

    let _ = ui_tx.send(UiEvent::AgentDone {
        input_tokens: total_input,
        output_tokens: total_output,
        tool_calls: response.tool_calls.len().min(1),
        compressed_count: 0,
        duration_secs: task_start.elapsed().as_secs() as u32,
        cwd: task_cwd,
    });

    Ok(())
}

// ── Tool execution ────────────────────────────────────────────────────────────

async fn execute_tool(
    tc: &ToolCall,
    config: &AgentConfig,
    cache: &mut FileCache,
    history: &History,
    ui_tx: &mpsc::UnboundedSender<UiEvent>,
    mcp: &McpClient,
) -> String {
    let args: Value = match serde_json::from_str(&tc.arguments) {
        Ok(v) => v,
        Err(e) => return format!("[Error parsing tool arguments: {e}]"),
    };

    // Display the tool call
    let _ = ui_tx.send(UiEvent::ToolCall {
        name: tc.name.clone(),
        args_summary: format_args_summary(&args),
    });

    if config.dry_run {
        return format!("[dry-run: {} not executed]", tc.name);
    }

    match tc.name.as_str() {
        "bash" => {
            match tools::bash::execute(&args).await {
                Ok(output) => output,
                Err(e) => format!("[Tool error: {e}]"),
            }
        }
        "recall" => {
            // Try by tool_call_id first, then by tool_name
            let by_id = args["tool_call_id"].as_str()
                .and_then(|id| history.recall(id));
            if let Some(full) = by_id {
                return full.to_string();
            }
            let by_name = args["tool_name"].as_str()
                .and_then(|name| history.recall_by_name(name));
            if let Some(full) = by_name {
                return full.to_string();
            }
            "[recall: no matching tool result found]".to_string()
        }
        "read_file" => {
            let path = args["path"].as_str().unwrap_or("");
            let is_symbols = args["symbols"].as_bool().unwrap_or(false);
            let has_range = !args["line_range"].is_null();

            // Cache only serves/stores full-content reads (no line_range, no symbols).
            // Symbol-index reads are navigation-only and must not be cached as content.
            if !has_range && !is_symbols {
                if let Some(hit) = cache.check(path) {
                    let _ = ui_tx.send(UiEvent::CacheHit { path: path.to_string() });
                    return hit.into_message();
                }
            }

            match tools::dispatch("read_file", &args) {
                Ok(output) => {
                    // Only cache full-content reads
                    if !has_range && !is_symbols {
                        cache.store(path, output.clone());
                    }
                    output
                }
                Err(e) => format!("[Tool error: {e}]"),
            }
        }
        "write_file" | "edit_file" | "patch_file" => {
            let path = args["path"].as_str().unwrap_or("");
            match tools::dispatch(&tc.name, &args) {
                Ok(o) => {
                    cache.invalidate(path);
                    o
                }
                Err(e) => {
                    // On edit failure, include the actual file content so the model
                    // can see exactly what's there and correct its old_str.
                    let hint = if let Some(hit) = cache.check(path) {
                        format!(
                            "\n\nCurrent file content for reference:\n{}",
                            hit.content
                        )
                    } else if let Ok(content) = std::fs::read_to_string(path) {
                        format!("\n\nCurrent file content for reference:\n{content}")
                    } else {
                        String::new()
                    };
                    format!("[Tool error: {e}]{hint}")
                }
            }
        }
        _ => {
            // Try native tools first, then fall through to MCP
            if tools::is_native(&tc.name) {
                match tools::dispatch(&tc.name, &args) {
                    Ok(output) => output,
                    Err(e) => format!("[Tool error: {e}]"),
                }
            } else {
                // MCP tool: qualified name contains a '.'
                match mcp.call(&tc.name, args).await {
                    Ok(output) => output,
                    Err(e) => format!("[MCP tool error: {e}]"),
                }
            }
        }
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn format_args_summary(args: &Value) -> String {
    if let Some(obj) = args.as_object() {
        let pairs: Vec<String> = obj
            .iter()
            .map(|(k, v)| {
                let val = match v {
                    Value::String(s) => {
                        if s.chars().count() > 60 {
                            let truncated: String = s.chars().take(57).collect();
                            format!("\"{}\"", truncated)
                        } else {
                            format!("\"{s}\"")
                        }
                    }
                    other => {
                        let s = other.to_string();
                        if s.chars().count() > 40 {
                            let truncated: String = s.chars().take(37).collect();
                            format!("{}", truncated)
                        } else {
                            s
                        }
                    }
                };
                format!("{k}={val}")
            })
            .collect();
        pairs.join(", ")
    } else {
        args.to_string()
    }
}