crewforth 3.4.0 → 3.5.0-rc.1
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.
- package/CHANGELOG.md +241 -0
- package/README.md +3 -2
- package/VERSION +1 -1
- package/adopt.sh +40 -5
- package/bin/cli.js +138 -2
- package/kit/AGENT_TEMPLATE.md +15 -0
- package/kit/CLAUDE.md +49 -17
- package/kit/agents/crew-backend-expert.md +4 -3
- package/kit/agents/crew-commit-agent.md +2 -0
- package/kit/agents/crew-database-expert.md +2 -1
- package/kit/agents/crew-devops-expert.md +1 -0
- package/kit/agents/crew-docs-agent.md +3 -1
- package/kit/agents/crew-frontend-expert.md +2 -0
- package/kit/agents/crew-performance-expert.md +7 -2
- package/kit/agents/crew-planner.md +15 -3
- package/kit/agents/crew-privacy-agent.md +2 -0
- package/kit/agents/crew-review-agent.md +8 -1
- package/kit/agents/crew-security-expert.md +4 -3
- package/kit/agents/crew-test-expert.md +6 -2
- package/kit/eval/doctor.sh +201 -1
- package/kit/eval/gate-report.sh +28 -0
- package/kit/eval/golden-holdout.txt +2 -0
- package/kit/eval/golden-routing.txt +17 -0
- package/kit/eval/lib/crew-map/contracts.mjs +77 -0
- package/kit/eval/lib/crew-map/crew-map.sh +2 -1
- package/kit/eval/lib/crew-map/detect.mjs +2 -1
- package/kit/eval/lib/crew-map/narrative.mjs +14 -3
- package/kit/eval/lib/crew-map/project.mjs +45 -6
- package/kit/eval/lib/crew-map/scan.mjs +153 -14
- package/kit/eval/lib/findings/findings.schema.json +11 -1
- package/kit/eval/lib/findings/findings.sh +24 -1
- package/kit/eval/lib/hook-inject.sh +159 -0
- package/kit/eval/lib/lockfiles.sh +71 -0
- package/kit/eval/lib/payload-check.sh +29 -0
- package/kit/eval/lib/preview.sh +3 -3
- package/kit/eval/lib/review-bench/bench.mjs +14 -2
- package/kit/eval/lib/review-prepass/prepass.mjs +32 -3
- package/kit/eval/lib/settings-json.awk +18 -0
- package/kit/eval/lib/untrusted-text.awk +38 -0
- package/kit/eval/scan-skill.sh +67 -20
- package/kit/eval/smoke-test.sh +1700 -40
- package/kit/eval/utilization.sh +77 -3
- package/kit/hooks/agent-outcome.sh +9 -1
- package/kit/hooks/board-sync.sh +49 -3
- package/kit/hooks/board.sh +8 -8
- package/kit/hooks/branch-fresh.sh +101 -0
- package/kit/hooks/floor-blocklist.txt +19 -1
- package/kit/hooks/guard-bash.sh +92 -8
- package/kit/hooks/guard-question.sh +2 -1
- package/kit/hooks/loop-guard.sh +445 -0
- package/kit/hooks/map-stale.sh +437 -0
- package/kit/hooks/pre-commit +48 -7
- package/kit/hooks/precompact-snapshot.sh +102 -6
- package/kit/hooks/prompt-approval.sh +45 -9
- package/kit/hooks/route-hint.sh +36 -7
- package/kit/hooks/secret-blocklist.txt +55 -1
- package/kit/hooks/session-guard.sh +45 -15
- package/kit/hooks/session-rehydrate.sh +34 -11
- package/kit/hooks/session-stats.sh +169 -8
- package/kit/hooks/session-update-check.sh +5 -1
- package/kit/hooks/skill-trust.sh +23 -14
- package/kit/hooks/turn-check.sh +190 -0
- package/kit/owned-blobs.tsv +4 -4
- package/kit/settings.json +64 -0
- package/kit/skills/a11y/SKILL.md +50 -3
- package/kit/skills/a11y/references/recipes.md +41 -0
- package/kit/skills/adr/SKILL.md +14 -2
- package/kit/skills/api-design/SKILL.md +6 -1
- package/kit/skills/api-design/references/idempotency.md +39 -0
- package/kit/skills/brainstorm/SKILL.md +4 -2
- package/kit/skills/brainstorm/references/asking.md +47 -0
- package/kit/skills/ci-pipeline/SKILL.md +11 -1
- package/kit/skills/ci-pipeline/references/agent-job.md +33 -0
- package/kit/skills/commit-message/SKILL.md +9 -3
- package/kit/skills/confidence-check/SKILL.md +9 -7
- package/kit/skills/confidence-check/references/source-check.md +37 -0
- package/kit/skills/crew-approve/SKILL.md +3 -0
- package/kit/skills/crew-audit/SKILL.md +63 -0
- package/kit/skills/crew-board/SKILL.md +2 -2
- package/kit/skills/crew-code-review/SKILL.md +27 -4
- package/kit/skills/crew-code-review/references/panel-mode.md +9 -9
- package/kit/skills/crew-code-review/references/simplify.md +28 -0
- package/kit/skills/crew-doctor/SKILL.md +3 -2
- package/kit/skills/crew-gates/SKILL.md +3 -0
- package/kit/skills/crew-gates/references/enforcers.md +30 -0
- package/kit/skills/crew-interview/SKILL.md +42 -0
- package/kit/skills/crew-map/SKILL.md +26 -6
- package/kit/skills/crew-plan/SKILL.md +9 -2
- package/kit/skills/crew-pr/SKILL.md +57 -0
- package/kit/skills/crew-pr/references/checks.md +35 -0
- package/kit/skills/crew-pr/references/threads.md +23 -0
- package/kit/skills/crew-review/SKILL.md +34 -1
- package/kit/skills/crew-ship/SKILL.md +5 -2
- package/kit/skills/crew-skill/SKILL.md +26 -1
- package/kit/skills/crew-update/SKILL.md +4 -1
- package/kit/skills/db-migration/SKILL.md +15 -0
- package/kit/skills/db-migration/references/expand-contract.md +52 -0
- package/kit/skills/dependency-audit/SKILL.md +34 -1
- package/kit/skills/dependency-audit/scripts/record.sh +37 -0
- package/kit/skills/dependency-upgrade/SKILL.md +29 -1
- package/kit/skills/deploy/SKILL.md +18 -0
- package/kit/skills/deploy/references/topology-a.md +7 -0
- package/kit/skills/deploy/references/topology-b.md +2 -2
- package/kit/skills/docs-writer/SKILL.md +37 -2
- package/kit/skills/eval-grader/SKILL.md +9 -0
- package/kit/skills/eval-grader/references/method.md +26 -0
- package/kit/skills/frontend/SKILL.md +5 -2
- package/kit/skills/frontend/references/from-design.md +29 -0
- package/kit/skills/frontend/references/page-metadata.md +17 -0
- package/kit/skills/frontend/references/verify-contract.md +18 -1
- package/kit/skills/frontend/references/worst-data.md +35 -0
- package/kit/skills/frontend-design/SKILL.md +20 -4
- package/kit/skills/frontend-design/references/design-review.md +21 -0
- package/kit/skills/frontend-design/references/detection.md +44 -0
- package/kit/skills/frontend-design/references/motion.md +54 -0
- package/kit/skills/frontend-design/references/ui-copy.md +35 -0
- package/kit/skills/frontend-rn-expo/SKILL.md +3 -0
- package/kit/skills/handoff/SKILL.md +28 -3
- package/kit/skills/handoff/references/cold-reader.md +13 -6
- package/kit/skills/i18n-integrity/SKILL.md +3 -0
- package/kit/skills/iterate/SKILL.md +7 -2
- package/kit/skills/mcp-builder/SKILL.md +21 -4
- package/kit/skills/mcp-builder/references/building.md +74 -13
- package/kit/skills/mcp-builder/references/cli-for-agents.md +26 -0
- package/kit/skills/observability/SKILL.md +13 -1
- package/kit/skills/performance/SKILL.md +25 -1
- package/kit/skills/performance/references/bottlenecks.md +11 -0
- package/kit/skills/performance/references/web-vitals.md +31 -0
- package/kit/skills/red-team/SKILL.md +25 -3
- package/kit/skills/reflect/SKILL.md +7 -2
- package/kit/skills/release/SKILL.md +13 -0
- package/kit/skills/security-scan/SKILL.md +6 -2
- package/kit/skills/security-scan/references/ai-agents.md +16 -0
- package/kit/skills/security-scan/references/fronts.md +59 -2
- package/kit/skills/security-scan/references/reporting.md +13 -0
- package/kit/skills/security-scan/references/verify.md +9 -0
- package/kit/skills/sonarqube-check/SKILL.md +32 -2
- package/kit/skills/sonarqube-check/references/ratchet.md +32 -0
- package/kit/skills/spec-planning/SKILL.md +25 -8
- package/kit/skills/systematic-debugging/SKILL.md +15 -7
- package/kit/skills/systematic-debugging/references/techniques.md +5 -0
- package/kit/skills/teamboard/SKILL.md +3 -3
- package/kit/skills/testing/SKILL.md +18 -3
- package/kit/skills/testing/references/browser-headless.md +21 -0
- package/kit/skills/token-budget/SKILL.md +26 -5
- package/kit/skills/worktree/SKILL.md +13 -5
- package/kit/studio/server/lib/session.js +23 -2
- package/kit/studio/server/lib/worktree-changes.js +64 -0
- package/kit/studio/web/convo.js +14 -0
- package/kit/studio/web/map-plan.js +54 -8
- package/kit/studio/web/map.js +15 -3
- package/package.json +5 -4
- package/start.sh +11 -2
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# map-stale.sh - says, once per agent, that the Crew Map was scanned before an edit to a file it cites, and which of
|
|
3
|
+
# the project's own rules (.crewforth/contracts.md) live in the edited file; at Stop, tells the user which rules the
|
|
4
|
+
# session's edits touched.
|
|
5
|
+
#
|
|
6
|
+
# WHY. .crewforth/crew-map.json describes modules from lines the scan read. Its module facts are true of the tree
|
|
7
|
+
# it was scanned from; after an edit to a cited file they may not be. A planner that reads a stale map plans
|
|
8
|
+
# against code that has moved. `crew-map.sh --check` can tell, but only when someone thinks to run it; this hook
|
|
9
|
+
# says, at the moment it becomes true, that the question is now open.
|
|
10
|
+
#
|
|
11
|
+
# WIRING (settings.json / hooks.json): PostToolUse, matcher Edit|Write|MultiEdit|NotebookEdit; and Stop.
|
|
12
|
+
#
|
|
13
|
+
# FIRES when ALL of these hold: .crewforth/crew-map.json exists in the project; the edited file is inside the
|
|
14
|
+
# project; the map cites that file (a `"file"` or `"manifest"` value); and this session and agent have not been
|
|
15
|
+
# told yet. Then one line of additionalContext, and a marker so the same agent is not told again. A subagent
|
|
16
|
+
# (`agent_id`) is told on its own: it did not see what the main session was told.
|
|
17
|
+
#
|
|
18
|
+
# CONTRACTS. When .crewforth/contracts.md cites the edited file (`- path:symbol` under a `## key`), the rules that
|
|
19
|
+
# live there are added to the same note, each key once per session and agent, and the key joins the session's list.
|
|
20
|
+
# At Stop, keys on that list the user has not been told yet go to the user as one systemMessage; a session that
|
|
21
|
+
# touched no rule is silent. Both the agent's and the user's text are facts: a rule that still holds needs no edit.
|
|
22
|
+
#
|
|
23
|
+
# WORDING. A fact, not an order: what happened and which command answers it. Measured elsewhere in Crewforth and in
|
|
24
|
+
# the hook-wording lessons it adopted: an imperative in a PostToolUse note is read as a task and derails the turn;
|
|
25
|
+
# a fact is weighed. (UserPromptSubmit is the opposite case; route-hint.sh keeps its instruction wording.)
|
|
26
|
+
#
|
|
27
|
+
# MODES (CREW_MAP_STALE): unset / on as above · off nothing runs and no marker is written.
|
|
28
|
+
#
|
|
29
|
+
# NOT A GATE. It never blocks and never asks: every path out is exit 0, and a failure of its own is silence.
|
|
30
|
+
#
|
|
31
|
+
# COST. It runs after every Edit and Write call and at every Stop. A call that does not fire starts no process: the payload and the
|
|
32
|
+
# map are read with the builtin `read`, the match is a `case`, the marker is a redirection. The first firing in a
|
|
33
|
+
# project may pay one `mkdir`.
|
|
34
|
+
MS_NOTE="Crew Map: .crewforth/crew-map.json cites lines in this file and was scanned before this edit, so its facts about the module may no longer hold. crew-map.sh --check (the scanner /crew-map runs) says whether the map still matches the tree, and the user can type /crew-map to rescan it. (Said once per agent.)"
|
|
35
|
+
case "${CREW_MAP_STALE:-on}" in off|OFF|0|false) exit 0 ;; esac
|
|
36
|
+
export LC_ALL=C
|
|
37
|
+
trap 'exit 0' EXIT
|
|
38
|
+
IFS= read -r -d '' INPUT || true
|
|
39
|
+
INPUT="${INPUT:0:65536}"
|
|
40
|
+
|
|
41
|
+
# ---- CREW-JSON-PARSE ------------------------------------------------------------------------------------
|
|
42
|
+
_json_find(){ # $1 = text, $2 = a literal -> _JF = the offset of its first occurrence, -1 when there is none
|
|
43
|
+
# `${text%%"$literal"*}` gives the same offset and costs the DISTANCE to the occurrence times the length of the
|
|
44
|
+
# text (bash measures the remaining string at every position it tries), so with no occurrence at all it is the
|
|
45
|
+
# square of the size. That was the whole cost of a large Write: the path key is found near the front, and the
|
|
46
|
+
# look for a SECOND one then ran over the content -- measured on macOS, 1 MB: 9.6 s for each of the two key
|
|
47
|
+
# 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
|
|
48
|
+
# timeout, which means not refused. Here the text is walked a piece at a time, each piece long enough to hold an
|
|
49
|
+
# occurrence that starts in it, and the expansion only ever runs inside a piece that `case` says holds one.
|
|
50
|
+
# Two sizes of piece, because taking a piece out of the text costs the length of the text as well: a block is
|
|
51
|
+
# taken from the whole text, and the small pieces from the block.
|
|
52
|
+
local LC_ALL=C
|
|
53
|
+
local i=0 j n=${#1} kl=${#2} B=262144 C=4096 b bn c pre
|
|
54
|
+
_JF=-1
|
|
55
|
+
while [ "$i" -lt "$n" ]; do
|
|
56
|
+
b="${1:i:B+kl-1}"
|
|
57
|
+
case "$b" in *"$2"*)
|
|
58
|
+
j=0; bn=${#b}
|
|
59
|
+
while [ "$j" -lt "$bn" ]; do
|
|
60
|
+
c="${b:j:C+kl-1}"
|
|
61
|
+
case "$c" in *"$2"*) pre="${c%%"$2"*}"; _JF=$((i+j+${#pre})); return 0 ;; esac
|
|
62
|
+
j=$((j+C))
|
|
63
|
+
done ;;
|
|
64
|
+
esac
|
|
65
|
+
i=$((i+B))
|
|
66
|
+
done
|
|
67
|
+
}
|
|
68
|
+
_json_slice(){ # $1 = whole payload, $2 = key -> the raw (still JSON-escaped) string value, "" if absent
|
|
69
|
+
local LC_ALL=C # FIRST, so every expansion below -- the key search included -- counts and cuts in bytes.
|
|
70
|
+
# Lengths from ${#x} are used as offsets into ${y:n}; with the locale set before any of
|
|
71
|
+
# them, the two never disagree about a unit. Walking bytes is safe because no UTF-8
|
|
72
|
+
# continuation byte can be 0x5C or 0x22, so a cut cannot land inside a character at a
|
|
73
|
+
# quote or backslash edge. What this line is worth in SPEED depends on the platform --
|
|
74
|
+
# read the table in _json_unescape below before quoting a number for it.
|
|
75
|
+
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=("")
|
|
76
|
+
# It ALSO sets `_JS`, so a caller on the hot path can read the value without `$( )`, which is a fork. The
|
|
77
|
+
# printf stays because the existing callers compose it (`_json_unescape "$(_json_slice …)"`), but the one
|
|
78
|
+
# caller that used to do its own fork-free extraction — `_CWD` — reads `_JS` and keeps costing nothing.
|
|
79
|
+
_JS=""
|
|
80
|
+
# Every "step past X" here is arithmetic on a length, never `${s#"$literal"}`. That shape reads like a
|
|
81
|
+
# constant-time strip and is not one: bash retries the pattern at every prefix length, so stripping an
|
|
82
|
+
# n-byte literal costs O(n^2). It was in this function twice, and both were measured:
|
|
83
|
+
# * `${1#*"$2"}` found the key -- cheap when the key sits near the front, quadratic in the distance to it
|
|
84
|
+
# otherwise. `permission_mode` behind a 100 KB command took 7.94s on Git Bash, 0.27s in front of it.
|
|
85
|
+
# Captured from Claude Code 2.1.267, the real order puts `permission_mode` BEFORE `tool_input`, so on
|
|
86
|
+
# that version this cost was not reachable -- this file's header had shown the opposite order, and was
|
|
87
|
+
# wrong. The parse stays order-independent regardless: the order is not a documented contract, and the
|
|
88
|
+
# payload is still moving (`effort` is absent from the field list recorded on 2.1.246).
|
|
89
|
+
# * `${tail#"$seg"\"}` stepped past each escaped quote: 16.8s for a single 100 KB step on Git Bash,
|
|
90
|
+
# against 0.002s for `${tail:${#seg}+1}` doing exactly the same thing.
|
|
91
|
+
# _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
|
|
92
|
+
# command "containing the literal text `\"command\":\"` still cannot relocate the parse", which is true of
|
|
93
|
+
# that byte sequence and irrelevant, because this search is for `"command"` WITHOUT the colon. A JSON VALUE
|
|
94
|
+
# equal to the key name is exactly those bytes between two unescaped quotes. Measured on the shipped hook:
|
|
95
|
+
# {"tool_name":"Bash","a":"command","ls":1,"tool_input":{"command":"rm -rf /"}} -> read `ls`
|
|
96
|
+
# {"a":"permission_mode","default":1,"permission_mode":"bypassPermissions",…} -> read `default`
|
|
97
|
+
# {"a":"file_path","/tmp/ok":1,"tool_input":{"file_path":".claude/hooks/x.sh"}} -> read `/tmp/ok`
|
|
98
|
+
# In each case the gate judged a harmless string while a dangerous one was the actual argument, and jq read
|
|
99
|
+
# the real value -- so on a stock Windows desktop, where this was already the only parser, the gate could be
|
|
100
|
+
# aimed with one extra key. The loop below therefore accepts an occurrence only when optional JSON whitespace
|
|
101
|
+
# and then `:` follow it, which is the only place a key can appear; everything else keeps searching.
|
|
102
|
+
# Stepping by length removed the quadratic strip, but each escaped quote still sliced and re-assigned the
|
|
103
|
+
# whole remainder, so an escape-dense command stayed k*n here as well (2.2s of the 6.8s described in
|
|
104
|
+
# _json_unescape below, on Git Bash). The walk is chunked the same way: the payload is read 4096 bytes at a
|
|
105
|
+
# time and every per-quote operation stays inside the chunk. One thing is carried across edges on purpose
|
|
106
|
+
# -- the length of the backslash run in front of a quote -- because that run can straddle a window or a
|
|
107
|
+
# chunk, and its parity is what decides whether the quote ends the value. Isolated, escape-dense 44030 B:
|
|
108
|
+
# 2.61s -> 0.41s on Git Bash 5.3.15, 0.93s -> 0.09s on macOS/bash 3.2.
|
|
109
|
+
# Output is byte-identical to the previous shape across 378 cases at four chunk/window sizes down to 7 and 1
|
|
110
|
+
# bytes, and that shape to the one before it across a 27-case battery (escaped quotes, backslash runs before
|
|
111
|
+
# a quote, the key twice, the key appearing first as a value, glob metacharacters, UTF-8, no closing quote,
|
|
112
|
+
# empty input). Broken twins -- the run not carried across a window, a byte skipped after a quote -- prove
|
|
113
|
+
# the battery sees both.
|
|
114
|
+
# THE NUMBER OF PASSES IS CAPPED, for the same reason guard-write caps the path length: an unbounded cost on
|
|
115
|
+
# a PreToolUse hook is a gate with an off switch, because a hook killed at its timeout emits no exit 2.
|
|
116
|
+
# Each decoy costs one more `%%` scan over the remainder, so the work is quadratic in the number of decoys.
|
|
117
|
+
# Measured on macOS/bash, single call, `"k<i>":"command"` decoys in front of the real key:
|
|
118
|
+
# 50 decoys 844 B 12 ms 800 decoys 13544 B 126 ms
|
|
119
|
+
# 200 decoys 3344 B 19 ms 3200 decoys 56544 B 1839 ms
|
|
120
|
+
# Git Bash's parameter expansion is several times slower again, so the tail is where the timeout lives. A
|
|
121
|
+
# real payload carries ZERO decoys -- 64 is far above anything a producer can legitimately emit -- and going
|
|
122
|
+
# over the cap is reported SEPARATELY (`_KC_CAPPED`), not as a large count, so the caller can refuse with a
|
|
123
|
+
# reason the reader can act on instead of a sentinel dressed up as a measurement. Fail-closed either way.
|
|
124
|
+
# THE COST IS ONE THIS CHANGE INTRODUCES, and it is worth saying so plainly rather than implying the cap
|
|
125
|
+
# protects something pre-existing: before the colon requirement the scan STOPPED at the first occurrence, so
|
|
126
|
+
# decoys cost essentially nothing (measured on stock Windows: 0 decoys 0.18 ms, 3200 decoys 1.60 ms per call,
|
|
127
|
+
# roughly linear) -- and it read the decoy, which is the hole. The cap is load-bearing for the new cost, so
|
|
128
|
+
# raising or removing it later is not a tidy-up.
|
|
129
|
+
# The bound is on the LOOP, not on the payload, so an ordinary command pays nothing: the same 56572 B flood
|
|
130
|
+
# is refused in 93-94 ms with jq/python3 present and 168-169 ms on the slice path this paragraph is about,
|
|
131
|
+
# 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
|
|
132
|
+
# predates this change and belongs to the walk, not to the cap.
|
|
133
|
+
local _cap=64 _seen=0
|
|
134
|
+
while :; do
|
|
135
|
+
_json_find "$hay" "$k" # where the next `"key"` starts
|
|
136
|
+
[ "$_JF" -ge 0 ] || return 0 # no further occurrence: emit nothing
|
|
137
|
+
_seen=$((_seen+1)); [ "$_seen" -le "$_cap" ] || return 0
|
|
138
|
+
rest="${hay:_JF+${#k}}" # past `"key"`
|
|
139
|
+
while [ -n "$rest" ] && [[ "${rest:0:1}" == [$' \t\n\r'] ]]; do rest="${rest:1}"; done
|
|
140
|
+
case "$rest" in
|
|
141
|
+
:*) rest="${rest:1}"; break ;; # whitespace then `:` -- this occurrence IS the key
|
|
142
|
+
*) hay="$rest" ;; # a value, or another token: keep looking
|
|
143
|
+
esac
|
|
144
|
+
done
|
|
145
|
+
# THE VALUE MUST BE A STRING. `"command":123}}` used to come back as the literal text `:123}}` -- the old
|
|
146
|
+
# shape skipped forward to the next quote wherever it was, so a non-string value handed the matchers the
|
|
147
|
+
# payload's own punctuation to judge. Emitting nothing instead lets the caller's "no readable command"
|
|
148
|
+
# refusal fire, which is the honest answer for a value these gates cannot read.
|
|
149
|
+
while [ -n "$rest" ] && [[ "${rest:0:1}" == [$' \t\n\r'] ]]; do rest="${rest:1}"; done
|
|
150
|
+
case "$rest" in '"'*) rest="${rest:1}" ;; *) return 0 ;; esac
|
|
151
|
+
# Walk to the closing quote that is NOT escaped. A `"` preceded by an odd number of backslashes is content.
|
|
152
|
+
n=${#rest}; chunk="${rest:0:C}"; cl=${#chunk}
|
|
153
|
+
while :; do
|
|
154
|
+
lim=$((cl - j))
|
|
155
|
+
if [ "$lim" -le 0 ]; then
|
|
156
|
+
[ $((base + cl)) -lt "$n" ] || break # no closing quote at all: everything is already taken
|
|
157
|
+
base=$((base + j)); chunk="${rest:base:C}"; cl=${#chunk}; j=0; continue
|
|
158
|
+
fi
|
|
159
|
+
[ "$lim" -le "$W" ] || lim=$W
|
|
160
|
+
w="${chunk:j:lim}"
|
|
161
|
+
seg="${w%%\"*}"
|
|
162
|
+
if [ "$seg" = "$w" ]; then # no quote in view: take all of it, carry the run
|
|
163
|
+
acc+=("$w"); j=$((j+lim))
|
|
164
|
+
r="${w##*[!\\]}"; case "$w" in *[!\\]*) run=${#r} ;; *) run=$((run+${#w})) ;; esac
|
|
165
|
+
continue
|
|
166
|
+
fi
|
|
167
|
+
acc+=("$seg"); j=$((j+${#seg}+1))
|
|
168
|
+
r="${seg##*[!\\]}"; case "$seg" in *[!\\]*) run=${#r} ;; *) run=$((run+${#seg})) ;; esac
|
|
169
|
+
if [ $((run % 2)) -eq 1 ]; then acc+=("\""); run=0; else break; fi
|
|
170
|
+
done
|
|
171
|
+
local IFS=''; _JS="${acc[*]}"; printf '%s' "$_JS"
|
|
172
|
+
}
|
|
173
|
+
_json_unescape(){ # left-to-right, a chunk at a time; a two-pass sed would corrupt `\\"` (escaped backslash + quote)
|
|
174
|
+
# This is the tier-3 path below -- the one a stock Windows install actually runs on. It used to walk ONE
|
|
175
|
+
# CHARACTER at a time, which is O(n^2) twice over: `${s%"${s#?}"}` matches a pattern the length of the
|
|
176
|
+
# entire remainder just to read one character, and `out="$out$c"` recopies the output for each one.
|
|
177
|
+
#
|
|
178
|
+
# 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
|
|
179
|
+
# default, and reading the default instead is how a first pass at this got the consequence wrong. A
|
|
180
|
+
# PreToolUse hook KILLED at its timeout emits no exit 2, so every rule below is simply skipped. Measured on
|
|
181
|
+
# the tier-3 path with jq and python3 both shadowed, the old shape crossed 60s at ~4.3 KB. And 4.3 KB is
|
|
182
|
+
# not exotic: across 6791 Bash calls in 280 real transcripts, 2.49% of commands are bigger, and the largest
|
|
183
|
+
# is 46815 B, which the old shape needed roughly 39 minutes to decode. One Bash call in forty walked past
|
|
184
|
+
# §4.4 and §4.5 entirely, silently, on every stock Windows desktop.
|
|
185
|
+
#
|
|
186
|
+
# Two changes, and their wins are NOT the same shape -- writing them down as one number was the mistake
|
|
187
|
+
# this comment exists to avoid repeating:
|
|
188
|
+
# * Taking the whole run up to the next backslash in ONE expansion, and indexing with `${s:0:1}` instead
|
|
189
|
+
# of matching a pattern, is 47x on a 4.4 KB payload. This one holds on every machine.
|
|
190
|
+
# * `local LC_ALL=C` makes these expansions byte-oriented instead of re-decoding the string on every
|
|
191
|
+
# substring operation. What that is WORTH depends on the platform, and putting a single number here
|
|
192
|
+
# would have been wrong three separate ways -- it was written as "another 5x" twice before this:
|
|
193
|
+
# macOS / bash 3.2, a locale set .................. 5x
|
|
194
|
+
# Git Bash 5.3.15, a locale set ................... 1.23x (measured on the OLD shape; the new one
|
|
195
|
+
# touches the locale once per escape
|
|
196
|
+
# rather than once per character, so its
|
|
197
|
+
# ratio is smaller and unmeasured)
|
|
198
|
+
# Git Bash, LANG empty -- what Claude Code starts .. nothing at all
|
|
199
|
+
# So speed is not what keeps this line; CORRECTNESS is, and that half holds everywhere. `[0-9a-fA-F]`
|
|
200
|
+
# below is a collation-defined range outside the C locale: on a tr_TR desktop it is not 0-9a-f. That
|
|
201
|
+
# alone would justify the line, and it costs nothing. `local` restores the previous locale on return
|
|
202
|
+
# -- verified on bash 3.2 and on Git Bash 5.3.15, not assumed.
|
|
203
|
+
#
|
|
204
|
+
# Then the cost moved rather than went away. With the per-character walk gone, each escape still sliced the
|
|
205
|
+
# whole REMAINDER -- `s="${s:${#pre}+1}"` -- so an escape-dense command stayed k*n: on Git Bash a 44080 B
|
|
206
|
+
# command with 2755 escapes took 6.8s, 11% of the timeout. The input is now touched only a chunk at a time
|
|
207
|
+
# (`${s:base:C}`, once per 4096 bytes) and every per-escape operation stays inside that chunk, so no escape
|
|
208
|
+
# pays for the length of the whole command. That is the change that matters: bounding only the lookahead,
|
|
209
|
+
# while still indexing the full string, was measured too and gave about 4x on both machines, against 6-12x
|
|
210
|
+
# for the chunked walk. Five bytes are held back at a chunk's end whenever more input follows, so a
|
|
211
|
+
# `\uXXXX` that starts in a chunk ends in it.
|
|
212
|
+
# The output goes into an array joined once, not a string recopied at every append, and `\n`/`\t` are
|
|
213
|
+
# written `$'\n'`/`$'\t'`, so this file carries no raw TAB for an editor or a copy to turn into spaces.
|
|
214
|
+
#
|
|
215
|
+
# Measured, isolated, previous shape -> this one:
|
|
216
|
+
# macOS/bash 3.2 escape-dense 44030 B, 2590 escapes .. 1.44s -> 0.12s
|
|
217
|
+
# sparse 100014 B, 4 escapes ........... 0.09s -> 0.02s
|
|
218
|
+
# Git Bash 5.3.15 escape-dense 44030 B, 2590 escapes .. 3.92s -> 0.54s
|
|
219
|
+
# (LANG empty) sparse 99997 B, 4 escapes ............ 0.49s -> 0.07s
|
|
220
|
+
# These numbers do not travel, which is why each one names its machine.
|
|
221
|
+
#
|
|
222
|
+
# Output is byte-identical to the previous shape across 478 cases, each run at four chunk/window sizes
|
|
223
|
+
# down to 7 and 1 bytes so that an edge falls every few bytes; that shape was itself byte-identical to
|
|
224
|
+
# the original character walk across a 31-case battery (escaped quotes, `\\`, a lone trailing backslash, a
|
|
225
|
+
# truncated `\u`, Turkish, emoji). Deliberately broken twins -- the 5-byte margin cut to 4, the backslash
|
|
226
|
+
# not stepped over -- prove the battery sees both edges, and tier 1 and tier 3 return the same verdict on
|
|
227
|
+
# the gate cases.
|
|
228
|
+
local LC_ALL=C
|
|
229
|
+
local s="$1" pre w c h n base=0 j=0 cl lim chunk C=4096 W=256; local -a acc=("")
|
|
230
|
+
case "$s" in *\\*) ;; *) _JU="$s"; printf '%s' "$s"; return 0 ;; esac # no escapes: the common case pays nothing
|
|
231
|
+
n=${#s}; chunk="${s:0:C}"; cl=${#chunk}
|
|
232
|
+
while :; do
|
|
233
|
+
lim=$((cl - j))
|
|
234
|
+
if [ $((base + cl)) -lt "$n" ]; then # more input after this chunk: hold 5 bytes back, so
|
|
235
|
+
lim=$((lim - 5)) # a `\uXXXX` that starts in a chunk also ends in it
|
|
236
|
+
if [ "$lim" -le 0 ]; then base=$((base + j)); chunk="${s:base:C}"; cl=${#chunk}; j=0; continue; fi
|
|
237
|
+
else
|
|
238
|
+
[ "$lim" -gt 0 ] || break
|
|
239
|
+
fi
|
|
240
|
+
[ "$lim" -le "$W" ] || lim=$W
|
|
241
|
+
w="${chunk:j:lim}"
|
|
242
|
+
pre="${w%%\\*}" # the literal run before the next backslash in view
|
|
243
|
+
if [ "$pre" = "$w" ]; then acc+=("$w"); j=$((j+lim)); continue; fi
|
|
244
|
+
acc+=("$pre"); j=$((j+${#pre}+1))
|
|
245
|
+
if [ $((base + j)) -ge "$n" ]; then acc+=("\\"); break; fi # a lone trailing backslash stays literal, as before
|
|
246
|
+
c="${chunk:j:1}"; j=$((j+1))
|
|
247
|
+
case "$c" in
|
|
248
|
+
n) acc+=($'\n') ;;
|
|
249
|
+
t) acc+=($'\t') ;;
|
|
250
|
+
r) ;;
|
|
251
|
+
b|f) acc+=(" ") ;;
|
|
252
|
+
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
|
|
253
|
+
# A `\uXXXX` used to become a literal `?`. That is not a lossy nicety, it is a hole: `\u002e` is `.`,
|
|
254
|
+
# so `\u002eclaude/hooks/guard-bash.sh` decoded to `?claude/…` and matched no gate pattern, while jq
|
|
255
|
+
# decoded the same bytes to the real path — the two tiers disagreed on whether a payload was an
|
|
256
|
+
# attack. Printable ASCII is decoded properly (builtin printf, no fork); anything else still becomes
|
|
257
|
+
# `?`, which is only ever a display concern because this value is used for MATCHING, never to write.
|
|
258
|
+
case "$h" in
|
|
259
|
+
00[2-7][0-9a-fA-F]) printf -v c "\\x${h#00}"; acc+=("$c") ;;
|
|
260
|
+
# The three dashes PowerShell accepts in front of a parameter name, as themselves: as `?` they hid
|
|
261
|
+
# `\u2013Recurse \u2013Force` from the delete rule, while the same bytes sent raw were caught.
|
|
262
|
+
2013) acc+=("–") ;; 2014) acc+=("—") ;; 2015) acc+=("―") ;;
|
|
263
|
+
*) acc+=("?") ;;
|
|
264
|
+
esac ;;
|
|
265
|
+
*) acc+=("$c") ;;
|
|
266
|
+
esac
|
|
267
|
+
done
|
|
268
|
+
# ALSO sets `_JU`, for the same reason `_json_slice` sets `_JS`: a caller on the hot path reads the value from it
|
|
269
|
+
# and sends this printf to /dev/null, instead of paying a `$( )` — a fork — on every tool call.
|
|
270
|
+
local IFS=''; _JU="${acc[*]}"; printf '%s' "$_JU"
|
|
271
|
+
}
|
|
272
|
+
_json_keycount(){ # $1 = payload, $2 = key -> sets _KC to how many times it occurs AS A KEY
|
|
273
|
+
# IT SETS A VARIABLE INSTEAD OF PRINTING, and that is not a style choice. Written as `n="$(_json_keycount
|
|
274
|
+
# …)"` first, which is a command substitution, which is a FORK -- on a hook that runs before every Bash and
|
|
275
|
+
# every Write call. Measured on macOS/bash, typical payload, 200 reps: the substitution form cost 0.790 ms
|
|
276
|
+
# per call against 0.090 ms for the raw byte test it replaces, and the whole hook went 15.00 -> 16.58 ms.
|
|
277
|
+
# This project's own recorded Git Bash process cost is 62-135 ms, rising to ~400 ms under load, so three
|
|
278
|
+
# added forks per call would have been a freeze on the platform the change is meant to protect. Setting _KC
|
|
279
|
+
# keeps the count in the caller's own shell: expansion only, zero forks.
|
|
280
|
+
# The counter the ambiguity refusals need, and it exists because the hand-written byte test they used to do
|
|
281
|
+
# looked for a DIFFERENT token than the parser above: `"key":` compact, while the parser searches `"key"` and
|
|
282
|
+
# tolerates whitespace before the colon. Two consequences, both measured on the shipped hook:
|
|
283
|
+
# * `{…"meta":{"command":"ls"},"tool_input":{"command" : "rm -rf /"}}` -- ONE SPACE and the duplicate-key
|
|
284
|
+
# refusal went blind while the parser happily read the first value.
|
|
285
|
+
# * a key name appearing as a VALUE was counted as an occurrence by neither, which is the hole the parser
|
|
286
|
+
# above now closes; counting the same form here is what keeps the two from drifting apart again.
|
|
287
|
+
# Sharing ONE definition with the parser is the point: a guard that searches for something else than the
|
|
288
|
+
# thing it guards is the defect, not an implementation detail.
|
|
289
|
+
# WHAT THE TOKEN CAN BE, stated correctly. An earlier version of this comment said the bytes `"command"`
|
|
290
|
+
# with both quotes unescaped "can only be a key or a value equal to the key name". That is wrong, and the
|
|
291
|
+
# counter-example was found by review, not by reasoning: a KEY whose own name ends with a quote spells the
|
|
292
|
+
# token out of its escaped quote plus the string's terminator --
|
|
293
|
+
# {"tool_input":{"x\"command":"DECOY","command":"rm -rf /"}} slice -> DECOY, count -> 2
|
|
294
|
+
# which is refused BECAUSE the count sees two, not because the invariant held. The same shape as a VALUE is
|
|
295
|
+
# harmless (a string's terminator is followed by `,` `}` `]`, never `:`, so it is skipped and not counted:
|
|
296
|
+
# {"description":"ends with \"command","command":"rm -rf /"} slice -> rm -rf /, count -> 1 ).
|
|
297
|
+
# What IS true, and is what keeps ordinary work from being refused: content can contribute at most one
|
|
298
|
+
# occurrence per string and only as that string's tail, where the next byte is never a colon. Measured on 19
|
|
299
|
+
# payloads built by a real JSON encoder -- `grep -rn '"command":' .`, a heredoc writing a hooks.json, `sed`
|
|
300
|
+
# over settings.json, a commit message quoting the word, commands ending in `"command` -- every count <= 1.
|
|
301
|
+
# Same pass cap as the slice, and over-cap reports AMBIGUOUS rather than a true count: the callers refuse on
|
|
302
|
+
# `> 1`, so a payload built to outrun the loop is refused instead of being timed out past the gate.
|
|
303
|
+
local LC_ALL=C hay="$1" k="\"$2\"" rest c=0 _cap=64 _seen=0
|
|
304
|
+
_KC=0; _KC_CAPPED=0
|
|
305
|
+
while :; do
|
|
306
|
+
_json_find "$hay" "$k"
|
|
307
|
+
[ "$_JF" -ge 0 ] || { _KC=$c; return 0; }
|
|
308
|
+
# OVER-CAP IS ITS OWN ANSWER, not a large count. `_KC_CAPPED` lets the caller say what actually happened:
|
|
309
|
+
# the occurrences it stopped at are candidate positions, key-form or not, so calling them "65 keys" was a
|
|
310
|
+
# sentinel dressed up as a measurement and the remedy it offered ("send one key") was already satisfied.
|
|
311
|
+
_seen=$((_seen+1)); [ "$_seen" -le "$_cap" ] || { _KC=$((_cap+1)); _KC_CAPPED=$_cap; return 0; }
|
|
312
|
+
rest="${hay:_JF+${#k}}"
|
|
313
|
+
while [ -n "$rest" ] && [[ "${rest:0:1}" == [$' \t\n\r'] ]]; do rest="${rest:1}"; done
|
|
314
|
+
case "$rest" in :*) c=$((c+1)) ;; esac
|
|
315
|
+
hay="$rest"
|
|
316
|
+
done
|
|
317
|
+
}
|
|
318
|
+
# ---- /CREW-JSON-PARSE -----------------------------------------------------------------------------------
|
|
319
|
+
|
|
320
|
+
# ---- helpers: builtins only ------------------------------------------------------------------------------
|
|
321
|
+
_ms_id(){ # $1 = raw id -> _MSI: letters, digits, _ and - only, 80 at most (it names a file)
|
|
322
|
+
_MSI="${1//[!A-Za-z0-9_-]/}"; _MSI="${_MSI:0:80}"; }
|
|
323
|
+
_ms_path(){ # $1 = a path, JSON-escaped or not -> _MSP: forward slashes, a drive letter in lower case, no trailing /
|
|
324
|
+
local p="${1//\\\\/\\}" u=ABCDEFGHIJKLMNOPQRSTUVWXYZ l=abcdefghijklmnopqrstuvwxyz
|
|
325
|
+
p="${p//\\//}"
|
|
326
|
+
case "$p" in [A-Z]:/*) u="${u%%"${p:0:1}"*}"; p="${l:${#u}:1}${p:1}" ;; esac # the drive letter, lower case, no fork
|
|
327
|
+
_MSP="${p%/}"; }
|
|
328
|
+
|
|
329
|
+
# ---- the payload -----------------------------------------------------------------------------------------
|
|
330
|
+
_json_slice "$INPUT" hook_event_name >/dev/null; EV="$_JS"
|
|
331
|
+
case "$EV" in PostToolUse|Stop) ;; *) exit 0 ;; esac
|
|
332
|
+
_json_slice "$INPUT" session_id >/dev/null; _ms_id "$_JS"; SID="$_MSI"; [ -n "$SID" ] || exit 0
|
|
333
|
+
# The project: the hook is started in it (settings.json cds there); the plugin edition reads it from the env or the payload.
|
|
334
|
+
P="${CLAUDE_PROJECT_DIR:-}"
|
|
335
|
+
if [ -z "$P" ]; then _json_slice "$INPUT" cwd >/dev/null; P="$_JS"; fi
|
|
336
|
+
[ -n "$P" ] || exit 0
|
|
337
|
+
_ms_path "$P"; P="$_MSP"
|
|
338
|
+
M="$P/.crewforth/crew-map.json"; C="$P/.crewforth/contracts.md"; ST="$P/.claude/state"
|
|
339
|
+
|
|
340
|
+
# ---- Stop: the user hears which of the project's rules this session's edits touched, each key once ------------
|
|
341
|
+
if [ "$EV" = Stop ]; then
|
|
342
|
+
S="$ST/contracts-$SID"
|
|
343
|
+
[ -f "$S" ] || exit 0
|
|
344
|
+
LIST=""; IFS= read -r -d '' LIST < "$S" || true
|
|
345
|
+
SAID=""; [ -f "$S-said" ] && { IFS= read -r -d '' SAID < "$S-said" || true; }
|
|
346
|
+
NEW=""; rest="$LIST"
|
|
347
|
+
while [ -n "$rest" ]; do
|
|
348
|
+
rest="${rest#|}"; k="${rest%%|*}"; rest="${rest#"$k"}"; rest="${rest#|}"
|
|
349
|
+
[ -n "$k" ] || continue
|
|
350
|
+
case "$SAID" in *"|$k|"*) ;; *) NEW="$NEW${NEW:+, }$k"; SAID="$SAID|$k|" ;; esac
|
|
351
|
+
done
|
|
352
|
+
[ -n "$NEW" ] || exit 0
|
|
353
|
+
printf '%s' "$SAID" > "$S-said" 2>/dev/null || exit 0
|
|
354
|
+
printf '{"systemMessage":"Contracts: this session edited files that .crewforth/contracts.md cites for %s. A review gives a verdict per key (the user can type /crew-review); a rule that still holds needs no change to the file."}\n' "$NEW"
|
|
355
|
+
exit 0
|
|
356
|
+
fi
|
|
357
|
+
|
|
358
|
+
_json_slice "$INPUT" tool_name >/dev/null
|
|
359
|
+
case "$_JS" in Edit|Write|MultiEdit|NotebookEdit) ;; *) exit 0 ;; esac
|
|
360
|
+
_json_slice "$INPUT" agent_id >/dev/null; _ms_id "$_JS"; AG="$_MSI"
|
|
361
|
+
[ -f "$M" ] || [ -f "$C" ] || exit 0
|
|
362
|
+
|
|
363
|
+
_json_slice "$INPUT" file_path >/dev/null; FP="$_JS"
|
|
364
|
+
[ -n "$FP" ] || { _json_slice "$INPUT" notebook_path >/dev/null; FP="$_JS"; }
|
|
365
|
+
[ -n "$FP" ] || exit 0
|
|
366
|
+
_ms_path "$FP"; FP="$_MSP"; PP="$P"
|
|
367
|
+
# Git Bash may give the project as /c/... while the tool names the file C:\...: compare them in one form.
|
|
368
|
+
case "$FP:$PP" in [a-z]:/*:/[A-Za-z]/*) _ms_path "${PP:1:1}:${PP:2}"; PP="$_MSP" ;; esac
|
|
369
|
+
case "$FP" in
|
|
370
|
+
"$PP"/*) REL="${FP:${#PP}+1}" ;;
|
|
371
|
+
/*|[A-Za-z]:/*) exit 0 ;; # outside the project: the map does not describe it
|
|
372
|
+
*) REL="${FP#./}" ;; # relative: already the project's
|
|
373
|
+
esac
|
|
374
|
+
case "$REL" in ''|../*|*/../*) exit 0 ;; esac
|
|
375
|
+
|
|
376
|
+
# Whatever is said below leaves through one line of JSON, written on the way out, so an early exit of the map's
|
|
377
|
+
# part still delivers the contracts' part. The map's note comes first.
|
|
378
|
+
MN=""; CN=""
|
|
379
|
+
_ms_out(){ [ -n "$MN$CN" ] || return 0
|
|
380
|
+
printf '{"hookSpecificOutput":{"hookEventName":"PostToolUse","additionalContext":"%s%s%s"}}\n' "$MN" "${MN:+${CN:+ }}" "$CN"; }
|
|
381
|
+
trap '_ms_out; exit 0' EXIT
|
|
382
|
+
|
|
383
|
+
# ---- the project's rules: .crewforth/contracts.md (eval/lib/crew-map/contracts.mjs) ------------------------
|
|
384
|
+
# A section is `## key`, its rule (the first paragraph) and `- path:symbol` cites. An edit to a cited file brings
|
|
385
|
+
# the rules that live there into the agent's context, each key once per session and agent; the session's list of
|
|
386
|
+
# touched keys is what Stop tells the user. At most 1 MB of the file is read, line by line, with builtins only.
|
|
387
|
+
if [ -f "$C" ]; then
|
|
388
|
+
CT=""; IFS= read -r -d '' -n 1048576 CT < "$C" || true
|
|
389
|
+
case "$CT" in *"$REL:"*)
|
|
390
|
+
TK="$ST/contracts-told-$SID${AG:+-$AG}"; TOLD=""; [ -f "$TK" ] && { IFS= read -r -d '' TOLD < "$TK" || true; }
|
|
391
|
+
SL="$ST/contracts-$SID"; LIST=""; [ -f "$SL" ] && { IFS= read -r -d '' LIST < "$SL" || true; }
|
|
392
|
+
key=""; rule=""; para=0; told0="$TOLD"; list0="$LIST"
|
|
393
|
+
while IFS= read -r l || [ -n "$l" ]; do
|
|
394
|
+
l="${l%$'\r'}"
|
|
395
|
+
case "$l" in "## "*) _ms_id "${l#"## "}"; key="$_MSI"; rule=""; para=1; continue ;; esac
|
|
396
|
+
[ -n "$key" ] || continue
|
|
397
|
+
t="${l#"${l%%[![:space:]]*}"}"
|
|
398
|
+
case "$t" in
|
|
399
|
+
"- $REL:"?*|"* $REL:"?*)
|
|
400
|
+
case "$LIST" in *"|$key|"*) ;; *) LIST="$LIST|$key|" ;; esac
|
|
401
|
+
case "$TOLD" in *"|$key|"*) ;; *)
|
|
402
|
+
TOLD="$TOLD|$key|"; r="${rule:0:300}"; r="${r//\\/\\\\}"; r="${r//\"/\\\"}"; r="${r//$'\t'/ }"
|
|
403
|
+
CN="$CN${CN:+ }$key: $r" ;;
|
|
404
|
+
esac ;;
|
|
405
|
+
"- "*|"* "*) para=2 ;;
|
|
406
|
+
'') [ "$para" = 3 ] && para=2 ;;
|
|
407
|
+
*) if [ "$para" = 1 ]; then rule="$t"; para=3; elif [ "$para" = 3 ]; then rule="$rule $t"; fi ;;
|
|
408
|
+
esac
|
|
409
|
+
done <<< "$CT"
|
|
410
|
+
if [ "$TOLD" != "$told0" ] || [ "$LIST" != "$list0" ]; then
|
|
411
|
+
[ -d "$ST" ] || mkdir -p "$ST" 2>/dev/null
|
|
412
|
+
printf '%s' "$TOLD" > "$TK" 2>/dev/null; printf '%s' "$LIST" > "$SL" 2>/dev/null
|
|
413
|
+
fi
|
|
414
|
+
[ -n "$CN" ] && CN="Contracts: .crewforth/contracts.md cites this file for these rules. $CN (Each rule said once per agent; a change that keeps a rule needs no edit to the file.)"
|
|
415
|
+
;; esac
|
|
416
|
+
fi
|
|
417
|
+
|
|
418
|
+
# ---- the map ------------------------------------------------------------------------------------------------
|
|
419
|
+
[ -f "$M" ] || exit 0
|
|
420
|
+
MK="$ST/map-stale-$SID${AG:+-$AG}"
|
|
421
|
+
[ -e "$MK" ] && exit 0
|
|
422
|
+
|
|
423
|
+
# The map is written by the scanner's serializer: `"file": "<path>"`, one space after the colon, the path relative
|
|
424
|
+
# and in forward slashes. The path is compared in that JSON form, so a quote or backslash in a name cannot widen it.
|
|
425
|
+
J="${REL//\\/\\\\}"; J="${J//\"/\\\"}"
|
|
426
|
+
# At most 4 MB of it is read: a map that large is a monorepo's, and a path cited past that point goes unsaid rather
|
|
427
|
+
# than costing every edit the time to read the rest.
|
|
428
|
+
MAPTXT=""; IFS= read -r -d '' -n 4194304 MAPTXT < "$M" || true
|
|
429
|
+
case "$MAPTXT" in
|
|
430
|
+
*"\"file\": \"$J\""*|*"\"manifest\": \"$J\""*) ;;
|
|
431
|
+
*) exit 0 ;;
|
|
432
|
+
esac
|
|
433
|
+
|
|
434
|
+
[ -d "$ST" ] || mkdir -p "$ST" 2>/dev/null || exit 0
|
|
435
|
+
: > "$MK" 2>/dev/null || exit 0
|
|
436
|
+
MN="$MS_NOTE"
|
|
437
|
+
exit 0
|
package/kit/hooks/pre-commit
CHANGED
|
@@ -236,20 +236,60 @@ FAL=""; [ -n "$GITROOT" ] && [ -f "$GITROOT/.floor-allowlist.txt" ] && FAL="$GIT
|
|
|
236
236
|
# EXACT pattern line. `path:` is matched as a shell `case` pattern: `*` crosses `/` and `**` is not special, so
|
|
237
237
|
# `path:*.cs` exempts every C# file in the tree. Documented in floor-blocklist.txt and pinned in smoke §7h2. The allowlist sits in the same diff as the code it excuses, which is the point: an exception
|
|
238
238
|
# is a decision someone can see, not a rule that quietly got weaker.
|
|
239
|
+
#
|
|
240
|
+
# EVERY EXCEPTION SAYS WHY, on its own line: `<entry> # <reason, at least 20 characters>`. A bare `rule:` line used to
|
|
241
|
+
# switch a rule off for the whole repository with nothing a reviewer could weigh; now an entry without its reason
|
|
242
|
+
# exempts nothing, and the commit names the line. The reason starts at the first " # " (space, hash, space), which no
|
|
243
|
+
# floor pattern contains (they spell a literal hash as [#]).
|
|
244
|
+
_fal_split(){ # $1 = allowlist line -> FAL_E (the entry) and FAL_R (the reason, trimmed; empty when there is none)
|
|
245
|
+
FAL_E="${1%% # *}"; FAL_R=""
|
|
246
|
+
[ "$FAL_E" != "$1" ] && FAL_R="${1#* # }"
|
|
247
|
+
FAL_E="${FAL_E%"${FAL_E##*[![:space:]]}"}"
|
|
248
|
+
FAL_R="${FAL_R#"${FAL_R%%[![:space:]]*}"}"; FAL_R="${FAL_R%"${FAL_R##*[![:space:]]}"}"
|
|
249
|
+
}
|
|
250
|
+
_FAL_SAID="|"
|
|
251
|
+
_fal_unreasoned(){ # $1 = the bare line: reported once per commit, and the commit is stopped
|
|
252
|
+
case "$_FAL_SAID" in *"|$1|"*) return 0 ;; esac
|
|
253
|
+
_FAL_SAID="$_FAL_SAID$1|"
|
|
254
|
+
echo "FLOOR-ALLOWLIST: '$1' exempts nothing without a reason — write it as: $1 # <why, at least 20 characters>"
|
|
255
|
+
HIT=1
|
|
256
|
+
}
|
|
239
257
|
_floor_allowed(){ # $1 = rule, $2 = pattern (may be empty), $3 = path
|
|
240
258
|
[ -n "$FAL" ] || return 1
|
|
241
|
-
local a
|
|
259
|
+
local a m
|
|
242
260
|
while IFS= read -r a || [ -n "$a" ]; do
|
|
243
261
|
a="${a%$'\r'}"
|
|
244
|
-
case "$a" in
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
262
|
+
case "$a" in ''|\#*) continue ;; esac
|
|
263
|
+
_fal_split "$a"; m=0
|
|
264
|
+
case "$FAL_E" in
|
|
265
|
+
path:*) case "$3" in ${FAL_E#path:}) m=1 ;; esac ;;
|
|
266
|
+
rule:*) [ "${FAL_E#rule:}" = "$1" ] && m=1 ;;
|
|
267
|
+
*) [ -n "$2" ] && [ "$FAL_E" = "$2" ] && m=1 ;;
|
|
249
268
|
esac
|
|
269
|
+
[ "$m" = 1 ] || continue
|
|
270
|
+
[ "${#FAL_R}" -ge 20 ] || _fal_unreasoned "$FAL_E"
|
|
271
|
+
return 0
|
|
250
272
|
done < "$FAL"
|
|
251
273
|
return 1
|
|
252
274
|
}
|
|
275
|
+
# A staged change to the allowlist itself is checked whole: every entry has a reason of 20+ characters, and no two
|
|
276
|
+
# entries share one word for word (a copied reason explains neither). Only when the file is in this commit, so a
|
|
277
|
+
# project whose old list has bare lines is not locked out of unrelated commits; those lines exempt nothing instead.
|
|
278
|
+
if [ -n "$FAL" ] && ! git diff --cached --quiet -- "$FAL" 2>/dev/null; then
|
|
279
|
+
_fal_reasons="
|
|
280
|
+
"
|
|
281
|
+
while IFS= read -r a || [ -n "$a" ]; do
|
|
282
|
+
a="${a%$'\r'}"
|
|
283
|
+
case "$a" in ''|\#*) continue ;; esac
|
|
284
|
+
_fal_split "$a"
|
|
285
|
+
if [ "${#FAL_R}" -lt 20 ]; then _fal_unreasoned "$FAL_E"; continue; fi
|
|
286
|
+
case "$_fal_reasons" in *"
|
|
287
|
+
$FAL_R
|
|
288
|
+
"*) echo "FLOOR-ALLOWLIST: '$FAL_E' repeats the reason of another entry word for word — say why THIS exception is needed"; HIT=1 ;; esac
|
|
289
|
+
_fal_reasons="$_fal_reasons$FAL_R
|
|
290
|
+
"
|
|
291
|
+
done < "$FAL"
|
|
292
|
+
fi
|
|
253
293
|
|
|
254
294
|
# Test files, by the naming each ecosystem uses. Names and not whole `test/` directories, because deleting a
|
|
255
295
|
# fixture or a helper there is not a test made easier. Rust integration tests are the one convention that is a
|
|
@@ -684,7 +724,8 @@ fi
|
|
|
684
724
|
if [ "$HIT" -ne 0 ]; then
|
|
685
725
|
echo "-----------------------------------------------------------"
|
|
686
726
|
echo "Commit stopped. Remove the AI-authorship trace / vendor name / secret / bloated file / bar-lowering move from the staged changes."
|
|
687
|
-
echo "A bar-lowering move that is genuinely warranted? record it in .floor-allowlist.txt at the repo root (path:<glob>, rule:<name>, or the exact pattern line) — in the same commit, where review sees it. A path: glob's * also crosses directories: write the narrowest path."
|
|
727
|
+
echo "A bar-lowering move that is genuinely warranted? record it in .floor-allowlist.txt at the repo root (path:<glob>, rule:<name>, or the exact pattern line, each followed by ' # <reason, 20+ characters>') — in the same commit, where review sees it. A path: glob's * also crosses directories: write the narrowest path."
|
|
728
|
+
echo "Not the way out: an allowlist line written only to get this commit through. Fix the code; a rule: line switches a rule off for the whole repository and needs an ADR."
|
|
688
729
|
echo "False positive (trace/secret)? add the EXACT pattern line to .trace-allowlist.txt or .secret-allowlist.txt at the repo root."
|
|
689
730
|
echo "Private path flagged but genuinely public? add that exact term to .private-allowlist.txt at the repo root."
|
|
690
731
|
echo "Large file is intentional? raise CREW_MAX_FILE_BYTES, use Git LFS, or gitignore the build/vendored path."
|