From 63d4179f387af85779e820266ed6f132df45029f Mon Sep 17 00:00:00 2001 From: openhands Date: Thu, 19 Mar 2026 21:19:58 +0000 Subject: [PATCH] fix: Deduplicate hook entries in settings.json on repeated create_agent_session calls (#299) Co-Authored-By: Claude Opus 4.6 (1M context) --- lib/agent-session.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/agent-session.sh b/lib/agent-session.sh index 95dc7b5..1d797fe 100644 --- a/lib/agent-session.sh +++ b/lib/agent-session.sh @@ -67,10 +67,13 @@ create_agent_session() { if [ -f "$settings" ]; then # Append our Stop hook to existing project settings jq --arg cmd "$hook_cmd" ' - .hooks.Stop = (.hooks.Stop // []) + [{ + if (.hooks.Stop // [] | any(.[]; .hooks[]?.command == $cmd)) + then . + else .hooks.Stop = (.hooks.Stop // []) + [{ matcher: "", hooks: [{type: "command", command: $cmd}] }] + end ' "$settings" > "${settings}.tmp" && mv "${settings}.tmp" "$settings" else jq -n --arg cmd "$hook_cmd" '{ @@ -95,10 +98,13 @@ create_agent_session() { local phase_hook_cmd="${phase_hook_script} ${phase_file} ${phase_marker}" if [ -f "$settings" ]; then jq --arg cmd "$phase_hook_cmd" ' - .hooks.PostToolUse = (.hooks.PostToolUse // []) + [{ + if (.hooks.PostToolUse // [] | any(.[]; .hooks[]?.command == $cmd)) + then . + else .hooks.PostToolUse = (.hooks.PostToolUse // []) + [{ matcher: "Bash|Write", hooks: [{type: "command", command: $cmd}] }] + end ' "$settings" > "${settings}.tmp" && mv "${settings}.tmp" "$settings" else jq -n --arg cmd "$phase_hook_cmd" '{