navi-core 0.3.6

Local agentic engine and terminal-first coding agent.
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
//! Plan Mode — a collaboration phase where the agent designs a plan before execution.
//!
//! - Read-only tools for exploration
//! - The session plan file (`{data_dir}/plans/{session}.md`) is the only writable path
//! - The model builds a **markdown design doc** (context, approach, files, verification)
//! - `plan(action='submit')` (or `create`) presents the plan for user review
//! - After approval the host exits plan mode and the agent implements
//!
//! Legacy: `<proposed_plan>` XML tags are still parsed for compatibility, but
//! markdown plan files are preferred.

use crate::tool::ToolKind;
use serde::{Deserialize, Serialize};

/// The collaboration mode of the agent.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AgentMode {
    /// Normal execution mode — all tools available, full agentic loop.
    Default,
    /// Plan mode — only read-only tools, model proposes a plan via text tags.
    Plan,
}

impl AgentMode {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Plan => "plan",
        }
    }

    /// Returns true if this mode restricts tool access.
    pub fn restricts_tools(&self) -> bool {
        matches!(self, Self::Plan)
    }
}

impl Default for AgentMode {
    fn default() -> Self {
        Self::Default
    }
}

impl std::fmt::Display for AgentMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// A proposed plan extracted from the model's text stream.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProposedPlan {
    /// Title/summary of the plan.
    pub title: String,
    /// Ordered list of steps to execute.
    pub steps: Vec<String>,
}

impl ProposedPlan {
    pub fn new(title: String, steps: Vec<String>) -> Self {
        Self { title, steps }
    }

    pub fn is_empty(&self) -> bool {
        self.steps.is_empty() && self.title.is_empty()
    }
}

/// Parses `<proposed_plan>` blocks from streaming text in real-time.
///
/// The model emits plans like:
/// ```text
/// <proposed_plan title="Fix the bug">
/// 1. Read the file
/// 2. Fix the function
/// 3. Run tests
/// </proposed_plan>
/// ```
///
/// The parser accumulates text chunks and extracts the plan when the
/// closing tag is found.
#[derive(Debug, Default)]
pub struct ProposedPlanParser {
    buffer: String,
    in_plan: bool,
    plan_title: Option<String>,
    plan_body: String,
    completed_plans: Vec<ProposedPlan>,
}

impl ProposedPlanParser {
    pub fn new() -> Self {
        Self::default()
    }

    /// Feed a text delta into the parser.
    /// Returns any plans that were completed by this chunk.
    pub fn push_text(&mut self, text: &str) -> Vec<ProposedPlan> {
        self.buffer.push_str(text);
        self.drain_completed()
    }

    /// Drain any pending plans (call at end of turn).
    pub fn drain(&mut self) -> Vec<ProposedPlan> {
        // Move any remaining buffer content into plan_body if inside a plan.
        if self.in_plan {
            self.plan_body.push_str(&self.buffer);
            self.buffer.clear();
            if !self.plan_body.is_empty() {
                let plan = self.finalize_plan();
                self.completed_plans.push(plan);
            }
        }
        std::mem::take(&mut self.completed_plans)
    }

    /// Returns true if the parser is currently inside a `<proposed_plan>` block.
    pub fn is_in_plan(&self) -> bool {
        self.in_plan
    }

    /// Returns the partial plan body being accumulated (for live UI preview).
    pub fn partial_body(&self) -> &str {
        if self.in_plan { &self.plan_body } else { "" }
    }

    /// Returns the parsed title of the current plan (for live UI preview).
    pub fn partial_title(&self) -> Option<&str> {
        if self.in_plan {
            self.plan_title.as_deref()
        } else {
            None
        }
    }

    fn drain_completed(&mut self) -> Vec<ProposedPlan> {
        loop {
            if self.in_plan {
                // Look for closing tag in buffer
                if let Some(pos) = self.buffer.find("</proposed_plan>") {
                    self.plan_body.push_str(&self.buffer[..pos]);
                    self.buffer = self.buffer[pos + "</proposed_plan>".len()..].to_string();
                    self.in_plan = false;
                    let plan = self.finalize_plan();
                    self.completed_plans.push(plan);
                } else {
                    // No closing tag yet. Move all buffer content into plan_body
                    // except a suffix that could be the start of "</proposed_plan>".
                    let tag = "</proposed_plan>";
                    if self.buffer.len() >= tag.len() {
                        let safe = self.buffer.len() - tag.len() + 1;
                        self.plan_body.push_str(&self.buffer[..safe]);
                        self.buffer = self.buffer[safe..].to_string();
                    } else {
                        // Buffer is shorter than the tag — move everything.
                        self.plan_body.push_str(&self.buffer);
                        self.buffer.clear();
                    }
                    break;
                }
            } else {
                // Look for opening tag
                if let Some(tag_info) = self.find_opening_tag() {
                    self.buffer = self.buffer[tag_info.consume_len..].to_string();
                    self.in_plan = true;
                    self.plan_title = tag_info.title;
                    self.plan_body.clear();
                } else {
                    // No opening tag found. Keep only a suffix that could be
                    // the start of "<proposed_plan".
                    let tag = "<proposed_plan";
                    let safe = self.buffer.len().saturating_sub(tag.len() - 1);
                    self.buffer = self.buffer[safe..].to_string();
                    break;
                }
            }
        }
        std::mem::take(&mut self.completed_plans)
    }

    fn finalize_plan(&mut self) -> ProposedPlan {
        let title = self.plan_title.take().unwrap_or_default();
        let body = std::mem::take(&mut self.plan_body);
        let steps = parse_plan_steps(&body);
        ProposedPlan::new(title, steps)
    }

    fn find_opening_tag(&self) -> Option<OpeningTagInfo> {
        let start = self.buffer.find("<proposed_plan")?;
        let rest = &self.buffer[start..];
        let tag_end = rest.find('>')?;
        let tag_content = &rest[..tag_end];
        let consume_len = start + tag_end + 1;

        let title = tag_content
            .find("title=\"")
            .and_then(|t_pos| {
                let value_start = t_pos + "title=\"".len();
                tag_content[value_start..]
                    .find('"')
                    .map(|end| tag_content[value_start..value_start + end].to_string())
            })
            .or_else(|| {
                tag_content.find("title='").and_then(|t_pos| {
                    let value_start = t_pos + "title='".len();
                    tag_content[value_start..]
                        .find('\'')
                        .map(|end| tag_content[value_start..value_start + end].to_string())
                })
            });

        Some(OpeningTagInfo { consume_len, title })
    }
}

struct OpeningTagInfo {
    consume_len: usize,
    title: Option<String>,
}

/// Parses plan body text into steps.
/// Supports numbered lists, bullet lists, and plain lines.
fn parse_plan_steps(body: &str) -> Vec<String> {
    body.lines()
        .map(|line| line.trim())
        .filter(|line| !line.is_empty())
        .map(|line| {
            // Strip leading markers: "1. ", "1) ", "- ", "* ", "• "
            let stripped = if let Some(rest) = line.strip_prefix("- ") {
                rest.to_string()
            } else if let Some(rest) = line.strip_prefix("* ") {
                rest.to_string()
            } else if let Some(rest) = line.strip_prefix("• ") {
                rest.to_string()
            } else {
                // Strip numbered prefix: "1. ", "1) ", "10. ", etc.
                let chars: Vec<char> = line.chars().collect();
                let mut idx = 0;
                while idx < chars.len() && chars[idx].is_ascii_digit() {
                    idx += 1;
                }
                if idx > 0 && idx < chars.len() && (chars[idx] == '.' || chars[idx] == ')') {
                    idx += 1;
                    while idx < chars.len() && chars[idx] == ' ' {
                        idx += 1;
                    }
                    line[idx..].to_string()
                } else {
                    line.to_string()
                }
            };
            if stripped.is_empty() {
                line.to_string()
            } else {
                stripped
            }
        })
        .collect()
}

/// Returns true if a tool is allowed in Plan mode.
///
/// - All **Read** tools (explore the codebase)
/// - **Write** tools only for the session plan file (enforced by [`SecurityPolicy`])
/// - `plan` (draft/submit markdown plan) and `question` (clarify requirements)
/// - Commands and other custom tools are denied
pub fn is_tool_allowed_in_plan_mode(kind: ToolKind) -> bool {
    is_tool_allowed_in_plan_mode_named("", kind)
}

/// Name-aware plan-mode allowlist (preferred).
pub fn is_tool_allowed_in_plan_mode_named(name: &str, kind: ToolKind) -> bool {
    match kind {
        ToolKind::Read => true,
        ToolKind::Write => matches!(name, "write_file" | "edit" | "multiedit" | "write"),
        ToolKind::Custom => matches!(name, "plan" | "question"),
        ToolKind::Command => false,
    }
}

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

    #[test]
    fn parser_extracts_simple_plan() {
        let mut parser = ProposedPlanParser::new();
        let plans = parser.push_text(
            "Let me analyze this.\n\
             <proposed_plan title=\"Fix the bug\">\n\
             1. Read the file\n\
             2. Fix the function\n\
             3. Run tests\n\
             </proposed_plan>\n\
             Done.",
        );
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].title, "Fix the bug");
        assert_eq!(plans[0].steps.len(), 3);
        assert_eq!(plans[0].steps[0], "Read the file");
        assert_eq!(plans[0].steps[1], "Fix the function");
        assert_eq!(plans[0].steps[2], "Run tests");
    }

    #[test]
    fn parser_extracts_plan_without_title() {
        let mut parser = ProposedPlanParser::new();
        let plans = parser.push_text(
            "<proposed_plan>\n\
             - Step one\n\
             - Step two\n\
             </proposed_plan>",
        );
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].title, "");
        assert_eq!(plans[0].steps, vec!["Step one", "Step two"]);
    }

    #[test]
    fn parser_handles_chunked_stream() {
        let mut parser = ProposedPlanParser::new();
        let chunks = [
            "Let me think.\n<propos",
            "ed_plan title=\"My Plan\">\n",
            "1. First step\n2. Second ",
            "step\n</proposed_",
            "plan>\nDone.",
        ];

        let mut all_plans = Vec::new();
        for chunk in &chunks {
            all_plans.extend(parser.push_text(chunk));
        }

        assert_eq!(all_plans.len(), 1);
        assert_eq!(all_plans[0].title, "My Plan");
        assert_eq!(all_plans[0].steps, vec!["First step", "Second step"]);
    }

    #[test]
    fn parser_handles_multiple_plans() {
        let mut parser = ProposedPlanParser::new();
        let plans = parser.push_text(
            "<proposed_plan title=\"Plan A\">\n1. A1\n</proposed_plan>\n\
             <proposed_plan title=\"Plan B\">\n1. B1\n</proposed_plan>",
        );
        assert_eq!(plans.len(), 2);
        assert_eq!(plans[0].title, "Plan A");
        assert_eq!(plans[1].title, "Plan B");
    }

    #[test]
    fn parser_drain_unclosed_plan() {
        let mut parser = ProposedPlanParser::new();
        parser.push_text("<proposed_plan title=\"Unclosed\">\n1. Step\n");
        let plans = parser.drain();
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].title, "Unclosed");
        assert_eq!(plans[0].steps, vec!["Step"]);
    }

    #[test]
    fn parser_partial_preview() {
        let mut parser = ProposedPlanParser::new();
        parser.push_text("<proposed_plan title=\"Live\">\n1. First");
        assert!(parser.is_in_plan());
        assert_eq!(parser.partial_title(), Some("Live"));
        assert!(parser.partial_body().contains("First"));
    }

    #[test]
    fn parser_no_plan_in_regular_text() {
        let mut parser = ProposedPlanParser::new();
        let plans = parser.push_text("Just regular text without any plan tags.");
        assert!(plans.is_empty());
        assert!(!parser.is_in_plan());
    }

    #[test]
    fn parse_plan_steps_strips_numbering() {
        let steps = parse_plan_steps("1. First\n2. Second\n3. Third");
        assert_eq!(steps, vec!["First", "Second", "Third"]);
    }

    #[test]
    fn parse_plan_steps_strips_bullets() {
        let steps = parse_plan_steps("- Alpha\n* Beta");
        assert_eq!(steps, vec!["Alpha", "Beta"]);
    }

    #[test]
    fn agent_mode_default_is_default() {
        assert_eq!(AgentMode::default(), AgentMode::Default);
    }

    #[test]
    fn agent_mode_plan_restricts_tools() {
        assert!(AgentMode::Plan.restricts_tools());
        assert!(!AgentMode::Default.restricts_tools());
    }

    #[test]
    fn plan_mode_allows_read_plan_write_and_question() {
        assert!(is_tool_allowed_in_plan_mode_named(
            "read_file",
            ToolKind::Read
        ));
        assert!(is_tool_allowed_in_plan_mode_named("search", ToolKind::Read));
        assert!(is_tool_allowed_in_plan_mode_named("plan", ToolKind::Read));
        assert!(is_tool_allowed_in_plan_mode_named(
            "write_file",
            ToolKind::Write
        ));
        assert!(is_tool_allowed_in_plan_mode_named("edit", ToolKind::Write));
        assert!(is_tool_allowed_in_plan_mode_named(
            "question",
            ToolKind::Custom
        ));
        assert!(!is_tool_allowed_in_plan_mode_named(
            "bash",
            ToolKind::Command
        ));
        assert!(!is_tool_allowed_in_plan_mode_named(
            "subagent",
            ToolKind::Command
        ));
        assert!(!is_tool_allowed_in_plan_mode_named(
            "apply_patch",
            ToolKind::Write
        ));
    }
}