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
//! Command-line interface.
use clap::{Parser, Subcommand, ValueEnum};
#[derive(Debug, Parser)]
#[command(
name = "calmrun",
version,
about = "A machine-wide queue for heavy commands, so parallel agents and terminals don't freeze the computer",
long_about = None,
args_conflicts_with_subcommands = true,
after_help = "Examples:\n calmrun -- npx vitest run\n calmrun --cost 3 --label e2e -- npx playwright test\n calmrun status"
)]
/// The command line.
pub struct Cli {
#[command(subcommand)]
pub sub: Option<Sub>,
/// Kind of job; detected from the command when omitted.
#[arg(long, value_name = "K", help = crate::kinds::kind_help())]
pub kind: Option<String>,
/// Points this job takes from the budget (clamped to the budget).
#[arg(long, value_name = "N")]
pub cost: Option<u32>,
/// Queue priority.
#[arg(long, value_enum, value_name = "PRIO")]
pub prio: Option<Prio>,
/// Label shown in status and stats (default: tool + subcommand).
#[arg(long, value_name = "L")]
pub label: Option<String>,
/// Also take a slot of this named resource (repeat or comma-separate: --needs db,gpu).
#[arg(long, value_name = "NAME", value_delimiter = ',')]
pub needs: Vec<String>,
/// Use a separate queue with this id instead of the machine-wide one (like GNU sem --id).
/// Also selects the queue for status and bump.
#[arg(long, value_name = "QUEUE", global = true)]
pub id: Option<String>,
/// Give up waiting after this many seconds: exit 75 and keep the place in line for
/// 10 minutes. calmrun prints the command that resumes it.
#[arg(long, value_name = "SECS")]
pub wait_timeout: Option<f64>,
/// Resume the place in line kept by an earlier --wait-timeout run.
#[arg(long, value_name = "ID")]
pub ticket: Option<u64>,
/// The command to run. Separate words run directly; one quoted string runs in the shell.
#[arg(trailing_var_arg = true, allow_hyphen_values = true, value_name = "COMMAND")]
pub command: Vec<String>,
}
/// Queue priority.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum Prio {
Normal,
High,
}
/// Subcommands.
#[derive(Debug, Subcommand)]
pub enum Sub {
/// Show running and waiting jobs, the budget and free RAM.
Status {
/// Machine-readable output.
#[arg(long)]
json: bool,
},
/// Per label/kind: runs, failures, wait and run times.
Stats {
/// How many days back to look.
#[arg(long, default_value_t = 7)]
days: u32,
/// Machine-readable output.
#[arg(long)]
json: bool,
},
/// Move waiting jobs whose label contains the text to the front of the queue.
Bump {
/// Part of the label to match (case-insensitive).
label: String,
},
/// Show cores, RAM, disk, battery and the suggested config.
Doctor,
/// Hooks for coding agents.
Hook {
#[command(subcommand)]
target: HookTarget,
},
/// Add calmrun instructions to coding agents' global instruction files (dry run unless
/// --write).
Setup {
/// Apply the changes (default: only show them).
#[arg(long)]
write: bool,
/// Take the calmrun block (and with --hook, the hook) out again.
#[arg(long)]
remove: bool,
/// Also add the Claude Code PreToolUse hook to its settings.json (a backup is kept).
#[arg(long)]
hook: bool,
/// Which agents (claude, codex, gemini, opencode). Default: those whose folder exists.
#[arg(long, value_delimiter = ',')]
agents: Option<Vec<String>>,
/// Home directory to use instead of yours (for tests and other users' profiles).
#[arg(long, value_name = "DIR")]
home: Option<std::path::PathBuf>,
},
/// Print short instructions for coding agents.
Guide,
/// Serve the queue to coding agents over MCP (JSON-RPC on stdin/stdout).
Mcp,
/// Write a commented default config file and print its path.
Init {
/// Overwrite an existing file.
#[arg(long)]
force: bool,
},
/// Test helper: sleep and log start/end times.
#[command(name = "__probe", hide = true)]
Probe(ProbeArgs),
/// Internal (macOS): end a command's process group when its calmrun disappears.
#[command(name = "__watchdog", hide = true)]
Watchdog { parent: i32, pgid: i32 },
/// Test helper: send Ctrl+C (Windows console event) or SIGINT (Unix) to a process.
#[command(name = "__ctrl_c", hide = true)]
CtrlC { pid: u32 },
}
/// Agents calmrun has a hook for.
#[derive(Debug, Subcommand)]
pub enum HookTarget {
/// Claude Code PreToolUse hook: reads the hook JSON on stdin.
Claude,
}
/// Arguments of the hidden test helper `__probe`.
#[derive(Debug, clap::Args)]
pub struct ProbeArgs {
#[arg(long)]
pub log: Option<std::path::PathBuf>,
#[arg(long, default_value = "")]
pub name: String,
#[arg(long, default_value_t = 0)]
pub sleep_ms: u64,
#[arg(long, default_value_t = 0)]
pub exit: i32,
/// Print the received arguments as JSON.
#[arg(long)]
pub print_args: bool,
/// Print the value of this environment variable (empty if unset).
#[arg(long)]
pub print_env: Option<String>,
/// Allocate and touch this many MB before sleeping.
#[arg(long, default_value_t = 0)]
pub alloc_mb: usize,
/// Start a detached helper that outlives this process and write its pid here.
#[arg(long)]
pub spawn_daemon: Option<std::path::PathBuf>,
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
pub rest: Vec<String>,
}