Skip to content
Merged
55 changes: 54 additions & 1 deletion bin/contributor-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,41 @@ with open(p, 'w') as f:
json.dump(d, f, indent=2)
SEEDEOF
chmod 600 "${HOME}/.claude.json" 2>/dev/null || true

# Pre-accept Claude Code's "Bypass Permissions mode" consent (#19049). The
# dialog's default selection is "No, exit", so a bare Enter from any dismissal
# loop makes claude exit to the shell. The documented switch is the user-scope
# setting skipDangerousModePermissionPrompt in ${HOME}/.claude/settings.json
# (https://code.claude.com/docs/en/permission-modes). It is written only when
# this launch already runs claude in bypass mode (the container's own
# --dangerously-skip-permissions --permission-mode bypassPermissions): it
# answers the prompt for a posture the relay already asked for and grants
# nothing new. The file is merged, never replaced, so the contributor's own
# staged keys (proxy env, model, permissions) survive; an unreadable file is
# left untouched rather than overwritten.
if [[ "${SEED_CLAUDE_BYPASS_CONSENT:-}" == "1" ]]; then
python3 - <<'SETTINGSEOF' 2>/dev/null || true
import json, os
d = os.path.join(os.path.expanduser('~'), '.claude')
p = os.path.join(d, 'settings.json')
settings = {}
if os.path.exists(p):
try:
with open(p) as f:
settings = json.load(f)
except Exception:
raise SystemExit(0)
if not isinstance(settings, dict):
raise SystemExit(0)
settings['skipDangerousModePermissionPrompt'] = True
os.makedirs(d, exist_ok=True)
tmp = p + '.tmp'
with open(tmp, 'w') as f:
json.dump(settings, f, indent=2)
os.chmod(tmp, 0o600)
os.replace(tmp, p)
SETTINGSEOF
fi
}

if [[ "${HIVE_CONTRIBUTOR_AGENT_TEST_SEED_CLAUDE_CONFIG:-}" == "1" ]]; then
Expand Down Expand Up @@ -1197,6 +1232,10 @@ fi
# CLI. See seed_claude_config above for why OAuth/subscription claude needs
# this just as much as the API-key paths do.
if [[ "$AGENT_BACKEND" == "litellm" || "$AGENT_BACKEND" == "claude" ]]; then
# Pre-accept the bypass consent only when this launch really is bypass mode.
case "$PERM_FLAG" in
*bypassPermissions*|*--dangerously-skip-permissions*) export SEED_CLAUDE_BYPASS_CONSENT=1 ;;
esac
seed_claude_config
fi

Expand All @@ -1219,7 +1258,21 @@ if [[ "$CONTRIBUTOR_MODE" == "interactive" ]]; then
for _attempt in $(seq 1 $AUTO_DISMISS_ATTEMPTS); do
sleep "$AUTO_DISMISS_INTERVAL"
PANE=$(tmux capture-pane -t "$TMUX_SESSION" -p -S -10 2>/dev/null || true)
if echo "$PANE" | grep -q "trust this folder\|trust the files\|Confirm folder trust\|Enter to confirm"; then
if echo "$PANE" | grep -q "Bypass Permissions mode" && echo "$PANE" | grep -q "No, exit" && echo "$PANE" | grep -q "Yes, I accept"; then
# Claude Code's bypass consent (#19049). It ALSO prints "Enter to
# confirm", and its default selection is "No, exit": the generic
# branch below would send a bare Enter and make claude exit. Move to
# "Yes, I accept" first and press Enter only once the pane shows the
# cursor there. The settings seed normally keeps this from appearing.
if ! echo "$PANE" | grep -Eq "^[[:space:]]*(❯|>|›)[[:space:]]*([0-9]+\.[[:space:]]*)?Yes, I accept"; then
tmux send-keys -t "$TMUX_SESSION" Down 2>/dev/null || true
sleep 1
PANE=$(tmux capture-pane -t "$TMUX_SESSION" -p -S -10 2>/dev/null || true)
fi
if echo "$PANE" | grep -Eq "^[[:space:]]*(❯|>|›)[[:space:]]*([0-9]+\.[[:space:]]*)?Yes, I accept"; then
tmux send-keys -t "$TMUX_SESSION" Enter 2>/dev/null || true
fi
elif echo "$PANE" | grep -q "trust this folder\|trust the files\|Confirm folder trust\|Enter to confirm"; then
tmux send-keys -t "$TMUX_SESSION" Enter 2>/dev/null || true
elif echo "$PANE" | grep -q "Do you trust the contents of this directory"; then
# Codex's own directory-trust prompt: a numbered menu ("1. Yes,
Expand Down
55 changes: 55 additions & 0 deletions bin/contributor-agent.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,61 @@ if [[ "$(seed_key hasCompletedOnboarding)" != "True" ]]; then
exit 1
fi

# 4b. hivecommons/hive#19049: Claude Code's "Bypass Permissions mode" consent
# (default selection "No, exit") is pre-accepted through the documented
# user-scope setting skipDangerousModePermissionPrompt in
# ~/.claude/settings.json, but ONLY when this launch really runs claude in
# bypass mode, and merged into the contributor's own staged settings.
agent_src_for_consent="$(dirname "$0")/contributor-agent.sh"
settings_file="${seed_home}/.claude/settings.json"
settings_key() {
python3 -c "import json,sys; print(json.load(open(sys.argv[1])).get(sys.argv[2]))" "$settings_file" "$1" 2>/dev/null
}
rm -rf "${seed_home}/.claude" "${seed_home}/.claude.json"
run_seed claude >/dev/null 2>&1 || true
if [[ -e "$settings_file" ]]; then
echo "#19049: settings.json must not be written when the launch is not bypass mode" >&2
exit 1
fi
mkdir -p "${seed_home}/.claude"
printf '%s' '{"model":"claude-opus-4","env":{"HTTPS_PROXY":"http://p.invalid:3128"},"permissions":{"deny":["Bash(sudo:*)"]}}' >"$settings_file"
run_seed claude SEED_CLAUDE_BYPASS_CONSENT=1 >/dev/null 2>&1 || true
if [[ "$(settings_key skipDangerousModePermissionPrompt)" != "True" ]]; then
echo "#19049: skipDangerousModePermissionPrompt must be seeded in ~/.claude/settings.json; got:" >&2
cat "$settings_file" >&2
exit 1
fi
if [[ "$(settings_key model)" != "claude-opus-4" ]] || ! grep -q "HTTPS_PROXY" "$settings_file" || ! grep -q 'Bash(sudo:\*)' "$settings_file"; then
echo "#19049: the seed must MERGE into the contributor's staged settings.json, not replace it; got:" >&2
cat "$settings_file" >&2
exit 1
fi
if [[ "$(settings_key defaultMode)" != "None" ]] || grep -q "bypassPermissions" "$settings_file"; then
echo "#19049: the seed must not widen permissions (no defaultMode / bypassPermissions key)" >&2
cat "$settings_file" >&2
exit 1
fi
# An unreadable settings.json is left alone rather than overwritten.
printf '%s' '{not json' >"$settings_file"
run_seed claude SEED_CLAUDE_BYPASS_CONSENT=1 >/dev/null 2>&1 || true
if [[ "$(cat "$settings_file")" != '{not json' ]]; then
echo "#19049: an unparsable settings.json must be left untouched" >&2
exit 1
fi
# The main launch path derives the flag from PERM_FLAG, and the startup dismiss
# loop answers the dialog before its generic "Enter to confirm" branch.
if ! grep -qF 'export SEED_CLAUDE_BYPASS_CONSENT=1' "$agent_src_for_consent" || ! grep -qF '*bypassPermissions*|*--dangerously-skip-permissions*)' "$agent_src_for_consent"; then
echo "#19049: SEED_CLAUDE_BYPASS_CONSENT must be exported only for a bypass-mode PERM_FLAG" >&2
exit 1
fi
consent_line="$(grep -n 'grep -q "Bypass Permissions mode"' "$agent_src_for_consent" | cut -d: -f1 | head -1)"
generic_line="$(grep -n 'grep -q "trust this folder' "$agent_src_for_consent" | cut -d: -f1 | head -1)"
if [[ -z "$consent_line" || -z "$generic_line" || "$consent_line" -ge "$generic_line" ]]; then
echo "#19049: the Bypass Permissions branch must come BEFORE the generic Enter-to-confirm branch (consent=${consent_line:-none} generic=${generic_line:-none})" >&2
exit 1
fi
rm -rf "${seed_home}/.claude"

# 5. hivecommons/hive#7866: the interactive tmux pane must advertise true colour.
# The test hook exits before the tmux session is created, so pin the contract
# at the source: COLORTERM is exported (operator value honoured) before
Expand Down
Loading
Loading