#!/usr/bin/env bash # Grimoire — the operator's answer to a project fence ask, read from what ran. # # grimoire_project_fence.sh asks the operator before an act lands in another # project, reports the ask to the server, and leaves the tool call's id in # `grimoire-fence/.fence.pending`. The answer itself never reaches a # hook, but Claude Code sends PostToolUse only for a call that ran. So when # this fires for the pending call, the operator said yes, and the server is # told (POST /api/plugin/gate-ran). An ask that is never followed by this was # declined, or the call failed. # # Registered PostToolUse on the tools the fence checks. Nearly every call has # no ask pending, so the first thing it does is look for a pending file, and # with none it exits before reading the event. # # EXIT 0, ALWAYS, and silent: it decorates a record. set -uo pipefail fence_dir="${TMPDIR:-/tmp}/grimoire-fence" pending="" for pending in "$fence_dir"/*.fence.pending; do [ -e "$pending" ] && break done [ -e "$pending" ] || exit 0 # shellcheck source=plugin/hooks/grimoire_defs.sh . "$(dirname "${BASH_SOURCE[0]}")/grimoire_defs.sh" event=$(cat 2>/dev/null || true) [ -n "$event" ] || exit 0 event_flat=$(printf '%s' "$event" | grimoire_json_flat) session_id=$(grimoire_json_pick "$event_flat" '.session_id') call_id=$(grimoire_json_pick "$event_flat" '.tool_use_id') [ -n "$session_id" ] && [ -n "$call_id" ] || exit 0 safe_sid=$(printf '%s' "$session_id" | tr -c 'A-Za-z0-9._-' '_') pending_file="$fence_dir/${safe_sid}.fence.pending" [ -s "$pending_file" ] || exit 0 [ "$(cat "$pending_file" 2>/dev/null)" = "$call_id" ] || exit 0 rm -f "$pending_file" 2>/dev/null || true grimoire_config || exit 0 command -v curl >/dev/null 2>&1 || exit 0 printf '{"session_id":"%s","call_id":"%s"}' \ "$(printf '%s' "$session_id" | grimoire_json_escape)" \ "$(printf '%s' "$call_id" | grimoire_json_escape)" \ | curl -fsS --max-time 2 \ -H "Authorization: Bearer ${token}" \ -H "X-Grimoire-Plugin: ${plugin_version:-}" \ -H "Content-Type: application/json" \ --data-binary @- \ "${url%/}/api/plugin/gate-ran" >/dev/null 2>&1 || true exit 0