crewforth 3.1.0-rc.4 → 3.1.0-rc.5

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,392 @@
1
+ #!/usr/bin/env bash
2
+ # guard-agent-model.sh - a crew agent is called with a model, and not below its floor.
3
+ #
4
+ # WHY. The model for a piece of work is chosen per call, by risk (the table in CLAUDE.md): haiku for mechanical
5
+ # work, sonnet for ordinary work, opus where a mistake is expensive. Left to the session's default, every agent ran
6
+ # on the session's model; left to a habit of "cheapest", an audit could run below the code it clears. Two things
7
+ # are held here, at the tool level:
8
+ # * a call to a crew-* agent NAMES its model, so the choice is made and can be read in the transcript;
9
+ # * some agents have a floor: crew-security-expert runs on opus; crew-privacy-agent, crew-review-agent,
10
+ # crew-planner and crew-database-expert on sonnet or above.
11
+ # It only refuses, with the reason and the model that is needed. It never rewrites a call. THE WORDING IS READ BY
12
+ # ANOTHER PROGRAM (the Studio panel): a refusal is one line that begins `GUARD (agent model):`, and a floor is said
13
+ # as ` runs on <model> or above`. The suite pins both. Measured on Claude Code
14
+ # 2.1.294: refused this way, the session repeated the same call with the model named, and a call with
15
+ # model: "haiku" ran its agent on Haiku (the agent's own transcript names the model).
16
+ #
17
+ # NOT TOUCHED: an agent that is not crew-* (Explore, general-purpose, a project's own), and any tool but Agent.
18
+ # SWITCHES, read from the session's environment:
19
+ # CREW_MODEL_ROUTING=off the gate does nothing, which is how it was before 3.1.0.
20
+ # CREW_ALLOW_FABLE=1 `fable` may be named; without it a call that names it is refused.
21
+ # HONEST SCOPE: which model fits the TASK is the caller's judgement and nothing here reads the task. The model is
22
+ # read from the call's own `model` field; an agent file's frontmatter is not read.
23
+ set -uo pipefail
24
+ # ---- CREW-LOCALE -----------------------------------------------------------------------------------------
25
+ # Everything this gate matches with runs in the C locale, whatever the session's own is. Under a Turkish locale
26
+ # the letters i and I are not each other's other case (their partners are İ and ı), and every case-insensitive
27
+ # match here is written in ASCII. Measured under tr_TR.UTF-8 with the locale left as it came:
28
+ # GNU grep 3.11 / bash 5.2 on Linux: `grep -i init` does not find INIT (-F, -E and plain alike);
29
+ # bash's nocasematch does not match GIT against git; `[A-Za-z]` in a regex does not hold I; awk's tolower
30
+ # turns GIT into gıt. The suite, run whole under that locale, went from 4 failures to 30: a recursive
31
+ # Remove-Item, a write to a gate file, `git config --remove-section core`, a read of a nested .env and a
32
+ # staged key all passed.
33
+ # GNU grep 3.0 in Git Bash on Windows: `grep -iF` with an ASCII pattern aborts (exit 134), and the pre-commit
34
+ # scan for private strings read that as "no match".
35
+ # macOS (BSD grep, bash 3.2): none of it; tr_TR.UTF-8 folds i and I the ASCII way there.
36
+ # The cost: a letter outside ASCII has no other case in the C locale. The two git hooks that scan a user's own
37
+ # words look a second time under the session's locale for a pattern that holds such a letter (_CREW_LOCALE).
38
+ # Byte-identical in every gate; the suite pins it.
39
+ _CREW_LOCALE="${LC_ALL:-${LC_CTYPE:-${LANG:-}}}"
40
+ case "$_CREW_LOCALE" in C|POSIX) _CREW_LOCALE="" ;; esac
41
+ export LC_ALL=C
42
+ # ---- /CREW-LOCALE
43
+ # ---- CREW-FAILCLOSED ---------------------------------------------------------------------------------------
44
+ # A gate that stops on an error of its own must refuse the call, not let it through. Claude Code blocks a tool call
45
+ # on exit 2 only; a hook that dies with 1, or runs on past the rule that broke, has allowed it. Both happened in
46
+ # one defect (measured, bash 5): a local array declared and not set is "unbound" under `set -u`, the rule that read
47
+ # it was abandoned in the middle, the script went on with the NEXT top-level command and left with 0, and
48
+ # `rm .claude/hooks/pre-commit` passed. bash does one of two things with such an error, by the kind of error:
49
+ # * it leaves the shell with a status that is neither 0 nor 2 -> the EXIT trap below turns that into a refusal;
50
+ # * it abandons the top-level command it was in and continues -> the whole gate IS one top-level command,
51
+ # _gate_main, which only ever ends by `exit`. If it RETURNS, it was abandoned, and the line after it refuses.
52
+ # Not covered, and said so: an error inside a command substitution (only that subshell ends), and a command
53
+ # that is not found (status 127, the script continues inside the same command).
54
+ # Byte-identical in every PreToolUse gate; the suite pins it. No process is started.
55
+ _crew_stop(){ # $1 = what happened
56
+ declare -F gatelog >/dev/null 2>&1 && gatelog BLOCK 4.5 "the gate stopped on an error of its own"
57
+ echo "GUARD (§4.5): this gate stopped on an error of its own ($1) before it finished judging the call, so the call is refused." >&2
58
+ echo "Nothing about the call itself was found. The error is the line above these two; it is a defect in Crewforth, not in what you ran. Run the doctor (bash .claude/eval/doctor.sh, Bash tool, not PowerShell) and report it." >&2
59
+ exit 2
60
+ }
61
+ trap '_crew_rc=$?; trap - EXIT; case "$_crew_rc" in 0|2) exit "$_crew_rc" ;; esac; _crew_stop "exit status $_crew_rc"' EXIT
62
+ # ---- /CREW-FAILCLOSED
63
+ _gate_main(){
64
+ local tn st m ti rank=0 floor=0 fname=""
65
+ [ "${CREW_MODEL_ROUTING:-}" = off ] && exit 0
66
+ IFS= read -r -d '' INPUT || true
67
+ # ---- CREW-JSON-PARSE ------------------------------------------------------------------------------------
68
+ _json_find(){ # $1 = text, $2 = a literal -> _JF = the offset of its first occurrence, -1 when there is none
69
+ # `${text%%"$literal"*}` gives the same offset and costs the DISTANCE to the occurrence times the length of the
70
+ # text (bash measures the remaining string at every position it tries), so with no occurrence at all it is the
71
+ # square of the size. That was the whole cost of a large Write: the path key is found near the front, and the
72
+ # look for a SECOND one then ran over the content -- measured on macOS, 1 MB: 9.6 s for each of the two key
73
+ # counts, against 0.02 s for one `case`; a 6 MB Write to a gate file was refused after 636 s, past the 600 s
74
+ # timeout, which means not refused. Here the text is walked a piece at a time, each piece long enough to hold an
75
+ # occurrence that starts in it, and the expansion only ever runs inside a piece that `case` says holds one.
76
+ # Two sizes of piece, because taking a piece out of the text costs the length of the text as well: a block is
77
+ # taken from the whole text, and the small pieces from the block.
78
+ local LC_ALL=C
79
+ local i=0 j n=${#1} kl=${#2} B=262144 C=4096 b bn c pre
80
+ _JF=-1
81
+ while [ "$i" -lt "$n" ]; do
82
+ b="${1:i:B+kl-1}"
83
+ case "$b" in *"$2"*)
84
+ j=0; bn=${#b}
85
+ while [ "$j" -lt "$bn" ]; do
86
+ c="${b:j:C+kl-1}"
87
+ case "$c" in *"$2"*) pre="${c%%"$2"*}"; _JF=$((i+j+${#pre})); return 0 ;; esac
88
+ j=$((j+C))
89
+ done ;;
90
+ esac
91
+ i=$((i+B))
92
+ done
93
+ }
94
+ _json_slice(){ # $1 = whole payload, $2 = key -> the raw (still JSON-escaped) string value, "" if absent
95
+ local LC_ALL=C # FIRST, so every expansion below -- the key search included -- counts and cuts in bytes.
96
+ # Lengths from ${#x} are used as offsets into ${y:n}; with the locale set before any of
97
+ # them, the two never disagree about a unit. Walking bytes is safe because no UTF-8
98
+ # continuation byte can be 0x5C or 0x22, so a cut cannot land inside a character at a
99
+ # quote or backslash edge. What this line is worth in SPEED depends on the platform --
100
+ # read the table in _json_unescape below before quoting a number for it.
101
+ local pre rest seg w r n base=0 j=0 cl lim run=0 chunk C=4096 W=256 hay="$1" k="\"$2\""; local -a acc=("")
102
+ # It ALSO sets `_JS`, so a caller on the hot path can read the value without `$( )`, which is a fork. The
103
+ # printf stays because the existing callers compose it (`_json_unescape "$(_json_slice …)"`), but the one
104
+ # caller that used to do its own fork-free extraction — `_CWD` — reads `_JS` and keeps costing nothing.
105
+ _JS=""
106
+ # Every "step past X" here is arithmetic on a length, never `${s#"$literal"}`. That shape reads like a
107
+ # constant-time strip and is not one: bash retries the pattern at every prefix length, so stripping an
108
+ # n-byte literal costs O(n^2). It was in this function twice, and both were measured:
109
+ # * `${1#*"$2"}` found the key -- cheap when the key sits near the front, quadratic in the distance to it
110
+ # otherwise. `permission_mode` behind a 100 KB command took 7.94s on Git Bash, 0.27s in front of it.
111
+ # Captured from Claude Code 2.1.267, the real order puts `permission_mode` BEFORE `tool_input`, so on
112
+ # that version this cost was not reachable -- this file's header had shown the opposite order, and was
113
+ # wrong. The parse stays order-independent regardless: the order is not a documented contract, and the
114
+ # payload is still moving (`effort` is absent from the field list recorded on 2.1.246).
115
+ # * `${tail#"$seg"\"}` stepped past each escaped quote: 16.8s for a single 100 KB step on Git Bash,
116
+ # against 0.002s for `${tail:${#seg}+1}` doing exactly the same thing.
117
+ # _json_find gives the FIRST occurrence. THAT ALONE IS NOT ENOUGH, and the sentence that used to stand here said it was: it claimed a
118
+ # command "containing the literal text `\"command\":\"` still cannot relocate the parse", which is true of
119
+ # that byte sequence and irrelevant, because this search is for `"command"` WITHOUT the colon. A JSON VALUE
120
+ # equal to the key name is exactly those bytes between two unescaped quotes. Measured on the shipped hook:
121
+ # {"tool_name":"Bash","a":"command","ls":1,"tool_input":{"command":"rm -rf /"}} -> read `ls`
122
+ # {"a":"permission_mode","default":1,"permission_mode":"bypassPermissions",…} -> read `default`
123
+ # {"a":"file_path","/tmp/ok":1,"tool_input":{"file_path":".claude/hooks/x.sh"}} -> read `/tmp/ok`
124
+ # In each case the gate judged a harmless string while a dangerous one was the actual argument, and jq read
125
+ # the real value -- so on a stock Windows desktop, where this was already the only parser, the gate could be
126
+ # aimed with one extra key. The loop below therefore accepts an occurrence only when optional JSON whitespace
127
+ # and then `:` follow it, which is the only place a key can appear; everything else keeps searching.
128
+ # Stepping by length removed the quadratic strip, but each escaped quote still sliced and re-assigned the
129
+ # whole remainder, so an escape-dense command stayed k*n here as well (2.2s of the 6.8s described in
130
+ # _json_unescape below, on Git Bash). The walk is chunked the same way: the payload is read 4096 bytes at a
131
+ # time and every per-quote operation stays inside the chunk. One thing is carried across edges on purpose
132
+ # -- the length of the backslash run in front of a quote -- because that run can straddle a window or a
133
+ # chunk, and its parity is what decides whether the quote ends the value. Isolated, escape-dense 44030 B:
134
+ # 2.61s -> 0.41s on Git Bash 5.3.15, 0.93s -> 0.09s on macOS/bash 3.2.
135
+ # Output is byte-identical to the previous shape across 378 cases at four chunk/window sizes down to 7 and 1
136
+ # bytes, and that shape to the one before it across a 27-case battery (escaped quotes, backslash runs before
137
+ # a quote, the key twice, the key appearing first as a value, glob metacharacters, UTF-8, no closing quote,
138
+ # empty input). Broken twins -- the run not carried across a window, a byte skipped after a quote -- prove
139
+ # the battery sees both.
140
+ # THE NUMBER OF PASSES IS CAPPED, for the same reason guard-write caps the path length: an unbounded cost on
141
+ # a PreToolUse hook is a gate with an off switch, because a hook killed at its timeout emits no exit 2.
142
+ # Each decoy costs one more `%%` scan over the remainder, so the work is quadratic in the number of decoys.
143
+ # Measured on macOS/bash, single call, `"k<i>":"command"` decoys in front of the real key:
144
+ # 50 decoys 844 B 12 ms 800 decoys 13544 B 126 ms
145
+ # 200 decoys 3344 B 19 ms 3200 decoys 56544 B 1839 ms
146
+ # Git Bash's parameter expansion is several times slower again, so the tail is where the timeout lives. A
147
+ # real payload carries ZERO decoys -- 64 is far above anything a producer can legitimately emit -- and going
148
+ # over the cap is reported SEPARATELY (`_KC_CAPPED`), not as a large count, so the caller can refuse with a
149
+ # reason the reader can act on instead of a sentinel dressed up as a measurement. Fail-closed either way.
150
+ # THE COST IS ONE THIS CHANGE INTRODUCES, and it is worth saying so plainly rather than implying the cap
151
+ # protects something pre-existing: before the colon requirement the scan STOPPED at the first occurrence, so
152
+ # decoys cost essentially nothing (measured on stock Windows: 0 decoys 0.18 ms, 3200 decoys 1.60 ms per call,
153
+ # roughly linear) -- and it read the decoy, which is the hole. The cap is load-bearing for the new cost, so
154
+ # raising or removing it later is not a tidy-up.
155
+ # The bound is on the LOOP, not on the payload, so an ordinary command pays nothing: the same 56572 B flood
156
+ # is refused in 93-94 ms with jq/python3 present and 168-169 ms on the slice path this paragraph is about,
157
+ # both far inside the 60s timeout these hooks had when this was measured, while a 46 KB legitimate command is 325 ms on macOS -- a figure that
158
+ # predates this change and belongs to the walk, not to the cap.
159
+ local _cap=64 _seen=0
160
+ while :; do
161
+ _json_find "$hay" "$k" # where the next `"key"` starts
162
+ [ "$_JF" -ge 0 ] || return 0 # no further occurrence: emit nothing
163
+ _seen=$((_seen+1)); [ "$_seen" -le "$_cap" ] || return 0
164
+ rest="${hay:_JF+${#k}}" # past `"key"`
165
+ while [ -n "$rest" ] && [[ "${rest:0:1}" == [$' \t\n\r'] ]]; do rest="${rest:1}"; done
166
+ case "$rest" in
167
+ :*) rest="${rest:1}"; break ;; # whitespace then `:` -- this occurrence IS the key
168
+ *) hay="$rest" ;; # a value, or another token: keep looking
169
+ esac
170
+ done
171
+ # THE VALUE MUST BE A STRING. `"command":123}}` used to come back as the literal text `:123}}` -- the old
172
+ # shape skipped forward to the next quote wherever it was, so a non-string value handed the matchers the
173
+ # payload's own punctuation to judge. Emitting nothing instead lets the caller's "no readable command"
174
+ # refusal fire, which is the honest answer for a value these gates cannot read.
175
+ while [ -n "$rest" ] && [[ "${rest:0:1}" == [$' \t\n\r'] ]]; do rest="${rest:1}"; done
176
+ case "$rest" in '"'*) rest="${rest:1}" ;; *) return 0 ;; esac
177
+ # Walk to the closing quote that is NOT escaped. A `"` preceded by an odd number of backslashes is content.
178
+ n=${#rest}; chunk="${rest:0:C}"; cl=${#chunk}
179
+ while :; do
180
+ lim=$((cl - j))
181
+ if [ "$lim" -le 0 ]; then
182
+ [ $((base + cl)) -lt "$n" ] || break # no closing quote at all: everything is already taken
183
+ base=$((base + j)); chunk="${rest:base:C}"; cl=${#chunk}; j=0; continue
184
+ fi
185
+ [ "$lim" -le "$W" ] || lim=$W
186
+ w="${chunk:j:lim}"
187
+ seg="${w%%\"*}"
188
+ if [ "$seg" = "$w" ]; then # no quote in view: take all of it, carry the run
189
+ acc+=("$w"); j=$((j+lim))
190
+ r="${w##*[!\\]}"; case "$w" in *[!\\]*) run=${#r} ;; *) run=$((run+${#w})) ;; esac
191
+ continue
192
+ fi
193
+ acc+=("$seg"); j=$((j+${#seg}+1))
194
+ r="${seg##*[!\\]}"; case "$seg" in *[!\\]*) run=${#r} ;; *) run=$((run+${#seg})) ;; esac
195
+ if [ $((run % 2)) -eq 1 ]; then acc+=("\""); run=0; else break; fi
196
+ done
197
+ local IFS=''; _JS="${acc[*]}"; printf '%s' "$_JS"
198
+ }
199
+ _json_unescape(){ # left-to-right, a chunk at a time; a two-pass sed would corrupt `\\"` (escaped backslash + quote)
200
+ # This is the tier-3 path below -- the one a stock Windows install actually runs on. It used to walk ONE
201
+ # CHARACTER at a time, which is O(n^2) twice over: `${s%"${s#?}"}` matches a pattern the length of the
202
+ # entire remainder just to read one character, and `out="$out$c"` recopies the output for each one.
203
+ #
204
+ # That was not a comfort question. This hook's timeout was 60s then -- set in settings.json (600s since 3.1.0), NOT Claude Code's
205
+ # default, and reading the default instead is how a first pass at this got the consequence wrong. A
206
+ # PreToolUse hook KILLED at its timeout emits no exit 2, so every rule below is simply skipped. Measured on
207
+ # the tier-3 path with jq and python3 both shadowed, the old shape crossed 60s at ~4.3 KB. And 4.3 KB is
208
+ # not exotic: across 6791 Bash calls in 280 real transcripts, 2.49% of commands are bigger, and the largest
209
+ # is 46815 B, which the old shape needed roughly 39 minutes to decode. One Bash call in forty walked past
210
+ # §4.4 and §4.5 entirely, silently, on every stock Windows desktop.
211
+ #
212
+ # Two changes, and their wins are NOT the same shape -- writing them down as one number was the mistake
213
+ # this comment exists to avoid repeating:
214
+ # * Taking the whole run up to the next backslash in ONE expansion, and indexing with `${s:0:1}` instead
215
+ # of matching a pattern, is 47x on a 4.4 KB payload. This one holds on every machine.
216
+ # * `local LC_ALL=C` makes these expansions byte-oriented instead of re-decoding the string on every
217
+ # substring operation. What that is WORTH depends on the platform, and putting a single number here
218
+ # would have been wrong three separate ways -- it was written as "another 5x" twice before this:
219
+ # macOS / bash 3.2, a locale set .................. 5x
220
+ # Git Bash 5.3.15, a locale set ................... 1.23x (measured on the OLD shape; the new one
221
+ # touches the locale once per escape
222
+ # rather than once per character, so its
223
+ # ratio is smaller and unmeasured)
224
+ # Git Bash, LANG empty -- what Claude Code starts .. nothing at all
225
+ # So speed is not what keeps this line; CORRECTNESS is, and that half holds everywhere. `[0-9a-fA-F]`
226
+ # below is a collation-defined range outside the C locale: on a tr_TR desktop it is not 0-9a-f. That
227
+ # alone would justify the line, and it costs nothing. `local` restores the previous locale on return
228
+ # -- verified on bash 3.2 and on Git Bash 5.3.15, not assumed.
229
+ #
230
+ # Then the cost moved rather than went away. With the per-character walk gone, each escape still sliced the
231
+ # whole REMAINDER -- `s="${s:${#pre}+1}"` -- so an escape-dense command stayed k*n: on Git Bash a 44080 B
232
+ # command with 2755 escapes took 6.8s, 11% of the timeout. The input is now touched only a chunk at a time
233
+ # (`${s:base:C}`, once per 4096 bytes) and every per-escape operation stays inside that chunk, so no escape
234
+ # pays for the length of the whole command. That is the change that matters: bounding only the lookahead,
235
+ # while still indexing the full string, was measured too and gave about 4x on both machines, against 6-12x
236
+ # for the chunked walk. Five bytes are held back at a chunk's end whenever more input follows, so a
237
+ # `\uXXXX` that starts in a chunk ends in it.
238
+ # The output goes into an array joined once, not a string recopied at every append, and `\n`/`\t` are
239
+ # written `$'\n'`/`$'\t'`, so this file carries no raw TAB for an editor or a copy to turn into spaces.
240
+ #
241
+ # Measured, isolated, previous shape -> this one:
242
+ # macOS/bash 3.2 escape-dense 44030 B, 2590 escapes .. 1.44s -> 0.12s
243
+ # sparse 100014 B, 4 escapes ........... 0.09s -> 0.02s
244
+ # Git Bash 5.3.15 escape-dense 44030 B, 2590 escapes .. 3.92s -> 0.54s
245
+ # (LANG empty) sparse 99997 B, 4 escapes ............ 0.49s -> 0.07s
246
+ # These numbers do not travel, which is why each one names its machine.
247
+ #
248
+ # Output is byte-identical to the previous shape across 478 cases, each run at four chunk/window sizes
249
+ # down to 7 and 1 bytes so that an edge falls every few bytes; that shape was itself byte-identical to
250
+ # the original character walk across a 31-case battery (escaped quotes, `\\`, a lone trailing backslash, a
251
+ # truncated `\u`, Turkish, emoji). Deliberately broken twins -- the 5-byte margin cut to 4, the backslash
252
+ # not stepped over -- prove the battery sees both edges, and tier 1 and tier 3 return the same verdict on
253
+ # the gate cases.
254
+ local LC_ALL=C
255
+ local s="$1" pre w c h n base=0 j=0 cl lim chunk C=4096 W=256; local -a acc=("")
256
+ case "$s" in *\\*) ;; *) _JU="$s"; printf '%s' "$s"; return 0 ;; esac # no escapes: the common case pays nothing
257
+ n=${#s}; chunk="${s:0:C}"; cl=${#chunk}
258
+ while :; do
259
+ lim=$((cl - j))
260
+ if [ $((base + cl)) -lt "$n" ]; then # more input after this chunk: hold 5 bytes back, so
261
+ lim=$((lim - 5)) # a `\uXXXX` that starts in a chunk also ends in it
262
+ if [ "$lim" -le 0 ]; then base=$((base + j)); chunk="${s:base:C}"; cl=${#chunk}; j=0; continue; fi
263
+ else
264
+ [ "$lim" -gt 0 ] || break
265
+ fi
266
+ [ "$lim" -le "$W" ] || lim=$W
267
+ w="${chunk:j:lim}"
268
+ pre="${w%%\\*}" # the literal run before the next backslash in view
269
+ if [ "$pre" = "$w" ]; then acc+=("$w"); j=$((j+lim)); continue; fi
270
+ acc+=("$pre"); j=$((j+${#pre}+1))
271
+ if [ $((base + j)) -ge "$n" ]; then acc+=("\\"); break; fi # a lone trailing backslash stays literal, as before
272
+ c="${chunk:j:1}"; j=$((j+1))
273
+ case "$c" in
274
+ n) acc+=($'\n') ;;
275
+ t) acc+=($'\t') ;;
276
+ r) ;;
277
+ b|f) acc+=(" ") ;;
278
+ u) if [ $((n - base - j)) -ge 4 ]; then h="${chunk:j:4}"; j=$((j+4)); else h=""; fi # a short tail is left alone, as before
279
+ # A `\uXXXX` used to become a literal `?`. That is not a lossy nicety, it is a hole: `\u002e` is `.`,
280
+ # so `\u002eclaude/hooks/guard-bash.sh` decoded to `?claude/…` and matched no gate pattern, while jq
281
+ # decoded the same bytes to the real path — the two tiers disagreed on whether a payload was an
282
+ # attack. Printable ASCII is decoded properly (builtin printf, no fork); anything else still becomes
283
+ # `?`, which is only ever a display concern because this value is used for MATCHING, never to write.
284
+ case "$h" in
285
+ 00[2-7][0-9a-fA-F]) printf -v c "\\x${h#00}"; acc+=("$c") ;;
286
+ # The three dashes PowerShell accepts in front of a parameter name, as themselves: as `?` they hid
287
+ # `\u2013Recurse \u2013Force` from the delete rule, while the same bytes sent raw were caught.
288
+ 2013) acc+=("–") ;; 2014) acc+=("—") ;; 2015) acc+=("―") ;;
289
+ *) acc+=("?") ;;
290
+ esac ;;
291
+ *) acc+=("$c") ;;
292
+ esac
293
+ done
294
+ # ALSO sets `_JU`, for the same reason `_json_slice` sets `_JS`: a caller on the hot path reads the value from it
295
+ # and sends this printf to /dev/null, instead of paying a `$( )` — a fork — on every tool call.
296
+ local IFS=''; _JU="${acc[*]}"; printf '%s' "$_JU"
297
+ }
298
+ _json_keycount(){ # $1 = payload, $2 = key -> sets _KC to how many times it occurs AS A KEY
299
+ # IT SETS A VARIABLE INSTEAD OF PRINTING, and that is not a style choice. Written as `n="$(_json_keycount
300
+ # …)"` first, which is a command substitution, which is a FORK -- on a hook that runs before every Bash and
301
+ # every Write call. Measured on macOS/bash, typical payload, 200 reps: the substitution form cost 0.790 ms
302
+ # per call against 0.090 ms for the raw byte test it replaces, and the whole hook went 15.00 -> 16.58 ms.
303
+ # This project's own recorded Git Bash process cost is 62-135 ms, rising to ~400 ms under load, so three
304
+ # added forks per call would have been a freeze on the platform the change is meant to protect. Setting _KC
305
+ # keeps the count in the caller's own shell: expansion only, zero forks.
306
+ # The counter the ambiguity refusals need, and it exists because the hand-written byte test they used to do
307
+ # looked for a DIFFERENT token than the parser above: `"key":` compact, while the parser searches `"key"` and
308
+ # tolerates whitespace before the colon. Two consequences, both measured on the shipped hook:
309
+ # * `{…"meta":{"command":"ls"},"tool_input":{"command" : "rm -rf /"}}` -- ONE SPACE and the duplicate-key
310
+ # refusal went blind while the parser happily read the first value.
311
+ # * a key name appearing as a VALUE was counted as an occurrence by neither, which is the hole the parser
312
+ # above now closes; counting the same form here is what keeps the two from drifting apart again.
313
+ # Sharing ONE definition with the parser is the point: a guard that searches for something else than the
314
+ # thing it guards is the defect, not an implementation detail.
315
+ # WHAT THE TOKEN CAN BE, stated correctly. An earlier version of this comment said the bytes `"command"`
316
+ # with both quotes unescaped "can only be a key or a value equal to the key name". That is wrong, and the
317
+ # counter-example was found by review, not by reasoning: a KEY whose own name ends with a quote spells the
318
+ # token out of its escaped quote plus the string's terminator --
319
+ # {"tool_input":{"x\"command":"DECOY","command":"rm -rf /"}} slice -> DECOY, count -> 2
320
+ # which is refused BECAUSE the count sees two, not because the invariant held. The same shape as a VALUE is
321
+ # harmless (a string's terminator is followed by `,` `}` `]`, never `:`, so it is skipped and not counted:
322
+ # {"description":"ends with \"command","command":"rm -rf /"} slice -> rm -rf /, count -> 1 ).
323
+ # What IS true, and is what keeps ordinary work from being refused: content can contribute at most one
324
+ # occurrence per string and only as that string's tail, where the next byte is never a colon. Measured on 19
325
+ # payloads built by a real JSON encoder -- `grep -rn '"command":' .`, a heredoc writing a hooks.json, `sed`
326
+ # over settings.json, a commit message quoting the word, commands ending in `"command` -- every count <= 1.
327
+ # Same pass cap as the slice, and over-cap reports AMBIGUOUS rather than a true count: the callers refuse on
328
+ # `> 1`, so a payload built to outrun the loop is refused instead of being timed out past the gate.
329
+ local LC_ALL=C hay="$1" k="\"$2\"" rest c=0 _cap=64 _seen=0
330
+ _KC=0; _KC_CAPPED=0
331
+ while :; do
332
+ _json_find "$hay" "$k"
333
+ [ "$_JF" -ge 0 ] || { _KC=$c; return 0; }
334
+ # OVER-CAP IS ITS OWN ANSWER, not a large count. `_KC_CAPPED` lets the caller say what actually happened:
335
+ # the occurrences it stopped at are candidate positions, key-form or not, so calling them "65 keys" was a
336
+ # sentinel dressed up as a measurement and the remedy it offered ("send one key") was already satisfied.
337
+ _seen=$((_seen+1)); [ "$_seen" -le "$_cap" ] || { _KC=$((_cap+1)); _KC_CAPPED=$_cap; return 0; }
338
+ rest="${hay:_JF+${#k}}"
339
+ while [ -n "$rest" ] && [[ "${rest:0:1}" == [$' \t\n\r'] ]]; do rest="${rest:1}"; done
340
+ case "$rest" in :*) c=$((c+1)) ;; esac
341
+ hay="$rest"
342
+ done
343
+ }
344
+ # ---- /CREW-JSON-PARSE -----------------------------------------------------------------------------------
345
+ _json_slice "$INPUT" tool_name >/dev/null; tn="$_JS"
346
+ case "$tn" in Agent|Task) ;; *) exit 0 ;; esac
347
+ # The call's own fields: everything from "tool_input" on, so a key of the same name in front of it is not read.
348
+ _json_find "$INPUT" '"tool_input"'
349
+ [ "$_JF" -ge 0 ] || exit 0
350
+ ti="${INPUT:_JF}"
351
+ _json_keycount "$ti" subagent_type
352
+ if [ "$_KC" -gt 1 ]; then echo "GUARD (agent model): this call names subagent_type more than once, so which agent it starts cannot be read and the call is refused. Send one." >&2; exit 2; fi
353
+ _json_slice "$ti" subagent_type >/dev/null; _json_unescape "$_JS" >/dev/null; st="$_JU"
354
+ st="${st##*:}" # a plugin names its agents <plugin>:crew-…
355
+ case "$st" in crew-*) ;; *) exit 0 ;; esac
356
+ _json_keycount "$ti" model
357
+ if [ "$_KC" -gt 1 ]; then echo "GUARD (agent model): this call names model more than once, so which model it asks for cannot be read and the call is refused. Send one." >&2; exit 2; fi
358
+ _json_slice "$ti" model >/dev/null; _json_unescape "$_JS" >/dev/null; m="$_JU"
359
+ shopt -s nocasematch
360
+ case "$m" in
361
+ '') rank=0 ;;
362
+ *haiku*) rank=1; m=haiku ;;
363
+ *sonnet*) rank=2; m=sonnet ;;
364
+ *opus*) rank=3; m=opus ;;
365
+ *fable*) rank=4; m=fable ;;
366
+ *) rank=-1 ;;
367
+ esac
368
+ shopt -u nocasematch
369
+ case "$st" in
370
+ crew-security-expert) floor=3; fname=opus ;;
371
+ crew-privacy-agent|crew-review-agent|crew-planner|crew-database-expert) floor=2; fname=sonnet ;;
372
+ esac
373
+ if [ "$rank" = 0 ]; then
374
+ echo "GUARD (agent model): a call to $st has to name its model. Choose haiku, sonnet or opus by the table in CLAUDE.md (risk decides, not size) and repeat the same call with model set.${fname:+ $st runs on $fname or above.}" >&2
375
+ exit 2
376
+ fi
377
+ if [ "$rank" = -1 ]; then
378
+ echo "GUARD (agent model): '$m' is not a model this gate knows. Repeat the call to $st with model haiku, sonnet or opus.${fname:+ $st runs on $fname or above.}" >&2
379
+ exit 2
380
+ fi
381
+ if [ "$rank" = 4 ] && [ "${CREW_ALLOW_FABLE:-}" != 1 ]; then
382
+ echo "GUARD (agent model): fable is not used for a crew agent unless the user has set CREW_ALLOW_FABLE=1 for the session.${fname:+ $st runs on $fname or above.} Repeat the call to $st with haiku, sonnet or opus, by the table in CLAUDE.md." >&2
383
+ exit 2
384
+ fi
385
+ if [ "$rank" -lt "$floor" ]; then
386
+ echo "GUARD (agent model): $st runs on $fname or above, and this call asks for $m. Repeat the same call with model $fname. An audit does not run below the work it clears." >&2
387
+ exit 2
388
+ fi
389
+ exit 0
390
+ }
391
+ _gate_main "$@"
392
+ _crew_stop "a command of the gate was abandoned"
package/kit/settings.json CHANGED
@@ -97,6 +97,17 @@
97
97
  "timeout": 600
98
98
  }
99
99
  ]
100
+ },
101
+ {
102
+ "matcher": "Agent|Task",
103
+ "hooks": [
104
+ {
105
+ "type": "command",
106
+ "shell": "bash",
107
+ "command": "cd \"$CLAUDE_PROJECT_DIR\" 2>/dev/null; bash .claude/hooks/guard-agent-model.sh",
108
+ "timeout": 600
109
+ }
110
+ ]
100
111
  }
101
112
  ],
102
113
  "UserPromptSubmit": [
@@ -205,7 +205,7 @@ async function handle(req, res) {
205
205
  const session = await findSession(id);
206
206
  if (!session) continue; // a session on another machine: not read here, and not guessed
207
207
  const u = await cachedUsage(session);
208
- usage[id] = { durationMs: u.durationMs, workedMs: u.workedMs, tokens: u.tokens.total, cost: u.cost, unpriced: u.unpriced };
208
+ usage[id] = { durationMs: u.durationMs, workedMs: u.workedMs, startedAt: u.startedAt, tokens: u.tokens.fresh, cost: u.cost, unpriced: u.unpriced };
209
209
  }
210
210
  return sendJson(res, 200, { measured: true, usage });
211
211
  }
@@ -19,7 +19,7 @@ import path from 'node:path';
19
19
 
20
20
  import { readAll, contextFill } from './transcript.js';
21
21
  import { agentMetaFiles } from './projects.js';
22
- import { Usage, timesOf } from './usage.js';
22
+ import { Usage, withTimes } from './usage.js';
23
23
  import { detailOf } from './permissions.js';
24
24
 
25
25
  const SESSION_NODE = 'session';
@@ -60,6 +60,74 @@ function harvestCompletions(text, into, at = null) {
60
60
  }
61
61
  }
62
62
 
63
+ // The line the model gate (kit/hooks/guard-agent-model.sh) refuses a call with starts with this, whatever follows.
64
+ const MODEL_GATE = 'GUARD (agent model):';
65
+
66
+ /**
67
+ * What the model gate said when it refused a call, or null when the text is not its refusal.
68
+ * `floor` is the model it says the agent runs on "or above", when it says one; nothing is read into a sentence
69
+ * that does not say it.
70
+ */
71
+ export function refusalOf(text) {
72
+ const at = typeof text === 'string' ? text.indexOf(MODEL_GATE) : -1;
73
+ if (at === -1) return null;
74
+ const line = text.slice(at).split('\n')[0].trim();
75
+ const floor = line.match(/ runs on ([a-z0-9.-]+) or above/);
76
+ return { text: line.slice(0, 400), floor: floor ? floor[1] : null };
77
+ }
78
+
79
+ // Which of two models is the larger, for the two questions asked of it: was a call repeated on a higher model,
80
+ // and which is "one model up". A name this does not know has no rank, and nothing is concluded from it.
81
+ const FAMILY = ['haiku', 'sonnet', 'opus', 'fable'];
82
+ export function modelRank(model) {
83
+ if (typeof model !== 'string') return null;
84
+ const i = FAMILY.findIndex((f) => model === f || model.includes(`-${f}-`) || model.startsWith(`${f}-`) || model.endsWith(`-${f}`));
85
+ return i === -1 ? null : i;
86
+ }
87
+
88
+ /** How a report says it ended: the `confidence: high|low` line a crew agent closes with, or null. */
89
+ export function confidenceOf(text) {
90
+ const m = typeof text === 'string' ? text.trimEnd().match(/(?:^|\n)\W*confidence:\s*(high|low)\W*$/i) : null;
91
+ return m ? m[1].toLowerCase() : null;
92
+ }
93
+
94
+ /**
95
+ * Tie each agent call to what came before it, where the transcript says so.
96
+ *
97
+ * `firstTry` — the gate refused a call and the session made the same call again (same agent, same task): the
98
+ * call that ran carries what was asked first and why it was refused.
99
+ * Returns nothing; it writes onto the calls.
100
+ */
101
+ export function linkRetries(calls) {
102
+ const list = [...calls.values()].sort((a, b) => a.order - b.order);
103
+ list.forEach((c, i) => {
104
+ if (!c.refused) return;
105
+ const again = list.slice(i + 1).find((n) => n.subagentType === c.subagentType && n.description === c.description);
106
+ if (!again) return;
107
+ // A call refused twice keeps the first thing that was asked.
108
+ again.firstTry = c.firstTry ?? { asked: c.model, floor: c.refused.floor, text: c.refused.text };
109
+ });
110
+ }
111
+
112
+ /**
113
+ * `escalatedFrom` / `escalatedTo` — an agent's report closed with `confidence: low`, and the next call to the same
114
+ * agent after it reported asked for a higher model: the same work, done again one model up. Read from the order
115
+ * of the calls, the two reports and the two models; where any of the three is missing nothing is linked.
116
+ */
117
+ export function linkEscalations(nodes) {
118
+ const agents = nodes.filter((n) => n.kind === 'agent' && n.order != null).sort((a, b) => a.order - b.order);
119
+ agents.forEach((low, i) => {
120
+ if (low.confidence !== 'low' || low.reportedAt == null) return;
121
+ const from = modelRank(low.model ?? low.modelAsked);
122
+ if (from === null) return;
123
+ const again = agents.slice(i + 1).find((n) => n.agentType === low.agentType && n.calledAt != null && n.calledAt >= low.reportedAt);
124
+ const to = again ? modelRank(again.modelAsked ?? again.model) : null;
125
+ if (to === null || to <= from || again.escalatedFrom) return;
126
+ again.escalatedFrom = { id: low.id, from: low.model ?? low.modelAsked, to: again.modelAsked ?? again.model };
127
+ low.escalatedTo = again.id;
128
+ });
129
+ }
130
+
63
131
  function scanMain(records) {
64
132
  const out = {
65
133
  cwd: null, gitBranch: null, version: null, model: null,
@@ -110,6 +178,8 @@ function scanMain(records) {
110
178
  order: out.agentCalls.size + 1,
111
179
  wave: out.waves.get(mid),
112
180
  calledAt: at,
181
+ // The model the call asked for: its own `model` field. Absent, the agent runs on its default.
182
+ model: typeof c.input?.model === 'string' ? c.input.model : null,
113
183
  });
114
184
  }
115
185
  }
@@ -119,6 +189,12 @@ function scanMain(records) {
119
189
  }
120
190
 
121
191
  if (r?.type === 'user') {
192
+ // A call the model gate refused: the answer is an error that carries the gate's own line. No agent started.
193
+ for (const c of Array.isArray(r.message?.content) ? r.message.content : []) {
194
+ if (c?.type !== 'tool_result' || c.is_error !== true || !out.agentCalls.has(c.tool_use_id)) continue;
195
+ const said = refusalOf(textOf(typeof c.content === 'string' ? [{ type: 'text', text: c.content }] : c.content));
196
+ if (said) out.agentCalls.get(c.tool_use_id).refused = said;
197
+ }
122
198
  out.userTurns += 1;
123
199
  harvestCompletions(textOf(r.message?.content), out.completions, at);
124
200
  }
@@ -205,11 +281,13 @@ export function openCalls(records) {
205
281
  }
206
282
 
207
283
  /** Roll one agent's own transcript into the numbers its node shows. */
208
- async function scanAgent(file, usage = null) {
284
+ async function scanAgent(file, usage = null, agentId = null) {
209
285
  const { records } = await readAll(file);
210
- usage?.add(records);
286
+ usage?.add(records, agentId ?? 'agents:unnamed');
211
287
  const stats = {
212
288
  tools: {}, toolCount: 0, lastTool: null, errors: 0,
289
+ model: null, // the model its own transcript names: what it actually ran on
290
+ lastText: null, // the end of the last thing it said, for the line a report closes with
213
291
  tokens: null, startedAt: null, endedAt: null, turns: 0,
214
292
  owns: [], // tool_use ids this agent emitted — how nesting is resolved
215
293
  };
@@ -224,6 +302,10 @@ async function scanAgent(file, usage = null) {
224
302
  }
225
303
  if (r?.type === 'assistant') {
226
304
  stats.turns += 1;
305
+ const mdl = r.message?.model;
306
+ if (typeof mdl === 'string' && mdl && mdl !== '<synthetic>') stats.model = mdl;
307
+ const said = textOf(r.message?.content);
308
+ if (said.trim()) stats.lastText = said.slice(-400);
227
309
  const u = r.message?.usage;
228
310
  if (u && u.cache_read_input_tokens != null) {
229
311
  stats.tokens = (u.input_tokens ?? 0) + (u.cache_read_input_tokens ?? 0) + (u.cache_creation_input_tokens ?? 0);
@@ -274,7 +356,7 @@ async function readAgentDir(subagentsDir, usage = null) {
274
356
  // `stats` needs the transcript read; a missing file is a normal state (the
275
357
  // agent has not written yet), so absence is a null rather than a throw.
276
358
  let stats = null;
277
- if (mtime !== null) stats = await scanAgent(jsonl, usage);
359
+ if (mtime !== null) stats = await scanAgent(jsonl, usage, agentId);
278
360
 
279
361
  out.push({
280
362
  agentId,
@@ -310,6 +392,7 @@ export async function buildGraph(session, { staleMs = STALE_MS } = {}) {
310
392
  const { records, malformed } = await readAll(session.file);
311
393
  const main = scanMain(records);
312
394
  // What the session spent, its agents' tokens included and each response counted once.
395
+ linkRetries(main.agentCalls);
313
396
  const usage = new Usage().add(records);
314
397
  const agents = await readAgentDir(session.subagentsDir, usage);
315
398
  const now = Date.now();
@@ -341,7 +424,7 @@ export async function buildGraph(session, { staleMs = STALE_MS } = {}) {
341
424
  tokens: contextFill(records),
342
425
  startedAt: main.startedAt,
343
426
  endedAt: main.updatedAt,
344
- usage: { ...usage.result(), ...timesOf(records) },
427
+ usage: withTimes(usage, records),
345
428
  // Commands sent to the background that no notice has ended, oldest first. A session that is over has none
346
429
  // running whatever this says: the page shows them only while the session is live.
347
430
  backgroundCommands: [...main.background]
@@ -394,7 +477,16 @@ export async function buildGraph(session, { staleMs = STALE_MS } = {}) {
394
477
  spawnDepth: a.spawnDepth,
395
478
  workflow: a.workflow,
396
479
  toolUseId: a.toolUseId,
397
- model: link?.model ?? null,
480
+ // The model it ran on: what its own transcript names, and failing that what the launch answer resolved.
481
+ model: a.stats?.model ?? link?.model ?? null,
482
+ // The model the call asked for; null when the call named none and the agent ran on its default.
483
+ modelAsked: call?.model ?? null,
484
+ // Set when the model gate refused this call the first time it was made: what was asked then, and why not.
485
+ firstTry: call?.firstTry ?? null,
486
+ // `confidence: high|low`, the line a crew agent's report closes with; null when it has none.
487
+ confidence: confidenceOf(a.stats?.lastText ?? null),
488
+ // Its own share of the session's tokens, and what they cost at list price.
489
+ usage: usage.of(a.agentId),
398
490
  launchStatus: link?.status ?? null,
399
491
  // Started in the background, and its transcript stops in the middle of a turn. Such an agent can be quiet
400
492
  // for a long time inside one tool call; whether it is still working is the live session's to say, so the
@@ -426,6 +518,8 @@ export async function buildGraph(session, { staleMs = STALE_MS } = {}) {
426
518
  edges.push({ id: `${parentId}->${a.agentId}`, source: parentId, target: a.agentId, kind: 'spawn' });
427
519
  }
428
520
 
521
+ linkEscalations(nodes);
522
+
429
523
  // A workflow run is a real container, not a rendering trick: its agents were
430
524
  // spawned by one orchestration script, not by the session directly. Hanging
431
525
  // 243 of them straight off the root produced a graph nobody could read, and