Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

6.5 KB
Raw Blame

Resource guard β€” bound a run's memory cost

A long acceptance run boots services, drives a browser, and records media. Left unbounded, its memory use grows until the host swaps itself to a crawl and the machine β€” and the client driving the run β€” freezes. The guard samples the host, classifies a tier, and at red stops only the processes this run started.

What actually grows

Source Why it grows
agent-browser sessions Each named session is a detached daemon plus a headless browser. An unclosed one never exits: hundreds of MB to over 1 GB each, one more per run.
One long-lived session Renderer heap, page cache, full-page screenshots, HAR buffers and CDP frame captures accumulate as long as the session stays open.
Dev servers and type-check workers A dev server and a type checker per run; concurrent runs multiply them.
Recordings Frame sequences held in memory until they are assembled and written.

Low unused RAM is not necessarily memory pressure. macOS can retain large reclaimable caches with little free RAM and no swapping. Use the kernel pressure signal rather than terminating services because of a low free-page percentage.

Platform policy

  • macOS: kern.memorystatus_vm_pressure_level exports 1 = normal, 2 = warning, 4 = critical (not the kernel's internal enum). These map to green/yellow/red. swap and free stay in samples as diagnostics; their percentage thresholds do not apply on macOS. Swap allocation grows dynamically, so its used percentage is not an exhaustion signal either. Explicit RSS/count budgets still apply. Missing or unrecognized pressure is unknown, exits 2 on check, and never triggers automatic termination.
  • Linux: host memory tiers use MemAvailable / MemTotal: yellow at 25% or less, red at 10% or less by default. Swap occupancy is diagnostic only: swapped pages can remain in zram after memory pressure has subsided, so high swap use alone must not block healthy work. This guard does not yet account for cgroup limits or PSI. Do not infer container safety from a green host sample.
  • Native Windows: unsupported. WSL uses the Linux policy, not Windows host memory monitoring.

On macOS, stop-owned requires three consecutive red samples (about 20 seconds between the first and third at the default interval). Earlier red samples record action: pending; the third permits action: stop-owned. Any non-red sample, including unknown, resets the consecutive count. A one-shot check still reports the current tier immediately and never kills processes. Linux keeps its existing immediate red action. This confirmation window is an initial policy, not a guarantee that every workload tolerates that delay.

Run it

Start one guard per run, before the first heavy command, and stop it at teardown:

GUARD=.agents/skills/acceptance/scripts/resource-guard.sh
RUN_DIR=".acceptances/guard/$RUN_TAG"
# The guard may start before any round exists; keep its state out of git too.
mkdir -p .acceptances && { [ -e .acceptances/.gitignore ] || printf '*\n' > .acceptances/.gitignore; }

bash "$GUARD" start \
  --state-dir "$RUN_DIR" --run-tag "$RUN_TAG" --interval 10 \
  --group browser='Google Chrome for Testing|agent-browser' \
  --group devserver='next-server|vite' \
  --yellow 'free=25' \
  --red 'free=10,total.rss=4000,group.browser.rss=1500' \
  --on-red stop-owned

bash "$GUARD" claim --state-dir "$RUN_DIR" --pid "$BROWSER_DAEMON_PID"  # what this run started

bash "$GUARD" check  --json                        # exit 0 green, 10 yellow, 20 red, 2 unknown/error
bash "$GUARD" status --state-dir "$RUN_DIR" --json # samples, red/yellow counts, recorded events
bash "$GUARD" stop   --state-dir "$RUN_DIR"        # at teardown, always

Threshold spec: comma-separated KEY=VALUE, any subset of free=PCT, total.rss=MB, group.NAME.rss=MB, group.NAME.count=N. free thresholds apply on Linux only, per the platform policy above. Swap is still included in samples but never determines the tier on either platform. total.rss is the summed RSS of the declared groups, so it stays 0 without --group. --group NAME=PATTERN is repeatable; the pattern is an ERE matched against each process command line. Keep patterns specific to the processes this run starts.

Ownership β€” what stop-owned may stop

Ownership is proven, never inferred from timing. A process belongs to this run when its command line contains the run tag, or the run registered it with claim, or it descends from a claimed process. stop-owned signals only those, only in the declared groups, TERM first and then KILL after --grace.

A process that merely appeared after the guard started is not owned. Two runs overlapping on one repository each start their own browser and dev server; if "started after my snapshot" counted as ownership, whichever run hit red first would stop the other run's services mid-capture. Register what you start:

bash "$GUARD" claim --state-dir "$RUN_DIR" --pid "$DEV_SERVER_PID"

It never issues a process-name kill, and never touches a process it cannot positively identify β€” a sibling run's browser and a dev server the user started keep running. Anything wider (close --all, pkill) kills other agents' work mid-capture.

Reacting to a tier

Tier What to do
green Continue.
yellow Recycle before it becomes red: close idle browser sessions, drop the oldest recording frames, re-check.
red Pause new heavy work; inspect events.jsonl for pending confirmation or actual stops. After a stop, keep existing evidence and mark interrupted/unexecuted checks blocked until the environment recovers and they are exercised.
unknown Pressure could not be determined. No automatic termination; resolve the missing signal before treating the environment as healthy.

A red tier is a real observation, not a failure to hide: a check the run never exercised is blocked, never passed.

Bound it by construction

The guard is the backstop, not the only defense:

  • Idle timeout. Export AGENT_BROWSER_IDLE_TIMEOUT_MS before the first agent-browser call so an abandoned session shuts itself down.
  • Recycle long sessions. Close and reopen a session (with --restore to keep auth) after a run of heavy captures instead of holding one browser open for the whole round.
  • Write media to disk. Let screenshots, HAR files and frames land in files; do not hold them in memory.
  • One dev server per repository. Reuse a running one instead of starting a second, and stop only what this run started.