From 16ea1f938d24e194225cf13c7a94dde82bf3c539 Mon Sep 17 00:00:00 2001 From: Chad El Kurdi <162061136+Chad-Mufasax@users.noreply.github.com> Date: Mon, 29 Jun 2026 10:37:16 +0200 Subject: [PATCH] engine: harden brain pipeline (index.lock sweep, gbq exit code, lessons append-order) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - nightly: sweep an orphan .git/index.lock before commit. A crashed git leaves it behind; the vault then never commits and 'gbrain sync' stalls on "No commits in repo", silently freezing the index (root-caused after a 17-day freeze). Only removed when no git runs in the repo and the lock is >5 min old. - gbq: propagate gbrain's real exit code instead of the trailing lock-sweep test's (false — hence exit 1 — whenever there is no stale lock, breaking 'gbq sync && ...'). - correction-detector: spell out the append order (chronological, newest at the bottom, never at the top) so tail-injection re-reads recent lessons and the file doesn't split into two regions. --- engine/bin/gbq | 7 ++++++- engine/hooks/correction-detector.py | 2 +- engine/nightly/gbrain-nightly.sh | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/engine/bin/gbq b/engine/bin/gbq index d9498ee..7908085 100755 --- a/engine/bin/gbq +++ b/engine/bin/gbq @@ -55,9 +55,10 @@ if [ "$IS_READ" = "1" ]; then sleep 0.5 done kill -9 "$GP" 2>/dev/null + RC=0 # read output captured; the force-kill is intentional, not a failure else # Writes exit cleanly and may legitimately pause (LLM call) — just wait. - wait "$GP" 2>/dev/null + wait "$GP"; RC=$? fi cat "$TMP" @@ -68,3 +69,7 @@ if ! pgrep -f "bun.*gbrain" >/dev/null 2>&1; then L="$HOME/.gbrain/brain.pglite/.gbrain-lock" [ -d "$L" ] && mv "$L" "$L.stale-$(date +%s)" 2>/dev/null fi + +# Propagate gbrain's real exit code — NOT the lock-sweep test above (which is +# false, hence exit 1, whenever there's no stale lock to clear → false failures). +exit $RC diff --git a/engine/hooks/correction-detector.py b/engine/hooks/correction-detector.py index 070cbbb..1c02d1d 100755 --- a/engine/hooks/correction-detector.py +++ b/engine/hooks/correction-detector.py @@ -26,7 +26,7 @@ reminder = ( "⚠️ CORRECTION DETECTED — BEFORE doing anything else:\n" - f"1. Append a line to {brain}/Profile/lessons.md under today's header (## YYYY-MM-DD).\n" + f"1. Append a line to {brain}/Profile/lessons.md under today's header (## YYYY-MM-DD). The file is chronological (most recent day at the BOTTOM — that is what the tail-injection re-reads): today's header lives at the END of the file. Create it at the end if missing and add the entry right below it, NEVER at the top.\n" " Format: - **[short context]** -> rule: [what to do] (when: [trigger condition])\n" "2. Confirm visibly: ✓ noted in lessons.md\n" "3. Only then apply the correction and continue." diff --git a/engine/nightly/gbrain-nightly.sh b/engine/nightly/gbrain-nightly.sh index 621cb3e..cbab3e4 100755 --- a/engine/nightly/gbrain-nightly.sh +++ b/engine/nightly/gbrain-nightly.sh @@ -14,6 +14,19 @@ VAULT_CO="$HOME/Documents/BrainCo" GREPO="$HOME/DEV/gbrain" LOG="$HOME/.gbrain/nightly.log" +# Sweep an orphan .git/index.lock that silently breaks `git commit` (a crashed +# git leaves it behind → the vault never commits → gbrain sync stalls on "No +# commits in repo" → the index freezes, unnoticed). Only remove it when no git +# runs in the repo AND the lock is >5 min old (a fresh one may be a legit +# concurrent commit). Root-caused 2026-06-28: a 14-day-old lock froze the index. +sweep_git_lock() { + local repo="$1" lock="$1/.git/index.lock" + [ -f "$lock" ] || return 0 + if ! pgrep -f "[g]it .*$repo" >/dev/null 2>&1 && [ -z "$(find "$lock" -mmin -5 2>/dev/null)" ]; then + rm -f "$lock" && echo "[lock] removed stale .git/index.lock in $repo" >> "$LOG" + fi +} + echo "===== $(date '+%Y-%m-%d %H:%M:%S') nightly start =====" >> "$LOG" # -1. Stale-lock recovery (a gbrain killed with SIGKILL leaves a .gbrain-lock that blocks everything). @@ -21,6 +34,8 @@ if ! pgrep -f "bun.*gbrain" >/dev/null 2>&1; then L="$HOME/.gbrain/brain.pglite/.gbrain-lock" [ -d "$L" ] && mv "$L" "${L}.stale-$(date +%s)" 2>/dev/null && echo "[lock] rotated stale lock" >> "$LOG" fi +sweep_git_lock "$VAULT" +[ -d "$VAULT_CO/.git" ] && sweep_git_lock "$VAULT_CO" # 0. Self-update GBrain (resilient: a failure must never block the cycle). if cd "$GREPO" 2>/dev/null; then