calmrun 0.0.0-stage → 0.1.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/.gitignore +2 -0
- package/CHANGELOG.md +55 -0
- package/LICENSE +21 -0
- package/README.md +472 -2
- package/binary-install.js +348 -0
- package/binary.js +124 -0
- package/install.js +4 -0
- package/npm-shrinkwrap.json +52 -0
- package/package.json +102 -3
- package/run-calmrun.js +4 -0
package/.gitignore
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented here. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
|
|
5
|
+
[Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [0.1.0] - 2026-11-23
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `calmrun -- <command>`: a machine-wide queue for heavy commands, with a point budget, a
|
|
12
|
+
free-RAM gate, first-come order with limited passing, `--prio high`, and exclusive keys per
|
|
13
|
+
`node_modules` / Cargo `target` folder.
|
|
14
|
+
- Kind detection for vitest, jest, tsc, Playwright-style capture, docker, eslint-style linters,
|
|
15
|
+
pytest, builds and test runners, with default costs and a `[kinds]` config table.
|
|
16
|
+
- `--maxWorkers` for direct vitest calls; refusal of attached `docker compose up`.
|
|
17
|
+
- Low CPU priority for the command (below-normal class on Windows, `nice +10` on Unix) and
|
|
18
|
+
process-tree cleanup (Job Object on Windows, process group on Unix).
|
|
19
|
+
- `status`, `stats`, `bump`, `doctor`, `init` subcommands and `history.jsonl`.
|
|
20
|
+
- `calmrun hook claude`: a Claude Code PreToolUse hook that sends heavy shell commands
|
|
21
|
+
through calmrun.
|
|
22
|
+
- `--wait-timeout SECS`: exit 75 when not started in time and keep the place in line for 10
|
|
23
|
+
minutes; `--ticket <id>` resumes it. The `next:` line gives the exact command.
|
|
24
|
+
- `calmrun guide`, and `next:` hints on refusals and missing commands.
|
|
25
|
+
- Memory ceiling: a job whose process tree passes `ceiling_gb` (default half the RAM), or the
|
|
26
|
+
biggest job when free RAM stays below `critical_free_gb`, is stopped with exit 137.
|
|
27
|
+
- Disk gate: a job waits while its working directory's drive has less than `min_free_disk_gb`
|
|
28
|
+
(default 5) free; it starts anyway, with a warning, when nothing else runs.
|
|
29
|
+
- `CARGO_BUILD_JOBS` for cargo and `--max-workers` for gradle invoked directly, sized to the
|
|
30
|
+
job's share of the cores.
|
|
31
|
+
- The waiting line names what is running: `running: npx vitest in web (3m10s), …`.
|
|
32
|
+
- `calmrun setup`: a dry-run-by-default writer for the calmrun block in the global instruction
|
|
33
|
+
files of Claude Code, Codex, Gemini CLI and OpenCode (marker comments, re-runnable,
|
|
34
|
+
`--remove`), plus `--hook` to merge the Claude Code hook into `settings.json` with a backup.
|
|
35
|
+
- Named resources: `--needs db` takes a slot of `db` (`[resources] db = 1`, `CALMRUN_RESOURCES`).
|
|
36
|
+
- Separate queues: `--id QUEUE` for running, `status` and `bump` (GNU sem style).
|
|
37
|
+
- Relative programs (`./gradlew`, `tool`) are looked up in the job's working directory, also for
|
|
38
|
+
MCP tasks with a `cwd`.
|
|
39
|
+
- Endless commands (watch modes, attached `docker compose up`, interactive `docker run`) are
|
|
40
|
+
refused with the one-shot form; dev servers and `--help` / `--version` run outside the queue.
|
|
41
|
+
- Safety: bare program names never come from the working directory on Windows; `setup` backs
|
|
42
|
+
up every file it rewrites and refuses non-UTF-8 or unreadable files; a corrupt
|
|
43
|
+
`state.json` is set aside, an unreadable one is an error instead of an empty queue.
|
|
44
|
+
- The RAM gate shares one reading per second across waiters and reserves memory for jobs
|
|
45
|
+
that just started. Unix: the command dies with calmrun (Linux parent-death signal, macOS
|
|
46
|
+
watchdog), Ctrl+Z suspends both, and `calmrun mcp` stops its tasks on SIGTERM.
|
|
47
|
+
- `npx tsc@5`-style package specs are detected; `status` shows places kept by a ticket; the
|
|
48
|
+
hook suggests calmrun's absolute path when it is not on PATH.
|
|
49
|
+
- `calmrun mcp`: an MCP server over stdio with `run_task` (same queue as the CLI, captured
|
|
50
|
+
log, last 200 lines, progress notifications, timeout, cancellation), `queue_status` and
|
|
51
|
+
`bump`.
|
|
52
|
+
- Daemons a command leaves running on purpose (Gradle, Kotlin) survive a normal exit; the
|
|
53
|
+
tree is ended only after Ctrl+C or when calmrun is killed.
|
|
54
|
+
- A command ended by Ctrl+C on Windows exits 130; Ctrl+C works even when the parent started
|
|
55
|
+
calmrun with Ctrl+C disabled.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Van Nguyen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,473 @@
|
|
|
1
|
-
#
|
|
1
|
+
# calmrun
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A machine-wide queue for heavy commands, so several coding agents and terminals on one computer
|
|
4
|
+
don't run `vitest`, `tsc`, builds, Playwright and `docker compose` all at once and freeze it.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
calmrun -- npx vitest run
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
calmrun waits until the machine has room (budget points and free RAM), then runs the command
|
|
11
|
+
at low CPU priority and passes its exit code back. There is no daemon: every `calmrun` process
|
|
12
|
+
coordinates through a small state file guarded by an OS file lock.
|
|
13
|
+
|
|
14
|
+
Works on Windows, macOS and Linux (x64 and arm64).
|
|
15
|
+
|
|
16
|
+
## Why not just run the commands?
|
|
17
|
+
|
|
18
|
+
One test run is fine. Four agents each starting `vitest` with a worker per core, plus a `tsc`
|
|
19
|
+
and a build, is not: the machine swaps, the editor stalls, and every run gets slower than if
|
|
20
|
+
they had taken turns. calmrun makes them take turns:
|
|
21
|
+
|
|
22
|
+
| Without calmrun | With calmrun |
|
|
23
|
+
| --- | --- |
|
|
24
|
+
| Every agent starts its heavy command at once | Jobs start only while budget points are free |
|
|
25
|
+
| Runs pile up until RAM runs out | A job waits when free RAM is below a threshold |
|
|
26
|
+
| Two test runs share one `node_modules` / `target` and fight over caches | Same folder means one at a time |
|
|
27
|
+
| Test runners spawn a worker per core | Direct vitest calls get a small `--maxWorkers` |
|
|
28
|
+
| Commands compete with your editor at normal priority | Commands run at low CPU priority |
|
|
29
|
+
| A killed terminal can leave orphaned workers | The command's process tree dies with calmrun |
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
Prebuilt binaries are attached to each GitHub release, with installers:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
# macOS / Linux
|
|
37
|
+
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/vannt-dev/calmrun/releases/latest/download/calmrun-installer.sh | sh
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```powershell
|
|
41
|
+
# Windows (PowerShell)
|
|
42
|
+
powershell -ExecutionPolicy Bypass -c "irm https://github.com/vannt-dev/calmrun/releases/latest/download/calmrun-installer.ps1 | iex"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
# npm (any OS)
|
|
47
|
+
npm install -g calmrun
|
|
48
|
+
|
|
49
|
+
# from source
|
|
50
|
+
cargo install --git https://github.com/vannt-dev/calmrun --locked
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Quick start
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
calmrun doctor # what this machine has and the suggested budget
|
|
57
|
+
calmrun -- npx vitest run # queue, then run at low priority
|
|
58
|
+
calmrun -- cargo build --release
|
|
59
|
+
calmrun --cost 3 --label e2e -- npx playwright test
|
|
60
|
+
calmrun "npm ci && npm test" # one quoted string runs through the shell
|
|
61
|
+
calmrun status # what runs, what waits, and why
|
|
62
|
+
calmrun stats --days 30 # how long jobs waited and ran
|
|
63
|
+
calmrun bump e2e # move a waiting job to the front
|
|
64
|
+
calmrun init # write a commented config file
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
While waiting, calmrun prints one line to stderr, then one every 30 seconds:
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
calmrun: waiting — 4 of 6 points in use, 2nd in line (needs 2); running: npx vitest in web (3m10s), cargo build in api (1m02s)
|
|
71
|
+
calmrun: waiting for RAM: 2.1 GB free, needs 3.0 GB; running: npx playwright in e2e (4m55s)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## CLI
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
calmrun [--kind K] [--cost N] [--prio high] [--label L] [--needs NAME,…] [--id QUEUE]
|
|
78
|
+
[--wait-timeout SECS] [--ticket ID] -- <command...>
|
|
79
|
+
calmrun status [--json] [--id QUEUE]
|
|
80
|
+
calmrun stats [--days N] [--json]
|
|
81
|
+
calmrun bump <label-substring> [--id QUEUE]
|
|
82
|
+
calmrun doctor
|
|
83
|
+
calmrun hook claude
|
|
84
|
+
calmrun mcp
|
|
85
|
+
calmrun guide
|
|
86
|
+
calmrun setup [--write] [--remove] [--hook] [--agents claude,codex,gemini,opencode]
|
|
87
|
+
calmrun init [--force]
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- Separate words (`calmrun -- npx vitest run`) run the program directly. On Windows calmrun
|
|
91
|
+
looks the name up on `PATH` with `PATHEXT`, so `npm`, `npx` and other `.cmd` shims work, and
|
|
92
|
+
arguments are escaped for `cmd.exe`. A bare name is never taken from the working directory
|
|
93
|
+
(a repository's own `npm.cmd` cannot stand in for npm); `./gradlew` or `.\tool.cmd` with a
|
|
94
|
+
path runs from the job's folder.
|
|
95
|
+
- One string containing spaces (`calmrun "a && b"`) runs through the shell: `cmd /d /s /c` on
|
|
96
|
+
Windows, `sh -c` on Unix. calmrun never rewrites such a string.
|
|
97
|
+
- The exit code is the command's exit code (on Unix a signal `n` becomes `128 + n`). A missing
|
|
98
|
+
command exits 127; a refused command exits 2.
|
|
99
|
+
- Ctrl+C reaches the command; a job still waiting leaves the queue and exits 130.
|
|
100
|
+
- `--wait-timeout SECS` caps the wait. If the job has not started by then, calmrun exits **75**
|
|
101
|
+
(EX_TEMPFAIL) and keeps its place in line for 10 minutes. It prints the exact command that
|
|
102
|
+
resumes that place, the same arguments with `--ticket <id>` in front:
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
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.
|
|
106
|
+
calmrun: next: calmrun --ticket 17 --wait-timeout 120 -- npx vitest run
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
While the owner is away, the kept place does not block anyone; when the ticket comes back
|
|
110
|
+
it rejoins at its old position. An expired or unknown ticket joins the end of the line.
|
|
111
|
+
- Messages meant for agents say what happened and what to run next (`calmrun: next: …`), for
|
|
112
|
+
example after a refused `docker compose up` or a command that is not on `PATH`.
|
|
113
|
+
`calmrun guide` prints short instructions to paste into an agent's context.
|
|
114
|
+
|
|
115
|
+
| Exit code | Meaning |
|
|
116
|
+
| --- | --- |
|
|
117
|
+
| the command's | it ran |
|
|
118
|
+
| 2 | refused (e.g. attached `docker compose up`) |
|
|
119
|
+
| 75 | not started within `--wait-timeout`; place kept, see the `next:` line |
|
|
120
|
+
| 124 | stopped by the MCP `timeout_s` |
|
|
121
|
+
| 127 | command not found |
|
|
122
|
+
| 130 | Ctrl+C |
|
|
123
|
+
| 137 | stopped by the memory ceiling or the critical free-RAM floor |
|
|
124
|
+
|
|
125
|
+
## Rules
|
|
126
|
+
|
|
127
|
+
**Budget.** The machine has `budget` points (default: logical cores − 2, capped at
|
|
128
|
+
floor(RAM GB / 4), at least 2). Each job costs points by kind; `--cost` overrides, and a cost
|
|
129
|
+
above the budget counts as the whole budget.
|
|
130
|
+
|
|
131
|
+
| Kind | Detected from | Default cost |
|
|
132
|
+
| --- | --- | --- |
|
|
133
|
+
| `vitest` | `vitest` | 2 |
|
|
134
|
+
| `jest` | `jest` | 2 |
|
|
135
|
+
| `tsc` | `tsc`, `vue-tsc`, `tsgo`, `npm run typecheck` | 2 |
|
|
136
|
+
| `build` | `vite/next/ng build`, `webpack`, `rollup`, `esbuild`, `gradle(w)`, `mvn`, `cargo build/check/clippy`, `flutter build`, `dotnet build`, `go build`, `npm run build` | 2 |
|
|
137
|
+
| `capture` | `playwright`, `puppeteer`, `cypress` | 2 |
|
|
138
|
+
| `docker` | `docker compose up/build/run`, `docker-compose …`, `docker build/run` | 2 |
|
|
139
|
+
| `eslint` | `eslint`, `oxlint`, `biome`, `npm run lint` | 1 |
|
|
140
|
+
| `pytest` | `pytest`, `python -m pytest` | 1 |
|
|
141
|
+
| `test` | `cargo test`, `go test`, `flutter test`, `dotnet test`, `npm test` | 1 |
|
|
142
|
+
| `other` | anything else | 1 |
|
|
143
|
+
|
|
144
|
+
Detection looks at the command position only: the program name (basename, without
|
|
145
|
+
`.cmd/.bat/.exe/.ps1/.js`) after `VAR=value` prefixes and launchers such as `npx`, `pnpx`,
|
|
146
|
+
`bunx`, `pnpm exec`, `yarn exec`, `npm exec`, `uv run`, `python -m`. So
|
|
147
|
+
`git commit -m "fix vitest"` is not a vitest run. `--kind` overrides detection.
|
|
148
|
+
|
|
149
|
+
**Starting.** A job starts when its cost fits in the free points **and** free RAM is at least
|
|
150
|
+
`min_free_gb` (default max(3, RAM / 8) GB; 0 disables). The RAM check is skipped when nothing
|
|
151
|
+
is running, so a job never waits forever on an idle machine.
|
|
152
|
+
|
|
153
|
+
**Disk.** A job also waits while the drive of its working directory has less than
|
|
154
|
+
`min_free_disk_gb` free (default 5; `0` turns it off), and says so:
|
|
155
|
+
`calmrun: waiting for disk space: 3.2 GB free on C:\, needs 5.0 GB`. When nothing else is
|
|
156
|
+
running it starts anyway, with a warning, so it never waits for a drive it cannot clean up.
|
|
157
|
+
|
|
158
|
+
**Named resources.** `--needs db` makes a job also take one slot of the resource `db`.
|
|
159
|
+
Configure slots with `[resources] db = 1` or `CALMRUN_RESOURCES="db=1,gpu=2"`; a resource
|
|
160
|
+
that is not configured has one slot. Use it for things that cannot be shared: one test
|
|
161
|
+
database, one emulator, one GPU. Waiters say who holds it:
|
|
162
|
+
`calmrun: waiting for resource "db" (1 of 1 slot(s) held by npm test), 2nd in line`.
|
|
163
|
+
|
|
164
|
+
**Separate queues.** `--id QUEUE` uses its own queue with its own budget, like GNU `sem --id`
|
|
165
|
+
(for example one queue per project, or one for long e2e runs). `status --id QUEUE` and
|
|
166
|
+
`bump … --id QUEUE` act on it. The default queue and named queues do not see each other's
|
|
167
|
+
points; history and config are shared.
|
|
168
|
+
|
|
169
|
+
**Order.** `--prio high` first, then first come. A later job may pass earlier ones only if it
|
|
170
|
+
fits right now and no job ahead of it has waited longer than `pass_limit_min` (default 10;
|
|
171
|
+
0 = strict order). Running jobs are never stopped.
|
|
172
|
+
|
|
173
|
+
**Same folder, one at a time.** vitest, jest and build jobs take an exclusive key for their
|
|
174
|
+
nearest `node_modules` folder; Cargo builds and tests take one for their `target` folder (or
|
|
175
|
+
`CARGO_TARGET_DIR`). Two jobs with the same key never run together. Points and keys are taken
|
|
176
|
+
all-or-nothing.
|
|
177
|
+
|
|
178
|
+
**Command rules.**
|
|
179
|
+
|
|
180
|
+
- Commands that never end would hold their points forever, so they are refused with the
|
|
181
|
+
one-shot form to run instead (`calmrun: next: …`):
|
|
182
|
+
- `docker compose up` / `docker-compose up` without `-d`, `--detach` or `--wait` → add `-d`;
|
|
183
|
+
- `tsc --watch` / `-w`, `jest --watch` / `--watchAll` → without the flag;
|
|
184
|
+
- `vitest watch` / `vitest dev`, and plain `vitest` when stdin is a terminal (it watches
|
|
185
|
+
then) → `vitest run`;
|
|
186
|
+
- `docker run -it` / `-i` / `-t` (interactive containers) → run it outside calmrun.
|
|
187
|
+
- Dev servers (`vite`, `next dev`, `ng serve`, `npm run dev|start|serve|preview`, …) and
|
|
188
|
+
commands that only print help or a version (`--help`, `-h`, `--version`, `-V`) run at once,
|
|
189
|
+
outside the queue.
|
|
190
|
+
- vitest started directly as separate words (`vitest …`, `npx|pnpx|bunx vitest …`,
|
|
191
|
+
`pnpm|yarn exec vitest …`) gets `--maxWorkers=N` appended unless it already passes
|
|
192
|
+
`--maxWorkers`, `--pool` or `--no-file-parallelism`. N is `vitest_workers` (default 1 for
|
|
193
|
+
up to 4 cores, 2 below 12, else 3). `npm run …` scripts and shell strings are never changed.
|
|
194
|
+
- gradle / gradlew started directly as words gets `--max-workers=N` unless it already sets
|
|
195
|
+
`--max-workers` or `-Dorg.gradle.workers.max`.
|
|
196
|
+
- Cargo commands get `CARGO_BUILD_JOBS=N` unless it is already set. If a cargo config
|
|
197
|
+
(`.cargo/config.toml` up from the working directory, then `$CARGO_HOME/config.toml`) sets a
|
|
198
|
+
lower `build.jobs`, that lower number is used.
|
|
199
|
+
- For both, N is the job's share of the machine: logical cores × cost ÷ budget (at least 1).
|
|
200
|
+
|
|
201
|
+
**Nesting.** calmrun sets `CALMRUN_HELD=<pid>` for the command. A calmrun started inside it
|
|
202
|
+
(for example a script that calls calmrun again) runs at once instead of queuing behind its own
|
|
203
|
+
parent.
|
|
204
|
+
|
|
205
|
+
**Low priority.** The command runs below normal priority on Windows and with `nice +10` in its
|
|
206
|
+
own process group on Unix. `CALMRUN_LOW_CPU=0` turns the priority change off.
|
|
207
|
+
|
|
208
|
+
**Memory ceiling.** While a job runs, calmrun adds up the memory of its whole process tree
|
|
209
|
+
(the command and every process below it, including nested calmrun runs) about once a second.
|
|
210
|
+
If the tree goes above `ceiling_gb` (default half the RAM; `0` turns it off), or free RAM
|
|
211
|
+
stays below `critical_free_gb` (default 5% of RAM, at most 1 GB) for 3 seconds and this job's
|
|
212
|
+
tree is the single largest of the running jobs (on a tie, the newer one), calmrun stops the
|
|
213
|
+
tree (gracefully, then forced) and exits **137**. A tree smaller than max(1 GB, 10% of RAM)
|
|
214
|
+
is never stopped for low free RAM: calmrun warns once instead, since something outside the
|
|
215
|
+
queue is using the memory. A ceiling stop looks like:
|
|
216
|
+
|
|
217
|
+
```
|
|
218
|
+
calmrun: stopped npx vitest run: its processes used 9.1 GB, above the 8.0 GB ceiling
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
The stop is recorded in `history.jsonl` (`"status": "ceiling"` or `"low-memory"`), and
|
|
222
|
+
`status --json` shows each running job's `mem_bytes`.
|
|
223
|
+
|
|
224
|
+
**Process tree.** When the command exits normally, processes it deliberately left running
|
|
225
|
+
(a Gradle or Kotlin daemon, a dev server started in the background) are left alone. When the
|
|
226
|
+
run ends abnormally — Ctrl+C, or calmrun itself being killed — the whole tree is ended: on
|
|
227
|
+
Windows the command runs in a Job Object that kills the tree when calmrun's handle closes; on
|
|
228
|
+
Unix the command's process group is killed after Ctrl+C.
|
|
229
|
+
|
|
230
|
+
**History.** Each run appends one JSON line to `history.jsonl`: `at`, `label`, `kind`, `cost`,
|
|
231
|
+
`wait_ms`, `run_ms`, `status`. The label is `--label` or the tool and subcommand only
|
|
232
|
+
(`npx vitest run`, `cargo build`), never the arguments, which may contain secrets.
|
|
233
|
+
|
|
234
|
+
## Config
|
|
235
|
+
|
|
236
|
+
`calmrun init` writes a commented `config.toml` and prints its path. Every key is optional.
|
|
237
|
+
|
|
238
|
+
```toml
|
|
239
|
+
budget = 6
|
|
240
|
+
min_free_gb = 4
|
|
241
|
+
pass_limit_min = 10
|
|
242
|
+
vitest_workers = 2
|
|
243
|
+
ceiling_gb = 8 # 0 = off
|
|
244
|
+
min_free_disk_gb = 5 # 0 = off
|
|
245
|
+
critical_free_gb = 0.8 # 0 = off
|
|
246
|
+
|
|
247
|
+
[resources] # named resources for --needs, with their slots
|
|
248
|
+
db = 1
|
|
249
|
+
emulator = 1
|
|
250
|
+
|
|
251
|
+
[kinds.vitest]
|
|
252
|
+
cost = 3 # override a built-in cost
|
|
253
|
+
|
|
254
|
+
[kinds.heavy] # add a kind with command patterns (first words)
|
|
255
|
+
cost = 2
|
|
256
|
+
patterns = ["make", "bazel build"]
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Environment
|
|
260
|
+
|
|
261
|
+
| Variable | Meaning |
|
|
262
|
+
| --- | --- |
|
|
263
|
+
| `CALMRUN_BUDGET` | Budget points (overrides `budget`) |
|
|
264
|
+
| `CALMRUN_MIN_FREE_GB` | Free RAM needed to start while busy; `0` disables |
|
|
265
|
+
| `CALMRUN_PASS_MIN` | Pass limit in minutes; `0` = strict order |
|
|
266
|
+
| `CALMRUN_VITEST_WORKERS` | `--maxWorkers` value for vitest |
|
|
267
|
+
| `CALMRUN_LOW_CPU` | `0` keeps normal priority |
|
|
268
|
+
| `CALMRUN_RESOURCES` | Named resource slots, e.g. `db=1,gpu=2` (adds to `[resources]`) |
|
|
269
|
+
| `CALMRUN_MIN_FREE_DISK_GB` | Free space needed on the cwd's drive; `0` = off |
|
|
270
|
+
| `CALMRUN_CEILING_GB` | Memory ceiling for one job's process tree; `0` = off |
|
|
271
|
+
| `CALMRUN_CRITICAL_FREE_GB` | Free-RAM floor that stops the biggest job; `0` = off |
|
|
272
|
+
| `CALMRUN_HOME` | State directory |
|
|
273
|
+
| `CALMRUN_CONFIG` | Config file path |
|
|
274
|
+
| `CALMRUN_HOOK_MODE` | `rewrite` makes the Claude Code hook wrap commands itself |
|
|
275
|
+
|
|
276
|
+
### Where state lives
|
|
277
|
+
|
|
278
|
+
| OS | Default state directory |
|
|
279
|
+
| --- | --- |
|
|
280
|
+
| Windows | `%LOCALAPPDATA%\calmrun` |
|
|
281
|
+
| macOS | `~/Library/Application Support/calmrun` |
|
|
282
|
+
| Linux | `$XDG_STATE_HOME/calmrun` or `~/.local/state/calmrun` |
|
|
283
|
+
|
|
284
|
+
It holds `state.json` (running and waiting jobs), `state.lock`, `history.jsonl` and
|
|
285
|
+
`config.toml`. Entries of processes that died are removed automatically: calmrun checks the
|
|
286
|
+
pid together with the process start time, so a reused pid is not mistaken for a live job.
|
|
287
|
+
|
|
288
|
+
## Coding agents
|
|
289
|
+
|
|
290
|
+
### Claude Code hook
|
|
291
|
+
|
|
292
|
+
`calmrun hook claude` is a
|
|
293
|
+
[PreToolUse hook](https://code.claude.com/docs/en/hooks). For the `Bash` and `PowerShell`
|
|
294
|
+
tools it checks each part of the command (split on `&&`, `||`, `;`, `|`, `&` and new lines).
|
|
295
|
+
If a part runs a heavy kind and is not already wrapped (`calmrun …`, `npx calmrun …`,
|
|
296
|
+
`lane with …`), the hook denies the call and tells Claude the exact command to run instead,
|
|
297
|
+
with each heavy part wrapped in place:
|
|
298
|
+
|
|
299
|
+
```
|
|
300
|
+
cd web && npm ci && npx vitest run → cd web && npm ci && calmrun -- npx vitest run
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
Light commands (`git`, `ls`, `cat`, `npm install`, …) are never blocked, heredoc bodies (for
|
|
304
|
+
example a commit message written with `<<'EOF'`) are not treated as commands, and any input
|
|
305
|
+
the hook does not understand is allowed (no output, exit 0). Wrappers such as `VAR=1`, `time`
|
|
306
|
+
or `nice` stay in front: `time npx vitest` becomes `time calmrun -- npx vitest`.
|
|
307
|
+
|
|
308
|
+
Add it to `.claude/settings.json` in a project, or to `~/.claude/settings.json` for every
|
|
309
|
+
project:
|
|
310
|
+
|
|
311
|
+
```json
|
|
312
|
+
{
|
|
313
|
+
"hooks": {
|
|
314
|
+
"PreToolUse": [
|
|
315
|
+
{
|
|
316
|
+
"matcher": "Bash|PowerShell",
|
|
317
|
+
"hooks": [{ "type": "command", "command": "calmrun hook claude" }]
|
|
318
|
+
}
|
|
319
|
+
]
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
With `CALMRUN_HOOK_MODE=rewrite` the hook instead returns `updatedInput` with the wrapped
|
|
325
|
+
command and no permission decision, so your normal permission rules still apply to the wrapped
|
|
326
|
+
command. In auto mode Claude Code may decline a changed input, in which case the original
|
|
327
|
+
command runs unwrapped; the default deny mode does not have that gap.
|
|
328
|
+
|
|
329
|
+
### MCP server
|
|
330
|
+
|
|
331
|
+
`calmrun mcp` is an [MCP](https://modelcontextprotocol.io) server over stdio. Agents that
|
|
332
|
+
speak MCP can queue work without a shell wrapper. It offers three tools:
|
|
333
|
+
|
|
334
|
+
| Tool | What it does |
|
|
335
|
+
| --- | --- |
|
|
336
|
+
| `run_task` | `{command, 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 (kept under `logs/` in the state directory; the newest 200 are kept). While it waits, the server sends progress notifications if the client passed a `progressToken`. `timeout_s` stops the command (exit 124). `wait_timeout_s` gives up waiting (exit 75) and returns a `ticket`; pass it back as `ticket` to resume the same place in line. Cancelling the request leaves the queue or stops the command. |
|
|
337
|
+
| `queue_status` | The same data as `calmrun status --json`. |
|
|
338
|
+
| `bump` | `{label}`: move matching waiting jobs to the front. |
|
|
339
|
+
|
|
340
|
+
A `command` made of plain words runs the program directly (and gets the vitest
|
|
341
|
+
`--maxWorkers` rule); leading `VAR=value` words become the command's environment, so
|
|
342
|
+
`CI=1 npm test` works on Windows too. One that uses shell syntax (`&&`, pipes, redirects,
|
|
343
|
+
`$VAR`, …) runs in `sh -c` on macOS/Linux and `cmd /d /s /c` on Windows. The output tail is in
|
|
344
|
+
the text content and, for clients that show structured results, in `output_tail`.
|
|
345
|
+
|
|
346
|
+
Register it with your agent:
|
|
347
|
+
|
|
348
|
+
```sh
|
|
349
|
+
# Claude Code
|
|
350
|
+
claude mcp add calmrun -- calmrun mcp
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
```toml
|
|
354
|
+
# Codex: ~/.codex/config.toml
|
|
355
|
+
[mcp_servers.calmrun]
|
|
356
|
+
command = "calmrun"
|
|
357
|
+
args = ["mcp"]
|
|
358
|
+
tool_timeout_sec = 3600 # tasks may wait in the queue and run for a while
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
```json
|
|
362
|
+
// Cursor: ~/.cursor/mcp.json
|
|
363
|
+
{
|
|
364
|
+
"mcpServers": {
|
|
365
|
+
"calmrun": { "type": "stdio", "command": "calmrun", "args": ["mcp"] }
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
```json
|
|
371
|
+
// Gemini CLI: ~/.gemini/settings.json (timeout in ms; the default is 10 minutes)
|
|
372
|
+
{
|
|
373
|
+
"mcpServers": {
|
|
374
|
+
"calmrun": { "command": "calmrun", "args": ["mcp"], "timeout": 3600000 }
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
Long test runs can outlast a client's default tool timeout; raise it where the client allows.
|
|
380
|
+
Cancelling a `run_task` request (`notifications/cancelled`) stops the command or leaves the
|
|
381
|
+
queue, and, as the MCP spec asks, the cancelled request gets no response.
|
|
382
|
+
|
|
383
|
+
### `calmrun setup`
|
|
384
|
+
|
|
385
|
+
`calmrun setup` adds a short calmrun section to each coding agent's **global** instruction
|
|
386
|
+
file. It is a dry run by default: it prints what it would change and the exact block. Add
|
|
387
|
+
`--write` to apply.
|
|
388
|
+
|
|
389
|
+
| Agent | File |
|
|
390
|
+
| --- | --- |
|
|
391
|
+
| `claude` | `~/.claude/CLAUDE.md` (or `$CLAUDE_CONFIG_DIR/CLAUDE.md`) |
|
|
392
|
+
| `codex` | `~/.codex/AGENTS.md` (or `$CODEX_HOME/AGENTS.md`) |
|
|
393
|
+
| `gemini` | `~/.gemini/GEMINI.md` |
|
|
394
|
+
| `opencode` | `~/.config/opencode/AGENTS.md` |
|
|
395
|
+
|
|
396
|
+
```sh
|
|
397
|
+
calmrun setup # show what would change (agents whose folder exists)
|
|
398
|
+
calmrun setup --write # apply
|
|
399
|
+
calmrun setup --write --agents codex # only some agents
|
|
400
|
+
calmrun setup --write --hook # also add the Claude Code hook to ~/.claude/settings.json
|
|
401
|
+
calmrun setup --write --remove --hook # take both out again
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
The block sits between `<!-- calmrun:start … -->` and `<!-- calmrun:end -->`. Running setup
|
|
405
|
+
again replaces the block in place instead of adding a second one, and `--remove` deletes it
|
|
406
|
+
and leaves the rest of the file as it was. Every file setup rewrites is first copied to
|
|
407
|
+
`<file>.calmrun.bak`; a file that cannot be read or is not UTF-8 text is left alone (exit 1).
|
|
408
|
+
`--hook` merges the PreToolUse entry into `settings.json`, keeps every other setting, and
|
|
409
|
+
leaves a file it cannot parse untouched.
|
|
410
|
+
|
|
411
|
+
### Instructions for agents
|
|
412
|
+
|
|
413
|
+
To write the instructions by hand instead, add this to `CLAUDE.md`, `AGENTS.md` or your
|
|
414
|
+
agent's instruction file:
|
|
415
|
+
|
|
416
|
+
```markdown
|
|
417
|
+
## Heavy commands
|
|
418
|
+
|
|
419
|
+
This machine is shared by several agents. Run heavy commands through the calmrun queue:
|
|
420
|
+
`calmrun -- <command>` (tests, type checks, linters, builds, Playwright, docker compose,
|
|
421
|
+
pytest, cargo/gradle builds). Example: `calmrun -- npx vitest run`.
|
|
422
|
+
Run anything that may take more than a minute or two in the background and check on it later.
|
|
423
|
+
Use `calmrun status` to see what is running or waiting. Do not wrap light commands
|
|
424
|
+
(git, ls, cat, package installs).
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
## Per-OS notes
|
|
428
|
+
|
|
429
|
+
- **Windows.** `.cmd` and `.bat` programs run through `cmd.exe /d /s /c` with every argument
|
|
430
|
+
quoted and caret-escaped for both cmd parses a batch shim performs, so arguments such as
|
|
431
|
+
`a&b`, `100%` or `"quoted"` arrive unchanged. `.ps1` files run with
|
|
432
|
+
`powershell -NoProfile -ExecutionPolicy Bypass -File`. Ctrl+C reaches the command through the
|
|
433
|
+
shared console; calmrun turns Ctrl+C processing back on if its parent had it disabled. A
|
|
434
|
+
command ended by Ctrl+C exits 130.
|
|
435
|
+
- **macOS / Linux.** The command gets its own process group. When calmrun runs in the
|
|
436
|
+
foreground of a terminal, that group becomes the terminal's foreground group (so the command
|
|
437
|
+
can read input and gets Ctrl+C directly) and the terminal is handed back afterwards; SIGINT,
|
|
438
|
+
SIGTERM and SIGHUP that calmrun receives are forwarded to the group as SIGINT. Ctrl+Z stops
|
|
439
|
+
the command and calmrun together; `fg` continues both.
|
|
440
|
+
- **When calmrun itself is killed hard.** On Windows the Job Object ends the command's tree
|
|
441
|
+
even after `taskkill /F`. On Linux the command gets SIGTERM when calmrun dies, even by
|
|
442
|
+
`kill -9` (parent-death signal). On macOS a small `calmrun __watchdog` process does the same
|
|
443
|
+
within a second. `calmrun mcp` stops its running tasks on Ctrl+C / SIGTERM and when its
|
|
444
|
+
client closes stdin, then exits.
|
|
445
|
+
- **Memory figures.** calmrun counts resident memory: the working set on Windows and the
|
|
446
|
+
resident set size (RSS) on macOS and Linux. Shared pages count once per process, so the
|
|
447
|
+
tree total can be higher than what the OS reports as used; the default ceiling leaves room.
|
|
448
|
+
- **What CI covers.** The test suite runs on Windows, macOS and Linux (GitHub-hosted x64 and
|
|
449
|
+
arm64 runners): queue rules, exit codes, Ctrl+C while running and while waiting, a SIGKILLed
|
|
450
|
+
calmrun taking its command with it (Unix), SIGTERM to the MCP server (Unix), and daemons
|
|
451
|
+
surviving a normal exit, and the memory ceiling stopping a nested tree. The critical
|
|
452
|
+
free-RAM floor is covered by unit tests only (it depends on the machine). Ctrl+Z is not
|
|
453
|
+
covered by CI (runners have no terminal).
|
|
454
|
+
The MCP server, tickets, the disk gate, named resources, separate queues and `setup` run
|
|
455
|
+
their integration tests on all three systems as well. Killing calmrun ending the tree is tested on Windows only. The
|
|
456
|
+
terminal hand-over is not covered by CI (runners have no terminal).
|
|
457
|
+
- **Battery.** `calmrun doctor` reports whether the machine runs on battery; consider a smaller
|
|
458
|
+
budget then.
|
|
459
|
+
|
|
460
|
+
## Development
|
|
461
|
+
|
|
462
|
+
[ARCHITECTURE.md](ARCHITECTURE.md) maps the modules and explains how to add a tool, a
|
|
463
|
+
gate, an MCP tool or an agent.
|
|
464
|
+
|
|
465
|
+
```sh
|
|
466
|
+
cargo fmt --all --check
|
|
467
|
+
cargo clippy --all-targets -- -D warnings
|
|
468
|
+
cargo test
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
## License
|
|
472
|
+
|
|
473
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
const {
|
|
2
|
+
createWriteStream,
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
mkdtemp,
|
|
6
|
+
rmSync,
|
|
7
|
+
} = require("fs");
|
|
8
|
+
const { join, sep } = require("path");
|
|
9
|
+
const { spawnSync } = require("child_process");
|
|
10
|
+
const { tmpdir } = require("os");
|
|
11
|
+
|
|
12
|
+
const https = require("node:https");
|
|
13
|
+
const http = require("node:http");
|
|
14
|
+
|
|
15
|
+
const tmpDir = tmpdir();
|
|
16
|
+
|
|
17
|
+
const error = (msg) => {
|
|
18
|
+
console.error(msg);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function getProxyForUrl(urlString) {
|
|
23
|
+
const url = new URL(urlString);
|
|
24
|
+
const isHttps = url.protocol === "https:";
|
|
25
|
+
|
|
26
|
+
const noProxy = process.env.NO_PROXY || process.env.no_proxy || "";
|
|
27
|
+
if (noProxy === "*") return null;
|
|
28
|
+
if (noProxy) {
|
|
29
|
+
const hostname = url.hostname.toLowerCase();
|
|
30
|
+
const noProxyList = noProxy.split(",").map((s) => s.trim().toLowerCase());
|
|
31
|
+
for (const entry of noProxyList) {
|
|
32
|
+
if (hostname === entry || hostname.endsWith("." + entry)) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const proxyEnv = isHttps
|
|
39
|
+
? process.env.HTTPS_PROXY || process.env.https_proxy
|
|
40
|
+
: process.env.HTTP_PROXY || process.env.http_proxy;
|
|
41
|
+
|
|
42
|
+
if (!proxyEnv) return null;
|
|
43
|
+
|
|
44
|
+
const proxyUrl = new URL(proxyEnv);
|
|
45
|
+
|
|
46
|
+
let auth = null;
|
|
47
|
+
if (proxyUrl.username || proxyUrl.password) {
|
|
48
|
+
auth = `${proxyUrl.username}:${proxyUrl.password}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
hostname: proxyUrl.hostname,
|
|
53
|
+
port: proxyUrl.port || (proxyUrl.protocol === "https:" ? 443 : 80),
|
|
54
|
+
auth: auth,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function connectThroughProxy(proxy, target) {
|
|
59
|
+
return new Promise((resolve, reject) => {
|
|
60
|
+
const headers = {};
|
|
61
|
+
if (proxy.auth) {
|
|
62
|
+
headers["Proxy-Authorization"] =
|
|
63
|
+
"Basic " + Buffer.from(proxy.auth).toString("base64");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const connectReq = http.request({
|
|
67
|
+
hostname: proxy.hostname,
|
|
68
|
+
port: proxy.port,
|
|
69
|
+
method: "CONNECT",
|
|
70
|
+
path: `${target.hostname}:${target.port || 443}`,
|
|
71
|
+
headers,
|
|
72
|
+
});
|
|
73
|
+
connectReq.on("connect", (res, socket) => {
|
|
74
|
+
if (res.statusCode === 200) {
|
|
75
|
+
resolve(socket);
|
|
76
|
+
} else {
|
|
77
|
+
reject(new Error(`Proxy CONNECT failed with status ${res.statusCode}`));
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
connectReq.on("error", reject);
|
|
81
|
+
connectReq.end();
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function download(urlString, maxRedirects) {
|
|
86
|
+
if (maxRedirects === undefined) maxRedirects = 5;
|
|
87
|
+
return new Promise((resolve, reject) => {
|
|
88
|
+
if (maxRedirects < 0) {
|
|
89
|
+
return reject(new Error("Too many redirects"));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const parsed = new URL(urlString);
|
|
93
|
+
const isHttps = parsed.protocol === "https:";
|
|
94
|
+
const mod = isHttps ? https : http;
|
|
95
|
+
const proxy = getProxyForUrl(urlString);
|
|
96
|
+
|
|
97
|
+
const doRequest = (extraOptions) => {
|
|
98
|
+
const options = Object.assign(
|
|
99
|
+
{
|
|
100
|
+
hostname: parsed.hostname,
|
|
101
|
+
port: parsed.port || (isHttps ? 443 : 80),
|
|
102
|
+
path: parsed.pathname + parsed.search,
|
|
103
|
+
method: "GET",
|
|
104
|
+
headers: { "User-Agent": "cargo-dist-npm-installer" },
|
|
105
|
+
},
|
|
106
|
+
extraOptions || {},
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
if (proxy && !isHttps) {
|
|
110
|
+
// HTTP through HTTP proxy: request the full URL via the proxy
|
|
111
|
+
options.hostname = proxy.hostname;
|
|
112
|
+
options.port = proxy.port;
|
|
113
|
+
options.path = urlString;
|
|
114
|
+
if (proxy.auth) {
|
|
115
|
+
options.headers["Proxy-Authorization"] =
|
|
116
|
+
"Basic " + Buffer.from(proxy.auth).toString("base64");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const req = mod.request(options, (res) => {
|
|
121
|
+
if (
|
|
122
|
+
res.statusCode >= 300 &&
|
|
123
|
+
res.statusCode < 400 &&
|
|
124
|
+
res.headers.location
|
|
125
|
+
) {
|
|
126
|
+
res.resume();
|
|
127
|
+
const nextUrl = new URL(res.headers.location, urlString).toString();
|
|
128
|
+
return download(nextUrl, maxRedirects - 1).then(resolve, reject);
|
|
129
|
+
}
|
|
130
|
+
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
131
|
+
res.resume();
|
|
132
|
+
return reject(new Error(`HTTP ${res.statusCode} from ${urlString}`));
|
|
133
|
+
}
|
|
134
|
+
resolve(res);
|
|
135
|
+
});
|
|
136
|
+
req.on("error", reject);
|
|
137
|
+
req.end();
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
if (proxy && isHttps) {
|
|
141
|
+
connectThroughProxy(proxy, parsed).then(
|
|
142
|
+
(socket) => doRequest({ socket, agent: false }),
|
|
143
|
+
reject,
|
|
144
|
+
);
|
|
145
|
+
} else {
|
|
146
|
+
doRequest();
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
class Package {
|
|
152
|
+
constructor(platform, name, url, filename, zipExt, binaries) {
|
|
153
|
+
let errors = [];
|
|
154
|
+
if (typeof url !== "string") {
|
|
155
|
+
errors.push("url must be a string");
|
|
156
|
+
} else {
|
|
157
|
+
try {
|
|
158
|
+
new URL(url);
|
|
159
|
+
} catch (e) {
|
|
160
|
+
errors.push(e);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (name && typeof name !== "string") {
|
|
164
|
+
errors.push("package name must be a string");
|
|
165
|
+
}
|
|
166
|
+
if (!name) {
|
|
167
|
+
errors.push("You must specify the name of your package");
|
|
168
|
+
}
|
|
169
|
+
if (binaries && typeof binaries !== "object") {
|
|
170
|
+
errors.push("binaries must be a string => string map");
|
|
171
|
+
}
|
|
172
|
+
if (!binaries) {
|
|
173
|
+
errors.push("You must specify the binaries in the package");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (errors.length > 0) {
|
|
177
|
+
let errorMsg =
|
|
178
|
+
"One or more of the parameters you passed to the Binary constructor are invalid:\n";
|
|
179
|
+
errors.forEach((error) => {
|
|
180
|
+
errorMsg += error;
|
|
181
|
+
});
|
|
182
|
+
errorMsg +=
|
|
183
|
+
'\n\nCorrect usage: new Package("my-binary", "https://example.com/binary/download.tar.gz", {"my-binary": "my-binary"})';
|
|
184
|
+
error(errorMsg);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
this.platform = platform;
|
|
188
|
+
this.url = url;
|
|
189
|
+
this.name = name;
|
|
190
|
+
this.filename = filename;
|
|
191
|
+
this.zipExt = zipExt;
|
|
192
|
+
this.installDirectory = join(__dirname, "node_modules", ".bin_real");
|
|
193
|
+
this.binaries = binaries;
|
|
194
|
+
|
|
195
|
+
if (!existsSync(this.installDirectory)) {
|
|
196
|
+
mkdirSync(this.installDirectory, { recursive: true });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
exists() {
|
|
201
|
+
for (const binaryName in this.binaries) {
|
|
202
|
+
const binRelPath = this.binaries[binaryName];
|
|
203
|
+
const binPath = join(this.installDirectory, binRelPath);
|
|
204
|
+
if (!existsSync(binPath)) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
install(suppressLogs = false) {
|
|
212
|
+
if (this.exists()) {
|
|
213
|
+
if (!suppressLogs) {
|
|
214
|
+
console.error(
|
|
215
|
+
`${this.name} is already installed, skipping installation.`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
return Promise.resolve();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
rmSync(this.installDirectory, { recursive: true, force: true });
|
|
223
|
+
} catch {
|
|
224
|
+
// ignore - directory may not exist
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
mkdirSync(this.installDirectory, { recursive: true });
|
|
228
|
+
|
|
229
|
+
if (!suppressLogs) {
|
|
230
|
+
console.error(`Downloading release from ${this.url}`);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return download(this.url)
|
|
234
|
+
.then((res) => {
|
|
235
|
+
return new Promise((resolve, reject) => {
|
|
236
|
+
mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
|
|
237
|
+
if (err) return reject(err);
|
|
238
|
+
let tempFile = join(directory, this.filename);
|
|
239
|
+
const sink = res.pipe(createWriteStream(tempFile));
|
|
240
|
+
sink.on("error", (err) => reject(err));
|
|
241
|
+
sink.on("close", () => {
|
|
242
|
+
if (/\.tar\.*/.test(this.zipExt)) {
|
|
243
|
+
const result = spawnSync("tar", [
|
|
244
|
+
"xf",
|
|
245
|
+
tempFile,
|
|
246
|
+
// The tarballs are stored with a leading directory
|
|
247
|
+
// component; we strip one component in the
|
|
248
|
+
// shell installers too.
|
|
249
|
+
"--strip-components",
|
|
250
|
+
"1",
|
|
251
|
+
"-C",
|
|
252
|
+
this.installDirectory,
|
|
253
|
+
]);
|
|
254
|
+
if (result.status == 0) {
|
|
255
|
+
resolve();
|
|
256
|
+
} else if (result.error) {
|
|
257
|
+
reject(result.error);
|
|
258
|
+
} else {
|
|
259
|
+
reject(
|
|
260
|
+
new Error(
|
|
261
|
+
`An error occurred untarring the artifact: stdout: ${result.stdout}; stderr: ${result.stderr}`,
|
|
262
|
+
),
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
} else if (this.zipExt == ".zip") {
|
|
266
|
+
let result;
|
|
267
|
+
if (this.platform.artifactName.includes("windows")) {
|
|
268
|
+
// Windows does not have "unzip" by default on many installations, instead
|
|
269
|
+
// we use Expand-Archive from powershell
|
|
270
|
+
result = spawnSync("powershell.exe", [
|
|
271
|
+
"-NoProfile",
|
|
272
|
+
"-NonInteractive",
|
|
273
|
+
"-Command",
|
|
274
|
+
`& {
|
|
275
|
+
param([string]$LiteralPath, [string]$DestinationPath)
|
|
276
|
+
Expand-Archive -LiteralPath $LiteralPath -DestinationPath $DestinationPath -Force
|
|
277
|
+
}`,
|
|
278
|
+
tempFile,
|
|
279
|
+
this.installDirectory,
|
|
280
|
+
]);
|
|
281
|
+
} else {
|
|
282
|
+
result = spawnSync("unzip", [
|
|
283
|
+
"-q",
|
|
284
|
+
tempFile,
|
|
285
|
+
"-d",
|
|
286
|
+
this.installDirectory,
|
|
287
|
+
]);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (result.status == 0) {
|
|
291
|
+
resolve();
|
|
292
|
+
} else if (result.error) {
|
|
293
|
+
reject(result.error);
|
|
294
|
+
} else {
|
|
295
|
+
reject(
|
|
296
|
+
new Error(
|
|
297
|
+
`An error occurred unzipping the artifact: stdout: ${result.stdout}; stderr: ${result.stderr}`,
|
|
298
|
+
),
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
} else {
|
|
302
|
+
reject(
|
|
303
|
+
new Error(`Unrecognized file extension: ${this.zipExt}`),
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
})
|
|
310
|
+
.then(() => {
|
|
311
|
+
if (!suppressLogs) {
|
|
312
|
+
console.error(`${this.name} has been installed!`);
|
|
313
|
+
}
|
|
314
|
+
})
|
|
315
|
+
.catch((e) => {
|
|
316
|
+
error(`Error fetching release: ${e.message}`);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
run(binaryName) {
|
|
321
|
+
const promise = !this.exists() ? this.install(true) : Promise.resolve();
|
|
322
|
+
|
|
323
|
+
promise
|
|
324
|
+
.then(() => {
|
|
325
|
+
const [, , ...args] = process.argv;
|
|
326
|
+
|
|
327
|
+
const options = { cwd: process.cwd(), stdio: "inherit" };
|
|
328
|
+
|
|
329
|
+
const binRelPath = this.binaries[binaryName];
|
|
330
|
+
if (!binRelPath) {
|
|
331
|
+
error(`${binaryName} is not a known binary in ${this.name}`);
|
|
332
|
+
}
|
|
333
|
+
const binPath = join(this.installDirectory, binRelPath);
|
|
334
|
+
const result = spawnSync(binPath, args, options);
|
|
335
|
+
|
|
336
|
+
if (result.error) {
|
|
337
|
+
error(result.error);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
process.exit(result.status);
|
|
341
|
+
})
|
|
342
|
+
.catch((e) => {
|
|
343
|
+
error(e.message);
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
module.exports.Package = Package;
|
package/binary.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
const { Package } = require("./binary-install");
|
|
2
|
+
const os = require("os");
|
|
3
|
+
const libc = require("detect-libc");
|
|
4
|
+
|
|
5
|
+
const error = (msg) => {
|
|
6
|
+
console.error(msg);
|
|
7
|
+
process.exit(1);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
name,
|
|
12
|
+
artifactDownloadUrls,
|
|
13
|
+
supportedPlatforms,
|
|
14
|
+
glibcMinimum,
|
|
15
|
+
} = require("./package.json");
|
|
16
|
+
|
|
17
|
+
// FIXME: implement NPM installer handling of fallback download URLs
|
|
18
|
+
const artifactDownloadUrl = artifactDownloadUrls[0];
|
|
19
|
+
const builderGlibcMajorVersion = glibcMinimum.major;
|
|
20
|
+
const builderGlibcMinorVersion = glibcMinimum.series;
|
|
21
|
+
|
|
22
|
+
const getPlatform = () => {
|
|
23
|
+
const rawOsType = os.type();
|
|
24
|
+
const rawArchitecture = os.arch();
|
|
25
|
+
|
|
26
|
+
// We want to use rust-style target triples as the canonical key
|
|
27
|
+
// for a platform, so translate the "os" library's concepts into rust ones
|
|
28
|
+
let osType = "";
|
|
29
|
+
switch (rawOsType) {
|
|
30
|
+
case "Windows_NT":
|
|
31
|
+
osType = "pc-windows-msvc";
|
|
32
|
+
break;
|
|
33
|
+
case "Darwin":
|
|
34
|
+
osType = "apple-darwin";
|
|
35
|
+
break;
|
|
36
|
+
case "Linux":
|
|
37
|
+
osType = "unknown-linux-gnu";
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let arch = "";
|
|
42
|
+
switch (rawArchitecture) {
|
|
43
|
+
case "x64":
|
|
44
|
+
arch = "x86_64";
|
|
45
|
+
break;
|
|
46
|
+
case "arm64":
|
|
47
|
+
arch = "aarch64";
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (rawOsType === "Linux") {
|
|
52
|
+
if (libc.familySync() == "musl") {
|
|
53
|
+
osType = "unknown-linux-musl-dynamic";
|
|
54
|
+
} else if (libc.isNonGlibcLinuxSync()) {
|
|
55
|
+
console.warn(
|
|
56
|
+
"Your libc is neither glibc nor musl; trying static musl binary instead",
|
|
57
|
+
);
|
|
58
|
+
osType = "unknown-linux-musl-static";
|
|
59
|
+
} else {
|
|
60
|
+
let libcVersion = libc.versionSync();
|
|
61
|
+
let splitLibcVersion = libcVersion.split(".");
|
|
62
|
+
let libcMajorVersion = splitLibcVersion[0];
|
|
63
|
+
let libcMinorVersion = splitLibcVersion[1];
|
|
64
|
+
if (
|
|
65
|
+
libcMajorVersion != builderGlibcMajorVersion ||
|
|
66
|
+
libcMinorVersion < builderGlibcMinorVersion
|
|
67
|
+
) {
|
|
68
|
+
// We can't run the glibc binaries, but we can run the static musl ones
|
|
69
|
+
// if they exist
|
|
70
|
+
console.warn(
|
|
71
|
+
"Your glibc isn't compatible; trying static musl binary instead",
|
|
72
|
+
);
|
|
73
|
+
osType = "unknown-linux-musl-static";
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Assume the above succeeded and build a target triple to look things up with.
|
|
79
|
+
// If any of it failed, this lookup will fail and we'll handle it like normal.
|
|
80
|
+
let targetTriple = `${arch}-${osType}`;
|
|
81
|
+
let platform = supportedPlatforms[targetTriple];
|
|
82
|
+
|
|
83
|
+
if (!platform) {
|
|
84
|
+
error(
|
|
85
|
+
`Platform with type "${rawOsType}" and architecture "${rawArchitecture}" is not supported by ${name}.\nYour system must be one of the following:\n\n${Object.keys(
|
|
86
|
+
supportedPlatforms,
|
|
87
|
+
).join(",")}`,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return platform;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const getPackage = () => {
|
|
95
|
+
const platform = getPlatform();
|
|
96
|
+
const url = `${artifactDownloadUrl}/${platform.artifactName}`;
|
|
97
|
+
let filename = platform.artifactName;
|
|
98
|
+
let ext = platform.zipExt;
|
|
99
|
+
let binary = new Package(platform, name, url, filename, ext, platform.bins);
|
|
100
|
+
|
|
101
|
+
return binary;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const install = (suppressLogs) => {
|
|
105
|
+
if (!artifactDownloadUrl || artifactDownloadUrl.length === 0) {
|
|
106
|
+
console.warn("in demo mode, not installing binaries");
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const pkg = getPackage();
|
|
110
|
+
|
|
111
|
+
return pkg.install(suppressLogs);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const run = (binaryName) => {
|
|
115
|
+
const pkg = getPackage();
|
|
116
|
+
|
|
117
|
+
pkg.run(binaryName);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
module.exports = {
|
|
121
|
+
install,
|
|
122
|
+
run,
|
|
123
|
+
getPackage,
|
|
124
|
+
};
|
package/install.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"lockfileVersion": 3,
|
|
3
|
+
"name": "calmrun",
|
|
4
|
+
"packages": {
|
|
5
|
+
"": {
|
|
6
|
+
"bin": {
|
|
7
|
+
"calmrun": "run-calmrun.js"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"detect-libc": "^2.1.2"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"prettier": "^3.9.10"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=14.18",
|
|
17
|
+
"npm": ">=6"
|
|
18
|
+
},
|
|
19
|
+
"hasInstallScript": true,
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"name": "calmrun",
|
|
22
|
+
"version": "0.1.0"
|
|
23
|
+
},
|
|
24
|
+
"node_modules/detect-libc": {
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=8"
|
|
27
|
+
},
|
|
28
|
+
"integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==",
|
|
29
|
+
"license": "Apache-2.0",
|
|
30
|
+
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz",
|
|
31
|
+
"version": "2.1.2"
|
|
32
|
+
},
|
|
33
|
+
"node_modules/prettier": {
|
|
34
|
+
"bin": {
|
|
35
|
+
"prettier": "bin/prettier.cjs"
|
|
36
|
+
},
|
|
37
|
+
"dev": true,
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=14"
|
|
40
|
+
},
|
|
41
|
+
"funding": {
|
|
42
|
+
"url": "https://github.com/prettier/prettier?sponsor=1"
|
|
43
|
+
},
|
|
44
|
+
"integrity": "sha512-skGHTH1Yb9QvaTC4hWnRTzEYtCpApF0UMBPz+t/0ZPzP8ihefo/btKd+QA7pUE0fEwA1SbS/NmhyEcstdXOIdA==",
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.9.10.tgz",
|
|
47
|
+
"version": "3.9.10"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"requires": true,
|
|
51
|
+
"version": "0.1.0"
|
|
52
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,105 @@
|
|
|
1
1
|
{
|
|
2
|
+
"artifactDownloadUrls": [
|
|
3
|
+
"https://github.com/vannt-dev/calmrun/releases/download/v0.1.0"
|
|
4
|
+
],
|
|
5
|
+
"bin": {
|
|
6
|
+
"calmrun": "run-calmrun.js"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"detect-libc": "^2.1.2"
|
|
10
|
+
},
|
|
11
|
+
"description": "A machine-wide queue for heavy commands, so parallel coding agents and terminals don't freeze the computer",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"prettier": "^3.9.10"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=14.18",
|
|
17
|
+
"npm": ">=6"
|
|
18
|
+
},
|
|
19
|
+
"glibcMinimum": {
|
|
20
|
+
"major": 2,
|
|
21
|
+
"series": 35
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/vannt-dev/calmrun",
|
|
24
|
+
"keywords": [
|
|
25
|
+
"command-line-utilities",
|
|
26
|
+
"development-tools",
|
|
27
|
+
"queue",
|
|
28
|
+
"cli",
|
|
29
|
+
"agents",
|
|
30
|
+
"scheduler",
|
|
31
|
+
"build"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
2
34
|
"name": "calmrun",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
35
|
+
"preferUnplugged": true,
|
|
36
|
+
"repository": "https://github.com/vannt-dev/calmrun",
|
|
37
|
+
"scripts": {
|
|
38
|
+
"fmt": "prettier --write **/*.js",
|
|
39
|
+
"fmt:check": "prettier --check **/*.js",
|
|
40
|
+
"postinstall": "node ./install.js"
|
|
41
|
+
},
|
|
42
|
+
"supportedPlatforms": {
|
|
43
|
+
"aarch64-apple-darwin": {
|
|
44
|
+
"artifactName": "calmrun-aarch64-apple-darwin.tar.xz",
|
|
45
|
+
"bins": {
|
|
46
|
+
"calmrun": "calmrun"
|
|
47
|
+
},
|
|
48
|
+
"zipExt": ".tar.xz"
|
|
49
|
+
},
|
|
50
|
+
"aarch64-pc-windows-gnu": {
|
|
51
|
+
"artifactName": "calmrun-aarch64-pc-windows-msvc.zip",
|
|
52
|
+
"bins": {
|
|
53
|
+
"calmrun": "calmrun.exe"
|
|
54
|
+
},
|
|
55
|
+
"zipExt": ".zip"
|
|
56
|
+
},
|
|
57
|
+
"aarch64-pc-windows-msvc": {
|
|
58
|
+
"artifactName": "calmrun-aarch64-pc-windows-msvc.zip",
|
|
59
|
+
"bins": {
|
|
60
|
+
"calmrun": "calmrun.exe"
|
|
61
|
+
},
|
|
62
|
+
"zipExt": ".zip"
|
|
63
|
+
},
|
|
64
|
+
"aarch64-unknown-linux-gnu": {
|
|
65
|
+
"artifactName": "calmrun-aarch64-unknown-linux-gnu.tar.xz",
|
|
66
|
+
"bins": {
|
|
67
|
+
"calmrun": "calmrun"
|
|
68
|
+
},
|
|
69
|
+
"zipExt": ".tar.xz"
|
|
70
|
+
},
|
|
71
|
+
"x86_64-apple-darwin": {
|
|
72
|
+
"artifactName": "calmrun-x86_64-apple-darwin.tar.xz",
|
|
73
|
+
"bins": {
|
|
74
|
+
"calmrun": "calmrun"
|
|
75
|
+
},
|
|
76
|
+
"zipExt": ".tar.xz"
|
|
77
|
+
},
|
|
78
|
+
"x86_64-pc-windows-gnu": {
|
|
79
|
+
"artifactName": "calmrun-x86_64-pc-windows-msvc.zip",
|
|
80
|
+
"bins": {
|
|
81
|
+
"calmrun": "calmrun.exe"
|
|
82
|
+
},
|
|
83
|
+
"zipExt": ".zip"
|
|
84
|
+
},
|
|
85
|
+
"x86_64-pc-windows-msvc": {
|
|
86
|
+
"artifactName": "calmrun-x86_64-pc-windows-msvc.zip",
|
|
87
|
+
"bins": {
|
|
88
|
+
"calmrun": "calmrun.exe"
|
|
89
|
+
},
|
|
90
|
+
"zipExt": ".zip"
|
|
91
|
+
},
|
|
92
|
+
"x86_64-unknown-linux-gnu": {
|
|
93
|
+
"artifactName": "calmrun-x86_64-unknown-linux-gnu.tar.xz",
|
|
94
|
+
"bins": {
|
|
95
|
+
"calmrun": "calmrun"
|
|
96
|
+
},
|
|
97
|
+
"zipExt": ".tar.xz"
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
"version": "0.1.0",
|
|
101
|
+
"volta": {
|
|
102
|
+
"node": "18.14.1",
|
|
103
|
+
"npm": "9.5.0"
|
|
104
|
+
}
|
|
6
105
|
}
|
package/run-calmrun.js
ADDED