MISSION CRITICAL: LaTeX Data Loss Recovery & CL-Sync Safety Overhaul

CONTEXT & EMERGENCY REPORT

The CL-sync master utility has failed catastrophically during its execution sequence, resulting in a severe data loss event within the LaTeX_CL submodule. Thousands of euros worth of development time and critical legal framework coding have been compromised.

The script failed to restore the LaTeX environment after switching branches for production deployment (Phase 5: Deploy-Quartz). GitHub and TimeMachine backups are confirmed to be unavailable or lacking the latest critical commits. Recovery must be conducted via Git forensics and chat history reconstruction.

Terminal Output Excerpt (Failure Point):

[5/6] Preparing Production Deployment...
   Switching to deploy-quartz...
   Merging main into deploy-quartz...
   Removing LaTeX submodule for deployment...
   🚀 Pushing to deploy-quartz...
   ❌ Failed to push to deploy-quartz
   Restoring Development Environment...

Issue: The script hangs/fails after “Removing LaTeX submodule for deployment…” and the subsequent “Restoring Development Environment…” step does not successfully bring back the local, uncommitted LaTeX work.


OBJECTIVE 1: Forensic Git Recovery

Before rewriting code, you must attempt to locate the orphaned files in the local Git architecture. Because the script removed the submodule, the objects might still exist in the hidden .git/modules directory or as dangling commits.

Action Required: Execute and guide me through the following terminal commands to hunt down the lost files:

  1. Check Submodule Cache: Navigate to .git/modules/LaTeX_CL and check if the uncommitted objects were cached before the directory was wiped.
  2. Dangling Objects Search: Run git fsck --lost-found inside the submodule directory (if it exists) or the main directory. Look inside .git/lost-found/other/ for the raw .tex files.
  3. Stash and Reflog: Run git stash list and git reflog to see if the CL-sync script temporarily stashed the working directory before branch switching.

OBJECTIVE 2: Manual Reconstruction from Chat History

You must reconstruct the lost files based on the conversational history found strictly within the chat session named “Reconstructing Lost LaTeX” (or similar variations).

You must scan the chat history to reconstruct and output the precise LaTeX code for:

  1. Defining Parties Macro: The custom macro built to layout the contracting parties dynamically.
  2. Attachment G (_G_Tutela_Asset_Non-tangibili.tex): Reconstruct the strict IP, SQL database protection, and non-disclosure clauses we defined.
  3. Marketing Plan: I will provide the PDF. You must generate a Python script using PyPDF2 or pdfplumber to extract the text and automatically wrap it into the \Bilingual{}{} LaTeX macro format to reverse-engineer the .tex file.

(Note: The Contract for Internship will be handled in a separate dedicated prompt).


OBJECTIVE 3: CL-sync Safety Protocol Overhaul

The deployment sequence is fundamentally flawed. Attempting to manipulate submodules without a guaranteed hard backup is a catastrophic risk.

Risk Assessment

  • Current Flow: Database Sync LaTeX Sync Git Main Commit Remove Submodule & Switch Branch Push Restore.
  • Failure Point: If the script fails during the push to deploy-quartz, the “Restore” function (which likely relies on Git checkout) fails because the submodule was forcefully removed (git rm), permanently deleting uncommitted/unpushed local changes.

The New Bulletproof Protocol

You must implement a strict, sequential Bash script architecture. If any step fails, the script MUST ABORT immediately. No subsequent steps may run.

  1. Mandatory Local Hard Backup: Before interacting with Git, copy the entire repository to a safe, external Temp_Backups directory.
  2. Overleaf Push: Push to the Overleaf remote. Abort on failure.
  3. GitHub Main Push: Push to the GitHub origin. Abort on failure.
  4. Isolated Deployment: Only proceed to deploy-quartz if Steps 1-3 are 100% successful. Use a trap to restore from the Local Hard Backup if deployment fails.

[INJECT THE FOLLOWING BASH OVERHAUL INTO CL-SYNC]:

#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status.
 
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_DIR="/Users/nianoble-wedd/Projects/CL/Temp_Backups/CL_Backup_$TIMESTAMP"
REPO_DIR="/Users/nianoble-wedd/Projects/CL"
LATEX_DIR="$REPO_DIR/LaTeX_CL"
 
echo "====================================================="
echo "  INITIATING BULLETPROOF CL-SYNC PROTOCOL"
echo "====================================================="
 
# STEP 1: MANDATORY LOCAL HARD BACKUP
echo "[1/4] Creating Local Hard Backup..."
mkdir -p "$BACKUP_DIR"
cp -a "$REPO_DIR/." "$BACKUP_DIR/" || { echo "❌ CRITICAL ERROR: Local backup failed. Aborting."; exit 1; }
echo "✅ Backup secured at $BACKUP_DIR"
 
# STEP 2: OVERLEAF PUSH
echo "[2/4] Syncing LaTeX to Overleaf..."
cd "$LATEX_DIR"
git add .
git commit -m "Auto-sync before deployment $TIMESTAMP" || echo "No changes to commit in LaTeX."
git push overleaf master || { echo "❌ CRITICAL ERROR: Failed to push to Overleaf. Aborting to protect data."; exit 1; }
echo "✅ Overleaf sync successful."
 
# STEP 3: GITHUB MAIN PUSH
echo "[3/4] Syncing Main Repository to GitHub..."
cd "$REPO_DIR"
git add .
git commit -m "Master sync $TIMESTAMP" || echo "No changes in main repo."
git push origin main || { echo "❌ CRITICAL ERROR: Failed to push to GitHub main. Aborting."; exit 1; }
echo "✅ GitHub main sync successful."
 
# STEP 4: DEPLOYMENT WITH ROLLBACK TRAP
echo "[4/4] Preparing Production Deployment..."
 
# Define rollback function
rollback() {
    echo "⚠️ DEPLOYMENT FAILED. INITIATING EMERGENCY ROLLBACK..."
    cd "$REPO_DIR"
    git reset --hard HEAD
    git checkout main
    git submodule update --init --recursive
    # Restore from hard backup if Git is completely corrupted
    cp -a "$BACKUP_DIR/LaTeX_CL/." "$LATEX_DIR/"
    echo "🔄 Environment restored from local backup."
    exit 1
}
 
# Trap ERR signal to trigger rollback automatically
trap 'rollback' ERR
 
git checkout deploy-quartz
git merge main -m "Merge main for deployment"
# Safely deinit submodule instead of rm -rf
git submodule deinit -f LaTeX_CL
git rm -f LaTeX_CL
git commit -m "Remove LaTeX submodule for quartz deployment"
git push origin deploy-quartz
 
# If successful, restore normal environment cleanly
trap - ERR
git checkout main
git submodule update --init --recursive
echo "✅ Deployment successful. Development environment restored."
echo "====================================================="

Final Directive for the Agent

Acknowledge these instructions, provide the Git forensic output immediately so we can begin the hunt for the orphaned files, and confirm the integration of the new bash safety protocol into the system architecture.