diff --git a/AGENTS.md b/AGENTS.md index 7ffab02..2a39bd1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -73,6 +73,8 @@ By default, the container image is pulled from `quay.io/guimou/ccbox` (or `ocbox # Shared memory budget for all agent sessions (default 80% of RAM; set per machine) CODEBOX_MEMORY_MAX=24G ./ccbox +# Limit for one session inside that budget (default half of it) +CODEBOX_MEMORY_SESSION_MAX=8G ./ccbox ./ccbox --no-memory-limit # Open a bash shell in the container instead of the harness (same mounts, for troubleshooting) @@ -286,7 +288,7 @@ The launchers also run inside a long-lived pod (`docs/kubernetes.md`): the pod i - **SELinux**: Uses `:z` volume labels for shared relabeling (supports multi-session) - **Logging**: Podman runs with `--log-driver=none` so the TUI output never reaches the journal (`podman logs` is empty) - **Firewall**: Optional, requires `NET_ADMIN` and `NET_RAW` capabilities -- **Memory budget**: Podman sessions of all launchers run in one shared systemd user slice (`codebox.slice` via `--cgroup-parent`), generated by `setup_memory_budget` in `lib/box-common.sh` from `CODEBOX_MEMORY_MAX` (default `80%`, `off` disables) / `CODEBOX_MEMORY_HIGH` (default 90% of max) / `CODEBOX_MEMORY_SWAP_MAX` (default `2G`) / `CODEBOX_SLICE`; `--no-memory-limit` opts one session out. Skipped with a warning when cgroup v2, a delegated systemd user session or the systemd cgroup manager is missing; not used by the Apptainer backend (the pod limit applies) +- **Memory budget**: Podman sessions of all launchers run in one shared systemd user slice (`codebox.slice` via `--cgroup-parent`), generated by `setup_memory_budget` in `lib/box-common.sh` from `CODEBOX_MEMORY_MAX` (default `80%`, `off` disables) / `CODEBOX_MEMORY_HIGH` (default 90% of max) / `CODEBOX_MEMORY_SWAP_MAX` (default `2G`) / `CODEBOX_SLICE`; each session also gets `--memory` from `CODEBOX_MEMORY_SESSION_MAX` (default half of the max, or of an explicit `CODEBOX_MEMORY_HIGH`; `infinity` for none; percentages resolved against `/proc/meminfo`), below `MemoryHigh` by default so a runaway session is OOM-killed in its own container instead of pushing the slice into pressure, where `systemd-oomd` kills whole containers, including other sessions'; `--no-memory-limit` opts one session out. Skipped with a warning when cgroup v2, a delegated systemd user session or the systemd cgroup manager is missing; not used by the Apptainer backend (the pod limit applies) - **Project Isolation**: Each project gets its own history and session data (`~/.claude/ccbox-projects/` for ccbox, `~/.local/share/ocbox-projects/` for ocbox, `~/.qwen/qcbox-projects/` for qcbox, `~/.codex/cxbox-projects/` for cxbox, `~/.omp/ompbox-projects/` for ompbox) - **Multi-Session**: Multiple sessions can run simultaneously per project, each with a unique container name (`{box}-{project}-{hash}-{session-id}`), also set inside the container as `CODEBOX_SESSION` (both runtimes) - **Per-harness mounts**: the mounts listed above are ccbox's (`.credentials.json` is opt-in via `ccbox --with-credentials`). ocbox mounts `~/.config/opencode` (shared config), a per-project data dir as `~/.local/share/opencode`, and a shared `~/.cache/opencode`; the shared `auth.json` is opt-in via `ocbox --with-credentials` (mounted on top of the per-project data dir). qcbox mounts shared `~/.qwen/{settings.json,QWEN.md}` plus per-project `projects/` (chat transcripts, enables `qwen --resume`), `tmp/`, and `file-history/` dirs; `oauth_creds.json` is opt-in via `qcbox --with-credentials`. cxbox mounts a per-project data dir as the entire `~/.codex` (sessions, state DB, memories, goals) and copies the shared `config.toml` into it at launch (refreshed whenever the host file changed; not bind-mounted because Codex saves config edits via rename, which fails on a file mount); `auth.json` is opt-in via `cxbox --with-credentials` (mounted on top of the per-project data dir). ompbox mounts a per-project agent dir as `~/.omp/agent` (agent.db, sessions, blobs, history, plus a `config.yml` copy seeded and refreshed from the shared file like cxbox's `config.toml`), bind-mounts the shared config directories (`commands rules prompts instructions skills hooks tools extensions`) read-write from the host `~/.omp/agent`, and bind-mounts the shared config files (`SYSTEM.md APPEND_SYSTEM.md RULES.md TITLE_SYSTEM.md AGENTS.md models.yml mcp.json keybindings.json`) read-only when present on the host; `~/.omp/.env` and `~/.omp/agent/.env` are opt-in via `ompbox --with-credentials` (both read-only). `ompbox --profile ` selects a host-side account profile from `~/.omp/profiles//` shared across projects: its `config.yml` becomes the source of the per-project config copy (per-profile sync snapshot; profile switches replace the in-container copy), and its `.env`/`models.yml`, when present, replace the shared `agent/.env`/`models.yml` mounts for that run (see docs/usage.md "Account Profiles"). Workspace, credentials (`--with-credentials`, per harness), gcloud (opt-in `--with-gcloud`), gitconfig (opt-in `--with-gitconfig`), extra directories (opt-in `--mount`), clipboard, audio, timezone, npm-global, and GitHub token mounts are common to all launchers (handled by `lib/box-common.sh` and the harness wrappers). diff --git a/docs/architecture.md b/docs/architecture.md index f5f1fe1..fa78c27 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -64,7 +64,7 @@ The wrappers locate the engine relative to their (symlink-resolved) location, su - **Container name**: `{box}-{project}-{hash}-{session-id}` — multiple sessions can run simultaneously in the same project, sharing project data - **Session name inside the container**: `CODEBOX_SESSION` holds the container name, with both runtimes, so a process in the box can tell which session it is (the hostname is the launcher's name, and Podman fills `/run/.containerenv` only for privileged containers). Tools outside the box that read the name from `podman run --name` can match it, e.g. to give each session its own status file - **Firewall**: optional (`--with-firewall`), iptables/ipset allowlist, requires `NET_ADMIN`/`NET_RAW` (added automatically), Linux only -- **Memory budget**: every Podman session of every launcher joins one systemd user slice (`codebox.slice`, `--cgroup-parent`), generated from `CODEBOX_MEMORY_MAX` / `_HIGH` / `_SWAP_MAX` (defaults 80% of RAM / 90% of that / 2G), so all agents share one budget and are throttled and OOM-killed inside it rather than freezing the host; skipped with a warning when cgroup v2, a delegated user session or the systemd cgroup manager is missing (see [usage.md](usage.md#memory-budget)) +- **Memory budget**: every Podman session of every launcher joins one systemd user slice (`codebox.slice`, `--cgroup-parent`), generated from `CODEBOX_MEMORY_MAX` / `_HIGH` / `_SWAP_MAX` (defaults 80% of RAM / 90% of that / 2G), so all agents share one budget and are throttled and OOM-killed inside it rather than freezing the host; each session also gets `--memory` from `CODEBOX_MEMORY_SESSION_MAX` (default half of the max, or of an explicit `CODEBOX_MEMORY_HIGH`, so below `MemoryHigh`), so a runaway session is OOM-killed inside its own container before the slice comes under pressure and `systemd-oomd` kills a whole container, possibly another session's; skipped with a warning when cgroup v2, a delegated user session or the systemd cgroup manager is missing (see [usage.md](usage.md#memory-budget)) > **Breaking change:** the container user and home folder were renamed from `claude` to `coder`. Images built before this change use `/home/claude`; rebuild or re-pull (`ccbox --build` / re-pull from the registry) so mounts line up with `/home/coder`. Host-side data is unaffected — `~/.claude/`, `~/.local/share/ocbox-projects/`, `~/.qwen/qcbox-projects/`, `~/.codex/`, and `~/.omp/` are unchanged; only the in-container home path moved. diff --git a/docs/usage.md b/docs/usage.md index 78c20b4..a0f5233 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -103,6 +103,8 @@ Apptainer environment variables: Coding agents (and the builds, test runs and browsers they start) can use a lot of memory, and several sessions at once can exhaust the host until the desktop freezes. With the Podman runtime on Linux, every session of every launcher (ccbox, ocbox, qcbox, cxbox, ompbox) therefore runs inside one systemd user slice, `codebox.slice`, that holds a **single memory budget shared by all of them**. When the agents together reach it, the kernel first slows them down (`MemoryHigh`), then OOM-kills a process inside the slice (`MemoryMax`). Your desktop, browser and system services stay outside the slice and keep running. +Each session also has **its own limit** inside that budget (half of it by default), so one runaway session is stopped before it can disturb the others: see [One session cannot take the others down](#one-session-cannot-take-the-others-down). + It is on by default. The defaults are percentages of physical RAM, so the same defaults fit any machine: | Variable | Default | Meaning | @@ -110,6 +112,7 @@ It is on by default. The defaults are percentages of physical RAM, so the same d | `CODEBOX_MEMORY_MAX` | `80%` | Hard limit for all sessions together. `off` disables the budget. | | `CODEBOX_MEMORY_HIGH` | 90% of the max | Throttling threshold: above it, the agents are slowed down and their memory reclaimed aggressively | | `CODEBOX_MEMORY_SWAP_MAX` | `2G` | Swap the sessions may use together (`0` for none). Kept small on purpose: an agent swapping heavily (zram included) is what makes a host thrash. | +| `CODEBOX_MEMORY_SESSION_MAX` | half of the max (half of `CODEBOX_MEMORY_HIGH` when you set that one) | Hard limit for one session (`podman run --memory`). `infinity` removes it. | | `CODEBOX_SLICE` | `codebox.slice` | Slice name | Sizes use systemd syntax: a number with an optional `K`/`M`/`G`/`T` suffix, a percentage of RAM (not for the swap limit), or `infinity`. @@ -129,9 +132,21 @@ How it works: - The launcher writes `~/.config/systemd/user/codebox.slice` from these variables and reloads the user manager when the content changed (and updates a running slice live), then starts the container with `--cgroup-parent=codebox.slice`. Configure it through the variables, not by editing the file: the next launch would rewrite it. - The slice is shared, so the values from the most recent launch apply to all running sessions. Keep the variables in your shell profile so every launch agrees. -- A single session can still use the whole budget. If one agent hits it, the kernel kills a process inside the slice (often the agent's biggest child: a build, a test run, a browser), not your desktop. +- A single session can use up to `CODEBOX_MEMORY_SESSION_MAX`. When it gets there, the kernel kills a process inside that session's container (usually its biggest child: a build, a test run, a browser; the agent itself if it is the biggest), and the other sessions keep running. - `ccbox --no-memory-limit` (or `CODEBOX_MEMORY_MAX=off`) runs a session outside the budget. -- Requirements, checked at launch: Linux with cgroup v2, a systemd user session with the memory controller delegated to it, and Podman using the `systemd` cgroup manager (all defaults on Fedora). If one is missing, the launcher warns and starts without a budget. macOS (Podman machine) and the Apptainer runtime are not covered; in a pod, the pod's own memory limit plays this role. +- Requirements, checked at launch: Linux with cgroup v2, a systemd user session with the memory controller delegated to it, and Podman using the `systemd` cgroup manager (all defaults on Fedora). If one is missing, the launcher warns and starts without a budget and without the per-session limit. macOS (Podman machine) and the Apptainer runtime are not covered; in a pod, the pod's own memory limit plays this role. + +### One session cannot take the others down + +The shared budget alone does not protect the sessions from each other. Above `MemoryHigh` the kernel throttles the whole slice instead of killing anything, and on a host running `systemd-oomd` (the default on Fedora) that sustained memory pressure makes `systemd-oomd` kill a **whole container** in the slice after 20 seconds, and not necessarily the one using the memory. A single runaway process (a test run left behind in the background, for example) can this way end an idle session in another project, then its own. + +The per-session limit prevents this: by default it sits below `MemoryHigh` (half of `CODEBOX_MEMORY_MAX`, against 90% for `MemoryHigh`; half of `CODEBOX_MEMORY_HIGH` when you set that one), so the runaway process reaches it first and is OOM-killed by the kernel inside its own container. Notes: + +- A percentage is resolved against physical RAM at launch, as `podman run --memory` takes no percentages. The limit applies to sessions started after you change it; running sessions keep theirs. +- If you set it yourself, keep it below `MemoryHigh`, or it no longer comes first. +- Two sessions that both run away can still reach the shared budget together. +- If one session really needs more (a large build), raise it for that launch: `CODEBOX_MEMORY_SESSION_MAX=40G ccbox`. +- To see why a session disappeared: `journalctl -u systemd-oomd` (a container killed for memory pressure) and `journalctl -k | grep -i oom` (a process killed at a limit). Check usage with `systemctl --user status codebox.slice` or `systemd-cgtop -m user.slice`. Both count the page cache of files the agents touched, most of which the kernel reclaims first under pressure, so they read higher than what the agents really hold. diff --git a/lib/box-common.sh b/lib/box-common.sh index 24b088d..1df7735 100644 --- a/lib/box-common.sh +++ b/lib/box-common.sh @@ -335,6 +335,7 @@ show_help() { echo " CODEBOX_MEMORY_MAX Hard limit for all sessions together (default 80% of RAM; 'off' disables)" echo " CODEBOX_MEMORY_HIGH Throttling threshold (default 90% of the max)" echo " CODEBOX_MEMORY_SWAP_MAX Swap the sessions may use (default 2G)" + echo " CODEBOX_MEMORY_SESSION_MAX Hard limit for one session (default half of the max; 'infinity' for none)" } # Read version from the harness version file if not specified via command line @@ -521,17 +522,25 @@ podman_rt_resolve_image() { # Every podman session of every launcher (ccbox, ocbox, qcbox, cxbox, ompbox) # joins one systemd user slice, so all agents together share a single memory # budget: when they reach it, the kernel throttles and then OOM-kills inside -# the slice, and the rest of the desktop keeps running. The slice unit is -# generated from these variables (percentages are of physical RAM, so the -# defaults fit any machine; set absolute sizes per machine to override): +# the slice, and the rest of the desktop keeps running. Each session also gets +# its own hard limit (podman --memory) below the slice's throttling threshold, +# so one runaway session is OOM-killed inside its own container before it can +# put the whole slice under pressure: on a host running systemd-oomd (Fedora's +# default), sustained pressure on the slice gets a whole container killed, and +# not necessarily the one using the memory. The slice unit and the session +# limit are generated from these variables (percentages are of physical RAM, +# so the defaults fit any machine; set absolute sizes per machine to override): # CODEBOX_MEMORY_MAX hard limit of the slice (default 80%), or "off" # to run sessions without a budget # CODEBOX_MEMORY_HIGH throttling threshold (default 90% of the max) # CODEBOX_MEMORY_SWAP_MAX swap the slice may use (default 2G) +# CODEBOX_MEMORY_SESSION_MAX hard limit of one session (default half of +# the slice's limit, or of CODEBOX_MEMORY_HIGH when +# that is set), or "infinity" for none # CODEBOX_SLICE slice name (default codebox.slice) # Sizes take systemd syntax: a number with an optional K/M/G/T suffix, a -# percentage, or "infinity". CODEBOX_CGROUP_ROOT (default /sys/fs/cgroup) -# exists for the tests. +# percentage, or "infinity". CODEBOX_CGROUP_ROOT (default /sys/fs/cgroup) and +# CODEBOX_MEMINFO (default /proc/meminfo) exist for the tests. MEMORY_BUDGET_STATUS="" @@ -549,12 +558,13 @@ check_memory_size() { exit 1 } -# Default throttling threshold: 90% of the hard limit, in the same unit. -default_memory_high() { - local max="$1" num suffix +# A fraction ($2/$3) of a size ($1): a percentage stays a percentage, an +# absolute size comes back in M. Empty for "infinity" or a result under 1M. +memory_fraction() { + local max="$1" mul="$2" div="$3" num suffix case "$max" in infinity) echo "" ;; - *%) echo "$(( 10#${max%\%} * 9 / 10 ))%" ;; + *%) echo "$(( 10#${max%\%} * mul / div ))%" ;; *) num="${max%[KMGT]}" suffix="${max#"$num"}" @@ -565,12 +575,27 @@ default_memory_high() { G) num=$(( num * 1024 * 1024 * 1024 )) ;; T) num=$(( num * 1024 * 1024 * 1024 * 1024 )) ;; esac - num=$(( num * 9 / 10 / 1024 / 1024 )) + num=$(( num * mul / div / 1024 / 1024 )) (( num > 0 )) && echo "${num}M" || echo "" ;; esac } +# A size ($1) as podman --memory takes it: podman has no percentages, so one +# is resolved against physical RAM here. Empty for no limit. +podman_memory_size() { + local value="$1" total_kb + case "$value" in + ""|infinity) echo "" ;; + *%) + total_kb="$(awk '/^MemTotal:/ {print $2; exit}' "${CODEBOX_MEMINFO:-/proc/meminfo}" 2>/dev/null)" + [[ "$total_kb" =~ ^[0-9]+$ ]] || { echo ""; return 0; } + echo "$(( total_kb * 10#${value%\%} / 100 / 1024 ))m" + ;; + *) echo "$value" ;; + esac +} + # Why the budget cannot be applied on this host (empty when it can). memory_budget_unavailable() { local cg_root="${CODEBOX_CGROUP_ROOT:-/sys/fs/cgroup}" uid @@ -589,14 +614,17 @@ memory_budget_unavailable() { } # Write the slice unit if its content changed and apply it, then point -# podman at it. Sets MEMORY_BUDGET_SLICE (empty when no budget applies) and -# MEMORY_BUDGET_STATUS for the launch summary. +# podman at it. Sets MEMORY_BUDGET_SLICE (empty when no budget applies), +# MEMORY_SESSION_LIMIT (the session's own limit for podman --memory, empty +# for none) and MEMORY_BUDGET_STATUS for the launch summary. setup_memory_budget() { MEMORY_BUDGET_SLICE="" + MEMORY_SESSION_LIMIT="" local slice="${CODEBOX_SLICE:-codebox.slice}" local max="${CODEBOX_MEMORY_MAX:-80%}" local high="${CODEBOX_MEMORY_HIGH:-}" local swap="${CODEBOX_MEMORY_SWAP_MAX:-2G}" + local session="${CODEBOX_MEMORY_SESSION_MAX:-}" if $NO_MEMORY_LIMIT || [[ "$max" == off ]]; then MEMORY_BUDGET_STATUS="Memory budget: disabled" @@ -609,7 +637,12 @@ setup_memory_budget() { check_memory_size CODEBOX_MEMORY_MAX "$max" check_memory_size CODEBOX_MEMORY_HIGH "$high" check_memory_size CODEBOX_MEMORY_SWAP_MAX "$swap" false - [[ -z "$high" ]] && high="$(default_memory_high "$max")" + check_memory_size CODEBOX_MEMORY_SESSION_MAX "$session" + [[ -z "$high" ]] && high="$(memory_fraction "$max" 9 10)" + # Half of the throttling threshold when that one is set explicitly, so + # the default always stays below it; half of the hard limit otherwise + [[ -z "$session" ]] && session="$(memory_fraction "${CODEBOX_MEMORY_HIGH:-$max}" 1 2)" + [[ -z "$session" ]] && session="$(memory_fraction "$max" 1 2)" local reason reason="$(memory_budget_unavailable)" @@ -650,7 +683,8 @@ setup_memory_budget() { fi MEMORY_BUDGET_SLICE="$slice" - MEMORY_BUDGET_STATUS="Memory budget: ${slice} (max ${max}${high:+, high ${high}}, swap ${swap}; shared by all sessions)" + MEMORY_SESSION_LIMIT="$(podman_memory_size "$session")" + MEMORY_BUDGET_STATUS="Memory budget: ${slice} (max ${max}${high:+, high ${high}}, swap ${swap}; shared by all sessions${MEMORY_SESSION_LIMIT:+; ${session} per session})" } # Render BOX_SPEC + launch command into RUN_ARGS for `podman run` @@ -678,6 +712,7 @@ podman_rt_render() { setup_memory_budget if [[ -n "$MEMORY_BUDGET_SLICE" ]]; then RUN_ARGS+=("--cgroup-parent=${MEMORY_BUDGET_SLICE}") + [[ -n "$MEMORY_SESSION_LIMIT" ]] && RUN_ARGS+=("--memory=${MEMORY_SESSION_LIMIT}") fi local entry kind a b c diff --git a/tests/golden/ccbox-all-opts.txt b/tests/golden/ccbox-all-opts.txt index 1e201e2..ee009de 100644 --- a/tests/golden/ccbox-all-opts.txt +++ b/tests/golden/ccbox-all-opts.txt @@ -4,6 +4,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ccbox-default.txt b/tests/golden/ccbox-default.txt index baf4b55..b8f02eb 100644 --- a/tests/golden/ccbox-default.txt +++ b/tests/golden/ccbox-default.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ccbox-explicit.txt b/tests/golden/ccbox-explicit.txt index b67b72a..bcca84f 100644 --- a/tests/golden/ccbox-explicit.txt +++ b/tests/golden/ccbox-explicit.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ccbox-mount.txt b/tests/golden/ccbox-mount.txt index 1120793..54b9b01 100644 --- a/tests/golden/ccbox-mount.txt +++ b/tests/golden/ccbox-mount.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ccbox-no-github.txt b/tests/golden/ccbox-no-github.txt index 80fc58b..2cd650e 100644 --- a/tests/golden/ccbox-no-github.txt +++ b/tests/golden/ccbox-no-github.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ccbox-safe-mode-args.txt b/tests/golden/ccbox-safe-mode-args.txt index d812562..cd78227 100644 --- a/tests/golden/ccbox-safe-mode-args.txt +++ b/tests/golden/ccbox-safe-mode-args.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ccbox-settings-env.txt b/tests/golden/ccbox-settings-env.txt index a1ecafe..04bcbf7 100644 --- a/tests/golden/ccbox-settings-env.txt +++ b/tests/golden/ccbox-settings-env.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ccbox-settings.txt b/tests/golden/ccbox-settings.txt index a1ecafe..04bcbf7 100644 --- a/tests/golden/ccbox-settings.txt +++ b/tests/golden/ccbox-settings.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ccbox-shell.txt b/tests/golden/ccbox-shell.txt index 9d19ac5..48d7ec6 100644 --- a/tests/golden/ccbox-shell.txt +++ b/tests/golden/ccbox-shell.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ccbox-tmux.txt b/tests/golden/ccbox-tmux.txt index 7da67db..6336e35 100644 --- a/tests/golden/ccbox-tmux.txt +++ b/tests/golden/ccbox-tmux.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/clipboard-selinux.txt b/tests/golden/clipboard-selinux.txt index 84e5137..72ba5c4 100644 --- a/tests/golden/clipboard-selinux.txt +++ b/tests/golden/clipboard-selinux.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/clipboard-wayland.txt b/tests/golden/clipboard-wayland.txt index 6f0a5cf..39ef926 100644 --- a/tests/golden/clipboard-wayland.txt +++ b/tests/golden/clipboard-wayland.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=26214m --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/cxbox-all-opts.txt b/tests/golden/cxbox-all-opts.txt index 097bd8c..f03efe4 100644 --- a/tests/golden/cxbox-all-opts.txt +++ b/tests/golden/cxbox-all-opts.txt @@ -4,6 +4,7 @@ --hostname cxbox --init --log-driver=none +--memory=26214m --name cxbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/cxbox-args.txt b/tests/golden/cxbox-args.txt index 66bf93d..6a19269 100644 --- a/tests/golden/cxbox-args.txt +++ b/tests/golden/cxbox-args.txt @@ -2,6 +2,7 @@ --hostname cxbox --init --log-driver=none +--memory=26214m --name cxbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/cxbox-default.txt b/tests/golden/cxbox-default.txt index 2e72f4f..5d26cc9 100644 --- a/tests/golden/cxbox-default.txt +++ b/tests/golden/cxbox-default.txt @@ -2,6 +2,7 @@ --hostname cxbox --init --log-driver=none +--memory=26214m --name cxbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/memory-absolute-live.txt b/tests/golden/memory-absolute-live.txt index 6d1b73b..c4395fa 100644 --- a/tests/golden/memory-absolute-live.txt +++ b/tests/golden/memory-absolute-live.txt @@ -2,6 +2,7 @@ --hostname ccbox --init --log-driver=none +--memory=24576M --name ccbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/memory-percent.txt b/tests/golden/memory-percent.txt index be4a407..3100f6d 100644 --- a/tests/golden/memory-percent.txt +++ b/tests/golden/memory-percent.txt @@ -2,6 +2,7 @@ --hostname ompbox --init --log-driver=none +--memory=16384m --name ompbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/memory-session-off.txt b/tests/golden/memory-session-off.txt new file mode 100644 index 0000000..2e6f94f --- /dev/null +++ b/tests/golden/memory-session-off.txt @@ -0,0 +1,44 @@ +--cgroup-parent=codebox.slice +--hostname ocbox +--init +--log-driver=none +--name ocbox-demo-HASH-SESSION +--rm +--userns=keep-id:uid=1000,gid=1000 +-e ANTHROPIC_API_KEY=anthropic-stub +-e AWS_REGION=eu-west-1 +-e CODEBOX_SESSION=ocbox-demo-HASH-SESSION +-e GH_TOKEN=ghp_stubtoken +-e GITHUB_TOKEN=ghp_stubtoken +-e LANG=C.UTF-8 +-e NO_COLOR=1 +-e OPENAI_API_KEY=openai-stub +-e PROJECT_NAME=demo +-e TERM=xterm-256color +-e TZ=UTC +-it +-v /etc/localtime:/etc/localtime:ro +-v ROOT/home/.cache/opencode:/home/coder/.cache/opencode:z +-v ROOT/home/.config/opencode:/home/coder/.config/opencode:z +-v ROOT/home/.local/share/ocbox-projects/demo_HASH/data:/home/coder/.local/share/opencode:z +-v ROOT/npm-global:/home/coder/.npm-global:ro,z +-v ROOT/repos/demo:/workspace:z +-w /workspace +-- +quay.io/guimou/ocbox:TAG +opencode +## .config/systemd/user/codebox.slice +# Generated by the codebox launchers (ccbox, ocbox, qcbox, cxbox, ompbox). +# Shared memory budget for all agent containers. Do not edit: set +# CODEBOX_MEMORY_MAX / CODEBOX_MEMORY_HIGH / CODEBOX_MEMORY_SWAP_MAX instead; +# the next launch rewrites this file when they change. +[Unit] +Description=Shared memory budget for codebox agent containers + +[Slice] +MemoryAccounting=yes +MemoryHigh=72% +MemoryMax=80% +MemorySwapMax=2G +## systemctl calls +systemctl --user daemon-reload diff --git a/tests/golden/memory-session.txt b/tests/golden/memory-session.txt new file mode 100644 index 0000000..7685820 --- /dev/null +++ b/tests/golden/memory-session.txt @@ -0,0 +1,72 @@ +--cgroup-parent=codebox.slice +--hostname ccbox +--init +--log-driver=none +--memory=12G +--name ccbox-demo-HASH-SESSION +--rm +--userns=keep-id:uid=1000,gid=1000 +-e ANTHROPIC_API_KEY=anthropic-stub +-e AWS_REGION=eu-west-1 +-e CODEBOX_SESSION=ccbox-demo-HASH-SESSION +-e GH_TOKEN=ghp_stubtoken +-e GITHUB_TOKEN=ghp_stubtoken +-e LANG=C.UTF-8 +-e NO_COLOR=1 +-e PROJECT_NAME=demo +-e TERM=xterm-256color +-e TZ=UTC +-it +-v /etc/localtime:/etc/localtime:ro +-v ROOT/home/.claude.json:/home/coder/.claude.json:z +-v ROOT/home/.claude/CLAUDE.md:/home/coder/.claude/CLAUDE.md:z +-v ROOT/home/.claude/RULES.md:/home/coder/.claude/RULES.md:ro,z +-v ROOT/home/.claude/agents:/home/coder/.claude/agents:z +-v ROOT/home/.claude/backups:/home/coder/.claude/backups:z +-v ROOT/home/.claude/cache:/home/coder/.claude/cache:z +-v ROOT/home/.claude/ccbox-projects/demo_HASH/debug:/home/coder/.claude/debug:z +-v ROOT/home/.claude/ccbox-projects/demo_HASH/history.jsonl:/home/coder/.claude/history.jsonl:z +-v ROOT/home/.claude/ccbox-projects/demo_HASH/logs:/home/coder/.claude/logs:z +-v ROOT/home/.claude/ccbox-projects/demo_HASH/plans:/home/coder/.claude/plans:z +-v ROOT/home/.claude/ccbox-projects/demo_HASH/plugins:/home/coder/.claude/plugins:z +-v ROOT/home/.claude/ccbox-projects/demo_HASH/projects:/home/coder/.claude/projects:z +-v ROOT/home/.claude/ccbox-projects/demo_HASH/session-env:/home/coder/.claude/session-env:z +-v ROOT/home/.claude/ccbox-projects/demo_HASH/shell-snapshots:/home/coder/.claude/shell-snapshots:z +-v ROOT/home/.claude/ccbox-projects/demo_HASH/tasks:/home/coder/.claude/tasks:z +-v ROOT/home/.claude/ccbox-projects/demo_HASH/teams:/home/coder/.claude/teams:z +-v ROOT/home/.claude/ccbox-projects/demo_HASH/todos:/home/coder/.claude/todos:z +-v ROOT/home/.claude/commands:/home/coder/.claude/commands:z +-v ROOT/home/.claude/file-history:/home/coder/.claude/file-history:z +-v ROOT/home/.claude/hooks:/home/coder/.claude/hooks:z +-v ROOT/home/.claude/keybindings.json:/home/coder/.claude/keybindings.json:z +-v ROOT/home/.claude/paste-cache:/home/coder/.claude/paste-cache:z +-v ROOT/home/.claude/rules:/home/coder/.claude/rules:z +-v ROOT/home/.claude/settings.json:/home/coder/.claude/settings.json:z +-v ROOT/home/.claude/settings.local.json:/home/coder/.claude/settings.local.json:z +-v ROOT/home/.claude/skills:/home/coder/.claude/skills:z +-v ROOT/home/.claude/stats-cache.json:/home/coder/.claude/stats-cache.json:z +-v ROOT/home/.claude/statsig:/home/coder/.claude/statsig:z +-v ROOT/home/.claude/status-line.sh:/home/coder/.claude/status-line.sh:ro,z +-v ROOT/home/.claude/themes:/home/coder/.claude/themes:z +-v ROOT/home/.claude/workflows:/home/coder/.claude/workflows:z +-v ROOT/npm-global:/home/coder/.npm-global:ro,z +-v ROOT/repos/demo:/workspace:z +-w /workspace +-- +quay.io/guimou/ccbox:TAG +claude +## .config/systemd/user/codebox.slice +# Generated by the codebox launchers (ccbox, ocbox, qcbox, cxbox, ompbox). +# Shared memory budget for all agent containers. Do not edit: set +# CODEBOX_MEMORY_MAX / CODEBOX_MEMORY_HIGH / CODEBOX_MEMORY_SWAP_MAX instead; +# the next launch rewrites this file when they change. +[Unit] +Description=Shared memory budget for codebox agent containers + +[Slice] +MemoryAccounting=yes +MemoryHigh=44236M +MemoryMax=48G +MemorySwapMax=2G +## systemctl calls +systemctl --user daemon-reload diff --git a/tests/golden/ocbox-all-opts.txt b/tests/golden/ocbox-all-opts.txt index 2aa7e01..c30a16b 100644 --- a/tests/golden/ocbox-all-opts.txt +++ b/tests/golden/ocbox-all-opts.txt @@ -4,6 +4,7 @@ --hostname ocbox --init --log-driver=none +--memory=26214m --name ocbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ocbox-args.txt b/tests/golden/ocbox-args.txt index a833149..2e92d53 100644 --- a/tests/golden/ocbox-args.txt +++ b/tests/golden/ocbox-args.txt @@ -2,6 +2,7 @@ --hostname ocbox --init --log-driver=none +--memory=26214m --name ocbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ocbox-default.txt b/tests/golden/ocbox-default.txt index 2e6f94f..64558c1 100644 --- a/tests/golden/ocbox-default.txt +++ b/tests/golden/ocbox-default.txt @@ -2,6 +2,7 @@ --hostname ocbox --init --log-driver=none +--memory=26214m --name ocbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ompbox-all-opts.txt b/tests/golden/ompbox-all-opts.txt index bf0d111..12f96e2 100644 --- a/tests/golden/ompbox-all-opts.txt +++ b/tests/golden/ompbox-all-opts.txt @@ -4,6 +4,7 @@ --hostname ompbox --init --log-driver=none +--memory=26214m --name ompbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ompbox-args.txt b/tests/golden/ompbox-args.txt index a6d1864..419dbfe 100644 --- a/tests/golden/ompbox-args.txt +++ b/tests/golden/ompbox-args.txt @@ -2,6 +2,7 @@ --hostname ompbox --init --log-driver=none +--memory=26214m --name ompbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ompbox-default.txt b/tests/golden/ompbox-default.txt index e3eeff0..4566cce 100644 --- a/tests/golden/ompbox-default.txt +++ b/tests/golden/ompbox-default.txt @@ -2,6 +2,7 @@ --hostname ompbox --init --log-driver=none +--memory=26214m --name ompbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ompbox-profile-env.txt b/tests/golden/ompbox-profile-env.txt index 72bd06f..3204918 100644 --- a/tests/golden/ompbox-profile-env.txt +++ b/tests/golden/ompbox-profile-env.txt @@ -2,6 +2,7 @@ --hostname ompbox --init --log-driver=none +--memory=26214m --name ompbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/ompbox-profile.txt b/tests/golden/ompbox-profile.txt index 8d519eb..f70d61f 100644 --- a/tests/golden/ompbox-profile.txt +++ b/tests/golden/ompbox-profile.txt @@ -2,6 +2,7 @@ --hostname ompbox --init --log-driver=none +--memory=26214m --name ompbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/qcbox-all-opts.txt b/tests/golden/qcbox-all-opts.txt index 0647c3b..5704f38 100644 --- a/tests/golden/qcbox-all-opts.txt +++ b/tests/golden/qcbox-all-opts.txt @@ -4,6 +4,7 @@ --hostname qcbox --init --log-driver=none +--memory=26214m --name qcbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/golden/qcbox-default.txt b/tests/golden/qcbox-default.txt index 020a4bf..8c56c77 100644 --- a/tests/golden/qcbox-default.txt +++ b/tests/golden/qcbox-default.txt @@ -2,6 +2,7 @@ --hostname qcbox --init --log-driver=none +--memory=26214m --name qcbox-demo-HASH-SESSION --rm --userns=keep-id:uid=1000,gid=1000 diff --git a/tests/render-test.sh b/tests/render-test.sh index 46f4707..a97e91d 100755 --- a/tests/render-test.sh +++ b/tests/render-test.sh @@ -60,6 +60,10 @@ mkdir -p "$USER_CG" echo "cpuset cpu io memory hugetlb pids misc" > "${CGROUP_ROOT}/cgroup.controllers" echo "cpu memory pids" > "${USER_CG}/cgroup.controllers" SLICE_DIR="${FAKE_HOME}/.config/systemd/user" +# Fixed amount of RAM (64 GiB), so a per-session limit given as a percentage +# resolves to the same value on every machine (CODEBOX_MEMINFO points here) +MEMINFO="${ROOT}/meminfo" +echo "MemTotal: 67108864 kB" > "$MEMINFO" # Wayland socket and selinuxfs trees for the clipboard scenarios RUNTIME_DIR="${ROOT}/run" @@ -184,6 +188,8 @@ SCENARIOS=( # scenario above; these set the CODEBOX_MEMORY_* variables (see below) "memory-absolute-live|ccbox|" "memory-percent|ompbox|" + "memory-session|ccbox|" + "memory-session-off|ocbox|" "memory-env-off|cxbox|" "memory-flag-off|ccbox|--no-memory-limit" "memory-unavailable|qcbox|" @@ -194,6 +200,7 @@ SCENARIOS=( "clipboard-selinux|ccbox|" "!memory-invalid-max|ccbox|" "!memory-invalid-swap|ccbox|" + "!memory-invalid-session|ccbox|" "!memory-invalid-slice|ccbox|" ) @@ -218,6 +225,7 @@ run_launcher() { # uses the caller's name (scenario) and EXTRA_ENV AWS_REGION=eu-west-1 \ NO_COLOR=1 \ CODEBOX_CGROUP_ROOT="$CGROUP_ROOT" \ + CODEBOX_MEMINFO="$MEMINFO" \ SYSTEMCTL_LOG="${OUT}/${name}.systemctl" \ "${EXTRA_ENV[@]}" \ "${BIN}/${box}" "$@" > "${OUT}/${name}.log" 2>&1 @@ -269,12 +277,15 @@ for entry in "${SCENARIOS[@]}"; do case "${name#!}" in memory-absolute-live) EXTRA_ENV=(CODEBOX_MEMORY_MAX=48G CODEBOX_MEMORY_SWAP_MAX=0 STUB_SLICE_ACTIVE=0) ;; memory-percent) EXTRA_ENV=(CODEBOX_MEMORY_MAX=60% CODEBOX_MEMORY_HIGH=50% CODEBOX_SLICE=agents.slice) ;; + memory-session) EXTRA_ENV=(CODEBOX_MEMORY_MAX=48G CODEBOX_MEMORY_SESSION_MAX=12G) ;; + memory-session-off) EXTRA_ENV=(CODEBOX_MEMORY_SESSION_MAX=infinity) ;; memory-env-off) EXTRA_ENV=(CODEBOX_MEMORY_MAX=off) ;; memory-unavailable) EXTRA_ENV=(STUB_CGROUP_MANAGER=cgroupfs) ;; clipboard-wayland) EXTRA_ENV=("XDG_RUNTIME_DIR=${RUNTIME_DIR}" "CODEBOX_SELINUX_ROOT=${ROOT}/selinux-permissive") ;; clipboard-selinux) EXTRA_ENV=("XDG_RUNTIME_DIR=${RUNTIME_DIR}" WAYLAND_DISPLAY=wayland-0 "CODEBOX_SELINUX_ROOT=${ROOT}/selinux-enforcing") ;; memory-invalid-max) EXTRA_ENV=(CODEBOX_MEMORY_MAX=lots) ;; memory-invalid-swap) EXTRA_ENV=(CODEBOX_MEMORY_SWAP_MAX=50%) ;; + memory-invalid-session) EXTRA_ENV=(CODEBOX_MEMORY_SESSION_MAX=half) ;; memory-invalid-slice) EXTRA_ENV=(CODEBOX_SLICE=../evil) ;; esac rm -rf "$SLICE_DIR"