calmrun 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,687 @@
1
- # Temporary Holding Version
1
+ # calmrun
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![CI](https://github.com/vannt-dev/calmrun/actions/workflows/ci.yml/badge.svg)](https://github.com/vannt-dev/calmrun/actions/workflows/ci.yml)
4
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
5
+ [![Release](https://img.shields.io/github/v/release/vannt-dev/calmrun)](https://github.com/vannt-dev/calmrun/releases/latest)
6
+ [![crates.io](https://img.shields.io/crates/v/calmrun)](https://crates.io/crates/calmrun)
7
+ [![npm](https://img.shields.io/npm/v/calmrun)](https://www.npmjs.com/package/calmrun)
8
+
9
+ A machine-wide queue for heavy commands, so several coding agents and terminals on one computer
10
+ don't run their builds and tests all at once and freeze it, in any stack: `vitest`, `pytest`,
11
+ `cargo`, `gradle`, `dotnet`, `go`, `cmake`, Playwright, `docker compose` and more.
12
+
13
+ ```sh
14
+ calmrun -- npx vitest run
15
+ ```
16
+
17
+ calmrun waits until the machine has room (budget points, free RAM, free disk), then runs the
18
+ command at low CPU priority and passes its exit code back. There is no daemon: every `calmrun`
19
+ process coordinates through a small state file guarded by an OS file lock.
20
+
21
+ <picture>
22
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/vannt-dev/calmrun/main/docs/diagrams/overview-dark.svg">
23
+ <img alt="Several agents and terminals on one machine send heavy commands to calmrun, either directly as calmrun -- cmd or through the MCP tool run_task; the Claude Code hook catches a heavy command started without calmrun and denies it with the wrapped form, or rewrites it. calmrun first applies its command rules: a watch mode is refused with exit 2 and the one-shot form to run instead, and a dev server or app run starts at once outside the queue. Any other command is detected for its kind and cost, its exclusive key and a worker hint, then waits in the machine-wide queue until all gates hold: free budget points, enough free RAM and disk, its exclusive key and any named resource. With --wait-timeout it gives up with exit 75 and keeps its place as a ticket. Once admitted it runs at low CPU priority under a process-tree memory ceiling, and its exit code is passed back and recorded in the history." src="https://raw.githubusercontent.com/vannt-dev/calmrun/main/docs/diagrams/overview-light.svg">
24
+ </picture>
25
+
26
+ Works on Windows, macOS and Linux. Website: <https://vannt-dev.github.io/calmrun/>
27
+
28
+ **Contents:** [Why](#why) · [Install](#install) · [Quick start](#quick-start) ·
29
+ [How it decides](#how-it-decides) · [Commands](#commands) · [Config](#config) ·
30
+ [Coding agents](#coding-agents) · [Exit codes](#exit-codes) · [Platform notes](#platform-notes) ·
31
+ [Troubleshooting](#troubleshooting) · [Contributing](#contributing)
32
+
33
+ ## Why
34
+
35
+ One test run is fine. Four agents each starting `vitest` with a worker per core, plus a `tsc`
36
+ and a build, is not: the machine swaps, the editor stalls, tests time out for reasons that have
37
+ nothing to do with the code, and every run gets slower than if they had taken turns. calmrun
38
+ makes them take turns:
39
+
40
+ - Jobs start only while budget points are free, and wait while free RAM or disk is low.
41
+ - Two test runs or builds on the same `node_modules`, Cargo `target` or project folder (Gradle,
42
+ Maven, .NET, CMake, Swift, Flutter, …) never run together.
43
+ - Direct calls of vitest, cargo, gradle, dotnet, go, cmake, ninja, bazel and other tools that
44
+ use every core get a worker count that fits their share of the machine.
45
+ - Commands run at low CPU priority, so your editor and the agents stay responsive.
46
+ - A job whose process tree grows past a memory ceiling is stopped instead of taking the machine.
47
+ - A killed terminal does not leave orphaned workers: the command's tree dies with calmrun.
48
+
49
+ ## Install
50
+
51
+ Prebuilt binaries for x64 and arm64 are attached to each
52
+ [GitHub release](https://github.com/vannt-dev/calmrun/releases/latest).
53
+
54
+ ```sh
55
+ # macOS / Linux
56
+ curl --proto '=https' --tlsv1.2 -LsSf https://github.com/vannt-dev/calmrun/releases/latest/download/calmrun-installer.sh | sh
57
+ ```
58
+
59
+ ```powershell
60
+ # Windows (PowerShell)
61
+ powershell -ExecutionPolicy Bypass -c "irm https://github.com/vannt-dev/calmrun/releases/latest/download/calmrun-installer.ps1 | iex"
62
+ ```
63
+
64
+ The installers put `calmrun` in `$CARGO_HOME/bin` (by default `~/.cargo/bin`, or
65
+ `%USERPROFILE%\.cargo\bin` on Windows) and add that folder to `PATH`. Open a new terminal
66
+ afterwards.
67
+
68
+ ```sh
69
+ # npm (any OS; downloads the matching binary)
70
+ npm install -g calmrun
71
+
72
+ # from crates.io (Rust 1.95 or newer)
73
+ cargo install calmrun --locked
74
+ ```
75
+
76
+ **Manual download.** Each release has one archive per platform, each with a `.sha256` file:
77
+
78
+ | Platform | Archive |
79
+ | --- | --- |
80
+ | Windows x64 | `calmrun-x86_64-pc-windows-msvc.zip` |
81
+ | Windows arm64 | `calmrun-aarch64-pc-windows-msvc.zip` |
82
+ | macOS Intel | `calmrun-x86_64-apple-darwin.tar.xz` |
83
+ | macOS Apple silicon | `calmrun-aarch64-apple-darwin.tar.xz` |
84
+ | Linux x64 | `calmrun-x86_64-unknown-linux-gnu.tar.xz` |
85
+ | Linux arm64 | `calmrun-aarch64-unknown-linux-gnu.tar.xz` |
86
+
87
+ Unpack it and put `calmrun` (`calmrun.exe`) in a folder on `PATH`. Check with
88
+ `calmrun --version` and `calmrun doctor`.
89
+
90
+ ## Quick start
91
+
92
+ ```sh
93
+ calmrun doctor # what this machine has and the suggested budget
94
+ calmrun -- npx vitest run # queue, then run at low priority
95
+ calmrun -- cargo build --release
96
+ calmrun --cost 3 --label e2e -- npx playwright test
97
+ calmrun "npm ci && npm test" # one quoted string runs through the shell
98
+ calmrun status # what runs, what waits, and why
99
+ calmrun stats --days 30 # how long jobs waited and ran
100
+ calmrun bump e2e # move a waiting job to the front
101
+ calmrun init # write a commented config file
102
+ calmrun setup --write --hook # tell your coding agents to use calmrun
103
+ ```
104
+
105
+ While waiting, calmrun prints one line to stderr, then one every 30 seconds:
106
+
107
+ ```
108
+ calmrun: waiting — 4 of 6 points in use, 2nd in line (needs 2); running: npx vitest in web (3m10s), cargo build in api (1m02s)
109
+ calmrun: waiting for RAM: 2.1 GB free, needs 3.0 GB; running: npx playwright in e2e (4m55s)
110
+ ```
111
+
112
+ ## How it decides
113
+
114
+ ### Budget and kinds
115
+
116
+ The machine has `budget` points (default: logical cores − 2, capped at floor(RAM GB / 4), at
117
+ least 2). Each job costs points by kind; `--cost` overrides, and a cost above the budget counts
118
+ as the whole budget.
119
+
120
+ | Kind | Detected from | Default cost |
121
+ | --- | --- | --- |
122
+ | `vitest` | `vitest` | 2 |
123
+ | `jest` | `jest` | 2 |
124
+ | `tsc` | `tsc`, `vue-tsc`, `tsgo`, package scripts `typecheck` / `type-check` / `tsc` | 2 |
125
+ | `build` | `vite/next/ng build`, `webpack`, `rollup`, `esbuild`, package script `build`, and the build tools in the [table below](#build-tools) | 2 (1 for one compiler call: `gcc`, `clang`, `cl`, …) |
126
+ | `monorepo` | `turbo run`, `nx run-many/affected`, `lerna run`, `pnpm -r`, `rush build`, `moon run` | 3 |
127
+ | `capture` | `playwright`, `puppeteer`, `cypress` | 2 |
128
+ | `docker` | `docker compose up/build/run/create`, `docker-compose …` (same subcommands), `docker build/run/buildx`; the same for `podman` and `podman-compose`; `buildah build`, `packer build` | 2 |
129
+ | `typecheck` | `mypy`, `pyright`, `deno check`, `phpstan`, `psalm` | 2 |
130
+ | `eslint` | `eslint`, `oxlint`, `biome`, package script `lint` | 1 |
131
+ | `lint` | `ruff`, `pylint`, `flake8`, `go vet`, `golangci-lint run`, `rubocop` | 1 |
132
+ | `pytest` | `pytest`, `py.test`, `python -m pytest` | 1 |
133
+ | `test` | `npm/pnpm/yarn/bun test`, package script `test`, `mocha`, `tox`, `nox`, and the test runners in the [table below](#build-tools) | 1 |
134
+ | `other` | anything else | 1 |
135
+
136
+ "Package script" means `npm run <name>` / `npm run-script <name>` (also `<name>:suffix`, such as
137
+ `test:unit`), and `pnpm|yarn|bun <name>` directly. Detection looks at the command position
138
+ only: the program name (basename, without `.cmd/.bat/.exe/.ps1/.js`) after `VAR=value` prefixes
139
+ and launchers such as `npx`, `pnpx`, `bunx`, `pnpm exec`, `yarn exec`, `npm exec`, `uv run`,
140
+ `python -m`, `bundle exec`, `dotnet tool run`, `xcrun`. So `git commit -m "fix vitest"` is not a vitest run. `--kind` overrides detection;
141
+ `[kinds.<name>]` in the config changes a cost or adds a kind with its own patterns.
142
+
143
+ <a id="build-tools"></a>**Build tools.** What calmrun knows per tool beyond JavaScript and
144
+ Python. *Key* is the marker file of the project folder the job takes an exclusive key on
145
+ (see [Gates](#gates-when-a-job-starts)). *Hint* is the worker count calmrun passes, N being
146
+ the job's share of the machine (logical cores × cost ÷ budget, at least 1); in brackets, what
147
+ makes it leave the count alone because you already chose one.
148
+
149
+ | Tools | Kind | Key | Hint |
150
+ | --- | --- | --- | --- |
151
+ | `make`, `gmake`, `mingw32-make`, `nmake` | build | `Makefile` | none (serial unless you pass `-j`) |
152
+ | `cmake --build` | build | `CMakeCache.txt`, `CMakeLists.txt` | `--parallel N` (`-j`, `--parallel`, `CMAKE_BUILD_PARALLEL_LEVEL`) |
153
+ | `ctest` | test | as cmake | none (serial unless you pass `-j`) |
154
+ | `ninja` | build | `build.ninja` | `-jN` (`-j`) |
155
+ | `meson compile` / `meson test` | build / test | `meson.build` | `-j N` / `--num-processes N` (`MESON_TESTTHREADS`) |
156
+ | `bazel`, `bazelisk` `build` / `test`, `coverage` | build / test | `MODULE.bazel`, `WORKSPACE` | `--jobs=N` (`--jobs`, `-j`) |
157
+ | `buck2`, `buck` `build` / `test` | build / test | `.buckconfig` | `-jN` (`-j`, `--num-threads`) |
158
+ | `scons`, `xmake` | build | `SConstruct`, `xmake.lua` | none, `-j N` for xmake |
159
+ | `gcc`, `g++`, `clang`, `clang++`, `cl`, `cc` | build, cost 1 | none | none |
160
+ | `gradle`, `gradlew` | test for `test*`/`check*`/`connected*` tasks (`connected*` also takes the `emulator` resource), else build | `settings.gradle(.kts)` | `--max-workers=N` (`--max-workers`, `-Dorg.gradle.workers.max`) |
161
+ | `mvn`, `mvnw` / `mvnd` | test for `test`/`verify`, else build | `pom.xml` | none / `-T N` for mvnd (`-T`) |
162
+ | `sbt`, `ant` | test for `sbt test`/`testOnly`, else build | `build.sbt`, `build.xml` | none |
163
+ | `dotnet build`, `publish`, `pack`, `msbuild`, `restore` / `dotnet test` | build / test | `*.sln`, `*.slnx`, `*.csproj`, `*.fsproj`, … | `-m:N` (`-m`, `-maxcpucount`) |
164
+ | `msbuild` | build | as dotnet | none (serial unless you pass `-m`) |
165
+ | `go build`, `install` / `test` / `vet` | build / test / lint | none | `-p N` after the subcommand (`-p`, `GOFLAGS=-p…`) |
166
+ | `golangci-lint run` | lint | none | `--concurrency N` (`--concurrency`, `-j`) |
167
+ | `cargo build`, `check`, `clippy`, `doc` / `test`, `bench`, `nextest` | build / test | `target` folder | `CARGO_BUILD_JOBS=N`, also `NEXTEST_TEST_THREADS=N` for nextest |
168
+ | `maturin build`, `develop` | build | `target` folder | `CARGO_BUILD_JOBS=N` |
169
+ | `swift build` / `test` | build / test | `Package.swift` | `-j N` (`-j`, `--jobs`) |
170
+ | `xcodebuild` (also through `xcrun`) | test for test actions, else build | `*.xcworkspace`, `*.xcodeproj` | `-jobs N` (`-jobs`) |
171
+ | `flutter build` | build | `pubspec.yaml` | none |
172
+ | `flutter test`, `dart test` | test | `pubspec.yaml` | `--concurrency=N` (`--concurrency`, `-j`) |
173
+ | `dart compile`, `dart run build_runner` | build | `pubspec.yaml` | none |
174
+ | `rspec`, `cucumber`, `rake test`, `rake spec` | test | none | none |
175
+ | `rails test` | test | none | `PARALLEL_WORKERS=N` |
176
+ | `parallel_rspec`, `parallel_test` | test | none | `PARALLEL_TEST_PROCESSORS=N` |
177
+ | `phpunit`, `pest`, `composer test`, `php artisan test` | test | none | none |
178
+ | `paratest` | test | none | `--processes=N` (`-p`, `--processes`) |
179
+ | `mix test` / `mix compile`, `release` | test / build | `mix.exs` | `--max-cases N` (`--max-cases`) / none |
180
+ | `rebar3 eunit`, `ct` / `compile`, `release` | test / build | `rebar.config` | none |
181
+ | `stack build`, `install`, `test` | build / test | `stack.yaml` | `-j N` (`-j`, `--jobs`) |
182
+ | `cabal build`, `install`, `test` | build / test | `cabal.project`, `*.cabal` | `-jN` (`-j`, `--jobs`) |
183
+ | `dune build` / `test` | build / test | `dune-project` | `-j N` (`-j`) |
184
+ | `zig build` | build | `build.zig` | `-jN` (`-j`) |
185
+ | `turbo run` | monorepo | `node_modules` | `--concurrency=N`, at most 10, turbo's default (`--concurrency`) |
186
+ | `nx run-many`, `nx affected` | monorepo | `node_modules` | `--parallel=N`, at most 3, nx's default (`--parallel`) |
187
+ | `lerna run`, `exec` | monorepo | `node_modules` | `--concurrency N` (`--concurrency`) |
188
+ | `pnpm -r` (not `install`, `add`, `update`, …) | monorepo | `node_modules` | `--workspace-concurrency=N`, at most 4, pnpm's default (`--workspace-concurrency`) |
189
+ | `rush build`, `rebuild`, `test` | monorepo | `node_modules` | `--parallelism N` (`-p`, `--parallelism`) |
190
+ | `moon run`, `ci`, `check` | monorepo | `node_modules` | `MOON_CONCURRENCY=N` |
191
+ | `pytest -n auto`, `tox -p auto` | pytest / test | none, `tox.ini` | `auto` becomes N |
192
+ | `deno test`, `bench` | test | none | `DENO_JOBS=N` |
193
+ | `docker`, `podman`, `buildah`, `packer` builds | docker | none | none |
194
+
195
+ A variable in the Hint column is set only when it is not set already, and an argument is
196
+ added only when the tool is started directly as words (also after `VAR=value`, `npx`,
197
+ `bundle exec`, `xcrun`, …), never to package scripts or shell strings. `calmrun doctor` prints
198
+ the numbers for this machine. Tools that run one job at a time unless asked (make, ctest,
199
+ scons, plain Maven, msbuild without `-m`) get no hint on purpose: turning parallelism on can
200
+ break a build that was never written for it. Pass the flag yourself (`make -j8`) and calmrun
201
+ leaves it alone.
202
+
203
+ ### Gates: when a job starts
204
+
205
+ A job starts when **all** of these hold:
206
+
207
+ - **Points.** Its cost fits in the free points of its queue.
208
+ - **RAM.** Free RAM is at least `min_free_gb` (default max(3, RAM / 8) GB; `0` disables). The
209
+ check is skipped when nothing is running, so a job never waits forever on an idle machine.
210
+ Waiters share one RAM reading per second, and memory is reserved for jobs that just started.
211
+ - **Disk.** The drive of its working directory has at least `min_free_disk_gb` free (default 5;
212
+ `0` turns it off): `calmrun: waiting for disk space: 3.2 GB free on C:\, needs 5.0 GB`. When
213
+ nothing else runs it starts anyway, with a warning.
214
+ - **Same folder, one at a time.** vitest, jest and JavaScript build jobs take an exclusive key
215
+ for their nearest `node_modules` folder; Cargo builds and tests take one for their `target`
216
+ folder (or `CARGO_TARGET_DIR`); other build tools take one for their project folder, the
217
+ nearest folder holding the marker file in the [build tools table](#build-tools) (two Gradle
218
+ runs in one build share `build/`, two `dotnet` runs share `obj/`). Two jobs with the same key
219
+ never run together.
220
+ - **Named resources.** `--needs db` also takes one slot of the resource `db`. Configure slots
221
+ with `[resources] db = 1` or `CALMRUN_RESOURCES="db=1,gpu=2"`; a resource that is not
222
+ configured has one slot. Use it for things that cannot be shared: one test database, one
223
+ emulator, one GPU. Waiters say who holds it:
224
+ `calmrun: waiting for resource "db" (1 of 1 slot(s) held by npm test), 2nd in line`.
225
+
226
+ Points, keys and resource slots are taken all-or-nothing.
227
+
228
+ ### Order and passing
229
+
230
+ `--prio high` first, then first come. A later job may pass earlier ones only if it fits right
231
+ now and no job ahead of it has waited longer than `pass_limit_min` (default 10; `0` = strict
232
+ order). Running jobs are never stopped to make room. `calmrun bump <label>` moves matching
233
+ waiting jobs to the front.
234
+
235
+ ### Separate queues
236
+
237
+ `--id QUEUE` uses its own queue with its own budget, like GNU `sem --id` (for example one queue
238
+ per project, or one for long e2e runs). `status --id QUEUE` and `bump … --id QUEUE` act on it.
239
+ The default queue and named queues do not see each other's points; history and config are
240
+ shared. A queue id uses letters, digits, `-`, `_` and `.` (not first).
241
+
242
+ ### Tickets: capped waits
243
+
244
+ `--wait-timeout SECS` caps the wait. If the job has not started by then, calmrun exits **75**
245
+ (EX_TEMPFAIL) and keeps its place in line for 10 minutes. It prints the exact command that
246
+ resumes that place, the same arguments with `--ticket <id>` in front:
247
+
248
+ ```
249
+ calmrun: not started within 120s (waiting — 6 of 6 points in use, 2nd in line (needs 2)); your place in line is kept for 10 min.
250
+ calmrun: next: calmrun --ticket 17 --wait-timeout 120 -- npx vitest run
251
+ ```
252
+
253
+ While the owner is away, the kept place does not block anyone; when the ticket comes back it
254
+ rejoins at its old position. An expired or unknown ticket joins the end of the line. `status`
255
+ shows places kept by a ticket.
256
+
257
+ ### Command rules
258
+
259
+ - **Endless commands are refused** (exit 2), because they would hold their points forever.
260
+ calmrun prints the one-shot form to run instead (`calmrun: next: …`):
261
+ - `docker compose up` / `docker-compose up` without `-d`, `--detach` or `--wait` → add `-d`;
262
+ - `tsc --watch` / `-w` (also `vue-tsc`, `tsgo`), `jest --watch` / `--watchAll`,
263
+ `vitest --watch` / `-w` → without the flag;
264
+ - `vitest watch` / `vitest dev`, and plain `vitest` when stdin is a terminal (it watches
265
+ then) → `vitest run`;
266
+ - `gradle --continuous` / `-t`, `webpack|rollup|esbuild|mocha --watch`,
267
+ `vite build --watch`, `deno|bun --watch`, `stack --file-watch`, `dune -w`,
268
+ `zig build --watch` → without the flag;
269
+ - `dotnet watch <cmd>` → `dotnet <cmd>`; `turbo watch` → `turbo run`; `mix test.watch` →
270
+ `mix test`; `parcel watch` → `parcel build`;
271
+ - `docker run` / `docker exec` (also `podman`) with `-i`, `-t`, `--interactive` or `--tty`,
272
+ bare `sbt` and `sbt shell|console`, `sbt ~task`, `cargo watch`, `nx watch`, `lerna watch`,
273
+ `dart run build_runner watch|serve`, and watchers (`ibazel`, `nodemon`, `watchexec`, `ptw`,
274
+ `bacon`, `air`, `guard`) → run it outside calmrun.
275
+ - **Light commands run at once**, outside the queue and with their arguments unchanged: dev
276
+ servers and app runs (`vite`, `vite dev|serve|preview`, `next|nuxt|astro dev|start|preview`,
277
+ `ng serve`, `webpack serve`, `npm run dev|start|serve|preview` and the pnpm/yarn/bun forms,
278
+ `parcel <entry>`, `turbo run dev`, `nx serve`, `make dev|run|serve|start|watch`, `cargo run`,
279
+ `go run`, `dotnet run`, `flutter run`, `swift run`, `zig build run`, `bazel run`, `sbt run`,
280
+ `gradle bootRun`, `mvn spring-boot:run`, `rails server`, `mix phx.server`,
281
+ `php artisan serve`, `symfony serve`, …), installs and infrastructure commands
282
+ (`pod install`, `composer install`, `terraform plan|apply`, `pulumi up`), and commands that
283
+ only print help, a version or project information (`--help`, `-h`, `--version`, `-V`,
284
+ `xcodebuild -list`). The Claude Code hook leaves them alone.
285
+ - **Worker counts.** vitest started directly as separate words (`vitest …`,
286
+ `npx|pnpx|bunx vitest …`, `pnpm|yarn exec vitest …`) gets `--maxWorkers=N` unless it already
287
+ passes `--maxWorkers`, `--pool` or `--no-file-parallelism`; N is `vitest_workers` (default 1
288
+ for up to 4 cores, 2 below 12, else 3). Build tools get the hint in the
289
+ [build tools table](#build-tools). Cargo gets `CARGO_BUILD_JOBS=N` unless it is already set;
290
+ a lower `build.jobs` in a cargo config (`.cargo/config.toml` up from the working directory,
291
+ then `$CARGO_HOME/config.toml`) wins. `npm run …` scripts and shell strings are never
292
+ changed.
293
+ - **Nesting.** calmrun sets `CALMRUN_HELD=<pid>` for the command. A calmrun started inside it
294
+ (for example a script that calls calmrun again) runs at once instead of queuing behind its
295
+ own parent.
296
+
297
+ ### While it runs
298
+
299
+ - **Low priority.** Below normal priority on Windows, `nice +10` in its own process group on
300
+ Unix. `CALMRUN_LOW_CPU=0` (or `false`, `no`, `off`) turns this off.
301
+ - **Memory ceiling.** About once a second calmrun adds up the memory of the job's whole process
302
+ tree (including nested calmrun runs). If the tree goes above `ceiling_gb` (default half the
303
+ RAM; `0` turns it off), or free RAM stays below `critical_free_gb` (default 5% of RAM, at most
304
+ 1 GB) for 3 seconds and this job's tree is the largest of the running jobs (on a tie, the
305
+ newer one), calmrun stops the tree (gracefully, then forced) and exits **137**. A tree smaller
306
+ than max(1 GB, 10% of RAM) is never stopped for low free RAM: calmrun warns once instead,
307
+ since something outside the queue is using the memory.
308
+
309
+ ```
310
+ calmrun: stopped npx vitest run: its processes used 9.1 GB, above the 8.0 GB ceiling
311
+ ```
312
+
313
+ - **Process tree.** When the command exits normally, processes it deliberately left running (a
314
+ Gradle or Kotlin daemon, a dev server started in the background) are left alone. When the run
315
+ ends abnormally (Ctrl+C, or calmrun itself being killed) the whole tree is ended.
316
+ - **History.** Each run appends one JSON line to `history.jsonl`: `at`, `label`, `kind`, `cost`,
317
+ `wait_ms`, `run_ms`, `status` (`ceiling` or `low-memory` for memory stops). The label is
318
+ `--label` or the tool and subcommand only (`npx vitest run`, `cargo build`), never the
319
+ arguments, which may contain secrets.
320
+
321
+ ## Commands
322
+
323
+ ```
324
+ calmrun [--kind K] [--cost N] [--prio normal|high] [--label L] [--needs NAME,…] [--id QUEUE]
325
+ [--wait-timeout SECS] [--ticket ID] -- <command...>
326
+ calmrun status [--json] [--id QUEUE]
327
+ calmrun stats [--days N] [--json]
328
+ calmrun bump <label-substring> [--id QUEUE]
329
+ calmrun doctor
330
+ calmrun init [--force]
331
+ calmrun guide
332
+ calmrun setup [--write] [--remove] [--hook] [--agents claude,codex,gemini,opencode] [--home DIR]
333
+ calmrun hook claude
334
+ calmrun mcp
335
+ ```
336
+
337
+ ### Running a command
338
+
339
+ | Flag | Meaning |
340
+ | --- | --- |
341
+ | `--kind K` | Kind of job (a built-in kind or one from the config); detected when omitted |
342
+ | `--cost N` | Points this job takes (clamped to the budget) |
343
+ | `--prio normal\|high` | Queue priority; `high` goes before every `normal` job |
344
+ | `--label L` | Name in `status` and `stats` (default: tool + subcommand) |
345
+ | `--needs NAME` | Also take a slot of this named resource; repeat or comma-separate (`--needs db,gpu`) |
346
+ | `--id QUEUE` | Use a separate queue instead of the machine-wide one |
347
+ | `--wait-timeout SECS` | Give up waiting after this long: exit 75, place kept for 10 minutes |
348
+ | `--ticket ID` | Resume the place kept by an earlier `--wait-timeout` run |
349
+
350
+ - Separate words (`calmrun -- npx vitest run`) run the program directly. On Windows calmrun
351
+ looks the name up on `PATH` with `PATHEXT`, so `npm`, `npx` and other `.cmd` shims work, and
352
+ arguments are escaped for `cmd.exe`. A bare name is never taken from the working directory
353
+ (a repository's own `npm.cmd` cannot stand in for npm); `./gradlew` or `.\tool.cmd` with a
354
+ path runs from the job's folder.
355
+ - One string containing spaces (`calmrun "a && b"`) runs through the shell: `cmd /d /s /c` on
356
+ Windows, `sh -c` on Unix. calmrun never rewrites such a string.
357
+ - Ctrl+C reaches the command; a job still waiting leaves the queue and exits 130.
358
+
359
+ ### Subcommands
360
+
361
+ | Command | What it does |
362
+ | --- | --- |
363
+ | `status [--json]` | Running and waiting jobs with the reason each one waits, the budget, free RAM and kept tickets. `--json` (with `mem_bytes` per running job) for tools. |
364
+ | `stats [--days N] [--json]` | Runs, failures, average and maximum wait, average run time per label and kind, over the last N days (default 7). |
365
+ | `bump <text>` | Move waiting jobs whose label contains the text (case-insensitive) to the front. |
366
+ | `doctor` | Cores, RAM, disk type, battery, the state directory and config file, the settings in effect and the suggested ones. |
367
+ | `init [--force]` | Write a commented `config.toml` with this machine's defaults and print its path; `--force` overwrites. |
368
+ | `guide` | Short instructions for coding agents, to paste into their context. |
369
+ | `setup` | Add or remove the calmrun block in agents' global instruction files; see [below](#calmrun-setup). |
370
+ | `hook claude` | The Claude Code PreToolUse hook; reads the hook JSON on stdin. |
371
+ | `mcp` | An MCP server on stdin/stdout with `run_task`, `queue_status` and `bump`. |
372
+
373
+ `--id QUEUE` is accepted by every subcommand; it matters for running, `status` and `bump`.
374
+
375
+ ## Config
376
+
377
+ `calmrun init` writes a commented `config.toml` and prints its path. Every key is optional;
378
+ environment variables override the file.
379
+
380
+ ```toml
381
+ budget = 6
382
+ min_free_gb = 4
383
+ pass_limit_min = 10
384
+ vitest_workers = 2
385
+ ceiling_gb = 8 # 0 = off
386
+ min_free_disk_gb = 5 # 0 = off
387
+ critical_free_gb = 0.8 # 0 = off
388
+
389
+ [resources] # named resources for --needs, with their slots
390
+ db = 1
391
+ emulator = 1
392
+
393
+ [kinds.vitest]
394
+ cost = 3 # override a built-in cost
395
+
396
+ [kinds.heavy] # add a kind with command patterns (first words)
397
+ cost = 2
398
+ patterns = ["make", "bazel build"]
399
+ ```
400
+
401
+ | Key | Variable | Default | Meaning |
402
+ | --- | --- | --- | --- |
403
+ | `budget` | `CALMRUN_BUDGET` | cores − 2, capped at floor(RAM GB / 4), at least 2 | Points that may run at once |
404
+ | `min_free_gb` | `CALMRUN_MIN_FREE_GB` | max(3, RAM / 8) | Free RAM needed to start while something runs; `0` = off |
405
+ | `min_free_disk_gb` | `CALMRUN_MIN_FREE_DISK_GB` | 5 | Free space needed on the working directory's drive; `0` = off |
406
+ | `pass_limit_min` | `CALMRUN_PASS_MIN` | 10 | Minutes a waiter may be passed; `0` = strict order |
407
+ | `vitest_workers` | `CALMRUN_VITEST_WORKERS` | 1 (≤ 4 cores), 2 (< 12), else 3 | `--maxWorkers` for direct vitest calls |
408
+ | `ceiling_gb` | `CALMRUN_CEILING_GB` | half the RAM | Memory ceiling for one job's process tree; `0` = off |
409
+ | `critical_free_gb` | `CALMRUN_CRITICAL_FREE_GB` | 5% of RAM, at most 1 GB | Free-RAM floor that stops the biggest job; `0` = off |
410
+ | `[resources]` | `CALMRUN_RESOURCES` | none (1 slot each) | Named resource slots, e.g. `db=1,gpu=2`; the variable adds to the table |
411
+ | `[kinds.<name>]` | | built-in costs | `cost` and `patterns` (command's first words) |
412
+ | | `CALMRUN_LOW_CPU` | on | `0`, `false`, `no` or `off` keeps normal priority |
413
+ | | `CALMRUN_HOOK_MODE` | deny | `rewrite` makes the Claude Code hook wrap commands itself |
414
+ | | `CALMRUN_HOME` | see below | State directory |
415
+ | | `CALMRUN_CONFIG` | `<state dir>/config.toml` | Config file path |
416
+ | | `CALMRUN_HELD` | | Set by calmrun for the command it runs (nesting); do not set it yourself |
417
+
418
+ An unknown key or a syntax error makes calmrun ignore the whole file with a warning
419
+ (`calmrun: ignoring config file: …`) and use the defaults; `calmrun doctor` shows what is in
420
+ effect.
421
+
422
+ ### Where state lives
423
+
424
+ | OS | Default state directory |
425
+ | --- | --- |
426
+ | Windows | `%LOCALAPPDATA%\calmrun` |
427
+ | macOS | `~/Library/Application Support/calmrun` |
428
+ | Linux | `$XDG_STATE_HOME/calmrun` or `~/.local/state/calmrun` |
429
+
430
+ It holds `state.json` (running and waiting jobs), `state.lock`, `history.jsonl`, `config.toml`
431
+ and `logs/` (MCP task output). Entries of processes that died are removed automatically:
432
+ calmrun checks the pid together with the process start time, so a reused pid is not mistaken for
433
+ a live job. A corrupt `state.json` is set aside; an unreadable one is an error.
434
+
435
+ ## Coding agents
436
+
437
+ ### Claude Code hook
438
+
439
+ `calmrun hook claude` is a [PreToolUse hook](https://code.claude.com/docs/en/hooks). For the
440
+ `Bash` and `PowerShell` tools it checks each part of the command (split on `&&`, `||`, `;`, `|`,
441
+ `&` and new lines). If a part runs a heavy kind and is not already wrapped (`calmrun …`,
442
+ `npx calmrun …`, `lane with …`), the hook denies the call and tells Claude the exact command to
443
+ run instead, with each heavy part wrapped in place:
444
+
445
+ ```
446
+ cd web && npm ci && npx vitest run → cd web && npm ci && calmrun -- npx vitest run
447
+ ```
448
+
449
+ Light commands (`git`, `ls`, `cat`, `npm install`, …) are never blocked, heredoc bodies (for
450
+ example a commit message written with `<<'EOF'`) are not treated as commands, and any input the
451
+ hook does not understand is allowed (no output, exit 0). Wrappers such as `VAR=1`, `time` or
452
+ `nice` stay in front: `time npx vitest run` becomes `time calmrun -- npx vitest run`. When
453
+ `calmrun` is not on the agent's `PATH`, the suggestion uses calmrun's absolute path.
454
+
455
+ The hook follows the same [command rules](#command-rules) as `calmrun --`, so it never
456
+ suggests a command calmrun would refuse. A watch mode with a one-shot form gets that form
457
+ (`dotnet watch test` → `calmrun -- dotnet test`, `npx vitest` → `calmrun -- npx vitest run`,
458
+ `docker compose up` → `calmrun -- docker compose up -d`). A watch mode or interactive session
459
+ without one (`sbt shell`, `cargo watch`, `nx watch`, `docker run -it`), dev servers and app
460
+ runs are left alone.
461
+
462
+ Add it to `.claude/settings.json` in a project, or to `~/.claude/settings.json` for every
463
+ project (or let `calmrun setup --write --hook` do it):
464
+
465
+ ```json
466
+ {
467
+ "hooks": {
468
+ "PreToolUse": [
469
+ {
470
+ "matcher": "Bash|PowerShell",
471
+ "hooks": [{ "type": "command", "command": "calmrun hook claude" }]
472
+ }
473
+ ]
474
+ }
475
+ }
476
+ ```
477
+
478
+ With `CALMRUN_HOOK_MODE=rewrite` the hook instead returns `updatedInput` with the wrapped
479
+ command and no permission decision, so your normal permission rules still apply to the wrapped
480
+ command. In auto mode Claude Code may decline a changed input, in which case the original
481
+ command runs unwrapped; the default deny mode does not have that gap.
482
+
483
+ ### MCP server
484
+
485
+ `calmrun mcp` is an [MCP](https://modelcontextprotocol.io) server over stdio. Agents that speak
486
+ MCP can queue work without a shell wrapper. It offers three tools:
487
+
488
+ | Tool | Arguments | What it does |
489
+ | --- | --- | --- |
490
+ | `run_task` | `command`, and optionally `cwd`, `kind`, `cost`, `label`, `timeout_s`, `prio`, `wait_timeout_s`, `ticket`, `needs`, `queue` | Waits in the same queue as the CLI, 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. |
491
+ | `queue_status` | `queue` | The same data as `calmrun status --json`. |
492
+ | `bump` | `label`, `queue` | Move matching waiting jobs to the front. |
493
+
494
+ - While a task waits, the server sends progress notifications if the client passed a
495
+ `progressToken`.
496
+ - `timeout_s` stops the command (exit 124). `wait_timeout_s` gives up waiting (exit 75) and
497
+ returns a `ticket`; pass it back as `ticket` to resume the same place in line.
498
+ - Cancelling the request (`notifications/cancelled`) leaves the queue or stops the command, and,
499
+ as the MCP spec asks, the cancelled request gets no response.
500
+ - Full logs are kept under `logs/` in the state directory; the newest 200 are kept.
501
+ - A `command` made of plain words runs the program directly (and gets the worker rules);
502
+ leading `VAR=value` words become the command's environment, so `CI=1 npm test` works on
503
+ Windows too. One that uses shell syntax (`&&`, pipes, redirects, `$VAR`, …) runs in `sh -c`
504
+ on macOS/Linux and `cmd /d /s /c` on Windows. The output tail is in the text content and, for
505
+ clients that show structured results, in `output_tail`.
506
+ - `calmrun mcp` stops its running tasks on Ctrl+C / SIGTERM and when its client closes stdin.
507
+
508
+ Register it with your agent:
509
+
510
+ ```sh
511
+ # Claude Code
512
+ claude mcp add calmrun -- calmrun mcp
513
+ ```
514
+
515
+ ```toml
516
+ # Codex: ~/.codex/config.toml
517
+ [mcp_servers.calmrun]
518
+ command = "calmrun"
519
+ args = ["mcp"]
520
+ tool_timeout_sec = 3600 # tasks may wait in the queue and run for a while
521
+ ```
522
+
523
+ ```json
524
+ // Cursor: ~/.cursor/mcp.json
525
+ {
526
+ "mcpServers": {
527
+ "calmrun": { "type": "stdio", "command": "calmrun", "args": ["mcp"] }
528
+ }
529
+ }
530
+ ```
531
+
532
+ ```json
533
+ // Gemini CLI: ~/.gemini/settings.json (timeout in ms; the default is 10 minutes)
534
+ {
535
+ "mcpServers": {
536
+ "calmrun": { "command": "calmrun", "args": ["mcp"], "timeout": 3600000 }
537
+ }
538
+ }
539
+ ```
540
+
541
+ Long test runs can outlast a client's default tool timeout; raise it where the client allows.
542
+
543
+ ### `calmrun setup`
544
+
545
+ `calmrun setup` adds a short calmrun section to each coding agent's **global** instruction file.
546
+ It is a dry run by default: it prints what it would change and the exact block. Add `--write`
547
+ to apply.
548
+
549
+ | Agent | File |
550
+ | --- | --- |
551
+ | `claude` | `~/.claude/CLAUDE.md` (or `$CLAUDE_CONFIG_DIR/CLAUDE.md`) |
552
+ | `codex` | `~/.codex/AGENTS.md` (or `$CODEX_HOME/AGENTS.md`) |
553
+ | `gemini` | `~/.gemini/GEMINI.md` |
554
+ | `opencode` | `~/.config/opencode/AGENTS.md` |
555
+
556
+ ```sh
557
+ calmrun setup # show what would change (agents whose folder exists)
558
+ calmrun setup --write # apply
559
+ calmrun setup --write --agents codex # only some agents
560
+ calmrun setup --write --hook # also add the Claude Code hook to ~/.claude/settings.json
561
+ calmrun setup --write --remove --hook # take both out again
562
+ ```
563
+
564
+ - The block sits between `<!-- calmrun:start … -->` and `<!-- calmrun:end -->`. Running setup
565
+ again replaces the block in place instead of adding a second one, and `--remove` deletes it
566
+ and leaves the rest of the file as it was.
567
+ - Every file setup rewrites is first copied to `<file>.calmrun.bak`; a file that cannot be read
568
+ or is not UTF-8 text is left alone (exit 1).
569
+ - `--hook` merges the PreToolUse entry into `settings.json`, keeps every other setting, and
570
+ leaves a file it cannot parse untouched.
571
+ - When Codex has an `AGENTS.override.md`, Codex reads that file instead; setup says so, and you
572
+ copy the block there too.
573
+ - `--home DIR` uses another home directory (for tests, or another user's profile).
574
+
575
+ ### `calmrun guide` and hand-written instructions
576
+
577
+ `calmrun guide` prints short instructions to paste into an agent's context. To write them by
578
+ hand instead, add this to `CLAUDE.md`, `AGENTS.md` or your agent's instruction file:
579
+
580
+ ```markdown
581
+ ## Heavy commands
582
+
583
+ This machine is shared by several agents. Run heavy commands through the calmrun queue:
584
+ `calmrun -- <command>` (tests, type checks, linters, builds, Playwright, docker compose,
585
+ pytest, cargo/gradle/make/dotnet/go builds). Example: `calmrun -- npx vitest run`.
586
+ Run anything that may take more than a minute or two in the background and check on it later.
587
+ Use `calmrun status` to see what is running or waiting. Do not wrap light commands
588
+ (git, ls, cat, package installs).
589
+ ```
590
+
591
+ Messages meant for agents say what happened and what to run next (`calmrun: next: …`), for
592
+ example after a refused `docker compose up` or a command that is not on `PATH`.
593
+
594
+ ## Exit codes
595
+
596
+ | Exit code | Meaning |
597
+ | --- | --- |
598
+ | the command's | It ran. On Unix a command ended by signal `n` gives `128 + n`. |
599
+ | 1 | calmrun itself failed (state directory, lock, a file `setup` could not change) |
600
+ | 2 | Refused (watch mode, attached `docker compose up`, interactive `docker run`), bad arguments, or an invalid `--id` |
601
+ | 75 | Not started within `--wait-timeout`; place kept, see the `next:` line |
602
+ | 124 | Stopped by the MCP `timeout_s` |
603
+ | 126 | The command was found but could not be started |
604
+ | 127 | Command not found |
605
+ | 130 | Ctrl+C (while waiting or running) |
606
+ | 137 | Stopped by the memory ceiling or the critical free-RAM floor |
607
+
608
+ ## Platform notes
609
+
610
+ - **Windows** is where calmrun is built and checked by hand, as well as in CI. `.cmd` and
611
+ `.bat` programs run through `cmd.exe /d /s /c` with every argument quoted and caret-escaped
612
+ for both cmd parses a batch shim performs, so arguments such as `a&b`, `100%` or `"quoted"`
613
+ arrive unchanged. `.ps1` files run with `powershell -NoProfile -ExecutionPolicy Bypass -File`.
614
+ The command runs in a Job Object, so its tree ends even after `taskkill /F` on calmrun.
615
+ Ctrl+C reaches the command through the shared console; calmrun turns Ctrl+C processing back
616
+ on if its parent had it disabled. A command ended by Ctrl+C exits 130.
617
+ - **macOS / Linux** are covered by the test suite in CI, not by hand yet. The command gets its
618
+ own process group. When calmrun runs in the foreground of a terminal, that group becomes the
619
+ terminal's foreground group (so the command can read input and gets Ctrl+C directly) and the
620
+ terminal is handed back afterwards; SIGINT, SIGTERM and SIGHUP that calmrun receives are
621
+ forwarded to the group as SIGINT. Ctrl+Z stops the command and calmrun together; `fg`
622
+ continues both. On Linux the command gets SIGTERM when calmrun dies, even by `kill -9`
623
+ (parent-death signal); on macOS a small `calmrun __watchdog` process does the same within a
624
+ second.
625
+ - **Memory figures.** calmrun counts resident memory: the working set on Windows and the
626
+ resident set size (RSS) on macOS and Linux. Shared pages count once per process, so the tree
627
+ total can be higher than what the OS reports as used; the default ceiling leaves room.
628
+ - **What CI covers.** CI runs the whole suite on `windows-latest` (x64), `ubuntu-latest` (x64)
629
+ and `macos-latest` (arm64): queue rules, exit codes, Ctrl+C while running and while waiting,
630
+ a SIGKILLed calmrun taking its command with it (Unix), SIGTERM to the MCP server (Unix),
631
+ daemons surviving a normal exit, the memory ceiling stopping a nested tree, the MCP server,
632
+ tickets, the disk gate, named resources, separate queues and `setup`. Killing calmrun ending
633
+ the tree is tested on Windows only. The critical free-RAM floor is covered by unit tests only
634
+ (it depends on the machine). Ctrl+Z and the terminal hand-over are not covered (runners have
635
+ no terminal). Release binaries for Windows arm64, Linux arm64 and macOS x64 are built but not
636
+ tested.
637
+ - **Battery.** `calmrun doctor` reports whether the machine runs on battery; consider a smaller
638
+ budget then.
639
+
640
+ ## Troubleshooting
641
+
642
+ **`calmrun: command not found` after installing.** The installers put the binary in
643
+ `~/.cargo/bin` (`%USERPROFILE%\.cargo\bin` on Windows). Open a new terminal, or add that folder
644
+ to `PATH`. Coding agents started before the install need a restart too; the Claude Code hook
645
+ falls back to calmrun's absolute path in its suggestions.
646
+
647
+ **A job waits although nothing seems to run.** `calmrun status` says why each job waits. On a
648
+ busy machine the RAM gate is the usual reason (`waiting for RAM: … free, needs …`): lower
649
+ `min_free_gb`, or set it to `0`. Entries of dead processes are removed automatically.
650
+
651
+ **A job I need now is stuck behind others.** `calmrun bump <label>`, or run it with
652
+ `--prio high`.
653
+
654
+ **My agent's tool call timed out while waiting.** Use `--wait-timeout` (CLI) or
655
+ `wait_timeout_s` (MCP) and resume with the ticket, or raise the client's tool timeout (see the
656
+ MCP section).
657
+
658
+ **`refused: … is watch mode`.** Queued jobs must end. Run the `calmrun: next:` command it
659
+ prints, or run the watcher outside calmrun.
660
+
661
+ **A job was stopped with exit 137.** Its process tree passed `ceiling_gb`, or free RAM stayed
662
+ below `critical_free_gb` and it was the biggest job. Raise or disable the limit
663
+ (`CALMRUN_CEILING_GB=0`), or give the job fewer workers.
664
+
665
+ **My config seems ignored.** An unknown key or a TOML error makes calmrun skip the whole file
666
+ with a warning on stderr. `calmrun doctor` shows the path it reads and the values in effect.
667
+
668
+ **I want a separate budget for one project.** Use `--id <name>` on its runs (and on `status` /
669
+ `bump`).
670
+
671
+ ## Contributing
672
+
673
+ [ARCHITECTURE.md](ARCHITECTURE.md) maps the modules and explains how to add a tool, a gate, an
674
+ MCP tool or an agent. Changes are listed in [CHANGELOG.md](CHANGELOG.md).
675
+
676
+ ```sh
677
+ cargo fmt --all --check
678
+ cargo clippy --all-targets -- -D warnings
679
+ cargo test
680
+ ```
681
+
682
+ Bug reports and pull requests are welcome at
683
+ <https://github.com/vannt-dev/calmrun/issues>.
684
+
685
+ ## License
686
+
687
+ [MIT](LICENSE)