testsAndMisc/meta/scripts/check_agent_contract.sh
Krzysztof kuhy Rudnicki 2e1e370c0f refactor: extract all inline Python from shell scripts into proper .py files
Move every multi-line python heredoc/`-c` block into a dedicated .py file so
ruff, mypy, pylint, bandit, and pytest can apply to it:
- linux_configuration/zsh/calc-live.zsh → python_pkg/live_calc/calc_eval.py (100% branch cov, 46 tests)
- meta/scripts/check_ai_evidence.sh → meta/scripts/validate_evidence.py
- meta/scripts/check_agent_contract.sh → meta/scripts/validate_contract.py
- phone_focus_mode/lib/monitor.sh → phone_focus_mode/lib/monitor_report.py
- phone_focus_mode/deploy.sh → phone_focus_mode/strip_workout_hosts.py
- linux_configuration/.../analyze_repo.sh → fast_count.py

Also: add zsh-syntax pre-commit hook (zsh -n); exclude zsh from shellcheck;
add tests for all 4 non-python_pkg helpers; update CLAUDE.md Shell Style with
the no-inline-Python rule.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-06 10:31:48 +02:00

68 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
# Require a workflow contract artifact for larger code changes.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
readonly CONTRACT_GLOB='docs/superpowers/contracts/*.json'
readonly MULTI_FILE_THRESHOLD=4
list_staged_code_files() {
git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(py|sh|c|h|cpp|hpp|cc|go|rs|ts|tsx|js|jsx|dart)$' || true
}
list_staged_contract_files() {
git diff --cached --name-only --diff-filter=ACMR | grep -E '^docs/superpowers/contracts/.*\.json$' || true
}
validate_contract_file() {
local file_path="$1"
python "${SCRIPT_DIR}/validate_contract.py" "$file_path"
}
main() {
local code_files
code_files="$(list_staged_code_files)"
if [[ -z "$code_files" ]]; then
echo "✓ No code files staged; workflow contract not required"
exit 0
fi
local code_file_count
code_file_count=$(printf '%s\n' "$code_files" | sed '/^$/d' | wc -l | tr -d ' ')
if (( code_file_count < MULTI_FILE_THRESHOLD )); then
echo "${code_file_count} code file(s) staged; no multi-file contract required"
exit 0
fi
local contract_files
contract_files="$(list_staged_contract_files)"
if [[ -z "$contract_files" ]]; then
echo "${code_file_count} code files staged but no workflow contract artifact found."
echo " Required: ${CONTRACT_GLOB}"
echo " Tip: start from docs/superpowers/contracts/template.json."
exit 1
fi
local failed=0
while IFS= read -r file_path; do
[[ -z "$file_path" ]] && continue
if ! validate_contract_file "$file_path"; then
failed=1
fi
done <<< "$contract_files"
if [[ $failed -eq 1 ]]; then
echo "❌ Workflow contract validation failed"
exit 1
fi
echo "✓ Multi-file workflow contract checks passed"
}
main "$@"