#!/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 four of its six 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 # - possible secrets a reduced set of credential shapes, and `` in wiki/ # Diverged crux quotes and the context budget need the full checker and are # 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_str() { # sha256 of a string, first 8 hex if command -v sha256sum >/dev/null 2>&1; then printf '%s' "$1" | sha256sum | cut -c1-8 else printf '%s' "$1" | shasum -a 256 | cut -c1-8; 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 # ---- secrets: a reduced pattern set (lint_report.py knows more shapes) ---- SECRET_RE='-----BEGIN ([A-Z0-9]+ )*PRIVATE KEY-----|(AKIA|ASIA)[0-9A-Z]{16}|gh[pousr]_[A-Za-z0-9]{36,}|github_pat_[A-Za-z0-9_]{50,}|xox[abprs]-[A-Za-z0-9-]{10,}|sk-(ant-|proj-)?[A-Za-z0-9_-]{20,}|AIza[0-9A-Za-z_-]{35}' INCLUDES=(--include='*.md' --include='*.txt' --include='*.eml' --include='*.csv' --include='*.json' --include='*.jsonl' --include='*.yaml' --include='*.yml' --include='*.html' --include='*.log' --include='*.env' --include='*.ini' --include='*.toml' --include='*.sh' --include='*.py' --include='*.js' --include='*.ts' --include='*.xml') secrets=0 scan=(); for d in wiki outputs workload raw; do [[ -d "$d" ]] && scan+=("$d"); done skip_secret() { case "$1" in outputs/starlight/*|outputs/okf/*|outputs/teaching/*|.*|*/.*) return 0 ;; esac; return 1; } if (( ${#scan[@]} )); then while IFS= read -r hit; do file=${hit%%:*}; rest=${hit#*:}; line=${rest#*:} skip_secret "$file" && continue trimmed=$(printf '%s' "$line" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//') if [[ -f .secrets-allow ]] && grep -Eq "^[[:space:]]*/?${file//./\\.}[[:space:]]+$(sha_str "$trimmed")([[:space:]]|#|$)" .secrets-allow; then continue; fi # ` match:` allows one matched string in every matching file. m=$(printf '%s' "$line" | grep -oE -e "$SECRET_RE" | head -n1); mh=$(sha_str "$m"); allowed=0 while read -r glob want _; do [[ "$want" == "match:$mh" && "$file" == ${glob#/} ]] && { allowed=1; break; } done < <(sed 's/#.*//' .secrets-allow 2>/dev/null | grep 'match:' || true) (( allowed )) && continue secrets=$((secrets+1)) done < <(grep -rEn "${INCLUDES[@]}" -e "$SECRET_RE" "${scan[@]}" 2>/dev/null) fi if [[ -d wiki ]]; then while IFS= read -r hit; do skip_secret "${hit%%:*}" || secrets=$((secrets+1)); done \ < <(grep -rEin "${INCLUDES[@]}" -e '' wiki 2>/dev/null) fi flagged=() (( malformed )) && flagged+=("$malformed malformed") (( stale )) && flagged+=("$stale past freshness window") (( drift )) && flagged+=("$drift source drift") (( secrets )) && flagged+=("$secrets possible secrets") note=" (fallback: crux quotes and context budget 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