Repository navigation
Expand file tree
/
Copy pathsurveyor-forge-readonly.sh
More file actions
executable file
·134 lines (122 loc) · 5.5 KB
/
Copy pathsurveyor-forge-readonly.sh
File metadata and controls
executable file
·134 lines (122 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env bash
# Surveyor-scoped PreToolUse wrapper around forge-readonly-guard.sh.
#
# This is the Claude Code command-hook implementation of the surveyor's
# read-only forge contract. It is NOT a second classifier: it extracts the
# tool command from hook stdin and delegates to forge-readonly-guard.sh.
#
# Stdin: Claude Code PreToolUse JSON (tool_input.command, or .command).
# Exit 0: allow (the guard admitted the command, or the call is out of scope).
# Exit 2: deny (guard refused, usage error, missing jq, or malformed stdin).
# A deny emits hookSpecificOutput.permissionDecision=deny JSON on stdout AND
# the bare reason on stderr. Exit 2 is deliberate: it blocks the command
# whether or not the JSON parses, so a malformed payload can never fail open.
# The reason is written to both streams because the runtime derives the
# blocking message from the JSON decision when it reads one and from stderr
# otherwise -- emitting both means the operator sees the reason either way.
#
# SCOPING. A PreToolUse `matcher` filters on tool name only, so a `Bash`
# matcher fires for every agent, including the engineer's own lane whose
# writes are legitimate. Attaching this wrapper to a bare `Bash` matcher with
# no scope would therefore refuse `gh pr create`, `git push` and test runs
# everywhere. The runtime does carry the agent identity in the same stdin this
# wrapper already parses: `agent_type` (the agent's name) and `agent_id`
# (present only inside a subagent call).
#
# Set SURVEYOR_FORGE_READONLY_SCOPE to the surveyor's agent name to enforce
# only for that agent. The gate is OPT-IN and strictly additive: with the
# variable unset this wrapper behaves exactly as it always has.
#
# The two failure directions are deliberately NOT symmetric:
# * command classification fails CLOSED -- an unreadable or unclassifiable
# command still denies. That is the property this guard exists for.
# * agent scoping fails OPEN -- if the wrapper cannot positively establish
# that the call is the scoped agent's, it exits 0. It exists to constrain
# the surveyor, so without that identification it has no mandate to refuse,
# and refusing would deny every main-thread call the moment it is installed.
#
# Override the guard path with SURVEYOR_FORGE_READONLY_GUARD (tests).
set -euo pipefail
HERE=$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
GUARD="${SURVEYOR_FORGE_READONLY_GUARD:-${HERE}/forge-readonly-guard.sh}"
SCOPE="${SURVEYOR_FORGE_READONLY_SCOPE:-}"
# Static deny payload used when jq is unavailable. The reason is a fixed
# ASCII string so this JSON needs no escaping.
STATIC_JQ_MISSING='{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"deny: jq is required to parse PreToolUse stdin"}}'
emit_deny() {
local reason="$1"
if command -v jq >/dev/null 2>&1; then
jq -nc --arg reason "$reason" \
'{hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:$reason}}'
else
printf '%s\n' "$STATIC_JQ_MISSING"
fi
# Also on stderr: a runtime that takes its blocking message from stderr
# rather than from the JSON decision would otherwise refuse with no reason.
printf '%s\n' "$reason" >&2
}
# Out of scope: not this wrapper's call to refuse.
#
# SILENT by design. Once installed on a `Bash` matcher this path is taken by
# every main-thread call in every lane, so anything written here is emitted on
# essentially every command the engineer runs. A guard that narrates each of
# its own no-ops is a tax on the everyday path, and the useful signal (a DENY)
# would be the one lost in it. Set SURVEYOR_FORGE_READONLY_DEBUG=1 to trace an
# install, where distinguishing "allowed, out of scope" from "allowed, guard
# admitted it" is exactly the question being asked.
out_of_scope() {
if [ -n "${SURVEYOR_FORGE_READONLY_DEBUG:-}" ]; then
printf 'surveyor-forge-readonly: out of scope (%s), allowing\n' "$1" >&2
fi
exit 0
}
if ! command -v jq >/dev/null 2>&1; then
# Without jq the agent cannot be identified, so a scoped install cannot know
# this call is the surveyor's. Denying would block every lane's Bash.
[ -z "$SCOPE" ] || out_of_scope "jq unavailable, agent unidentifiable"
emit_deny "deny: jq is required to parse PreToolUse stdin"
exit 2
fi
if ! payload="$(cat)"; then
[ -z "$SCOPE" ] || out_of_scope "stdin unreadable, agent unidentifiable"
emit_deny "deny: failed to read PreToolUse stdin"
exit 2
fi
# Scope gate, evaluated before any command handling: an out-of-scope call is
# none of this wrapper's business regardless of what it is asking to run.
if [ -n "$SCOPE" ]; then
agent="$(printf '%s\n' "$payload" | jq -er '.agent_type // empty' 2>/dev/null)" || agent=""
[ "$agent" = "$SCOPE" ] || out_of_scope "agent_type='${agent}' != '${SCOPE}'"
fi
if [ -z "$payload" ]; then
emit_deny "deny: empty PreToolUse stdin"
exit 2
fi
cmd="$(printf '%s\n' "$payload" | jq -er '.tool_input.command // .command // empty' 2>/dev/null)" || cmd=""
if [ -z "$cmd" ]; then
emit_deny "deny: PreToolUse stdin missing tool_input.command"
exit 2
fi
if [ ! -x "$GUARD" ]; then
emit_deny "deny: forge-readonly-guard is missing or not executable"
exit 2
fi
set +e
guard_out="$("$GUARD" --command "$cmd" 2>&1)"
guard_st=$?
set -e
case "$guard_st" in
0)
exit 0
;;
1|2)
reason="$guard_out"
[ -n "$reason" ] || reason="deny: forge-readonly-guard refused the command"
emit_deny "$reason"
exit 2
;;
*)
emit_deny "deny: forge-readonly-guard exited ${guard_st}"
exit 2
;;
esac