sparrow-cli 0.5.1

A local-first Rust agent cockpit — route, run, replay, rewind
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
use async_trait::async_trait;
use serde_json::json;
use std::sync::Arc;
use tokio::sync::mpsc;

use super::{Tool, ToolCtx, ToolResult};
use crate::config::Config;
use crate::engine::{Engine, Identity, Task};
use crate::event::{Block, Event, RiskLevel};
use crate::memory::Memory;
use crate::permissions::PermissionMode;
use crate::router::Router;

// ─── Subagent spawn ─────────────────────────────────────────────────────────────

/// Delegates a subtask to a child AgentRun with its own conversation and sandbox.
/// §15: "Each subagent gets its own conversation, terminal, and a Python RPC channel."
///
/// Holds the router + config (not a parent Engine) so it can build a fresh child
/// engine per call — this avoids a self-referential Arc<Engine> at registration.
pub struct SubagentSpawn {
    router: Arc<dyn Router>,
    config: Config,
    memory: Option<Arc<dyn Memory>>,
}

impl SubagentSpawn {
    pub fn new(router: Arc<dyn Router>, config: Config) -> Self {
        Self {
            router,
            config,
            memory: None,
        }
    }

    pub fn with_memory(mut self, memory: Arc<dyn Memory>) -> Self {
        self.memory = Some(memory);
        self
    }
}

#[async_trait]
impl Tool for SubagentSpawn {
    fn name(&self) -> &str {
        "subagent_spawn"
    }
    fn description(&self) -> &str {
        "Spawn a child agent to handle a subtask independently"
    }
    fn schema(&self) -> serde_json::Value {
        json!({
            "type": "object",
            "properties": {
                "task": { "type": "string", "description": "Subtask description" },
                "role": { "type": "string", "description": "Role for the subagent (e.g. tester, researcher, reviewer)" },
                "model": { "type": "string", "description": "Optional: provider:model or provider/model for the subagent" },
                "permission_mode": { "type": "string", "description": "Optional: read-only, plan, supervised, trusted, autonomous, emergency-stop" },
                "tools": { "type": "array", "items": { "type": "string" }, "description": "Optional explicit allowed tool patterns" },
                "disallowed_tools": { "type": "array", "items": { "type": "string" }, "description": "Optional denied tool patterns for this subagent" }
            },
            "required": ["task"]
        })
    }
    fn risk(&self) -> RiskLevel {
        RiskLevel::Exec
    }
    async fn call(&self, args: serde_json::Value, _ctx: &ToolCtx) -> anyhow::Result<ToolResult> {
        let task_desc = args["task"].as_str().unwrap_or("");
        let role = args["role"].as_str().unwrap_or("helper");
        let mut child_config = self.config.clone();
        if let Some(model_ref) = args["model"].as_str() {
            if let Some((provider, model)) = parse_model_ref(model_ref) {
                child_config.forced_model = Some((provider.clone(), model.clone()));
                for tier in ["trivial", "small", "medium", "hard", "vision"] {
                    child_config
                        .routing
                        .policy
                        .insert(tier.to_string(), provider.clone());
                }
                child_config
                    .providers
                    .entry(provider)
                    .or_insert_with(|| crate::config::ProviderConfig {
                        adapter: "openai-compatible".into(),
                        base_url: None,
                        models: vec![],
                        api_key_env: None,
                    })
                    .models = vec![model];
            }
        }
        if let Some(mode) = args["permission_mode"]
            .as_str()
            .and_then(PermissionMode::parse)
        {
            child_config.defaults.autonomy = mode.autonomy_level();
            child_config.permissions.mode = mode;
        }
        for tool in string_array(&args["tools"]) {
            if !child_config.permissions.tools.allow.contains(&tool) {
                child_config.permissions.tools.allow.push(tool);
            }
        }
        for tool in string_array(&args["disallowed_tools"]) {
            if !child_config.permissions.tools.deny.contains(&tool) {
                child_config.permissions.tools.deny.push(tool);
            }
        }

        let (tx, mut rx) = mpsc::unbounded_channel();

        let task = Task {
            description: task_desc.to_string(),
            context: vec![],
        };

        // Build a fresh child engine for this subagent.
        let mut child = Engine::new(self.router.clone(), child_config).with_identity(Identity {
            name: role.to_string(),
            role: role.to_string(),
            personality: format!("Focused {} subagent. Be concise and return evidence.", role),
        });
        if let Some(mem) = &self.memory {
            child = child.with_memory(mem.clone());
        }
        let engine = Arc::new(child);

        let handle = tokio::spawn(async move {
            match engine.drive(task, tx).await {
                Ok(outcome) => outcome,
                Err(e) => crate::event::OutcomeSummary {
                    status: format!("error: {}", e),
                    diffs: vec![],
                    cost_usd: 0.0,
                    tokens: crate::event::TokenUsage {
                        input: 0,
                        output: 0,
                    },
                },
            }
        });

        // Collect subagent output
        let mut output = String::new();
        while let Some(event) = rx.recv().await {
            match &event {
                Event::ThinkingDelta { text, .. } => {
                    output.push_str(text);
                }
                Event::AgentStatus { note, .. } => {
                    output.push_str(&format!("\n[{}]", note));
                }
                Event::RunFinished { outcome, .. } => {
                    output.push_str(&format!(
                        "\n[Subagent done: {} | ${:.4}]",
                        outcome.status, outcome.cost_usd
                    ));
                }
                Event::Error { message, .. } => {
                    output.push_str(&format!("\n[Error: {}]", message));
                }
                _ => {}
            }
        }

        let outcome = handle
            .await
            .unwrap_or_else(|e| crate::event::OutcomeSummary {
                status: format!("subagent panicked: {}", e),
                diffs: vec![],
                cost_usd: 0.0,
                tokens: crate::event::TokenUsage {
                    input: 0,
                    output: 0,
                },
            });

        Ok(ToolResult::ok(vec![Block::Text(format!(
            "Subagent '{}' completed.\nStatus: {}\nOutput:\n{}",
            role, outcome.status, output
        ))]))
    }
}

fn string_array(value: &serde_json::Value) -> Vec<String> {
    value
        .as_array()
        .map(|items| {
            items
                .iter()
                .filter_map(|item| item.as_str())
                .map(str::trim)
                .filter(|item| !item.is_empty())
                .map(str::to_string)
                .collect()
        })
        .unwrap_or_default()
}

fn parse_model_ref(model_ref: &str) -> Option<(String, String)> {
    let model_ref = model_ref.trim();
    if model_ref.is_empty() {
        return None;
    }
    if let Some((provider, model)) = model_ref.split_once(':') {
        let provider = provider.trim();
        let model = model.trim();
        if !provider.is_empty() && !model.is_empty() {
            return Some((provider.to_string(), model.to_string()));
        }
    }
    if let Some((provider, rest)) = model_ref.split_once('/') {
        let provider = provider.trim();
        if !provider.is_empty() {
            return Some((provider.to_string(), model_ref.to_string()));
        }
        if !rest.trim().is_empty() {
            return Some(("custom".into(), model_ref.to_string()));
        }
    }
    Some(("custom".into(), model_ref.to_string()))
}

// ─── Persistent Python kernel ─────────────────────────────────────────────────
// A long-lived `python3` process that keeps a single globals dict across calls,
// so variables/imports/state persist between tool invocations (§15). A small
// driver loop reads one JSON request per line, execs it capturing stdout, and
// emits a JSON result terminated by a unique sentinel.

use std::io::{BufRead, BufReader, Write};
use std::process::{Child, ChildStdin, ChildStdout};
use std::sync::Mutex;

const KERNEL_SENTINEL: &str = "__SPARROW_KERNEL_END__";

const KERNEL_DRIVER: &str = r#"
import sys, io, json, contextlib, traceback
_g = {"__name__": "__sparrow__"}
SENT = "__SPARROW_KERNEL_END__"
for line in sys.stdin:
    line = line.strip()
    if not line:
        continue
    try:
        req = json.loads(line)
    except Exception:
        print(json.dumps({"out": "", "err": "bad request"}), flush=True)
        print(SENT, flush=True)
        continue
    code = req.get("code", "")
    buf = io.StringIO()
    err = ""
    try:
        with contextlib.redirect_stdout(buf):
            exec(compile(code, "<sparrow>", "exec"), _g)
    except Exception:
        err = traceback.format_exc()
    print(json.dumps({"out": buf.getvalue(), "err": err}), flush=True)
    print(SENT, flush=True)
"#;

struct Kernel {
    child: Child,
    stdin: ChildStdin,
    stdout: BufReader<ChildStdout>,
}

pub struct PythonRpc {
    kernel: Mutex<Option<Kernel>>,
    python_bin: String,
}

impl PythonRpc {
    pub fn new() -> Self {
        // Prefer python3, fall back to python (Windows often only has `python`).
        let python_bin = if which_python("python3") {
            "python3".to_string()
        } else {
            "python".to_string()
        };
        Self {
            kernel: Mutex::new(None),
            python_bin,
        }
    }

    fn ensure_kernel(&self, kernel: &mut Option<Kernel>) -> anyhow::Result<()> {
        if kernel.is_some() {
            return Ok(());
        }
        use std::process::{Command, Stdio};
        let mut child = Command::new(&self.python_bin)
            .arg("-u")
            .arg("-c")
            .arg(KERNEL_DRIVER)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()?;
        let stdin = child
            .stdin
            .take()
            .ok_or_else(|| anyhow::anyhow!("no stdin"))?;
        let stdout = BufReader::new(
            child
                .stdout
                .take()
                .ok_or_else(|| anyhow::anyhow!("no stdout"))?,
        );
        *kernel = Some(Kernel {
            child,
            stdin,
            stdout,
        });
        Ok(())
    }
}

fn which_python(bin: &str) -> bool {
    std::process::Command::new(bin)
        .arg("--version")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

#[async_trait]
impl Tool for PythonRpc {
    fn name(&self) -> &str {
        "python_rpc"
    }
    fn description(&self) -> &str {
        "Execute Python in a PERSISTENT kernel — variables, imports and state persist across calls."
    }
    fn schema(&self) -> serde_json::Value {
        json!({
            "type": "object",
            "properties": {
                "code": { "type": "string", "description": "Python code to execute in the persistent kernel" }
            },
            "required": ["code"]
        })
    }
    fn risk(&self) -> RiskLevel {
        RiskLevel::Exec
    }
    async fn call(&self, args: serde_json::Value, _ctx: &ToolCtx) -> anyhow::Result<ToolResult> {
        let code = args["code"].as_str().unwrap_or("").to_string();

        // Kernel IO is blocking; run on a blocking thread without holding the
        // lock across an await. We take the kernel out, use it, put it back.
        let mut guard = self.kernel.lock().unwrap();
        if let Err(e) = self.ensure_kernel(&mut guard) {
            return Ok(ToolResult::error(format!(
                "Python kernel unavailable ({}). Is '{}' installed?",
                e, self.python_bin
            )));
        }
        let kernel = guard.as_mut().unwrap();

        // Send request as a single JSON line.
        let req = serde_json::json!({ "code": code }).to_string();
        if writeln!(kernel.stdin, "{}", req)
            .and_then(|_| kernel.stdin.flush())
            .is_err()
        {
            *guard = None; // kernel died; drop it so it respawns next time
            return Ok(ToolResult::error(
                "Python kernel write failed (kernel reset)",
            ));
        }

        // Read lines until the sentinel; the line before it is the JSON result.
        let mut last_json = String::new();
        loop {
            let mut line = String::new();
            match kernel.stdout.read_line(&mut line) {
                Ok(0) => {
                    *guard = None;
                    return Ok(ToolResult::error("Python kernel closed unexpectedly"));
                }
                Ok(_) => {
                    let trimmed = line.trim_end();
                    if trimmed == KERNEL_SENTINEL {
                        break;
                    }
                    last_json = trimmed.to_string();
                }
                Err(e) => {
                    *guard = None;
                    return Ok(ToolResult::error(format!(
                        "Python kernel read error: {}",
                        e
                    )));
                }
            }
        }

        let parsed: serde_json::Value = serde_json::from_str(&last_json)
            .unwrap_or_else(|_| serde_json::json!({"out": last_json, "err": ""}));
        let out = parsed["out"].as_str().unwrap_or("");
        let err = parsed["err"].as_str().unwrap_or("");
        if !err.is_empty() {
            Ok(ToolResult::ok(vec![Block::Text(format!("{}{}", out, err))]))
        } else {
            Ok(ToolResult::text(out.to_string()))
        }
    }
}

impl Drop for PythonRpc {
    fn drop(&mut self) {
        if let Ok(mut g) = self.kernel.lock() {
            if let Some(mut k) = g.take() {
                let _ = k.child.kill();
            }
        }
    }
}