Files
bchanot 6c10d3a220 fix(cc): raise oom_score_adj to 500 inside the session (BLK-044)
sshd's systemd override (OOMScoreAdjust=-1000) is inherited by tmux,
dtach and every claude started via cc, so the OOM killer never picks a
runaway claude; systemd-run --scope rejects the property. The inner bash
raises /proc/self/oom_score_adj to 500 (raise-only) before exec claude.
T7-oom asserts it through the stubbed chain.
2026-11-13 12:40:54 +01:00

57 lines
2.6 KiB
Bash

#!/usr/bin/env bash
# ============================================================
# lib/cc.sh — `cc <name> [profile]`: a named dtach session running claude,
# optionally under a per-conversation profile.
#
# Sourced from ~/.bashrc (user step, MIGRATION.md), guarded so a checkout
# without the file does not break the shell:
# [ -f "$HOME/.claude/lib/cc.sh" ] && . "$HOME/.claude/lib/cc.sh"
# The old `dtach_claude()` and `alias cc='dtach_claude'` must go (an alias
# wins over this function and drops the profile argument; doctor warns).
#
# With a profile, claude starts with `--settings lib/profiles/<profile>.json`
# and the session exports CLAUDE_PROFILE + CLAUDE_PROFILE_SETTINGS, which
# `profile.sh current` reads (source=flag). That conversation is frozen:
# `/reload-plugins` re-reads neither the flag file nor a local value the
# flag overrides; to change packs, start another `cc <name> <profile>`.
# Without a profile: manifest defaults + local settings, as before.
# TMPDIR goes to ~/.cache/claude-tmp either way (BLK-021).
# oom_score_adj is raised to 500 inside the session (BLK-044: sshd's -1000
# is inherited), raise-only.
# ============================================================
_cc_name_ok() { [[ "$1" =~ ^[a-z0-9][a-z0-9-]*$ ]]; }
cc() {
local name="${1:-claude-$(date +%H%M%S)}" profile="${2:-}"
local lib="$HOME/.claude/lib" settings=""
_cc_name_ok "$name" \
|| { echo "cc: invalid name '$name' (a-z 0-9 -)" >&2; return 2; }
if [ -n "$profile" ]; then
_cc_name_ok "$profile" \
|| { echo "cc: invalid profile '$profile' (a-z 0-9 -)" >&2; return 2; }
settings="$lib/profiles/$profile.json"
if [ ! -f "$settings" ]; then
echo "cc: unknown profile '$profile'. Known:" >&2
bash "$lib/profile.sh" list >&2
return 2
fi
fi
mkdir -p "$HOME/.cache/claude-tmp" "$HOME/.dtach"
# The inner shell expands $HOME; '^\' is dtach's detach key.
# shellcheck disable=SC2016,SC1003
CLAUDE_PROFILE="$profile" CLAUDE_PROFILE_SETTINGS="$settings" \
TMPDIR="$HOME/.cache/claude-tmp" \
systemd-run --user --quiet --collect --scope --slice=dev.slice \
--unit="cc-${name}" \
dtach -c "$HOME/.dtach/${name}" -e '^\' \
bash -c 'set -a; [ -f "$HOME/.claude/.env" ] && . "$HOME/.claude/.env"
set +a
[ "$(cat /proc/self/oom_score_adj 2>/dev/null)" -lt 500 ] \
2>/dev/null && echo 500 2>/dev/null > /proc/self/oom_score_adj
if [ -n "$CLAUDE_PROFILE_SETTINGS" ]; then
exec claude --settings "$CLAUDE_PROFILE_SETTINGS"
fi
exec claude'
}