Add full decision writing pipeline: classify, extract, brainstorm, write, QA, export

New services (11 files):
- classifier.py: auto doc-type classification + party identification (Claude Haiku)
- claims_extractor.py: claim extraction from pleadings (Claude Sonnet + regex)
- references_extractor.py: plan/case-law/legislation detection (regex)
- brainstorm.py: direction generation with 2-3 options (Claude Sonnet)
- block_writer.py: 12-block decision writer (template + Claude Sonnet/Opus)
- docx_exporter.py: DOCX export with David font, RTL, headings
- qa_validator.py: 6 QA checks with export blocking on critical failure
- learning_loop.py: draft vs final comparison + lesson extraction
- metrics.py: KPIs dashboard per case and global
- audit.py: action audit log
- cli.py: standalone CLI with 11 commands

Updated pipeline: extract → classify → chunk → embed → store → extract_references
New MCP tools: 29 total (was 16)
New DB tables: audit_log, decisions CRUD, claims CRUD
Config: Infisical support, external service allowlist

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-03 10:21:47 +00:00
parent df7cc4f5a5
commit d9e5ef0f46
21 changed files with 3957 additions and 14 deletions

56
scripts/backup-db.sh Executable file
View File

@@ -0,0 +1,56 @@
#!/bin/bash
# גיבוי יומי אוטומטי למסד הנתונים PostgreSQL
# Usage: ./backup-db.sh [backup_dir]
# Cron: 0 2 * * * /home/chaim/legal-ai/scripts/backup-db.sh
set -euo pipefail
BACKUP_DIR="${1:-/home/chaim/legal-ai/data/backups}"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="${BACKUP_DIR}/legal_ai_${TIMESTAMP}.sql.gz"
# Load DB credentials from .env
source "${HOME}/.env" 2>/dev/null || true
DB_HOST="${POSTGRES_HOST:-127.0.0.1}"
DB_PORT="${POSTGRES_PORT:-5433}"
DB_NAME="${POSTGRES_DB:-legal_ai}"
DB_USER="${POSTGRES_USER:-legal_ai}"
# Create backup directory
mkdir -p "${BACKUP_DIR}"
echo "[$(date)] Starting backup: ${BACKUP_FILE}"
# Dump and compress
PGPASSWORD="${POSTGRES_PASSWORD:-}" pg_dump \
-h "${DB_HOST}" \
-p "${DB_PORT}" \
-U "${DB_USER}" \
-d "${DB_NAME}" \
--format=custom \
--compress=9 \
-f "${BACKUP_FILE%.gz}"
gzip "${BACKUP_FILE%.gz}" 2>/dev/null || true
echo "[$(date)] Backup completed: ${BACKUP_FILE}"
# Verify backup
if [ -f "${BACKUP_FILE}" ]; then
SIZE=$(du -h "${BACKUP_FILE}" | cut -f1)
echo "[$(date)] Backup size: ${SIZE}"
else
# pg_dump --format=custom already compresses
BACKUP_FILE="${BACKUP_FILE%.gz}"
SIZE=$(du -h "${BACKUP_FILE}" | cut -f1)
echo "[$(date)] Backup size: ${SIZE} (custom format)"
fi
# Cleanup: keep last 30 days
find "${BACKUP_DIR}" -name "legal_ai_*.sql*" -mtime +30 -delete 2>/dev/null
echo "[$(date)] Old backups cleaned (>30 days)"
# Count remaining backups
COUNT=$(find "${BACKUP_DIR}" -name "legal_ai_*" | wc -l)
echo "[$(date)] Total backups: ${COUNT}"

69
scripts/restore-db.sh Executable file
View File

@@ -0,0 +1,69 @@
#!/bin/bash
# שחזור מסד נתונים מגיבוי
# Usage: ./restore-db.sh <backup_file>
set -euo pipefail
if [ $# -lt 1 ]; then
echo "שימוש: ./restore-db.sh <backup_file>"
echo ""
echo "גיבויים זמינים:"
ls -lhrt /home/chaim/legal-ai/data/backups/legal_ai_* 2>/dev/null || echo " (אין גיבויים)"
exit 1
fi
BACKUP_FILE="$1"
if [ ! -f "${BACKUP_FILE}" ]; then
echo "קובץ לא נמצא: ${BACKUP_FILE}"
exit 1
fi
# Load DB credentials
source "${HOME}/.env" 2>/dev/null || true
DB_HOST="${POSTGRES_HOST:-127.0.0.1}"
DB_PORT="${POSTGRES_PORT:-5433}"
DB_NAME="${POSTGRES_DB:-legal_ai}"
DB_USER="${POSTGRES_USER:-legal_ai}"
echo "⚠️ שחזור מגיבוי: ${BACKUP_FILE}"
echo " מסד נתונים: ${DB_NAME}@${DB_HOST}:${DB_PORT}"
echo ""
read -p "האם להמשיך? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "בוטל."
exit 0
fi
# Determine file format
if [[ "${BACKUP_FILE}" == *.gz ]]; then
echo "[$(date)] Decompressing..."
gunzip -k "${BACKUP_FILE}"
RESTORE_FILE="${BACKUP_FILE%.gz}"
else
RESTORE_FILE="${BACKUP_FILE}"
fi
echo "[$(date)] Restoring..."
PGPASSWORD="${POSTGRES_PASSWORD:-}" pg_restore \
-h "${DB_HOST}" \
-p "${DB_PORT}" \
-U "${DB_USER}" \
-d "${DB_NAME}" \
--clean \
--if-exists \
--no-owner \
"${RESTORE_FILE}" 2>&1 || true
echo "[$(date)] ✅ שחזור הושלם"
# Verify
PGPASSWORD="${POSTGRES_PASSWORD:-}" psql \
-h "${DB_HOST}" \
-p "${DB_PORT}" \
-U "${DB_USER}" \
-d "${DB_NAME}" \
-c "SELECT COUNT(*) as cases FROM cases; SELECT COUNT(*) as documents FROM documents;" 2>/dev/null || true