@chrono-meta/fh-gate 3.20.0 → 3.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env bash
2
+ # test_subagent_tally_hook_lanes.sh — known-pair lanes for scripts/subagent_tally_hook.sh.
3
+ #
4
+ # WHAT IS PINNED: SubagentStop fires for dispatched agents AND for Claude Code's internal agents.
5
+ # Fixture shapes below are the MEASURED payloads (claude 2.1.282 headless, 2026-09-25), trimmed to
6
+ # the fields that matter:
7
+ # dispatch, subagent_type=Explore → agent_type "Explore"
8
+ # dispatch, subagent_type omitted → agent_type "general-purpose"
9
+ # /compact (internal, PreCompact fired) → agent_type ""
10
+ # The tally must count the first two and skip the third — and must COUNT on every doubtful input
11
+ # (absent field · non-JSON · empty stdin), because under-counting a real dispatch is the silent
12
+ # fail-open ④-e exists to close.
13
+ #
14
+ # REVERT PROBE: the old inline hook (date-only append) is replayed against the internal-agent
15
+ # fixture and MUST count it. If it ever stops counting, the lane pair is no longer measuring the
16
+ # defect it was written for.
17
+ #
18
+ # Exit 0 = discriminates · 1 = would mis-tally.
19
+ set -uo pipefail
20
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
21
+ SUBJ="$SCRIPT_DIR/subagent_tally_hook.sh"
22
+ FAILED=0; PASS=0
23
+ chk() { if [ "$1" -eq 0 ]; then PASS=$((PASS+1)); echo " ✅ $2"; else FAILED=1; echo " ❌ $2"; fi; }
24
+ [ -f "$SUBJ" ] || { echo "FAIL subject $SUBJ missing"; exit 1; }
25
+ command -v python3 >/dev/null 2>&1 || { echo "SKIP python3 absent — the discriminator cannot run here (the hook then COUNTS everything, the safe direction). Not a pass."; exit 0; }
26
+
27
+ T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
28
+ TODAY=$(date +%Y-%m-%d)
29
+ run() { # $1 = stdin payload ; echoes today's tally count after one invocation on a fresh file
30
+ local f="$T/tally.$RANDOM$RANDOM"
31
+ printf '%s' "$1" | FH_TALLY_FILE="$f" bash "$SUBJ" >/dev/null 2>&1 || { echo RC_NONZERO; return; }
32
+ if [ -f "$f" ]; then grep -c "^$TODAY$" "$f"; else echo 0; fi
33
+ }
34
+ P_EXPLORE='{"session_id":"s1","hook_event_name":"SubagentStop","agent_id":"a1","agent_type":"Explore"}'
35
+ P_GENERAL='{"session_id":"s1","hook_event_name":"SubagentStop","agent_id":"a2","agent_type":"general-purpose"}'
36
+ P_INTERNAL='{"session_id":"s1","hook_event_name":"SubagentStop","agent_id":"a3","agent_type":""}'
37
+ P_NOFIELD='{"session_id":"s1","hook_event_name":"SubagentStop","agent_id":"a4"}'
38
+
39
+ echo "── known-positive: dispatched agents are counted"
40
+ [ "$(run "$P_EXPLORE")" = 1 ] ; chk $? "agent_type Explore → counted"
41
+ [ "$(run "$P_GENERAL")" = 1 ] ; chk $? "subagent_type omitted (general-purpose) → counted"
42
+ echo "── known-negative: the internal agent is skipped"
43
+ [ "$(run "$P_INTERNAL")" = 0 ] ; chk $? "agent_type \"\" (compaction) → NOT counted"
44
+ echo "── every doubt counts (under-count is the silent direction)"
45
+ [ "$(run "$P_NOFIELD")" = 1 ] ; chk $? "agent_type absent (older CLI) → counted"
46
+ [ "$(run 'not json {')" = 1 ] ; chk $? "non-JSON stdin → counted"
47
+ [ "$(run '')" = 1 ] ; chk $? "empty stdin → counted"
48
+ [ "$(run '[1,2]')" = 1 ] ; chk $? "JSON but not an object → counted"
49
+ [ "$(run '{"agent_type":null}')" = 1 ] ; chk $? "agent_type null (not the empty string) → counted"
50
+
51
+ echo "── tally line format unchanged (④-e matches ^DATE\$ exactly)"
52
+ f="$T/fmt"; printf '%s' "$P_EXPLORE" | FH_TALLY_FILE="$f" bash "$SUBJ"
53
+ [ "$(cat "$f")" = "$TODAY" ] ; chk $? "the tally line is the bare date, nothing appended"
54
+ printf '%s' "$P_INTERNAL" | FH_TALLY_FILE="$f" bash "$SUBJ"
55
+ grep -q "$(printf '%s\tinternal\t-\ts1' "$TODAY")" "$f.detail" ; chk $? "the skipped internal agent is still RECORDED in the side file (attributable, not vanished)"
56
+
57
+ echo "── rc"
58
+ out=$(printf 'garbage' | FH_TALLY_FILE=/nonexistent-dir/x/y bash "$SUBJ" 2>&1); rc=$?
59
+ [ "$rc" -eq 0 ] ; chk $? "unwritable tally path → still exit 0 (a hook must never block a subagent)"
60
+
61
+ echo "── revert probe: the OLD inline body counts the internal agent (control)"
62
+ old() { printf '%s\n' "$(date +%Y-%m-%d)" >> "$1"; }
63
+ f="$T/old"; printf '%s' "$P_INTERNAL" | old "$f"
64
+ [ "$(grep -c "^$TODAY$" "$f")" = 1 ] ; chk $? "control alive: date-only append DOES count compaction — the defect this lane exists for"
65
+
66
+ # 🟥 millisecond timer — whole-second `date +%s` rounds a 3.2 s run up to 4 or 5 across a second boundary,
67
+ # and under full-selfcheck load that alone turned the deadline lane red (2026-09-26, 607/608). perl is on
68
+ # every macOS/Linux runner this repo supports; missing perl → 99999 (loud red, never a silent pass).
69
+ now_ms() { perl -MTime::HiRes=time -e 'printf "%d", time*1000' 2>/dev/null || echo 0; }
70
+ echo "── stdin never closed (the caller keeps the pipe open) → still counts, within the limit"
71
+ f="$T/open"
72
+ # 🟥 time the HOOK only — timing the whole pipeline also waits for the producer's `sleep`.
73
+ ( sleep 5 ) | { s0=$(now_ms); FH_TALLY_FILE="$f" bash "$SUBJ"; s1=$(now_ms); [ "$s0" -gt 0 ] && [ "$s1" -gt 0 ] && echo $(( s1 - s0 )) > "$f.dur" || echo 99999 > "$f.dur"; }
74
+ dur=$(cat "$f.dur" 2>/dev/null || echo 99999)
75
+ [ "$(grep -c "^$TODAY$" "$f" 2>/dev/null || echo 0)" = 1 ] ; chk $? "open, silent stdin → counted (a bare \`cat\` blocked here until the hook was killed)"
76
+ [ "$dur" -le 4000 ] ; chk $? "and the hook returned by its read limit, not at the caller's EOF (${dur} ms)"
77
+ f="$T/open2"; ( printf '%s' "$P_INTERNAL"; sleep 5 ) | FH_TALLY_FILE="$f" bash "$SUBJ"
78
+ [ "$(grep -c "^$TODAY$" "$f" 2>/dev/null || echo 0)" = 0 ] ; chk $? "payload delivered but pipe left open → still classified (internal skipped)"
79
+
80
+ f="$T/nul"; printf '%s\0 ' "$P_INTERNAL" | FH_TALLY_FILE="$f" bash "$SUBJ"
81
+ [ "$(grep -c "^$TODAY$" "$f" 2>/dev/null || echo 0)" = 1 ] ; chk $? "raw NUL after an internal payload → counted (OUTCOME lane: json rejects NUL — the bash reader that turned it into whitespace is gone; codex 2026-09-25)"
82
+ f="$T/big"; { printf '{"agent_type":"","pad":"'; head -c 9000000 /dev/zero | tr '\0' x; printf '"}'; } | { s0=$(now_ms); FH_TALLY_FILE="$f" bash "$SUBJ"; s1=$(now_ms); [ "$s0" -gt 0 ] && [ "$s1" -gt 0 ] && echo $(( s1 - s0 )) > "$f.dur" || echo 99999 > "$f.dur"; }; e=$(cat "$f.dur" 2>/dev/null || echo 99999)
83
+ [ "$(grep -c "^$TODAY$" "$f" 2>/dev/null || echo 0)" = 1 ] ; chk $? "payload over the 8 MiB cap → truncated → DOUBT → counted"
84
+ [ "$e" -le 8000 ] ; chk $? "and the cap bounds the read time (${e} ms)"
85
+
86
+ # 🟥 time the HOOK only (see the «stdin never closed» lane above) — CI runners ignore SIGPIPE, so the
87
+ # producer keeps sleeping after the hook exits and a whole-pipeline timer read 8 s there (4 s on macOS).
88
+ f="$T/trickle"; ( for i in 1 2 3 4 5 6 7 8; do printf ' '; sleep 1; done; printf '%s' "$P_INTERNAL" ) | { s0=$(now_ms); FH_TALLY_FILE="$f" bash "$SUBJ"; s1=$(now_ms); [ "$s0" -gt 0 ] && [ "$s1" -gt 0 ] && echo $(( s1 - s0 )) > "$f.dur" || echo 99999 > "$f.dur"; }; e=$(cat "$f.dur" 2>/dev/null || echo 99999)
89
+ [ "$(grep -c "^$TODAY$" "$f" 2>/dev/null || echo 0)" = 1 ] ; chk $? "slow trickle (1 byte/s, never idle 2 s) → deadline cuts it → counted"
90
+ [ "$e" -le 4500 ] ; chk $? "and the deadline lands inside the settings.json hook timeout (5 s) with margin (${e} ms — a 5 s deadline raced the kill, which lands BEFORE the append)"
91
+
92
+ echo "── wiring: EXECUTE the shipped template command (a grep for the path matched the _readme text)"
93
+ TPL="$SCRIPT_DIR/../templates/subagent-tally-hook.json"
94
+ CMD=$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["hooks"]["SubagentStop"][0]["hooks"][0]["command"])' "$TPL" 2>/dev/null)
95
+ [ -n "$CMD" ] ; chk $? "template command extracted from hooks.SubagentStop[0].hooks[0].command"
96
+ tpl_run() { # $1 = hub dir · $2 = payload → today's count in that hub's tally
97
+ printf '%s' "$2" | CLAUDE_PROJECT_DIR="$1" bash -c "$CMD" >/dev/null 2>&1
98
+ local t="$1/tracks/_meta/.subagent_dispatch_tally"
99
+ if [ -f "$t" ]; then grep -c "^$TODAY$" "$t"; else echo 0; fi
100
+ }
101
+ H1="$T/hub_with"; mkdir -p "$H1/scripts"; cp "$SUBJ" "$H1/scripts/"
102
+ [ "$(tpl_run "$H1" "$P_INTERNAL")" = 0 ] ; chk $? "template + script present: internal agent → 0 (the command really calls the script)"
103
+ [ "$(tpl_run "$H1" "$P_EXPLORE")" = 1 ] ; chk $? "template + script present: Explore → 1"
104
+ H2="$T/hub_without"; mkdir -p "$H2"
105
+ [ "$(tpl_run "$H2" "$P_INTERNAL")" = 1 ] ; chk $? "template + script MISSING (older clone): falls back to counting → 1, never silence"
106
+
107
+ echo ""
108
+ if [ "$FAILED" -eq 0 ]; then echo "PASS subagent tally hook: $PASS lanes"; exit 0; fi
109
+ echo "FAIL subagent tally hook"; exit 1
@@ -112,6 +112,17 @@ Unlisted tool name → **ask** (fail-closed), then add it to the table. Listed-b
112
112
  is the same case: a name in the table earns its allow tier from confirmed behavior (§1),
113
113
  not from sounding harmless.
114
114
 
115
+ > ⚠️ **"ask" stops a call only if nothing answers the prompt for you** (measured 2026-09-25,
116
+ > Claude Code 2.1.282, interactive): a `PreToolUse` hook returning **ask** was *swallowed* when a
117
+ > `PermissionRequest` hook answered **allow** — the call ran **5/5** (auto mode 3 · acceptEdits 2).
118
+ > A hook returning **deny** stopped it **0/2** landed. Headless runs went the other way (the
119
+ > `PermissionRequest` hook was not called after ask), so the result is surface-specific. So under
120
+ > any auto-approver (a `PermissionRequest` allow hook, a desktop approval app, a wrapper that
121
+ > clicks "yes"), an ask-tier call is **not** fail-closed — only **deny / exit 2** stops it. Not
122
+ > measured: auto mode *without* such a hook (one run, did not land — below bar), and whether a
123
+ > `permissions.ask` **rule** (step 4 below) is swallowed the same way. Detail:
124
+ > `knowledge/shared/harness-core/hook_channel_visibility.md`.
125
+
115
126
  ## 3. Per-server table ([CUSTOMIZE] — fill at mount time)
116
127
 
117
128
  | Server | Tool name | Tier | Note |
@@ -127,7 +138,10 @@ not from sounding harmless.
127
138
  3. Anything that writes outside the repo, or grants/answers an approval → ask.
128
139
  4. Where supported, mirror the ask-tier into the platform's permission config
129
140
  (e.g. Claude Code `permissions.ask` entries for `mcp__{server}__{tool}`) so the
130
- gate is mechanical, not prose-only. This rule file is the fallback for hosts
141
+ gate is mechanical, not prose-only. ⚠️ Mechanical against a human at the prompt — whether an
142
+ auto-approver answers a `permissions.ask` rule the way it answered a hook's ask (§2 note)
143
+ is **unmeasured**; where one is installed, use a `permissions.deny` entry for calls that
144
+ must never run unattended. This rule file is the fallback for hosts
131
145
  without per-tool permission config.
132
146
  5. For `http`/`sse`-transport servers, record the resolved endpoint address at mount
133
147
  (host + path) in the §3 table's Note column. This checklist gates tool *behavior*
@@ -151,6 +165,8 @@ Done When (per mounted server):
151
165
  - §3 table filled, every enumerated tool name present (check class: mandatory-pass — file inspection)
152
166
  - ask-tier tools wired to a per-call approval surface: host per-tool permission entry exists,
153
167
  or this rule file is installed and loaded in the session (check class: mandatory-pass)
168
+ — and if the session runs under an auto-approver, the surface is a **deny** entry, not ask
169
+ (§2 note; check class: mandatory-pass — grep the settings for a `PermissionRequest` hook)
154
170
  - non-ask tiers assigned only with a behavior-confirmation note in the §3 Note column
155
171
  (check class: judged — pair with an adversarial pass asking "could this name mislead?")
156
172
  - for `http`/`sse`-transport servers, the mounted endpoint address is recorded in §3
@@ -1,5 +1,5 @@
1
1
  {
2
- "_readme": "FH dispatch-tally hook. Merge the `hooks.SubagentStop` block below into your .claude/settings.json (that file is gitignored, which is why this template exists). It feeds session_close_check.sh ④-e, which blocks a close push when sub-agents were dispatched and the day's invocation-log entries are zero. The hook TALLIES ONLY — it never writes a log entry, because a fabricated outcome/evidence field would poison the 60/40 promotion gate worse than a missing one. Root resolution is 3-tier (CLAUDE_PROJECT_DIR -> git rev-parse --show-toplevel -> hardcoded path), matching the SAME convention fh_node_check.sh already uses in its own SessionStart hook -- a 2026-08-16 standpoint review on a sibling harness (pmh-dev) found this template's earlier 2-tier version get copy-pasted without picking up that convention, silently diverging from session_close_check.sh's own git-toplevel-first resolution in a nested/worktree/subagent context. If you adapt this template for another repo, keep the 3-tier form.",
2
+ "_readme": "FH dispatch-tally hook. Merge the `hooks.SubagentStop` block below into your .claude/settings.json (that file is gitignored, which is why this template exists). It feeds session_close_check.sh ④-e, which blocks a close push when sub-agents were dispatched and the day's invocation-log entries are zero. The hook TALLIES ONLY — it never writes a log entry, because a fabricated outcome/evidence field would poison the 60/40 promotion gate worse than a missing one. Root resolution is 3-tier (CLAUDE_PROJECT_DIR -> git rev-parse --show-toplevel -> hardcoded path), matching the SAME convention fh_node_check.sh already uses in its own SessionStart hook -- a 2026-08-16 standpoint review on a sibling harness (pmh-dev) found this template's earlier 2-tier version get copy-pasted without picking up that convention, silently diverging from session_close_check.sh's own git-toplevel-first resolution in a nested/worktree/subagent context. If you adapt this template for another repo, keep the 3-tier form. 2026-09-25: the body moved to scripts/subagent_tally_hook.sh so it can read the payload — SubagentStop also fires for Claude Code's internal agents (context compaction arrives with agent_type \"\"), which the date-only inline form counted as dispatches (④-e false positive). If the script is missing (an older clone), the command falls back to the old date-only append — over-count, never under-count. RE-MERGE this block into your .claude/settings.json: an existing inline hook keeps the old behaviour.",
3
3
  "hooks": {
4
4
  "SubagentStop": [
5
5
  {
@@ -7,7 +7,7 @@
7
7
  "hooks": [
8
8
  {
9
9
  "type": "command",
10
- "command": "bash -c 'HUB=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null || echo \"$HOME/projects/forge-harness\")}\"; T=\"$HUB/tracks/_meta/.subagent_dispatch_tally\"; mkdir -p \"$(dirname \"$T\")\" 2>/dev/null; printf \"%s\\n\" \"$(date +%Y-%m-%d)\" >> \"$T\" 2>/dev/null; exit 0'",
10
+ "command": "bash -c 'HUB=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null || echo \"$HOME/projects/forge-harness\")}\"; S=\"$HUB/scripts/subagent_tally_hook.sh\"; if [ -f \"$S\" ]; then CLAUDE_PROJECT_DIR=\"$HUB\" bash \"$S\"; else T=\"$HUB/tracks/_meta/.subagent_dispatch_tally\"; mkdir -p \"$(dirname \"$T\")\" 2>/dev/null; printf \"%s\\n\" \"$(date +%Y-%m-%d)\" >> \"$T\" 2>/dev/null; fi; exit 0'",
11
11
  "timeout": 5
12
12
  }
13
13
  ]