#!/usr/bin/env bash # reconcile.sh — deterministic engine for /reconcile. # # Confronts DECLARATIVE sources (TODO checkboxes, registry statuses) against # REAL state (git, fs, registry BODY). It is the engine behind the /reconcile # skill; the skill orchestrates + gates, this file holds the mechanical # truth-probes. # # FOUNDING PRINCIPLE — recursive coherence (LRN-055): a reconciler must NEVER # trust a declarative source as an oracle. So this engine NEVER reads the # `## Index` table nor believes a `[x]`/`[ ]` checkbox; it enumerates registry # entries from the BODY `## ID —` headings and decides "done/stale" from # git/fs only. It practices what it preaches — the recursive-coherence test # (run-reconcile.sh T1) reds if this is broken. # v2 registries carry no Index at all (lib/memory.sh); v1 ones still may. # # HONEST LIMITS (graven, do not over-read): # - Deferral detection is LEXICAL (reconcile_deferrals): it catches # deferrals MARKED by a keyword, misses ones phrased without one ("à # reprendre quand X"). Deterministic on the detectable; the skill # SURFACES the ambiguous for human review, never asserts. # - Contradiction detection surfaces CANDIDATES (token overlap), never # asserts a verdict. # # Layers: # reconcile_enumerate_ids — body-only `## ID` heading enumeration # (recursive-coherence core); v1 `###` entries # are invisible to it # reconcile_oracle_* — live git/fs truth probes # reconcile_blk_current_status — registry status: lib/memory.sh shared # parser on v2, legacy lexical reader on v1 # reconcile_blk_open — blockers whose CURRENT status is not closed # (resolved | superseded | deprecated on v2) # reconcile_status_classify — entries of any prefix (BLK, BDR): open # (word) | unreadable, closed omitted # reconcile_blk_classify — its BLK wrapper, for a client deliverable # reconcile_verdict — pure kernel: declared checkbox × real fact # → verdict # reconcile_deferrals — lexical deferral sweep (honest limit) # reconcile_contradiction_candidates — accepted-BDR ⇄ open-chantier token # overlap (surface) set -uo pipefail # LRN-074: pin grep, never assume GNU flags RECONCILE_GREP="${RECONCILE_GREP:-/usr/bin/grep}" # the status parser and classifier are shared with lib/memory.sh (BDR-125 4a) # shellcheck source=/dev/null . "$(dirname "${BASH_SOURCE[0]}")/memory.sh" # markers that LEXICALLY signal a deferral/follow-up (honest limit: marked-only) RECONCILE_DEFER_RE='[Dd]efer|[Ff]ollow-?up|[Oo]ut.of.scope|OUT-OF-SCOPE' RECONCILE_DEFER_RE+='|[Rr]econsider|[Rr]evisit|2e passage|won.t.do' RECONCILE_DEFER_RE+='|optional\)|one-line ticket' # v1 decisions carry `**Statut** :` and `Superseded by …` lines the blocker # selector skips; an unlabelled winner stays unreadable (_mem_v1_word) RECONCILE_BDR_RE="$_MEM_LEGACY_RE|statut|superseded|deprecated" # --- recursive coherence: enumerate IDs from the BODY, never the ## Index --- # $1 registry file, $2 prefix (BDR|LRN|BLK|EVAL). Emits one id per line, unique. reconcile_enumerate_ids() { "$RECONCILE_GREP" -oE "^## ${2}-[0-9]+" "$1" \ | "$RECONCILE_GREP" -oE "${2}-[0-9]+" | sort -u } # --- live truth probes (query the REAL repo/fs; the "verify, don't believe" # core) --- reconcile_oracle_tree_clean() { # rc 0 = working tree clean [ -z "$(git -C "${1:-.}" status --porcelain 2>/dev/null)" ] } # $1 repo, $2 branch fragment → rc 0 if a merge commit exists reconcile_oracle_merge_done() { [ -n "$(git -C "${1:-.}" log --oneline --grep "Merge .*$2" -1 2>/dev/null)" ] } # $1 repo, $2 branch → rc 0 if nothing unpushed vs origin reconcile_oracle_pushed() { [ -z "$(git -C "${1:-.}" rev-list "origin/$2..$2" 2>/dev/null)" ] } # $1 repo, $2 sha → rc 0 if the commit object exists reconcile_oracle_sha_exists() { git -C "${1:-.}" cat-file -e "${2}^{commit}" 2>/dev/null } # $1 repo, $2 grep → rc 0 if a commit message matches reconcile_oracle_msg_committed() { [ -n "$(git -C "${1:-.}" log --oneline --grep "$2" -1 2>/dev/null)" ] } # $1 path → rc 0 if it still exists on disk reconcile_oracle_path_present() { [ -e "${1:?}" ]; } # --- registry status: LAST candidate wins (the BLK-008 trap A fell into) --- # $1 blockers file, $2 id. A v2 registry (lib/memory.sh): the entry's own # header `[tag]` or a labelled body line, never a status word in prose or in # the title (the BLK-032..034 false-open). A v1 registry keeps the lexical # reader (memory_legacy_status): its dialects (title suffix `— RESOLVED`, # `date: … · status:`, `**Statut** :`) carry status where the strict parser # cannot see it. reconcile_blk_current_status() { if memory_is_v2 "$1"; then memory_entry_status "$1" "$2"; return; fi MEM_GREP="$RECONCILE_GREP" memory_legacy_status "$1" "$2" } # blockers whose CURRENT status does not close them → "idstatus". # v2: memory_closed (resolved | superseded | deprecated); v1: the substring. reconcile_blk_open() { local id st v2="" memory_is_v2 "$1" && v2=1 for id in $(reconcile_enumerate_ids "$1" BLK); do st=$(reconcile_blk_current_status "$1" "$id") if [ -n "$v2" ]; then memory_closed "$(memory_status_word "$st")" && continue else case "$st" in *RESOLVED*|*resolved*) continue ;; esac fi printf '%s\t%s\n' "$id" "$st" done } _reconcile_status() { # → the current status line [ "$3" = BLK ] && { reconcile_blk_current_status "$1" "$2"; return; } if memory_is_v2 "$1"; then memory_entry_status "$1" "$2"; return; fi MEM_GREP="$RECONCILE_GREP" MEM_LEGACY_RE="$RECONCILE_BDR_RE" \ memory_legacy_status "$1" "$2" "^##* *$3-[0-9]" } # entries of one prefix (BLK blockers, BDR decisions) → "idopen| # unreadableword or line"; closed ones are omitted. A status no # reader maps to exactly one known word is unreadable, never open # (BDR-129, BDR-133): v2 strict word, v1 the single word of a # status/statut label (_mem_v1_word: prose, mixed or negated lines give # none). Entries under a heading the enumerator cannot see (`### BLK-`) # are unreadable too. # $1 file, $2 prefix. The status source: _reconcile_status. reconcile_status_classify() { local id st w v2="" memory_is_v2 "$1" && v2=1 for id in $(reconcile_enumerate_ids "$1" "$2"); do st=$(_reconcile_status "$1" "$id" "$2") if [ -n "$v2" ]; then w=$(memory_status_word "$st") else w=$(_mem_v1_word "$st"); fi [ -n "$st" ] || w="" memory_closed "$w" && continue case " $_MEM_WORDS " in *" $w "*) [ -n "$w" ] && { printf '%s\topen\t%s\n' "$id" "$w"; continue; } ;; esac printf '%s\tunreadable\t%s\n' "$id" "${st:0:80}" done MEM_GREP="$RECONCILE_GREP" _mem_heading_ids "$1" "$2" | sort -u \ | comm -23 - <(reconcile_enumerate_ids "$1" "$2") \ | while read -r id; do printf '%s\tunreadable\theading level\n' "$id"; done return 0 } reconcile_blk_classify() { reconcile_status_classify "$1" BLK; } # --- pure reconciliation kernel: declared checkbox × real fact → verdict # (no git, fully testable) --- # $1 checkbox char (x| |~), $2 real_done (true|false). reconcile_verdict() { case "$1:$2" in " :true") echo "STALE:open-but-done" ;; "x:false") echo "STALE:done-but-open" ;; "~:true") echo "STALE:partial-but-done" ;; *) echo "CONSISTENT" ;; esac } # --- lexical deferral sweep (HONEST LIMIT: marked-only) # → "srclinetext" --- reconcile_deferrals() { [ -f "$1" ] && "$RECONCILE_GREP" -nE "$RECONCILE_DEFER_RE" "$1" 2>/dev/null \ | sed 's/^/TODO\t/' [ -f "$2" ] && "$RECONCILE_GREP" -nE "$RECONCILE_DEFER_RE" "$2" 2>/dev/null \ | sed 's/^/BDR\t/' return 0 } # --- contradiction CANDIDATES (surface, never assert): CLI-flag token shared # by a BDR + the TODO --- # $1 decisions file, $2 todo file. CLI-flag-like tokens are distinctive enough # to flag for review. reconcile_contradiction_candidates() { local tok why=' in a BDR title and an open TODO chantier' why+=' — review for contradiction' for tok in $("$RECONCILE_GREP" -oE '\-\-[a-z][a-z-]+' "$1" 2>/dev/null \ | sort -u); do "$RECONCILE_GREP" -qF -- "$tok" "$2" 2>/dev/null \ && printf 'CANDIDATE\t%s\tflag "%s"%s\n' "$tok" "$tok" "$why" done return 0 }