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
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
//! Agent backends for workflow workers.
//!
//! - [`WorkerProbeBackend`]: exercises real tool registration + [`SecurityPolicy`]
//!   (no live model). Used for permission/concurrency integration tests and as
//!   the safe default when no parent executor is available.
//! - [`SubagentBridgeBackend`]: production path — each `agent()` runs through
//!   the real `subagent` tool / turn infrastructure with a filtered allowlist.

use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Weak};

use async_trait::async_trait;
use serde_json::json;

use super::AgentBackend;
use super::policy::EffectiveAgentPolicy;
use super::types::{AgentBackendResult, AgentRequest, NESTED_WORKFLOW_TOOLS};
use crate::security::{SecurityDecision, SecurityPolicy};
use crate::tool::{ToolExecutor, ToolInvocation, ToolResult};

/// Write-oriented tool names used when probing policy.
const WRITE_TOOLS: &[&str] = &[
    "write_file",
    "write",
    "edit",
    "multiedit",
    "apply_patch",
    "code_edit",
];
const COMMAND_TOOLS: &[&str] = &["bash", "sandbox"];

/// Builds a filtered worker executor and probes tool/path access via the real
/// [`SecurityPolicy`] and tool registry path (no model call).
pub struct WorkerProbeBackend {
    policy: SecurityPolicy,
    pub delay_ms: u64,
    pub in_flight: Option<Arc<AtomicUsize>>,
    pub peak_in_flight: Option<Arc<AtomicUsize>>,
}

impl WorkerProbeBackend {
    pub fn new(policy: SecurityPolicy) -> Self {
        Self {
            policy,
            delay_ms: 0,
            in_flight: None,
            peak_in_flight: None,
        }
    }

    pub fn with_delay(mut self, delay_ms: u64) -> Self {
        self.delay_ms = delay_ms;
        self
    }

    pub fn with_inflight(mut self, in_flight: Arc<AtomicUsize>, peak: Arc<AtomicUsize>) -> Self {
        self.in_flight = Some(in_flight);
        self.peak_in_flight = Some(peak);
        self
    }
}

#[async_trait]
impl AgentBackend for WorkerProbeBackend {
    async fn run_agent(&self, request: AgentRequest) -> AgentBackendResult {
        if let Some(ref inflight) = self.in_flight {
            let n = inflight.fetch_add(1, Ordering::SeqCst) + 1;
            if let Some(ref peak) = self.peak_in_flight {
                peak.fetch_max(n, Ordering::SeqCst);
            }
        }

        if self.delay_ms > 0 {
            let delay = std::time::Duration::from_millis(self.delay_ms);
            tokio::select! {
                _ = tokio::time::sleep(delay) => {}
                _ = request.cancel_token.notified() => {
                    if let Some(ref inflight) = self.in_flight {
                        inflight.fetch_sub(1, Ordering::SeqCst);
                    }
                    return AgentBackendResult {
                        ok: false,
                        output: json!({"error": "cancelled"}),
                        error: Some("cancelled".into()),
                    };
                }
            }
        }

        if request.cancel_token.is_requested() {
            if let Some(ref inflight) = self.in_flight {
                inflight.fetch_sub(1, Ordering::SeqCst);
            }
            return AgentBackendResult {
                ok: false,
                output: json!({"error": "cancelled"}),
                error: Some("cancelled".into()),
            };
        }

        let probe = probe_worker_capabilities_inner(&self.policy, &request.effective);

        if let Some(ref inflight) = self.in_flight {
            inflight.fetch_sub(1, Ordering::SeqCst);
        }

        AgentBackendResult {
            ok: true,
            output: json!({
                "ok": true,
                "backend": "worker_probe",
                "prompt": request.prompt,
                "label": request.label,
                "agent_index": request.agent_index,
                "profile": request.effective.profile,
                "tools": request.effective.tools,
                "create_files": request.effective.create_files,
                "create_dirs": request.effective.create_dirs,
                "write_allow": request.effective.write_allow,
                "path_allow": request.effective.path_allow,
                "path_deny": request.effective.path_deny,
                "can_write_file": probe.can_write_file,
                "can_edit": probe.can_edit,
                "can_bash": probe.can_bash,
                "can_subagent": probe.can_subagent,
                "can_workflow": probe.can_workflow,
                "write_path_allowed": probe.write_path_allowed,
                "write_path_denied": probe.write_path_denied,
                "create_new_file_allowed": probe.create_new_file_allowed,
                "registered_tools": probe.registered_tools,
                "policy_denials": probe.policy_denials,
            }),
            error: None,
        }
    }
}

#[derive(Debug, Default)]
struct ProbeResult {
    can_write_file: bool,
    can_edit: bool,
    can_bash: bool,
    can_subagent: bool,
    can_workflow: bool,
    write_path_allowed: Vec<String>,
    write_path_denied: Vec<String>,
    create_new_file_allowed: bool,
    registered_tools: Vec<String>,
    policy_denials: Vec<String>,
}

/// Public probe summary for unit tests (fields only; no mock echo).
#[derive(Debug, Clone)]
pub struct WorkerProbeSummary {
    pub can_bash: bool,
    pub can_write_file: bool,
    pub can_edit: bool,
    pub can_subagent: bool,
    pub can_workflow: bool,
    pub registered_tools: Vec<String>,
}

/// Probe real tool allowlist + SecurityPolicy for a worker's effective policy.
pub fn probe_worker_capabilities(
    base_policy: &SecurityPolicy,
    effective: &EffectiveAgentPolicy,
) -> WorkerProbeSummary {
    let inner = probe_worker_capabilities_inner(base_policy, effective);
    WorkerProbeSummary {
        can_bash: inner.can_bash,
        can_write_file: inner.can_write_file,
        can_edit: inner.can_edit,
        can_subagent: inner.can_subagent,
        can_workflow: inner.can_workflow,
        registered_tools: inner.registered_tools,
    }
}

fn scoped_policy(base: &SecurityPolicy, effective: &EffectiveAgentPolicy) -> SecurityPolicy {
    base.clone()
        .with_write_scope(crate::security::WritePathScope {
            write_allow: effective.write_allow.clone(),
            path_deny: effective.path_deny.clone(),
            create_files: effective.create_files,
            create_dirs: effective.create_dirs,
        })
}

fn probe_worker_capabilities_inner(
    base_policy: &SecurityPolicy,
    effective: &EffectiveAgentPolicy,
) -> ProbeResult {
    let mut out = ProbeResult::default();
    let project = base_policy.project_root().to_path_buf();

    // Nested orchestration must never appear after intersection + strip.
    out.can_subagent = effective.tools.iter().any(|t| t == "subagent");
    out.can_workflow = effective.tools.iter().any(|t| t == "workflow");

    // Worker executor with WritePathScope (same gate as production SubagentBridge).
    let policy = scoped_policy(base_policy, effective);
    let mut exec = ToolExecutor::empty(policy.clone());
    register_filtered_tools(&mut exec, &project, effective);
    out.registered_tools = exec.tool_names();
    out.registered_tools.sort();

    out.can_write_file = exec
        .tool_names()
        .iter()
        .any(|t| t == "write_file" || t == "write");
    out.can_edit = exec
        .tool_names()
        .iter()
        .any(|t| t == "edit" || t == "multiedit");
    out.can_bash = exec.tool_names().iter().any(|t| t == "bash");
    out.can_subagent = out.registered_tools.iter().any(|t| t == "subagent");
    out.can_workflow = out.registered_tools.iter().any(|t| t == "workflow");

    // Real ToolExecutor::validate path for representative writes.
    let probe_paths: Vec<String> = {
        let mut c = effective.write_allow.clone();
        if c.is_empty() {
            c.push("src/a.rs".into());
        }
        c.push("__outside_write_allow__.rs".into());
        for d in &effective.path_deny {
            let clean = d
                .trim_end_matches('/')
                .trim_end_matches('*')
                .trim_end_matches('/');
            if !clean.is_empty() {
                c.push(clean.to_string());
            }
        }
        // Non-existent path under write_allow for create_files probe.
        if let Some(first) = effective.write_allow.first() {
            c.push(format!("__new_create_probe__/{first}"));
        } else {
            c.push("__new_create_probe__/file.rs".into());
        }
        c.sort();
        c.dedup();
        c
    };

    for path in &probe_paths {
        let inv = ToolInvocation {
            id: format!("probe-write-{path}"),
            tool_name: "write_file".into(),
            input: json!({"path": path, "content": "x"}),
        };
        match exec.validate(&inv) {
            SecurityDecision::Deny(reason) => {
                out.policy_denials
                    .push(format!("write_file {path}: {reason}"));
                out.write_path_denied.push(path.clone());
            }
            SecurityDecision::Allow | SecurityDecision::NeedsApproval(_) => {
                // Only count as allowed if tool is registered AND validate ok.
                if out.can_write_file {
                    out.write_path_allowed.push(path.clone());
                } else {
                    out.write_path_denied.push(path.clone());
                    out.policy_denials
                        .push(format!("write_file {path}: tool not registered"));
                }
            }
        }
    }

    // create_files: writing a write_allow path that does not exist yet must Deny
    // when create_files=false (real SecurityPolicy WritePathScope).
    if let Some(wa) = effective.write_allow.first() {
        let abs = project.join(wa);
        // Use a unique non-existent path that still matches write_allow when
        // write_allow is a single file — probe that exact path if missing.
        let probe_path = if abs.exists() {
            // Existing path: also probe a sibling under same allow prefix if possible.
            format!("__wf_create_probe__/{wa}")
        } else {
            wa.clone()
        };
        let inv = ToolInvocation {
            id: "probe-create".into(),
            tool_name: "write_file".into(),
            input: json!({"path": probe_path, "content": "new"}),
        };
        match exec.validate(&inv) {
            SecurityDecision::Deny(reason) => {
                out.create_new_file_allowed = false;
                out.policy_denials.push(format!("create_new: {reason}"));
            }
            SecurityDecision::Allow | SecurityDecision::NeedsApproval(_) => {
                // Only true if tool registered, write_allow non-empty, create_files true,
                // and path is in write_allow (validate already checked scope).
                out.create_new_file_allowed = out.can_write_file && effective.create_files;
            }
        }
    } else {
        out.create_new_file_allowed = false;
    }

    // Empty write_allow ⇒ no writes even if tools listed.
    if effective.write_allow.is_empty() {
        out.can_write_file = false;
        out.can_edit = false;
        out.create_new_file_allowed = false;
    }

    out
}

fn register_filtered_tools(
    exec: &mut ToolExecutor,
    project: &std::path::Path,
    effective: &EffectiveAgentPolicy,
) {
    use super::super::{
        bash::BashTool, edit_tool::EditTool, read_tool::ReadTool, search_tool::SearchTool,
        write_tool::WriteTool,
    };

    // Never register orchestration tools.
    let allowed: Vec<&str> = effective
        .tools
        .iter()
        .map(|s| s.as_str())
        .filter(|t| !NESTED_WORKFLOW_TOOLS.contains(t))
        .collect();

    let has = |name: &str| allowed.contains(&name);

    if has("read_file") || has("read") || has("view_file") {
        exec.register_tool(Arc::new(ReadTool::new(project.to_path_buf())));
    }
    if has("search") || has("grep") || has("fs_browser") || has("list_dir") || has("glob") {
        exec.register_tool(Arc::new(SearchTool::new(project.to_path_buf())));
    }

    // Writes only when write_allow is non-empty (empty ⇒ no writes even for implementer).
    // create_files=false still registers tools; WritePathScope denies creates.
    let writes_ok = !effective.write_allow.is_empty();
    if writes_ok && (has("write_file") || has("write")) {
        exec.register_tool(Arc::new(WriteTool::write_file(project.to_path_buf())));
    }
    if writes_ok && (has("edit") || has("multiedit")) {
        exec.register_tool(Arc::new(EditTool::new(project.to_path_buf())));
    }
    if has("bash") {
        exec.register_tool(Arc::new(BashTool::new(project.to_path_buf())));
    }
}

/// Production backend: each worker is a real nested `subagent` turn with a
/// tool allowlist derived from the effective workflow policy.
pub struct SubagentBridgeBackend {
    tool_executor: Weak<ToolExecutor>,
}

impl SubagentBridgeBackend {
    pub fn new(tool_executor: Weak<ToolExecutor>) -> Self {
        Self { tool_executor }
    }
}

#[async_trait]
impl AgentBackend for SubagentBridgeBackend {
    async fn run_agent(&self, request: AgentRequest) -> AgentBackendResult {
        let Some(executor) = self.tool_executor.upgrade() else {
            return AgentBackendResult {
                ok: false,
                output: json!({"error": "tool executor unavailable"}),
                error: Some("tool executor dropped".into()),
            };
        };

        if request.cancel_token.is_requested() {
            return AgentBackendResult {
                ok: false,
                output: json!({"error": "cancelled"}),
                error: Some("cancelled".into()),
            };
        }

        // Embed path policy in the prompt (guidance) AND pass write scope fields
        // so SubagentTool forks a SecurityPolicy with WritePathScope (hard gate).
        let tools_for_note: Vec<String> = {
            let mut t = request.effective.tools.clone();
            t.retain(|n| !NESTED_WORKFLOW_TOOLS.contains(&n.as_str()));
            if request.effective.write_allow.is_empty() {
                t.retain(|n| {
                    !WRITE_TOOLS.contains(&n.as_str()) && !COMMAND_TOOLS.contains(&n.as_str())
                });
            }
            t
        };
        let path_note = format!(
            "\n\n[workflow worker policy]\n\
             profile={}\n\
             tools={:?}\n\
             write_allow={:?}\n\
             path_deny={:?}\n\
             create_files={}\n\
             create_dirs={}\n\
             You MUST NOT call subagent or workflow. \
             Writes are only allowed on write_allow paths (empty ⇒ no writes).",
            request.effective.profile,
            tools_for_note,
            request.effective.write_allow,
            request.effective.path_deny,
            request.effective.create_files,
            request.effective.create_dirs,
        );

        let prompt = format!("{}{path_note}", request.prompt);
        let input = build_subagent_bridge_input(
            &prompt,
            request.label.as_deref(),
            &request.effective,
            request.model.as_deref(),
            request.max_tokens,
        );
        let inv = ToolInvocation {
            id: format!("wf-agent-{}", request.agent_index),
            tool_name: "subagent".into(),
            input,
        };

        let result: ToolResult = executor
            .invoke_with_full_context(
                inv,
                crate::tool::ToolInvocationContext {
                    cancel_token: Some(request.cancel_token.clone()),
                    ..Default::default()
                },
                true, // workflow already approved at parent tool level
            )
            .await;

        if request.cancel_token.is_requested() {
            return AgentBackendResult {
                ok: false,
                output: json!({"error": "cancelled"}),
                error: Some("cancelled".into()),
            };
        }

        let err_msg = if result.ok {
            None
        } else {
            Some(
                result
                    .output
                    .get("error")
                    .and_then(|e| e.as_str())
                    .unwrap_or("subagent failed")
                    .to_string(),
            )
        };
        let mut output = result.output;
        if let Some(obj) = output.as_object_mut() {
            obj.insert("backend".into(), json!("subagent_bridge"));
            obj.insert("agent_index".into(), json!(request.agent_index));
            obj.insert("profile".into(), json!(request.effective.profile));
            obj.insert("tools".into(), json!(request.effective.tools));
            obj.insert("write_allow".into(), json!(request.effective.write_allow));
            obj.insert("create_files".into(), json!(request.effective.create_files));
        }

        AgentBackendResult {
            ok: result.ok,
            output,
            error: err_msg,
        }
    }
}

/// Build the JSON tool input the production bridge sends to `subagent`.
/// Extracted for unit tests (schema + null-description regressions).
pub(crate) fn build_subagent_bridge_input(
    prompt: &str,
    label: Option<&str>,
    effective: &EffectiveAgentPolicy,
    model: Option<&str>,
    max_tokens: Option<usize>,
) -> serde_json::Value {
    let mut tools = effective.tools.clone();
    tools.retain(|t| !NESTED_WORKFLOW_TOOLS.contains(&t.as_str()));
    if effective.write_allow.is_empty() {
        tools
            .retain(|t| !WRITE_TOOLS.contains(&t.as_str()) && !COMMAND_TOOLS.contains(&t.as_str()));
    }
    let approval = if effective.write_allow.is_empty() {
        "read_only"
    } else if effective.approval == "escalate" {
        "escalate"
    } else {
        effective.approval.as_str()
    };
    let mut options = json!({
        "agent_profile": effective.profile,
        "tools": tools,
        "approval": approval,
        "write_allow": effective.write_allow,
        "path_deny": effective.path_deny,
        "create_files": effective.create_files,
        "create_dirs": effective.create_dirs,
    });
    if let Some(model) = model {
        options
            .as_object_mut()
            .expect("options object")
            .insert("model".into(), json!(model));
    }
    if let Some(max_tokens) = max_tokens {
        options
            .as_object_mut()
            .expect("options object")
            .insert("max_tokens".into(), json!(max_tokens));
    }
    let mut input = json!({
        "prompt": prompt,
        "options": options,
    });
    if let Some(label) = label.map(str::trim).filter(|s| !s.is_empty()) {
        input
            .as_object_mut()
            .expect("input object")
            .insert("description".into(), json!(label));
    }
    input
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{HarnessConfig, NaviConfig};
    use crate::model::{ModelProvider, ModelRequest, ModelStream};
    use crate::prompt::PromptCache;
    use crate::runtime_components::RuntimeComponents;
    use crate::tool::Tool;
    use crate::tool::builtin::SubagentTool;
    use crate::tool::builtin::workflow::policy::default_run_policy;
    use std::sync::{Arc, RwLock};

    struct NoopProvider;
    impl ModelProvider for NoopProvider {
        fn stream(&self, _req: ModelRequest) -> ModelStream {
            Box::pin(futures_util::stream::empty())
        }
    }

    /// Schema from a real SubagentTool — never a hand-rolled duplicate that can drift.
    fn registered_subagent_schema() -> serde_json::Value {
        let tool = SubagentTool::new(
            std::sync::Weak::new(),
            Arc::new(RwLock::new(Arc::new(NoopProvider) as Arc<dyn ModelProvider>)),
            std::path::PathBuf::from("/tmp"),
            std::path::PathBuf::from("/tmp"),
            Arc::new(RwLock::new("test".into())),
            HarnessConfig::default(),
            Arc::new(RwLock::new(NaviConfig::default())),
            Arc::new(PromptCache::new()),
            RuntimeComponents::default(),
        );
        tool.definition().input_schema
    }

    #[test]
    fn bridge_input_omits_null_description_when_label_missing() {
        let mut run = default_run_policy();
        run.create_files = true;
        run.write_allow = vec!["scratch/a.txt".into()];
        run.tools = vec![
            "read_file".into(),
            "write_file".into(),
            "edit".into(),
            "search".into(),
        ];
        let effective = crate::tool::builtin::workflow::policy::intersect_agent_policy(
            &run,
            &crate::tool::builtin::workflow::policy::AgentPolicyOpts {
                profile: Some("implementer".into()),
                ..Default::default()
            },
        );
        assert!(effective.create_files);
        let input = build_subagent_bridge_input("do work", None, &effective, None, None);
        assert!(
            input.get("description").is_none(),
            "missing label must not serialize description:null, got {input}"
        );
        assert_eq!(input["options"]["create_files"], true);
        assert_eq!(input["options"]["write_allow"], json!(["scratch/a.txt"]));
        // Validate against the live SubagentTool schema (not a hand-rolled twin).
        let schema = registered_subagent_schema();
        let validator = jsonschema::validator_for(&schema).unwrap();
        let errors: Vec<_> = validator
            .iter_errors(&input)
            .map(|e| e.to_string())
            .collect();
        assert!(
            errors.is_empty(),
            "bridge input invalid vs registered SubagentTool schema: {errors:?} input={input}"
        );
    }

    #[test]
    fn bridge_input_includes_non_empty_label() {
        let run = default_run_policy();
        let effective = crate::tool::builtin::workflow::policy::intersect_agent_policy(
            &run,
            &Default::default(),
        );
        let input = build_subagent_bridge_input("p", Some("  collect  "), &effective, None, None);
        assert_eq!(input["description"], "collect");
        let schema = registered_subagent_schema();
        let validator = jsonschema::validator_for(&schema).unwrap();
        let errors: Vec<_> = validator
            .iter_errors(&input)
            .map(|e| e.to_string())
            .collect();
        assert!(
            errors.is_empty(),
            "labeled bridge input invalid vs SubagentTool schema: {errors:?} input={input}"
        );
    }
}