@bookedsolid/rea 0.9.3 → 0.9.4

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.
@@ -87,7 +87,12 @@ triage_score() {
87
87
  local diff_input
88
88
  diff_input=$(cat)
89
89
  local line_count
90
- line_count=$(printf '%s' "$diff_input" | grep -cE '^\+[^+]|^-[^-]' 2>/dev/null || echo "0")
90
+ # Defect K (rea#62) sibling: see `hooks/commit-review-gate.sh` for the
91
+ # full bug rationale. `|| echo "0"` captures "0\n0" on no-match, which
92
+ # breaks arithmetic comparisons downstream. `|| true` + bash default keeps
93
+ # the branch arithmetic-safe.
94
+ line_count=$(printf '%s' "$diff_input" | grep -cE '^\+[^+]|^-[^-]' 2>/dev/null || true)
95
+ line_count="${line_count:-0}"
91
96
 
92
97
  # Check for sensitive paths
93
98
  local sensitive=0
@@ -435,8 +435,8 @@ pr_core_run() {
435
435
  --arg os_uid "$SKIP_OS_UID" \
436
436
  --arg os_whoami "$SKIP_OS_WHOAMI" \
437
437
  --arg os_hostname "$SKIP_OS_HOST" \
438
- --arg os_pid "$SKIP_OS_PID" \
439
- --arg os_ppid "$SKIP_OS_PPID" \
438
+ --argjson os_pid "$SKIP_OS_PID" \
439
+ --argjson os_ppid "$SKIP_OS_PPID" \
440
440
  --arg os_ppid_cmd "$SKIP_OS_PPID_CMD" \
441
441
  --arg os_tty "$SKIP_OS_TTY" \
442
442
  --arg os_ci "$SKIP_OS_CI" \
@@ -924,17 +924,26 @@ pr_core_run() {
924
924
  fi
925
925
  done
926
926
 
927
+ # Defect J (rea#61): branch-deletion guard MUST fail closed regardless of
928
+ # whether another refspec in the same push resolved a SOURCE_SHA. A mixed
929
+ # push like `git push origin safe:safe :main` iterates both refspecs; the
930
+ # safe refspec sets SOURCE_SHA from its local_sha, and the deletion refspec
931
+ # sets only HAS_DELETE=1 via its `continue` branch. If we check HAS_DELETE
932
+ # INSIDE the `-z SOURCE_SHA` fallback, the delete slips through unchecked.
933
+ # Hoist the check above the fallback so any deletion anywhere in the push
934
+ # blocks the entire push.
935
+ if [[ "$HAS_DELETE" -eq 1 ]]; then
936
+ {
937
+ printf 'PUSH BLOCKED: refspec is a branch deletion.\n'
938
+ printf '\n'
939
+ printf ' Branch deletions are sensitive operations and require explicit\n'
940
+ printf ' human action outside the agent. Perform the deletion manually.\n'
941
+ printf '\n'
942
+ } >&2
943
+ exit 2
944
+ fi
945
+
927
946
  if [[ -z "$SOURCE_SHA" || -z "$MERGE_BASE" ]]; then
928
- if [[ "$HAS_DELETE" -eq 1 ]]; then
929
- {
930
- printf 'PUSH BLOCKED: refspec is a branch deletion.\n'
931
- printf '\n'
932
- printf ' Branch deletions are sensitive operations and require explicit\n'
933
- printf ' human action outside the agent. Perform the deletion manually.\n'
934
- printf '\n'
935
- } >&2
936
- exit 2
937
- fi
938
947
  {
939
948
  printf 'PUSH BLOCKED: could not resolve a merge-base for any push refspec.\n'
940
949
  printf '\n'
@@ -969,8 +978,13 @@ pr_core_run() {
969
978
  exit 0
970
979
  fi
971
980
 
981
+ # Defect K (rea#62): `grep -c ... || echo "0"` captures `0\n0` when grep
982
+ # exits non-zero on no-match — grep still prints its own `0` to stdout before
983
+ # exiting, and the `|| echo "0"` branch appends another. `|| true` swallows
984
+ # the non-zero exit, and `${LINE_COUNT:-0}` defaults an empty result to 0.
972
985
  local LINE_COUNT
973
- LINE_COUNT=$(printf '%s' "$DIFF_FULL" | grep -cE '^\+[^+]|^-[^-]' 2>/dev/null || echo "0")
986
+ LINE_COUNT=$(printf '%s' "$DIFF_FULL" | grep -cE '^\+[^+]|^-[^-]' 2>/dev/null || true)
987
+ LINE_COUNT="${LINE_COUNT:-0}"
974
988
 
975
989
  # ── 7a. Protected-path Codex adversarial review gate ──────────────────────
976
990
  # The per-refspec check runs inside the main loop (section 7, above) so
@@ -981,8 +995,36 @@ pr_core_run() {
981
995
  # refspec was either clean or had an acceptable audit.
982
996
 
983
997
  # ── 8. Check review cache ─────────────────────────────────────────────────
984
- local PUSH_SHA
985
- PUSH_SHA=$(printf '%s' "$DIFF_FULL" | shasum -a 256 | cut -d' ' -f1 2>/dev/null || echo "")
998
+ # Defect L (rea#63): `shasum` is not installed on Alpine, distroless, or
999
+ # most minimal Linux CI images only `sha256sum` is. The prior `shasum -a
1000
+ # 256 ... || echo ""` chain silently produced an empty PUSH_SHA, which the
1001
+ # rest of the gate treats as "no cache entry" rather than "hasher missing".
1002
+ # Combined with the silent-cache-miss fallback (Defect F), every push from
1003
+ # such a runner burned a full fresh codex review invisibly.
1004
+ #
1005
+ # Portable chain: sha256sum → shasum → openssl. The openssl branch uses
1006
+ # `awk '{print $NF}'` WITHOUT `-r` — `-r` was added in OpenSSL 3.0 /
1007
+ # LibreSSL 3.3+; on OpenSSL 1.1.1 (Debian 11, Ubuntu 20.04, RHEL 8,
1008
+ # Amazon Linux 2, Alpine 3.13–3.14) `-r` is rejected and stdout is empty.
1009
+ # `$NF` handles BOTH default output shapes: `(stdin)= <hex>` (1.1.x) and
1010
+ # `<hex> *stdin` (3.x/LibreSSL coreutils-style).
1011
+ #
1012
+ # Hex-64 validation catches broken pipes, partial reads, or unexpected
1013
+ # hasher output that would otherwise be silently cached as garbage.
1014
+ local PUSH_SHA=""
1015
+ if command -v sha256sum >/dev/null 2>&1; then
1016
+ PUSH_SHA=$(printf '%s' "$DIFF_FULL" | sha256sum 2>/dev/null | awk '{print $1}')
1017
+ elif command -v shasum >/dev/null 2>&1; then
1018
+ PUSH_SHA=$(printf '%s' "$DIFF_FULL" | shasum -a 256 2>/dev/null | awk '{print $1}')
1019
+ elif command -v openssl >/dev/null 2>&1; then
1020
+ PUSH_SHA=$(printf '%s' "$DIFF_FULL" | openssl dgst -sha256 2>/dev/null | awk '{print $NF}')
1021
+ else
1022
+ printf 'rea push-review: WARN no sha256 hasher found (sha256sum/shasum/openssl); cache disabled\n' >&2
1023
+ fi
1024
+ if [[ -n "$PUSH_SHA" && ! "$PUSH_SHA" =~ ^[0-9a-f]{64}$ ]]; then
1025
+ printf 'rea push-review: WARN hasher returned invalid output; cache disabled\n' >&2
1026
+ PUSH_SHA=""
1027
+ fi
986
1028
 
987
1029
  local -a REA_CLI_ARGS
988
1030
  REA_CLI_ARGS=()
@@ -1037,8 +1079,10 @@ pr_core_run() {
1037
1079
  fi
1038
1080
 
1039
1081
  # ── 9. Block and request review ───────────────────────────────────────────
1082
+ # Defect K (rea#62): same `0\n0` bug as LINE_COUNT above.
1040
1083
  local FILE_COUNT
1041
- FILE_COUNT=$(printf '%s' "$DIFF_FULL" | grep -c '^\+\+\+ ' 2>/dev/null || echo "0")
1084
+ FILE_COUNT=$(printf '%s' "$DIFF_FULL" | grep -c '^\+\+\+ ' 2>/dev/null || true)
1085
+ FILE_COUNT="${FILE_COUNT:-0}"
1042
1086
 
1043
1087
  {
1044
1088
  printf 'PUSH REVIEW GATE: Review required before pushing\n'
@@ -1050,8 +1094,23 @@ pr_core_run() {
1050
1094
  printf ' Action required:\n'
1051
1095
  printf ' 1. Spawn a code-reviewer agent to review: git diff %s..%s\n' "$MERGE_BASE" "$SOURCE_SHA"
1052
1096
  printf ' 2. Spawn a security-engineer agent for security review\n'
1053
- printf ' 3. After both pass, cache the result:\n'
1054
- printf ' rea cache set %s pass --branch %s --base %s\n' "$PUSH_SHA" "$SOURCE_BRANCH" "$TARGET_BRANCH"
1097
+ # Defect L (rea#63) follow-up: when no sha256 hasher is available the
1098
+ # cache is disabled and PUSH_SHA is empty. Emitting `rea cache set <blank>
1099
+ # pass ...` would be a dead-end — the CLI rejects the empty positional.
1100
+ # Print an alternate completion path in that case. The Codex-adversarial
1101
+ # review concerns list flagged this UX cliff in the 0.9.4 pass.
1102
+ if [[ -n "$PUSH_SHA" ]]; then
1103
+ printf ' 3. After both pass, cache the result:\n'
1104
+ printf ' rea cache set %s pass --branch %s --base %s\n' "$PUSH_SHA" "$SOURCE_BRANCH" "$TARGET_BRANCH"
1105
+ else
1106
+ printf ' 3. Cache is DISABLED on this host (no sha256 hasher found).\n'
1107
+ printf ' After both reviews pass, bypass the push-review gate with:\n'
1108
+ printf ' REA_SKIP_PUSH_REVIEW="<reason>" git push ...\n'
1109
+ printf ' The bypass is audited as push.review.skipped — this is the\n'
1110
+ printf ' documented escape hatch when cache is unavailable.\n'
1111
+ printf ' To restore the cache path, install one of: sha256sum,\n'
1112
+ printf ' shasum (Perl Digest::SHA), or openssl.\n'
1113
+ fi
1055
1114
  printf '\n'
1056
1115
  } >&2
1057
1116
  exit 2
@@ -114,7 +114,16 @@ if [[ -z "$DIFF_OUTPUT" ]]; then
114
114
  fi
115
115
 
116
116
  # Count changed lines (additions + deletions)
117
- LINE_COUNT=$(printf '%s' "$DIFF_FULL" | grep -cE '^\+[^+]|^-[^-]' 2>/dev/null || echo "0")
117
+ # Defect K (rea#62) sibling: `|| echo "0"` captures "0\n0" into LINE_COUNT
118
+ # when grep exits non-zero on a no-match — grep still prints its own `0` and
119
+ # `echo "0"` appends another. At this site the concatenated `"0\n0"` is then
120
+ # evaluated as arithmetic (`-gt $SIGNIFICANT_THRESHOLD`, `-ge $TRIVIAL_THRESHOLD`
121
+ # below) and bash emits a "syntax error in expression" at runtime on any
122
+ # rename-only / mode-only / empty-file-add diff. `|| true` + bash-default
123
+ # expansion fixes both the banner cosmetic and the arithmetic-unsafe control
124
+ # flow in one shot.
125
+ LINE_COUNT=$(printf '%s' "$DIFF_FULL" | grep -cE '^\+[^+]|^-[^-]' 2>/dev/null || true)
126
+ LINE_COUNT="${LINE_COUNT:-0}"
118
127
 
119
128
  # Check for sensitive paths
120
129
  SENSITIVE=0
@@ -162,17 +171,79 @@ fi
162
171
 
163
172
  # ── 10. Check review cache for all non-trivial commits ────────────────────────
164
173
  # Compute SHA and branch here so both standard and significant tiers share them.
165
- STAGED_SHA=$(cd "$REA_ROOT" && git diff --cached | shasum -a 256 | cut -d' ' -f1 2>/dev/null || echo "")
174
+ #
175
+ # Defect L (rea#63) sibling: `shasum` is not installed on Alpine, distroless,
176
+ # or most minimal Linux CI images — only `sha256sum` is. The prior chain
177
+ # silently produced an empty STAGED_SHA, which the cache block then skipped
178
+ # AND the banner at §11 rendered as `rea cache set pass` — a dead-end the
179
+ # agent cannot execute. Portable chain mirrors push-review-core.sh §8:
180
+ # sha256sum → shasum → openssl. The openssl branch uses `awk '{print $NF}'`
181
+ # WITHOUT `-r` to stay compatible with OpenSSL 1.1.x (Debian 11, Ubuntu
182
+ # 20.04, RHEL 8, Amazon Linux 2, Alpine 3.13–3.14).
183
+ STAGED_SHA=""
184
+ if command -v sha256sum >/dev/null 2>&1; then
185
+ STAGED_SHA=$(printf '%s' "$DIFF_FULL" | sha256sum 2>/dev/null | awk '{print $1}')
186
+ elif command -v shasum >/dev/null 2>&1; then
187
+ STAGED_SHA=$(printf '%s' "$DIFF_FULL" | shasum -a 256 2>/dev/null | awk '{print $1}')
188
+ elif command -v openssl >/dev/null 2>&1; then
189
+ STAGED_SHA=$(printf '%s' "$DIFF_FULL" | openssl dgst -sha256 2>/dev/null | awk '{print $NF}')
190
+ else
191
+ printf 'rea commit-review: WARN no sha256 hasher found (sha256sum/shasum/openssl); cache disabled\n' >&2
192
+ fi
193
+ if [[ -n "$STAGED_SHA" && ! "$STAGED_SHA" =~ ^[0-9a-f]{64}$ ]]; then
194
+ printf 'rea commit-review: WARN hasher returned invalid output; cache disabled\n' >&2
195
+ STAGED_SHA=""
196
+ fi
166
197
  BRANCH=$(cd "$REA_ROOT" && git branch --show-current 2>/dev/null || echo "")
167
198
  CACHE_FILE="${REA_ROOT}/.rea/review-cache.json"
168
199
 
200
+ # Codex pass-3 finding #1: `rea cache check` and `rea cache set` both declare
201
+ # `--base` as a `requiredOption` in src/cli/index.ts. Prior versions of this
202
+ # gate omitted `--base`, so (a) the CLI path exited non-zero and the
203
+ # `|| echo '{"hit":false}'` fallback quietly masked the contract error, and
204
+ # (b) the section-11 banner instructed the agent to run `rea cache set <sha>
205
+ # pass` — also missing `--base`, rejected by the CLI on every retry. A
206
+ # successful cache flow was unreachable.
207
+ #
208
+ # Resolve BASE_BRANCH by the same preference order the push-gate uses in
209
+ # push-review-core.sh §7 (lines 778-794): origin/HEAD → origin/main →
210
+ # origin/master → empty. If nothing resolves, disable the cache (the
211
+ # alternative is emitting a cache command the CLI rejects on every call).
212
+ BASE_BRANCH=""
213
+ _origin_head=$(cd "$REA_ROOT" && git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null || true)
214
+ if [[ -n "$_origin_head" ]]; then
215
+ BASE_BRANCH="${_origin_head#refs/remotes/origin/}"
216
+ fi
217
+ if [[ -z "$BASE_BRANCH" ]]; then
218
+ # Use `git -C` so the current-shell cwd is never mutated — matches the
219
+ # cross-repo guard at §1a and keeps the file's dominant idiom. Raw
220
+ # `cd "$REA_ROOT" && git …` would leave the hook process sitting in
221
+ # $REA_ROOT, which is safe today but breaks silently if a future edit
222
+ # adds a relative-path command downstream.
223
+ if git -C "$REA_ROOT" rev-parse --verify --quiet refs/remotes/origin/main >/dev/null 2>&1; then
224
+ BASE_BRANCH="main"
225
+ elif git -C "$REA_ROOT" rev-parse --verify --quiet refs/remotes/origin/master >/dev/null 2>&1; then
226
+ BASE_BRANCH="master"
227
+ fi
228
+ fi
229
+ if [[ -z "$BASE_BRANCH" && -n "$STAGED_SHA" ]]; then
230
+ printf 'rea commit-review: WARN could not resolve base branch (no origin/HEAD, no origin/main, no origin/master); cache disabled\n' >&2
231
+ STAGED_SHA=""
232
+ fi
233
+ unset _origin_head
234
+
169
235
  if [[ -n "$STAGED_SHA" ]]; then
170
236
  CACHE_HIT=false
171
237
 
172
- # Primary: use CLI when available — handles TTL, expiry, and branch-scoped keys
238
+ # Primary: use CLI when available — handles TTL, expiry, and branch-scoped keys.
239
+ # Cache predicate must require BOTH `.hit == true` AND `.result == "pass"` —
240
+ # a cached `fail` verdict would otherwise satisfy `.hit == true` and let the
241
+ # commit proceed despite a recorded negative review. Mirrors the push-gate
242
+ # predicate at push-review-core.sh §8; the §218-226 direct-cache fallback
243
+ # already enforces `result == "pass"`, so the two paths must agree.
173
244
  if [[ ${#REA_CLI_ARGS[@]} -gt 0 ]]; then
174
- CACHE_RESULT=$("${REA_CLI_ARGS[@]}" cache check "$STAGED_SHA" --branch "$BRANCH" 2>/dev/null || echo '{"hit":false}')
175
- if printf '%s' "$CACHE_RESULT" | jq -e '.hit == true' >/dev/null 2>&1; then
245
+ CACHE_RESULT=$("${REA_CLI_ARGS[@]}" cache check "$STAGED_SHA" --branch "$BRANCH" --base "$BASE_BRANCH" 2>/dev/null || echo '{"hit":false}')
246
+ if printf '%s' "$CACHE_RESULT" | jq -e '.hit == true and .result == "pass"' >/dev/null 2>&1; then
176
247
  CACHE_HIT=true
177
248
  fi
178
249
  fi
@@ -210,8 +281,25 @@ fi
210
281
  printf ' 1. Inspect: git diff --cached\n'
211
282
  printf ' 2. Decide: Is this safe to commit? (initial commits, refactors, and\n'
212
283
  printf ' feature work are normal — use judgement, not ceremony)\n'
213
- printf ' 3. Approve: rea cache set %s pass\n' "$STAGED_SHA"
214
- printf ' 4. Retry the git commit command\n'
284
+ # Defect L follow-up: when no sha256 hasher is available STAGED_SHA is empty
285
+ # and `rea cache set pass` is a dead-end the CLI rejects. Branch the banner
286
+ # to surface an actionable path instead. Unlike push-review-core.sh there is
287
+ # no `REA_SKIP_COMMIT_REVIEW` env escape hatch (the commit gate only fires
288
+ # under Claude Code's Bash `PreToolUse` matcher, so a human direct-shell
289
+ # commit bypasses it entirely). The only remediation is to install a sha256
290
+ # hasher or ask the user to commit directly.
291
+ if [[ -n "$STAGED_SHA" ]]; then
292
+ printf ' 3. Approve: rea cache set %s pass --branch %s --base %s\n' \
293
+ "$STAGED_SHA" "$BRANCH" "$BASE_BRANCH"
294
+ printf ' 4. Retry the git commit command\n'
295
+ else
296
+ printf ' 3. Cache is DISABLED on this host (no sha256 hasher or no base\n'
297
+ printf ' branch resolvable). Install one of: sha256sum (Linux coreutils),\n'
298
+ printf ' shasum (perl-core), or openssl; or ensure origin/HEAD is set so\n'
299
+ printf ' the gate can identify the merge target. Without these the cache\n'
300
+ printf ' path cannot complete — escalate to the user if neither can be\n'
301
+ printf ' provided.\n'
302
+ fi
215
303
  printf '\n'
216
304
  printf ' Only escalate to the user if you find a genuine problem in the diff.\n'
217
305
  } >&2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bookedsolid/rea",
3
- "version": "0.9.3",
3
+ "version": "0.9.4",
4
4
  "description": "Agentic governance layer for Claude Code — policy enforcement, hook-based safety gates, audit logging, and Codex-integrated adversarial review for AI-assisted projects",
5
5
  "license": "MIT",
6
6
  "author": "Booked Solid Technology <oss@bookedsolid.tech> (https://bookedsolid.tech)",