caproom 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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +63 -0
  3. package/bin/caproom +86 -0
  4. package/package.json +30 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 jimkalinov
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 ADDED
@@ -0,0 +1,63 @@
1
+ # caproom
2
+
3
+ Memory-cap any command — AI coding agents, builds, background jobs — on macOS/Linux.
4
+
5
+ ## Why
6
+
7
+ macOS has no working per-process memory limit in userspace. Verified empirically:
8
+
9
+ - `setrlimit(RLIMIT_AS, ...)`, `RLIMIT_DATA`, `RLIMIT_RSS` all return `EINVAL` on modern macOS — the kernel rejects them outright, `ulimit -v` included.
10
+ - launchd's own `HardResourceLimits.ResidentSetSize` is a no-op — a job capped at 100MB was observed running past 590MB, still `state = active`.
11
+
12
+ So a runaway process — a leaking build tool, an AI agent that gets stuck in a loop, a stray `find /` — has nothing standing between it and system OOM on macOS. `caproom` fills that gap with the two mechanisms that actually work.
13
+
14
+ ## What it does
15
+
16
+ ```
17
+ caproom --limit <mb> -- <command> [args...]
18
+ ```
19
+
20
+ Two backends, auto-selected:
21
+
22
+ 1. **Docker cgroup** (`--memory`) — used when Docker is installed and running. Hard cap, real kernel enforcement, zero race window.
23
+ 2. **Polling watchdog** (`ps` RSS + `SIGKILL`) — fallback when Docker isn't available. No dependencies, works anywhere `ps` exists. Has a small race window bounded by `--interval` (default 200ms) — a process can spike briefly past the cap between polls before being killed.
24
+
25
+ Both are honest about their mechanism: neither claims kernel-enforced rlimit, because that doesn't exist on macOS for arbitrary processes.
26
+
27
+ ## Install
28
+
29
+ ```
30
+ npm install -g caproom
31
+ ```
32
+
33
+ or clone and symlink `bin/caproom` onto your `PATH`.
34
+
35
+ ## Usage
36
+
37
+ ```bash
38
+ # cap a build at 2GB
39
+ caproom --limit 2048 -- npm run build
40
+
41
+ # cap an AI coding agent run at 512MB
42
+ caproom --limit 512 -- claude -p "refactor this module"
43
+
44
+ # force the watchdog even if Docker is available
45
+ caproom --limit 1024 --force-watchdog -- ./some-script.sh
46
+
47
+ # use a different docker image for the docker backend (default: node:22-slim)
48
+ caproom --limit 4096 --image python:3.12-slim -- python train.py
49
+ ```
50
+
51
+ Env var overrides: `CAPROOM_LIMIT_MB`, `CAPROOM_IMAGE`, `CAPROOM_INTERVAL`.
52
+
53
+ On cap breach, `caproom` kills the process and exits `137` (same convention as Docker's own OOM-kill exit code).
54
+
55
+ ## Limitations
56
+
57
+ - Docker backend mounts `$PWD` into the container at `/work` and runs there — paths outside `$PWD` aren't visible to the command.
58
+ - Watchdog backend has a real (if small) race window; for a hard guarantee, use the Docker backend.
59
+ - Neither backend can cap a process that immediately forks and hides children under a different watched PID tree in unusual ways — the watchdog only tracks the direct child.
60
+
61
+ ## License
62
+
63
+ MIT
package/bin/caproom ADDED
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env bash
2
+ # caproom — memory-cap any command (AI coding agents, builds, background jobs)
3
+ # on macOS/Linux. macOS has no working RLIMIT_AS/DATA/RSS or launchd RSS
4
+ # enforcement (verified empirically — both are no-ops on modern macOS), so
5
+ # this uses whichever real enforcement mechanism is available:
6
+ # 1. Docker cgroup (--memory) — hard cap, zero race window. Used when
7
+ # `docker` is installed and the daemon is running.
8
+ # 2. Polling watchdog (ps RSS + SIGKILL) — fallback, no dependencies,
9
+ # works everywhere, has a small race window (poll interval).
10
+ set -euo pipefail
11
+
12
+ usage() {
13
+ cat >&2 << 'EOF'
14
+ usage: caproom [--limit <mb>] [--image <docker-image>] [--interval <sec>] -- <command> [args...]
15
+
16
+ --limit <mb> memory cap in MB (default: 4096)
17
+ --image <name> docker image to run the command in, when using the docker
18
+ backend (default: node:22-slim)
19
+ --interval <sec> watchdog poll interval in seconds, fallback backend only
20
+ (default: 0.2)
21
+ --force-watchdog force the polling watchdog even if Docker is available
22
+
23
+ env vars (override flags): CAPROOM_LIMIT_MB, CAPROOM_IMAGE, CAPROOM_INTERVAL
24
+
25
+ examples:
26
+ caproom --limit 2048 -- npm run build
27
+ caproom --limit 512 -- claude --dangerously-skip-permissions -p "task"
28
+ EOF
29
+ exit 1
30
+ }
31
+
32
+ LIMIT_MB="${CAPROOM_LIMIT_MB:-4096}"
33
+ IMAGE="${CAPROOM_IMAGE:-node:22-slim}"
34
+ INTERVAL="${CAPROOM_INTERVAL:-0.2}"
35
+ FORCE_WATCHDOG=0
36
+
37
+ while [[ $# -gt 0 ]]; do
38
+ case "$1" in
39
+ --limit) LIMIT_MB="$2"; shift 2 ;;
40
+ --image) IMAGE="$2"; shift 2 ;;
41
+ --interval) INTERVAL="$2"; shift 2 ;;
42
+ --force-watchdog) FORCE_WATCHDOG=1; shift ;;
43
+ --) shift; break ;;
44
+ -h|--help) usage ;;
45
+ *) break ;;
46
+ esac
47
+ done
48
+
49
+ [[ $# -eq 0 ]] && usage
50
+
51
+ docker_available() {
52
+ [[ "$FORCE_WATCHDOG" -eq 0 ]] && command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1
53
+ }
54
+
55
+ run_docker() {
56
+ echo "caproom: docker cgroup backend, limit=${LIMIT_MB}m image=${IMAGE}" >&2
57
+ exec docker run --rm -i \
58
+ --memory="${LIMIT_MB}m" --memory-swap="${LIMIT_MB}m" \
59
+ -v "$PWD:/work" -w /work "$IMAGE" "$@"
60
+ }
61
+
62
+ run_watchdog() {
63
+ echo "caproom: watchdog backend (docker unavailable), limit=${LIMIT_MB}m poll=${INTERVAL}s" >&2
64
+ local limit_kb=$(( LIMIT_MB * 1024 ))
65
+ "$@" &
66
+ local pid=$!
67
+ local exit_code=0
68
+ while kill -0 "$pid" 2>/dev/null; do
69
+ local rss_kb
70
+ rss_kb=$(ps -o rss= -p "$pid" 2>/dev/null | tr -d ' ')
71
+ if [[ -n "$rss_kb" && "$rss_kb" -gt "$limit_kb" ]]; then
72
+ echo "caproom: pid $pid RSS ${rss_kb}KB exceeded ${limit_kb}KB cap — killing" >&2
73
+ kill -9 "$pid" 2>/dev/null || true
74
+ exit 137
75
+ fi
76
+ sleep "$INTERVAL"
77
+ done
78
+ wait "$pid" || exit_code=$?
79
+ exit "$exit_code"
80
+ }
81
+
82
+ if docker_available; then
83
+ run_docker "$@"
84
+ else
85
+ run_watchdog "$@"
86
+ fi
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "caproom",
3
+ "version": "0.1.0",
4
+ "description": "Memory-cap any command (AI coding agents, builds, background jobs) on macOS/Linux — real enforcement via Docker cgroups or a polling watchdog, since macOS has no working per-process memory rlimit.",
5
+ "bin": {
6
+ "caproom": "bin/caproom"
7
+ },
8
+ "files": [
9
+ "bin/caproom"
10
+ ],
11
+ "keywords": [
12
+ "memory",
13
+ "oom",
14
+ "cgroup",
15
+ "rlimit",
16
+ "macos",
17
+ "watchdog",
18
+ "ai-agent",
19
+ "process-limit"
20
+ ],
21
+ "os": [
22
+ "darwin",
23
+ "linux"
24
+ ],
25
+ "license": "MIT",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/intelogroup/caproom.git"
29
+ }
30
+ }