calmrun 0.1.0

A machine-wide queue for heavy commands, so parallel coding agents and terminals don't freeze the computer
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
//! `calmrun mcp`: an MCP server over stdio (newline-delimited JSON-RPC 2.0).
//!
//! Tools are entries in [`TOOLS`]: a name, a schema and a handler. `run_task` waits in the
//! same queue as the CLI (through [`crate::queue::run`]) on its own thread, so status calls
//! answer while a task waits. Adding a tool = one entry plus its handler.

use crate::error::Error;
use crate::queue::{self, Ctx, Notify, Request};
use crate::{history, runner, shell, status};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::io::{self, BufRead, Write};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};

/// Protocol versions we accept from the client; we answer with the client's if listed.
const SUPPORTED_VERSIONS: &[&str] = &["2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05"];
/// Answer for a client asking for a version we do not list.
const DEFAULT_VERSION: &str = "2025-06-18";
/// Lines of output returned by `run_task`.
const TAIL_LINES: usize = 200;
/// Log files kept in the state directory.
const KEEP_LOGS: usize = 200;
/// A progress notification is repeated at least this often while the line stays the same.
const PROGRESS_EVERY: Duration = Duration::from_secs(10);
/// JSON-RPC error codes.
const PARSE_ERROR: i64 = -32700;
const METHOD_NOT_FOUND: i64 = -32601;
const INVALID_PARAMS: i64 = -32602;

const INSTRUCTIONS: &str = "Use run_task for heavy commands (tests, type checks, linters, builds, \
Playwright, docker compose, pytest, cargo/gradle builds) so they wait their turn in the \
machine-wide queue instead of overloading the computer. Use queue_status to see what is running.";

type Out = Arc<Mutex<io::Stdout>>;

/// One MCP tool.
struct Tool {
    name: &'static str,
    schema: fn() -> Value,
    /// Long-running tools run on their own thread and can be cancelled.
    background: bool,
    call: fn(&Call) -> Value,
}

/// What a tool handler gets.
struct Call<'a> {
    out: &'a Out,
    args: &'a Value,
    progress_token: Option<Value>,
    cancel: Arc<AtomicBool>,
}

const TOOLS: &[Tool] = &[
    Tool {
        name: "run_task",
        schema: run_task_schema,
        background: true,
        call: run_task,
    },
    Tool {
        name: "queue_status",
        schema: queue_status_schema,
        background: false,
        call: queue_status,
    },
    Tool {
        name: "bump",
        schema: bump_schema,
        background: false,
        call: bump,
    },
];

fn send(out: &Out, v: &Value) {
    let mut line = v.to_string();
    line.push('\n');
    if let Ok(mut o) = out.lock() {
        let _ = o.write_all(line.as_bytes());
        let _ = o.flush();
    }
}

fn reply(out: &Out, id: &Value, result: Value) {
    send(out, &json!({"jsonrpc": "2.0", "id": id, "result": result}));
}

fn reply_error(out: &Out, id: &Value, code: i64, message: &str) {
    send(
        out,
        &json!({"jsonrpc": "2.0", "id": id, "error": {"code": code, "message": message}}),
    );
}

fn text_result(text: String, is_error: bool, structured: Option<Value>) -> Value {
    let mut r = json!({"content": [{"type": "text", "text": text}], "isError": is_error});
    if let Some(s) = structured {
        r["structuredContent"] = s;
    }
    r
}

fn error_result(e: &Error) -> Value {
    text_result(e.to_string(), true, None)
}

const QUEUE_PROP: &str = "A separate queue id instead of the machine-wide one";

fn run_task_schema() -> Value {
    json!({
        "name": "run_task",
        "title": "Run a heavy command through the calmrun queue",
        "description": "Run a heavy command (tests, type checks, linters, builds, Playwright, docker \
            compose, pytest, cargo/gradle builds) through the machine-wide calmrun queue, so parallel \
            agents do not overload this computer. Waits for its turn, runs the command at low \
            priority, and returns the exit code, wait and run time, the last 200 lines of output and \
            the path of the full log. The command runs in the platform shell (sh on macOS/Linux, cmd \
            on Windows) when it uses shell syntax; plain words run the program directly.",
        "inputSchema": {
            "type": "object",
            "properties": {
                "command": {"type": "string", "description": "The command line, e.g. \"npx vitest run\""},
                "cwd": {"type": "string", "description": "Working directory (default: the server's)"},
                "kind": {"type": "string", "description": "Job kind; detected from the command when omitted"},
                "cost": {"type": "integer", "minimum": 1, "description": "Budget points; default by kind"},
                "label": {"type": "string", "description": "Name shown in the queue"},
                "timeout_s": {"type": "number", "exclusiveMinimum": 0, "description": "Stop the command after this many seconds (exit 124)"},
                "prio": {"type": "string", "enum": ["normal", "high"]},
                "wait_timeout_s": {"type": "number", "exclusiveMinimum": 0, "description": "Give up waiting after this many seconds; the result carries a ticket that keeps the place in line for 10 minutes"},
                "ticket": {"type": "integer", "description": "Ticket from an earlier wait timeout: resume that place in line"},
                "needs": {"type": "array", "items": {"type": "string"}, "description": "Named resources to hold a slot of, e.g. [\"db\"]"},
                "queue": {"type": "string", "description": QUEUE_PROP}
            },
            "required": ["command"]
        }
    })
}

fn queue_status_schema() -> Value {
    json!({
        "name": "queue_status",
        "title": "Show the calmrun queue",
        "description": "Running and waiting jobs (label, kind, cost, pid, cwd, time), the point \
            budget, free RAM and warnings about jobs that have waited a long time.",
        "inputSchema": {"type": "object", "properties": {"queue": {"type": "string", "description": QUEUE_PROP}}}
    })
}

fn bump_schema() -> Value {
    json!({
        "name": "bump",
        "title": "Move waiting jobs to the front",
        "description": "Move waiting jobs whose label contains the given text to the front of the queue.",
        "inputSchema": {
            "type": "object",
            "properties": {
                "label": {"type": "string", "description": "Part of the label (case-insensitive)"},
                "queue": {"type": "string", "description": QUEUE_PROP}
            },
            "required": ["label"]
        }
    })
}

/// Whether a command line needs the shell to mean what it says.
fn needs_shell(cmd: &str) -> bool {
    cmd.contains(runner::SHELL_CHARS)
}

/// A command line split for spawning.
#[derive(Debug, PartialEq)]
pub struct CommandLine {
    pub argv: Vec<String>,
    /// Run `argv[0]` through the platform shell.
    pub shell: bool,
    /// Leading `VAR=value` words, set for the command instead of passed to a shell (so
    /// `CI=1 npm test` works on Windows too, where cmd has no such syntax).
    pub env: Vec<(String, String)>,
}

/// Split a command line: plain words run the program directly, shell syntax runs in the shell.
pub fn command_argv(cmd: &str) -> CommandLine {
    if needs_shell(cmd) {
        return CommandLine {
            argv: vec![cmd.trim().to_string()],
            shell: true,
            env: Vec::new(),
        };
    }
    let mut words = shell::tokenize(cmd);
    let assignments = words.iter().take_while(|w| is_assignment(w)).count();
    let env = words
        .drain(..assignments)
        .filter_map(|w| w.split_once('=').map(|(k, v)| (k.to_string(), v.to_string())))
        .collect();
    CommandLine {
        argv: words,
        shell: false,
        env,
    }
}

fn is_assignment(w: &str) -> bool {
    w.split_once('=').is_some_and(|(k, _)| {
        !k.is_empty()
            && !k.starts_with(|c: char| c.is_ascii_digit())
            && k.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
    })
}

/// Sends `notifications/progress` while a task waits, if the client gave a token.
struct McpNotify<'a> {
    out: &'a Out,
    token: Option<Value>,
    count: u64,
    last_line: String,
    last_sent: Option<Instant>,
    started: Instant,
}

impl McpNotify<'_> {
    fn progress(&mut self, message: String) {
        let Some(token) = &self.token else {
            return;
        };
        self.count += 1;
        let params = json!({"progressToken": token, "progress": self.count, "message": message});
        send(
            self.out,
            &json!({"jsonrpc": "2.0", "method": "notifications/progress", "params": params}),
        );
        self.last_sent = Some(Instant::now());
    }
}

impl Notify for McpNotify<'_> {
    fn wants_line(&self) -> bool {
        self.token.is_some()
    }

    fn waiting(&mut self, line: &str) {
        let due = self.last_sent.is_none_or(|t| t.elapsed() >= PROGRESS_EVERY);
        if line != self.last_line || due {
            self.last_line = line.to_string();
            let waited = history::human_ms(self.started.elapsed().as_millis() as u64);
            self.progress(format!("{line} (waited {waited})"));
        }
    }

    fn started(&mut self, wait_ms: u64) {
        self.progress(format!("started after waiting {}", history::human_ms(wait_ms)));
    }

    /// Notes go to stderr: stdout carries the protocol.
    fn note(&mut self, line: &str) {
        eprintln!("{line}");
    }
}

/// Settings for the queue named in the arguments.
fn ctx_for(args: &Value) -> Result<Ctx, Value> {
    let queue = args
        .get("queue")
        .and_then(Value::as_str)
        .filter(|q| !q.is_empty())
        .map(String::from);
    Ctx::load()
        .and_then(|c| c.with_queue(queue))
        .map_err(|e| error_result(&e))
}

fn secs(args: &Value, key: &str) -> Option<Duration> {
    args.get(key)
        .and_then(Value::as_f64)
        .filter(|t| *t > 0.0)
        .map(Duration::from_secs_f64)
}

fn string(args: &Value, key: &str) -> Option<String> {
    args.get(key).and_then(Value::as_str).map(String::from)
}

static LOG_SEQ: AtomicU64 = AtomicU64::new(0);

fn request_for(args: &Value, command: &str, log: PathBuf, cancel: Arc<AtomicBool>) -> Request {
    let line = command_argv(command);
    Request {
        argv: line.argv,
        shell: line.shell,
        cwd: string(args, "cwd").map(PathBuf::from),
        kind: string(args, "kind"),
        cost: args.get("cost").and_then(Value::as_u64).map(|c| c.max(1) as u32),
        high: args.get("prio").and_then(Value::as_str) == Some("high"),
        label: string(args, "label"),
        timeout: secs(args, "timeout_s"),
        log: Some(log),
        cancel: Some(cancel),
        wait_timeout: secs(args, "wait_timeout_s"),
        ticket: args.get("ticket").and_then(Value::as_u64),
        rerun_hint: Some("call run_task again with the same arguments plus \"ticket\": {ticket}".into()),
        env: line.env,
        needs: args
            .get("needs")
            .and_then(Value::as_array)
            .map(|a| a.iter().filter_map(Value::as_str).map(String::from).collect())
            .unwrap_or_default(),
    }
}

fn run_task(c: &Call) -> Value {
    let Some(command) = c
        .args
        .get("command")
        .and_then(Value::as_str)
        .filter(|s| !s.trim().is_empty())
    else {
        return text_result("run_task needs a non-empty \"command\" string".into(), true, None);
    };
    if let Some(d) = string(c.args, "cwd").map(PathBuf::from).filter(|d| !d.is_dir()) {
        return text_result(format!("cwd is not a directory: {}", d.display()), true, None);
    }
    let mut ctx = match ctx_for(c.args) {
        Ok(ctx) => ctx,
        Err(e) => return e,
    };
    let logs = queue::log_dir(&ctx.home);
    queue::prune_logs(&logs, KEEP_LOGS);
    let name = format!(
        "{}-{}-{}.log",
        queue::now_ms(),
        std::process::id(),
        LOG_SEQ.fetch_add(1, Ordering::SeqCst)
    );
    let log = logs.join(name);
    let req = request_for(c.args, command, log.clone(), c.cancel.clone());
    let mut notify = McpNotify {
        out: c.out,
        token: c.progress_token.clone(),
        count: 0,
        last_line: String::new(),
        last_sent: None,
        started: Instant::now(),
    };
    let o = queue::run(&mut ctx, req, &mut notify);
    task_result(&o, log.exists().then_some(log))
}

fn task_result(o: &queue::Outcome, log: Option<PathBuf>) -> Value {
    let mut text = format!(
        "exit code {} ({}) · {} · kind {} · waited {} · ran {}\n",
        o.code,
        o.status,
        o.label,
        o.kind,
        history::human_ms(o.wait_ms),
        history::human_ms(o.run_ms)
    );
    if let Some(m) = &o.message {
        text.push_str(m);
        text.push('\n');
    }
    let tail = log.as_ref().map(|l| queue::tail_lines(l, TAIL_LINES));
    if let (Some(l), Some(t)) = (&log, &tail) {
        text.push_str(&format!("full log: {}\n", l.display()));
        text.push_str(&format!("--- last {TAIL_LINES} lines of output ---\n{t}"));
    }
    let structured = json!({
        "exit_code": o.code,
        "status": o.status,
        "label": o.label,
        "kind": o.kind,
        "cost": o.cost,
        "wait_ms": o.wait_ms,
        "run_ms": o.run_ms,
        "log_path": log.map(|l| l.to_string_lossy().to_string()),
        "message": o.message,
        "ticket": o.ticket,
        // Clients that show structured content instead of the text still get the output.
        "output_tail": tail,
    });
    text_result(text, o.code != 0, Some(structured))
}

fn queue_status(c: &Call) -> Value {
    let mut ctx = match ctx_for(c.args) {
        Ok(ctx) => ctx,
        Err(e) => return e,
    };
    match status::snapshot(&mut ctx) {
        Ok(state) => {
            let v = status::status_json(&mut ctx, &state);
            text_result(
                serde_json::to_string_pretty(&v).unwrap_or_default(),
                false,
                Some(v),
            )
        }
        Err(e) => error_result(&e),
    }
}

fn bump(c: &Call) -> Value {
    let Some(label) = c
        .args
        .get("label")
        .and_then(Value::as_str)
        .filter(|l| !l.is_empty())
    else {
        return text_result("bump needs a \"label\" string".into(), true, None);
    };
    let mut ctx = match ctx_for(c.args) {
        Ok(ctx) => ctx,
        Err(e) => return e,
    };
    match status::bump(&mut ctx, label) {
        Ok(n) => text_result(
            format!("moved {n} waiting job(s) matching \"{label}\" to the front"),
            false,
            Some(json!({"moved": n, "matched": n})),
        ),
        Err(e) => error_result(&e),
    }
}

/// The server: tracks running background calls so they can be cancelled.
struct Server {
    out: Out,
    /// Background calls by request id. Taking an entry out (on cancel) also means: no reply.
    running: Arc<Mutex<HashMap<String, Arc<AtomicBool>>>>,
    threads: Vec<JoinHandle<()>>,
}

impl Server {
    fn handle_line(&mut self, line: &str) {
        let msg: Value = match serde_json::from_str(line) {
            Ok(v) => v,
            Err(_) => return reply_error(&self.out, &Value::Null, PARSE_ERROR, "parse error"),
        };
        let method = msg.get("method").and_then(Value::as_str).unwrap_or("");
        let params = msg.get("params").cloned().unwrap_or(Value::Null);
        match msg.get("id").cloned() {
            // A notification (or a response to us). Only cancellation matters.
            None if method == "notifications/cancelled" => self.cancel(&params),
            None => {}
            Some(_) if method.is_empty() => {}
            Some(id) => self.request(id, method, params),
        }
    }

    fn cancel(&mut self, params: &Value) {
        let Some(rid) = params.get("requestId") else {
            return;
        };
        // Per the MCP spec a cancelled request gets no response.
        if let Some(flag) = self
            .running
            .lock()
            .ok()
            .and_then(|mut m| m.remove(&rid.to_string()))
        {
            flag.store(true, Ordering::SeqCst);
        }
    }

    fn request(&mut self, id: Value, method: &str, params: Value) {
        match method {
            "initialize" => {
                let asked = params
                    .get("protocolVersion")
                    .and_then(Value::as_str)
                    .unwrap_or("");
                let version = SUPPORTED_VERSIONS
                    .iter()
                    .find(|v| **v == asked)
                    .copied()
                    .unwrap_or(DEFAULT_VERSION);
                let result = json!({
                    "protocolVersion": version,
                    "capabilities": {"tools": {"listChanged": false}},
                    "serverInfo": {"name": "calmrun", "title": "calmrun", "version": env!("CARGO_PKG_VERSION")},
                    "instructions": INSTRUCTIONS,
                });
                reply(&self.out, &id, result);
            }
            "ping" => reply(&self.out, &id, json!({})),
            "tools/list" => {
                let tools: Vec<Value> = TOOLS.iter().map(|t| (t.schema)()).collect();
                reply(&self.out, &id, json!({"tools": tools}));
            }
            "tools/call" => self.call(id, params),
            _ => reply_error(
                &self.out,
                &id,
                METHOD_NOT_FOUND,
                &format!("method not found: {method}"),
            ),
        }
    }

    fn call(&mut self, id: Value, params: Value) {
        let name = params.get("name").and_then(Value::as_str).unwrap_or("");
        let Some(tool) = TOOLS.iter().find(|t| t.name == name) else {
            return reply_error(&self.out, &id, INVALID_PARAMS, &format!("unknown tool: {name}"));
        };
        let args = params.get("arguments").cloned().unwrap_or_else(|| json!({}));
        let progress_token = params.get("_meta").and_then(|m| m.get("progressToken")).cloned();
        let cancel = Arc::new(AtomicBool::new(false));
        if !tool.background {
            let c = Call {
                out: &self.out,
                args: &args,
                progress_token,
                cancel,
            };
            return reply(&self.out, &id, (tool.call)(&c));
        }
        let key = id.to_string();
        if let Ok(mut m) = self.running.lock() {
            m.insert(key.clone(), cancel.clone());
        }
        let (out, running, call) = (self.out.clone(), self.running.clone(), tool.call);
        self.threads.push(std::thread::spawn(move || {
            let result = call(&Call {
                out: &out,
                args: &args,
                progress_token,
                cancel,
            });
            let still_wanted = running
                .lock()
                .map(|mut m| m.remove(&key).is_some())
                .unwrap_or(true);
            if still_wanted {
                reply(&out, &id, result);
            }
        }));
    }

    /// The client went away: stop what still runs, then wait for it.
    fn shutdown(self) {
        if let Ok(m) = self.running.lock() {
            m.values().for_each(|f| f.store(true, Ordering::SeqCst));
        }
        for t in self.threads {
            let _ = t.join();
        }
    }
}

/// How long running tasks get to stop after SIGTERM/Ctrl+C before the server exits.
const SIGNAL_GRACE: Duration = Duration::from_secs(5);

/// On Ctrl+C / SIGTERM: cancel every task (their process trees are stopped like any
/// cancelled task), give them a moment, then exit.
fn stop_tasks_on_signal(running: Arc<Mutex<HashMap<String, Arc<AtomicBool>>>>) {
    let _ = ctrlc::set_handler(move || {
        if let Ok(m) = running.lock() {
            m.values().for_each(|f| f.store(true, Ordering::SeqCst));
        }
        let deadline = Instant::now() + SIGNAL_GRACE;
        while Instant::now() < deadline && running.lock().is_ok_and(|m| !m.is_empty()) {
            std::thread::sleep(Duration::from_millis(100));
        }
        std::process::exit(runner::EXIT_INTERRUPTED);
    });
}

/// Serve until stdin closes. Returns the process exit code.
pub fn serve() -> i32 {
    let mut server = Server {
        out: Arc::new(Mutex::new(io::stdout())),
        running: Arc::default(),
        threads: Vec::new(),
    };
    stop_tasks_on_signal(server.running.clone());
    for line in io::stdin().lock().lines() {
        let Ok(line) = line else {
            break;
        };
        if !line.trim().is_empty() {
            server.handle_line(&line);
        }
    }
    server.shutdown();
    0
}

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

    #[test]
    fn plain_words_run_directly() {
        let l = command_argv("npx vitest run");
        assert_eq!(
            (l.argv, l.shell),
            (vec!["npx".into(), "vitest".into(), "run".into()], false)
        );
        let l = command_argv("npm ci && npm test");
        assert!(l.shell);
        assert_eq!(l.argv, vec!["npm ci && npm test".to_string()]);
        assert!(!command_argv("cargo test --features=x").shell);
    }

    #[test]
    fn leading_assignments_become_environment() {
        let l = command_argv("CI=1 NODE_ENV=test npm test");
        assert!(!l.shell);
        assert_eq!(l.argv, vec!["npm".to_string(), "test".to_string()]);
        assert_eq!(
            l.env,
            vec![("CI".into(), "1".into()), ("NODE_ENV".into(), "test".into())]
        );
    }

    #[test]
    fn every_tool_has_a_matching_schema() {
        let names: Vec<&str> = TOOLS.iter().map(|t| t.name).collect();
        assert_eq!(names, vec!["run_task", "queue_status", "bump"]);
        for t in TOOLS {
            let s = (t.schema)();
            assert_eq!(s["name"], t.name);
            assert_eq!(s["inputSchema"]["type"], "object");
        }
    }
}