Files
claude/.claude/tasks/contracts/2026-11-13-safety-lows-1730.md
T

6.8 KiB

CONTRACT — safety-lows (phase 2 + safety-residuals LOWs, block H4b)

  • date: 2026-11-13 | flow: bugfix | branch: bugfix/safety-lows (off develop)
  • status: active

REQUEST (verbatim — IMMUTABLE)

Phase 2 security gate follow-ups: LOW code-limits state dir (mkdir -m 700, owner check). safety-residuals gate LOWs (2026-11-06): session-start memory.sh check and digest unbounded when timeout is absent (stock macOS); pre-commit allowlist fails open if git diff --cached errors (pre-existing, Gitea backstop); lib/memory.sh _mem_path_safe line > 80 chars; claude-stub.sh interpolates the mktemp log path into the generated stub (test-only).

CLARIFICATIONS

  • [gated 2026-11-13, internal] (1) hooks/code-limits.sh:91: mkdir -m 700 -p "$dir", then require [ -O "$dir" ] && [ ! -L "$dir" ] else return 0 (skip the signal, never fail the tool). (2) session-start: ONE bounded runner chosen once near the top: timeout 2, else gtimeout 2, else perl -e 'alarm shift; exec @ARGV' 2 (stock macOS has perl), else none; the three sites (:252, :311, :414) use it. (3) githooks/pre-commit:41: capture staged=$(git diff --cached --name-only --no-renames) || { echo "gitflow pre-commit: cannot list staged paths: BLOCKED" >&2; exit 1; } then filter. (4) memory.sh _mem_path_safe rewrapped ≤ 80 cols, same logic. (5) lib/tests/claude-stub.sh: the generated stub reads the log path from an env var set at install (CLAUDE_STUB_LOG) instead of a printf-interpolated literal; claude_stub_install exports it.

  • [gated 2026-11-13, NEED-DECISION scope → (a), user brief: take the root-cause answer] githooks/pre-commit is generated by lib/gitflow.sh _gitflow_emit_hook (~:492): the fail-closed change goes into the generator, and the tracked .githooks/pre-commit copy is regenerated to match (gitflow-test T19d, run-review-guards G5). FILE SCOPE += lib/gitflow.sh (emit block only), .githooks/pre-commit.

  • [gated 2026-11-13, round 3 executor deviation accepted] [ -n "$staged" ] || exit 0 before the filter keeps an empty staged list allowed (grep -v on one empty line returns 0; gitflow-test T21g).

  • [gated 2026-11-13, round 3: GATE 1 gaps + GATE 2 MEDIUM/LOWs] (1) state dir without -p and without any shellcheck directive: mkdir -m 700 "$dir" 2>/dev/null (success = atomic proof), else accept an existing dir only through ONE lstat-based test [ -n "$(find "$dir" -maxdepth 0 -type d -user "$(id -u)" -perm 700 2>/dev/null)" ] (find does not follow a symlink operand without -H/-L), else return 0; the parent TMPDIR is created by the session, never by this hook. (2) perl runner perl -e 'alarm shift; exec { $ARGV[0] } @ARGV' 2 (never a shell). (3) pre-commit filter stage also fails closed: blocked=$(printf '%s\n' "$staged" | grep -vE '…'), grep rc 0 = blocked paths, rc 1 = none, rc ≥ 2 = BLOCKED with a message; generator, emitted hook and .githooks copy stay identical. (4) shellcheck clean on every changed .sh, pre-existing findings in touched files included (session-start.sh:186 SC2001, session-digest.test.sh:325 SC2016), without new directives: claude-stub and the precommit shim write their generated script through a quoted heredoc reading an env var (no interpolated path, no SC2016); code-limits.test uses stat/find instead of ls (SC2012) with a portable form (the portability census forbids stat -c: use find -perm).

ACCEPTANCE CRITERIA

  1. code-limits state dir 700 + owner/symlink check. CHECK: cd /home/bchanot/Documents/claude && echo $(grep -c 'mkdir -m 700 "$dir"' hooks/code-limits.sh)$(grep -c -- '-type d -user "$(id -u)" -perm 700' hooks/code-limits.sh) EXPECT: 11 EVIDENCE: MET exit=0 marker-found :: 11
  2. One bounded runner in session-start, with a perl fallback. CHECK: cd /home/bchanot/Documents/claude && echo $(grep -c "alarm shift" hooks/session-start.sh)$(grep -c 'command -v timeout' hooks/session-start.sh) EXPECT: 11 EVIDENCE: MET exit=0 marker-found :: 11
  3. pre-commit fails closed on a git diff error. CHECK: cd /home/bchanot/Documents/claude && grep -c 'cannot list staged paths' githooks/pre-commit EXPECT: 1 EVIDENCE: MET exit=0 marker-found :: 1
  4. memory.sh has no line over 80 cols. CHECK: cd /home/bchanot/Documents/claude && awk 'length > 80' lib/memory.sh | wc -l EXPECT: 0 EVIDENCE: MET exit=0 marker-found :: 0
  5. claude-stub no longer interpolates the log path. CHECK: cd /home/bchanot/Documents/claude && echo $(grep -c 'CLAUDE_STUB_LOG' lib/tests/claude-stub.sh | grep -c '^[2-9]')$(grep -c "printf '#!/bin/sh.*%s" lib/tests/claude-stub.sh) EXPECT: 10 EVIDENCE: MET exit=0 marker-found :: 10
  6. Regression tests: code-limits (dir mode 700, a pre-planted symlink dir → no signal, no write through it), session-digest (runner falls back to perl when timeout and gtimeout are absent from PATH), a pre-commit case where git diff --cached fails → rc 1 + BLOCKED (find the suite that tests githooks/pre-commit; add one if none, under lib/tests/). CHECK: cd /home/bchanot/Documents/claude && PATH=$HOME/.bun/bin:$PATH make test suite=lib/tests/code-limits.test.sh 2>&1 | tail -1 EXPECT: all suites green EVIDENCE: MET exit=0 marker-found :: all suites green
  7. session-digest suite green. CHECK: cd /home/bchanot/Documents/claude && PATH=$HOME/.bun/bin:$PATH make test suite=lib/tests/session-digest.test.sh 2>&1 | tail -1 EXPECT: all suites green EVIDENCE: MET exit=0 marker-found :: all suites green
  8. Generator, emitted hook and tracked copy agree. CHECK: cd /home/bchanot/Documents/claude && echo $(grep -c 'cannot list staged paths' lib/gitflow.sh githooks/pre-commit .githooks/pre-commit | grep -c ':1$') EXPECT: 3 EVIDENCE: MET exit=0 marker-found :: 3
  9. gitflow suite green. CHECK: cd /home/bchanot/Documents/claude && PATH=$HOME/.bun/bin:$PATH make test suite=lib/gitflow-test.sh 2>&1 | tail -1 EXPECT: all suites green EVIDENCE: MET exit=0 marker-found :: all suites green
  10. No shellcheck directive added; shellcheck clean on every changed .sh. CHECK: cd /home/bchanot/Documents/claude && echo $(git diff HEAD | grep -c '^+.*shellcheck disable')$(shellcheck -x hooks/code-limits.sh hooks/session-start.sh lib/gitflow.sh lib/memory.sh lib/tests/claude-stub.sh lib/tests/code-limits.test.sh lib/tests/session-digest.test.sh lib/tests/precommit-failclosed.test.sh >/dev/null 2>&1 && echo OK) EXPECT: 0OK EVIDENCE: MET exit=0 marker-found :: 0OK
  11. make test green (manual, bun on PATH). (manual: run 2026-11-13 on round 3, make test → all suites green; round 4 is lint-only, its 4 suites green, shellcheck githooks/pre-commit clean)

FILE SCOPE

hooks/code-limits.sh, hooks/session-start.sh, githooks/pre-commit, lib/memory.sh (:436-439), lib/tests/claude-stub.sh, lib/tests/code-limits.test.sh, lib/tests/session-digest.test.sh, + the pre-commit suite (existing or new lib/tests/*.test.sh).