ckb/.agents/skills/ckb-lint/scripts/lint_quick.sh
Michał Kopeć 7b812e545b Template 1.11.1: fix five defects reported downstream
Four deterministic-script defects plus a contract gap, all reported by a
downstream KB running 1.11.0. None of them crashed; each produced confident,
wrong output, which is why they went unnoticed.

retrieve_candidates.py:
- load_pages() walked rglob unsorted, so tests/expected/retrieve.json encoded
  one machine's directory order and the smoke test failed on a clean clone.
- cited_by used setdefault, crediting a shared source to whichever page was
  walked first and dropping the source from the results entirely, so the other
  citing pages reported corroborated_by: []. Now every citing page is credited.

lint_report.py:
- check 12 reported a cited directory as "does not exist", because is_file()
  is false for a directory. Citing a subtree is now explicitly legal and
  carries no fingerprint; same fix in lint_quick.sh and lint_quick.ps1.
- check 8 keyed log entries on timestamp + file with the action type dropped,
  so a Rule B POINTER was indistinguishable from a duplicate of the entry it
  points at. POINTER is now exempt; genuine duplicates still fire.
- check 13 required a backticked path, so "— Chat conversation, Name, date"
  counted as unattributed although AGENTS.md lists it as a first-class source.
  Attribution and verifiability are now separate questions.

AGENTS.md documents both resolved contract gaps: directory citations, and the
two legal attribution shapes. The fixture KB gained coverage for all three lint
defects — each finding fires under the old script and is silent under the new.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-09 21:47:53 +02:00

138 lines
6 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#/}"
[[ -e "$target" ]] || target="$tree/${cited#/}"
# A cited subtree is legal but has no single digest to recompute.
[[ -d "$target" ]] && continue
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