#!/bin/bash # @file tool-run.sh # @description Server mode, one Linux user per account (S3): installed as # /opt/agenthub/libexec/tool-run and named by CLAUDE_CODE_SHELL_PREFIX in # every member pane (pane-launch). Claude Code runs each Bash command (and # hook) as `tool-run ''`. The command runs at the kernel's # highest OOM preference, so when memory runs out it is the command — a test # run, a build — that is killed, never the Claude session above it that holds # the conversation (pane-launch puts that one at 300). When the cgroup's OOM # count moved while the command ran, Claude is told why it died. # @author Nguyễn Ngọc Trí Vĩ [ $# -gt 0 ] || exec /bin/bash echo 1000 >/proc/self/oom_score_adj 2>/dev/null cg=$(sed -n 's/^0:://p' /proc/self/cgroup 2>/dev/null) events=/sys/fs/cgroup${cg}/memory.events kills() { sed -n 's/^oom_kill //p' "$events" 2>/dev/null; } before=$(kills) /bin/bash -c "$1" rc=$? if [ "$rc" -ne 0 ] && [ -n "$before" ] && [ "$(kills)" != "$before" ]; then printf '\n%s\n' "AgentHub: the server ran out of memory, so a process of this command was stopped (exit $rc). This shared server is for writing code only: do not run tests, containers or other heavy jobs here — tell the user to run them on their own machine." >&2 fi exit "$rc"