ckb/.agents/skills/ckb-lint/scripts/lint_quick.sh
Michał Kopeć 27d84fe008 Template 1.11.0: optional Python with degraded fallbacks; lint check 13 per-block Crux matching
- D-0003: <python> interpreter resolution, per-OS install guide, lint_quick.sh/.ps1
  session-start fallback, manual lint checklist, no-script retrieval path,
  exporters declare Python a hard requirement
- Lint check 13 pairs each Crux quote block with the attribution under it, so a
  multi-source Crux no longer cross-fails; fixture KB gains a second source
- Session summaries for 2026-09-29 and 2026-10-01

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-01 21:26:43 +02:00

136 lines
5.9 KiB
Bash
Executable file

#!/usr/bin/env bash
# Copyright 2026 Michał Kopeć
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# No-Python fallback for `lint_report.py --quick` (Rule E session-start check).
#
# lint_quick.sh [--today YYYY-MM-DD]
#
# Run from the repository root. Read-only. Prints the same one-line summary
# shape as the Python script for three of its four quick checks:
# - malformed pages with no/unclosed frontmatter or no `type`
# - past freshness `last_updated` + `freshness_window_days` < today
# - source drift `## Sources` bullets whose sha256 no longer matches
# The fourth (diverged crux quotes) needs the full checker and is skipped;
# the summary says so. Exit 0 when clean, 1 when anything was flagged.
#
# Needs only bash 4+, coreutils (date, sha256sum or shasum) and grep/sed.
# Prefer the Python script whenever an interpreter is available — it is the
# reference implementation and covers everything.
set -uo pipefail
TODAY=""
while [[ $# -gt 0 ]]; do
case "$1" in
--today) TODAY="$2"; shift 2 ;;
*) echo "usage: lint_quick.sh [--today YYYY-MM-DD]" >&2; exit 2 ;;
esac
done
[[ -d wiki ]] || { echo "error: no wiki/ directory here — run from the repository root" >&2; exit 2; }
[[ -n "$TODAY" ]] || TODAY=$(date +%F)
to_epoch() { # YYYY-MM-DD -> days since epoch, empty if not a date
local d="$1" s
[[ "$d" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] || { echo ""; return; }
if s=$(date -d "$d" +%s 2>/dev/null) || s=$(date -j -f %Y-%m-%d "$d" +%s 2>/dev/null); then
echo $(( s / 86400 ))
else
echo ""
fi
}
sha() {
if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -c1-64
elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -c1-64
else echo ""; fi
}
TODAY_DAYS=$(to_epoch "$TODAY")
[[ -n "$TODAY_DAYS" ]] || { echo "error: --today is not a YYYY-MM-DD date: $TODAY" >&2; exit 2; }
malformed=0; stale=0; drift=0
# Trees: wiki/ plus every connector-backed lib (has source.yaml).
trees=(wiki)
for d in libs/*/; do [[ -f "${d}source.yaml" ]] && trees+=("${d%/}"); done
for tree in "${trees[@]}"; do
while IFS= read -r -d '' f; do
name=$(basename "$f")
[[ "$name" == "index.md" || "$name" == "log.md" ]] && continue
case "$f" in */.*) continue ;; esac
# ---- frontmatter --------------------------------------------------
first=$(head -n1 "$f")
if [[ "$first" != "---" ]]; then malformed=$((malformed+1)); continue; fi
fm=$(awk 'NR==1{next} /^---$/{exit} {print}' "$f")
if ! awk 'NR>1 && /^---$/{found=1; exit} END{exit !found}' "$f"; then
malformed=$((malformed+1)); continue
fi
if printf '%s\n' "$fm" | grep -Eq '^[[:space:]]+[^[:space:]#]'; then
malformed=$((malformed+1)); continue
fi
if printf '%s\n' "$fm" | grep -Ev '^[[:space:]]*(#|$)' | grep -Evq ':'; then
malformed=$((malformed+1)); continue
fi
type=$(printf '%s\n' "$fm" | sed -n 's/^type:[[:space:]]*//p' | head -n1 | sed "s/^[\"']//; s/[\"']$//")
if [[ -z "${type// }" ]]; then malformed=$((malformed+1)); continue; fi
# ---- freshness ----------------------------------------------------
win=$(printf '%s\n' "$fm" | sed -n 's/^freshness_window_days:[[:space:]]*//p' | head -n1 | tr -d "\"' ")
upd=$(printf '%s\n' "$fm" | sed -n 's/^last_updated:[[:space:]]*//p' | head -n1 | tr -d "\"' ")
if [[ "$win" =~ ^[0-9]+$ && -n "$upd" ]]; then
upd_days=$(to_epoch "$upd")
if [[ -n "$upd_days" ]]; then
age=$(( TODAY_DAYS - upd_days ))
(( age > win )) && stale=$((stale+1))
else
stale=$((stale+1)) # last_updated present but not a date (Python flags this under freshness too)
fi
elif [[ -n "$upd" && -z "$(to_epoch "$upd")" ]]; then
stale=$((stale+1))
fi
# ---- source fingerprints -----------------------------------------
grep -Eq '^## Sources[[:space:]]*$' "$f" || continue
src=$(awk '/^## Sources[[:space:]]*$/{on=1; next} /^## /{on=0} on{print}' "$f")
bullets=$(printf '%s\n' "$src" | grep -E '^[[:space:]]*[-*][[:space:]]+`[^`]+`' || true)
if [[ -z "$bullets" ]]; then drift=$((drift+1)); continue; fi
while IFS= read -r line; do
cited=$(printf '%s' "$line" | sed -E 's/^[[:space:]]*[-*][[:space:]]+`([^`]+)`.*/\1/')
rest=$(printf '%s' "$line" | sed -E 's/^[[:space:]]*[-*][[:space:]]+`[^`]+`//')
[[ "$cited" =~ ^[a-z][a-z0-9+.-]*:// ]] && continue
target="${cited#/}"
[[ -f "$target" ]] || target="$tree/${cited#/}"
if [[ ! -f "$target" ]]; then drift=$((drift+1)); continue; fi
kind=$(printf '%s' "$rest" | grep -oE '\b(sha256|etag|mtime):[^[:space:],)]+' | head -n1)
if [[ -z "$kind" ]]; then drift=$((drift+1)); continue; fi
[[ "$kind" == sha256:* ]] || continue
recorded=$(printf '%s' "${kind#sha256:}" | tr 'A-F' 'a-f')
actual=$(sha "$target")
[[ -n "$actual" ]] || continue
[[ "$recorded" == "${actual:0:${#recorded}}" ]] || drift=$((drift+1))
done <<< "$bullets"
done < <(find "$tree" -name '*.md' -type f -print0 | sort -z)
done
flagged=()
(( malformed )) && flagged+=("$malformed malformed")
(( stale )) && flagged+=("$stale past freshness window")
(( drift )) && flagged+=("$drift source drift")
note=" (fallback: crux quotes not checked — install Python for the full check)"
if (( ${#flagged[@]} == 0 )); then
echo "ckb check: clean$note"; exit 0
fi
IFS=', '; echo "ckb check: $(printf '%s, ' "${flagged[@]}" | sed 's/, $//') — run \"lint\" for detail$note"
exit 1