dirge-agent 0.13.7

Minimalistic coding agent written in Rust, optimized for memory footprint and performance
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
//! `dirge mcp` — run dirge itself as an MCP server so another agent
//! (e.g. Claude Code) can delegate implementation tasks to dirge and
//! review them: the caller does high-level planning/architecture and
//! hands the implementation details to dirge.
//!
//! The server speaks MCP over stdio and keeps a **persistent per-project
//! session**: each `delegate` runs against the same on-disk dirge session
//! (so dirge accumulates context across tasks), and `new_session` rotates
//! to a fresh one when the caller moves to a new task/thread. The current
//! session id is remembered in `<project>/.dirge/mcp_current_session.json`
//! so it survives a server restart.
//!
//! v1 executes each delegation by spawning `dirge -p --session <id>
//! --accept-all --output-format json <task>` as a child process — robust
//! and isolated. (A warm in-process executor that keeps the LSP/MCP/
//! semantic managers hot across delegations is a planned follow-up; it
//! swaps only the executor, not this MCP API.)

use std::path::{Path, PathBuf};
use std::sync::Arc;

use rmcp::ErrorData;
use rmcp::ServiceExt;
use rmcp::handler::server::ServerHandler;
use rmcp::handler::server::tool::ToolRouter;
use rmcp::handler::server::wrapper::Parameters;
use rmcp::model::{
    CallToolResult, Content, Implementation, InitializeResult, ServerCapabilities, ServerInfo,
};
use rmcp::transport::stdio;
use rmcp::{schemars, tool, tool_handler, tool_router};
use serde::Deserialize;
use serde_json::json;
use tokio::sync::Mutex;

/// Default per-delegation turn cap so a `delegate` call always returns
/// within bounds (the caller can continue with another delegation).
const DEFAULT_MAX_TURNS: u32 = 30;

#[derive(Clone)]
pub struct DirgeMcp {
    state: Arc<Mutex<State>>,
    // Read by the `#[tool_handler]`-generated `call_tool`/`list_tools`,
    // which the dead-code pass can't see through the macro — verified live
    // (the server routes all four tools). Silence the false positive.
    #[allow(dead_code)]
    tool_router: ToolRouter<DirgeMcp>,
}

struct State {
    /// The project the server operates in (its cwd at launch).
    project_dir: PathBuf,
    /// Path to the dirge binary (for spawning `-p` children).
    exe: PathBuf,
    /// Current session id — what `delegate` resumes/extends.
    session_id: String,
    /// Optional human label for the current session.
    label: Option<String>,
    /// Model override for delegated work (`--model`), if any.
    model: Option<String>,
    /// Sandbox mode for delegated bash (`--sandbox`), if any.
    sandbox: Option<String>,
}

#[derive(Deserialize, schemars::JsonSchema)]
struct DelegateArgs {
    /// The implementation task for dirge to carry out, in plain language,
    /// with any constraints (target files, what to avoid, how to verify).
    /// dirge works in the project, can edit files and run commands, and
    /// returns a summary of what it did.
    task: String,
    /// Start a fresh session before running — use this when moving to a
    /// new task/thread so dirge isn't anchored to the previous context.
    #[serde(default)]
    new_session: bool,
    /// Optional label for the new session (only when `new_session`).
    #[serde(default)]
    session_label: Option<String>,
    /// Cap on dirge's internal turns for this delegation (default 30).
    /// If hit, status is `max_turns` and you can continue with another
    /// `delegate` in the same session.
    #[serde(default)]
    max_turns: Option<u32>,
}

#[derive(Deserialize, schemars::JsonSchema)]
struct NewSessionArgs {
    /// Optional human label for the new session.
    #[serde(default)]
    label: Option<String>,
}

#[tool_handler]
impl ServerHandler for DirgeMcp {
    fn get_info(&self) -> ServerInfo {
        InitializeResult::new(ServerCapabilities::builder().enable_tools().build())
            .with_server_info(Implementation::new("dirge", env!("CARGO_PKG_VERSION")))
            .with_instructions(
                "dirge is a coding agent you delegate implementation work to. Call `delegate` \
                 with a task — dirge edits files and runs commands in this project on a \
                 persistent session, then returns a summary plus the files it changed for you \
                 to review. Call `delegate` again in the same session to ask for a fix (it keeps \
                 the context). Set new_session=true (or call `new_session`) when moving to a new \
                 task/thread so it isn't anchored to the old one. Use `session_info` / \
                 `list_sessions` for orientation.",
            )
    }
}

#[tool_router]
impl DirgeMcp {
    fn new(state: State) -> Self {
        Self {
            state: Arc::new(Mutex::new(state)),
            tool_router: Self::tool_router(),
        }
    }

    #[tool(
        description = "Delegate an implementation task to dirge. dirge works in the project \
            (editing files, running commands) on its persistent session and returns a summary \
            plus the list of files it changed, so you can review the result and either accept \
            it, ask for a fix (call delegate again — same session keeps the context), or move \
            on. Set new_session=true when starting a new task/thread."
    )]
    async fn delegate(
        &self,
        Parameters(args): Parameters<DelegateArgs>,
    ) -> Result<CallToolResult, ErrorData> {
        let max_turns = args.max_turns.unwrap_or(DEFAULT_MAX_TURNS);
        // Snapshot the session id + run params under the lock, optionally
        // rotating first; release the lock before the (long) child run.
        let (exe, project_dir, session_id, model, sandbox) = {
            let mut st = self.state.lock().await;
            if args.new_session {
                st.session_id = new_session_id();
                st.label = args.session_label.clone();
                if let Err(e) = persist_pointer(&st.project_dir, &st.session_id, &st.label) {
                    return Ok(tool_err(format!("failed to persist new session: {e}")));
                }
            }
            (
                st.exe.clone(),
                st.project_dir.clone(),
                st.session_id.clone(),
                st.model.clone(),
                st.sandbox.clone(),
            )
        };

        let before = git_status_set(&project_dir);
        let run = run_delegation(
            &exe,
            &project_dir,
            &session_id,
            &args.task,
            max_turns,
            model.as_deref(),
            sandbox.as_deref(),
        )
        .await;
        let after = git_status_set(&project_dir);
        let files_changed = changed_paths(&before, &after);

        match run {
            Ok(env) => {
                let result = json!({
                    "session_id": session_id,
                    "status": env.status,
                    "summary": env.summary,
                    "files_changed": files_changed,
                    "turns": env.turns,
                    "duration_ms": env.duration_ms,
                });
                Ok(tool_json(&result))
            }
            Err(e) => Ok(tool_err(format!("delegation failed to run: {e}"))),
        }
    }

    #[tool(
        description = "Start a fresh dirge session (new task/thread) without immediately \
            delegating. Returns the new session id; subsequent delegate calls use it."
    )]
    async fn new_session(
        &self,
        Parameters(args): Parameters<NewSessionArgs>,
    ) -> Result<CallToolResult, ErrorData> {
        let mut st = self.state.lock().await;
        st.session_id = new_session_id();
        st.label = args.label.clone();
        if let Err(e) = persist_pointer(&st.project_dir, &st.session_id, &st.label) {
            return Ok(tool_err(format!("failed to persist new session: {e}")));
        }
        Ok(tool_json(&json!({
            "session_id": st.session_id,
            "label": st.label,
        })))
    }

    #[tool(
        description = "Info about the current dirge session: its id, label, the project dir, \
            message count, last activity, and the model dirge uses for delegated work."
    )]
    async fn session_info(&self) -> Result<CallToolResult, ErrorData> {
        let st = self.state.lock().await;
        let (message_count, last_active) =
            match crate::session::storage::load_session(&st.session_id) {
                Ok(s) => (s.messages.len(), Some(s.updated_at.to_string())),
                Err(_) => (0, None),
            };
        Ok(tool_json(&json!({
            "session_id": st.session_id,
            "label": st.label,
            "project_dir": st.project_dir.to_string_lossy(),
            "message_count": message_count,
            "last_active": last_active,
            "model": st.model,
            "sandbox": st.sandbox,
        })))
    }

    #[tool(
        description = "List recent dirge sessions in this project (id, last activity, and a \
            one-line preview) so you can resume a past task thread by passing its id."
    )]
    async fn list_sessions(&self) -> Result<CallToolResult, ErrorData> {
        let sessions = crate::session::storage::find_recent_sessions(20).unwrap_or_default();
        let list: Vec<_> = sessions
            .iter()
            .map(|s| {
                json!({
                    "id": s.id,
                    "last_active": s.updated_at,
                    "messages": s.messages.len(),
                    "preview": crate::ui::events::session_preview(s, 80),
                })
            })
            .collect();
        Ok(tool_json(&json!({ "sessions": list })))
    }
}

/// The slice of the headless `--output-format json` envelope we surface.
struct Envelope {
    status: String,
    summary: String,
    turns: u64,
    duration_ms: u64,
}

/// Spawn `dirge -p --session <id> --accept-all --output-format json <task>`
/// in the project and parse its result envelope.
async fn run_delegation(
    exe: &Path,
    project_dir: &Path,
    session_id: &str,
    task: &str,
    max_turns: u32,
    model: Option<&str>,
    sandbox: Option<&str>,
) -> anyhow::Result<Envelope> {
    let mut cmd = tokio::process::Command::new(exe);
    cmd.current_dir(project_dir)
        .arg("--print")
        .arg("--accept-all")
        .arg("--session")
        .arg(session_id)
        .arg("--output-format")
        .arg("json")
        .arg("--max-agent-turns")
        .arg(max_turns.to_string());
    if let Some(m) = model {
        cmd.arg("--model").arg(m);
    }
    if let Some(s) = sandbox {
        cmd.arg("--sandbox").arg(s);
    }
    // `--` so a task that begins with `-` isn't parsed as a flag.
    cmd.arg("--").arg(task);
    cmd.stdin(std::process::Stdio::null());

    let out = cmd
        .output()
        .await
        .map_err(|e| anyhow::anyhow!("spawn dirge: {e}"))?;
    let stdout = String::from_utf8_lossy(&out.stdout);
    // `--output-format json` prints a single result object. Be tolerant of
    // any stray leading lines by taking the last non-empty line that parses.
    let env_val = stdout
        .lines()
        .rev()
        .find_map(|l| serde_json::from_str::<serde_json::Value>(l.trim()).ok())
        .ok_or_else(|| {
            anyhow::anyhow!(
                "dirge produced no JSON result (exit {}). stderr: {}",
                out.status,
                String::from_utf8_lossy(&out.stderr).trim()
            )
        })?;
    Ok(Envelope {
        status: env_val
            .get("subtype")
            .and_then(|v| v.as_str())
            .unwrap_or("error")
            .to_string(),
        summary: env_val
            .get("result")
            .and_then(|v| v.as_str())
            .unwrap_or_default()
            .to_string(),
        turns: env_val
            .get("num_turns")
            .and_then(|v| v.as_u64())
            .unwrap_or(0),
        duration_ms: env_val
            .get("duration_ms")
            .and_then(|v| v.as_u64())
            .unwrap_or(0),
    })
}

/// `git status --porcelain` as a set of `"XY path"` lines. Empty (not an
/// error) when the project isn't a git repo or git is unavailable.
/// Signature for the path under each `git status --porcelain` entry:
/// `path -> (size, mtime-nanos)`. Empty when the dir isn't a git repo.
///
/// We key on a content signature, not just the porcelain line, so a file
/// that was ALREADY dirty before a delegation (e.g. created untracked by
/// an earlier delegation in the same session) and is edited again is still
/// attributed — a plain porcelain-line diff misses that, since `?? path`
/// is identical before and after.
fn git_status_set(dir: &Path) -> std::collections::HashMap<String, (u64, i128)> {
    let out = std::process::Command::new("git")
        .current_dir(dir)
        .args(["status", "--porcelain"])
        .output();
    let mut map = std::collections::HashMap::new();
    if let Ok(o) = out
        && o.status.success()
    {
        for line in String::from_utf8_lossy(&o.stdout).lines() {
            let path = porcelain_path(line);
            if path.is_empty() {
                continue;
            }
            let sig = std::fs::metadata(dir.join(&path))
                .ok()
                .map(|m| (m.len(), mtime_nanos(&m)))
                .unwrap_or((0, 0));
            map.insert(path, sig);
        }
    }
    map
}

/// The path from a porcelain line: strip the 3-char `XY ` status prefix,
/// and for a rename/copy (`old -> new`) attribute the change to `new`.
fn porcelain_path(line: &str) -> String {
    let p = line.get(3..).unwrap_or("").trim();
    match p.find(" -> ") {
        Some(i) => p[i + 4..].to_string(),
        None => p.to_string(),
    }
}

fn mtime_nanos(m: &std::fs::Metadata) -> i128 {
    m.modified()
        .ok()
        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
        .map(|d| d.as_nanos() as i128)
        .unwrap_or(0)
}

/// Paths dirge touched during the run: dirty afterward AND either newly
/// dirty or with a changed `(size, mtime)` signature vs before — so edits
/// to an already-dirty file are caught, while the caller's pre-existing
/// untouched changes are not falsely attributed.
fn changed_paths(
    before: &std::collections::HashMap<String, (u64, i128)>,
    after: &std::collections::HashMap<String, (u64, i128)>,
) -> Vec<String> {
    let mut v: Vec<String> = after
        .iter()
        .filter(|(path, sig)| before.get(*path) != Some(*sig))
        .map(|(path, _)| path.clone())
        .collect();
    v.sort();
    v.dedup();
    v
}

fn new_session_id() -> String {
    format!("mcp-{}", crate::agent::runner::uuid_v4_simple())
}

fn pointer_path(project_dir: &Path) -> PathBuf {
    project_dir.join(".dirge").join("mcp_current_session.json")
}

fn persist_pointer(project_dir: &Path, id: &str, label: &Option<String>) -> anyhow::Result<()> {
    let path = pointer_path(project_dir);
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::write(&path, json!({ "id": id, "label": label }).to_string())?;
    Ok(())
}

/// Load the remembered session pointer, or mint + persist a new one.
fn load_or_create_pointer(project_dir: &Path) -> anyhow::Result<(String, Option<String>)> {
    let path = pointer_path(project_dir);
    if let Ok(bytes) = std::fs::read(&path)
        && let Ok(v) = serde_json::from_slice::<serde_json::Value>(&bytes)
        && let Some(id) = v.get("id").and_then(|x| x.as_str())
    {
        let label = v
            .get("label")
            .and_then(|x| x.as_str())
            .map(|s| s.to_string());
        return Ok((id.to_string(), label));
    }
    let id = new_session_id();
    persist_pointer(project_dir, &id, &None)?;
    Ok((id, None))
}

fn tool_json(value: &serde_json::Value) -> CallToolResult {
    let body = serde_json::to_string_pretty(value).unwrap_or_else(|_| value.to_string());
    CallToolResult::success(vec![Content::text(body)])
}

fn tool_err(msg: String) -> CallToolResult {
    CallToolResult::error(vec![Content::text(msg)])
}

/// Entry point for `dirge mcp`. Runs the MCP server over stdio until the
/// transport closes (the client disconnects).
pub async fn serve(
    _cli: &crate::cli::Cli,
    _cfg: &crate::config::Config,
    model: Option<String>,
    sandbox: Option<String>,
) -> anyhow::Result<()> {
    let project_dir = std::env::current_dir()?;
    let exe = std::env::current_exe()?;
    let (session_id, label) = load_or_create_pointer(&project_dir)?;

    let server = DirgeMcp::new(State {
        project_dir,
        exe,
        session_id,
        label,
        model,
        sandbox,
    });

    let service = server
        .serve(stdio())
        .await
        .map_err(|e| anyhow::anyhow!("MCP server failed to start: {e}"))?;
    service
        .waiting()
        .await
        .map_err(|e| anyhow::anyhow!("MCP server error: {e}"))?;
    Ok(())
}

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

    #[test]
    fn changed_paths_detects_new_and_modified_not_untouched() {
        use std::collections::HashMap;
        // src/a.rs dirty before with sig (10,1). After: a.rs unchanged,
        // b.rs newly dirty, c.rs already dirty but its signature changed
        // (further edited — the case the old line-diff missed).
        let before: HashMap<String, (u64, i128)> =
            [("src/a.rs".into(), (10, 1)), ("c.rs".into(), (5, 1))].into();
        let after: HashMap<String, (u64, i128)> = [
            ("src/a.rs".into(), (10, 1)), // untouched → not reported
            ("src/b.rs".into(), (3, 2)),  // new → reported
            ("c.rs".into(), (8, 9)),      // sig changed → reported
        ]
        .into();
        let changed = changed_paths(&before, &after);
        assert_eq!(changed, vec!["c.rs".to_string(), "src/b.rs".to_string()]);
    }

    #[test]
    fn porcelain_path_strips_status_and_handles_rename() {
        assert_eq!(porcelain_path("?? src/b.rs"), "src/b.rs");
        assert_eq!(porcelain_path(" M src/a.rs"), "src/a.rs");
        assert_eq!(porcelain_path("R  old.rs -> new.rs"), "new.rs");
    }

    #[test]
    fn new_session_id_is_prefixed() {
        let id = new_session_id();
        assert!(id.starts_with("mcp-"), "got {id}");
        assert!(id.len() > 4);
    }

    #[test]
    fn pointer_round_trips_and_persists() {
        let dir = std::env::temp_dir().join(format!(
            "dirge-mcp-ptr-{}",
            crate::agent::runner::uuid_v4_simple()
        ));
        std::fs::create_dir_all(&dir).unwrap();

        // First call mints + persists a fresh pointer.
        let (id1, label1) = load_or_create_pointer(&dir).unwrap();
        assert!(id1.starts_with("mcp-"));
        assert_eq!(label1, None);

        // Second call returns the SAME id (remembered across "restarts").
        let (id2, _) = load_or_create_pointer(&dir).unwrap();
        assert_eq!(id1, id2);

        // An explicit persist with a label round-trips.
        persist_pointer(&dir, "mcp-fixed", &Some("auth".to_string())).unwrap();
        let (id3, label3) = load_or_create_pointer(&dir).unwrap();
        assert_eq!(id3, "mcp-fixed");
        assert_eq!(label3, Some("auth".to_string()));

        let _ = std::fs::remove_dir_all(&dir);
    }
}