Files
legal-ai/scripts/retrofit_case.py
Chaim 726498126d
All checks were successful
Build & Deploy / build-and-deploy (push) Successful in 1m29s
Add Track Changes architecture for draft revisions (CMP + CMPA)
Fixes critical bug in 1033-25: user-uploaded עריכה-*.docx files were
orphaned on disk while exports kept rebuilding from stale DB blocks.

New architecture:
- User-uploaded DOCX becomes the source of truth (cases.active_draft_path)
- System edits via XML surgery with real Word <w:ins>/<w:del> revisions
- User can Accept/Reject each change from within Word

Components:
- docx_reviser.py: XML surgery for Track Changes (15 tests)
- docx_retrofit.py: retroactive bookmark injection with Hebrew marker
  detection + heading heuristic (9 tests)
- docx_exporter.py: emits bookmarks around each of the 12 blocks
- 3 new MCP tools: apply_user_edit, list_bookmarks, revise_draft
- 4 new/updated endpoints: upload (auto-registers active draft),
  /exports/revise, /exports/bookmarks, /exports/{filename}/retrofit,
  /active-draft
- DB migration: cases.active_draft_path column
- UI: correct banner using real v-numbers, "מקור האמת" badge,
  detailed upload toast with bookmarks_added/missing_blocks
- agents: legal-exporter (3 export modes), legal-ceo (stage G for
  revision handling), legal-writer (revision mode)

Multi-tenancy:
- Works for both CMP (1xxx cases) and CMPA (8xxx/9xxx cases)
- New revise-draft skill added to both companies
- deploy-track-changes.sh syncs skills CMP ↔ CMPA
- retrofit_case.py: one-off retrofit of existing files

Tests: 34 passing (15 reviser + 9 retrofit + 4 exporter bookmarks + 6 e2e)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-16 18:49:30 +00:00

85 lines
2.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""retrofit_case.py — הזרקת bookmarks רטרואקטיבית לקובץ קיים בתיק.
שימוש:
python scripts/retrofit_case.py <case_number> <filename>
דוגמה:
python scripts/retrofit_case.py 1033-25 עריכה-v1.docx
פעולה:
1. מזהה את הקובץ ב-data/cases/{case_number}/exports/
2. מזריק bookmarks ב-12 הבלוקים (heuristic)
3. שומר backup כ-{filename}.pre-retrofit.docx
4. מדפיס summary — אילו בלוקים זוהו, אילו חסרים
לתיק 1033-25 — הריצו פעם אחת על עריכה-v1.docx הקיים. אחרי זה תוכלו
להריץ revise_draft דרך ה-CEO.
הערה: השירות הזה נקרא גם אוטומטית דרך apply_user_edit tool ב-MCP,
אז אחרי deploy אין צורך להריץ ידנית. זה לגיבוי/ניפוי.
"""
from __future__ import annotations
import json
import sys
from pathlib import Path
# Make mcp-server importable when run from repo root
REPO_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO_ROOT / "mcp-server" / "src"))
def main() -> int:
if len(sys.argv) != 3:
print(__doc__)
return 2
case_number = sys.argv[1]
filename = sys.argv[2]
from legal_mcp.services import docx_retrofit, docx_reviser
case_dir = REPO_ROOT / "data" / "cases" / case_number / "exports"
file_path = case_dir / filename
if not file_path.exists():
print(f"✗ קובץ לא נמצא: {file_path}", file=sys.stderr)
return 1
print(f"מעבד: {file_path}")
print(f" גודל: {file_path.stat().st_size:,} בייט")
# Existing bookmarks
before = docx_reviser.list_bookmarks(file_path)
print(f" bookmarks קיימים: {before or '(ריק)'}")
result = docx_retrofit.retrofit_bookmarks(file_path)
print()
print("תוצאה:")
print(json.dumps(result, ensure_ascii=False, indent=2))
# Verify post-state
after = docx_reviser.list_bookmarks(file_path)
print()
print(f"bookmarks אחרי: {len(after)}{after}")
backup = file_path.with_suffix(".pre-retrofit.docx")
if backup.exists():
print(f"גיבוי נשמר: {backup}")
# Build an MCP-callable invocation hint
rel = file_path.relative_to(REPO_ROOT)
print()
print("השלב הבא: לעדכן active_draft_path ב-DB. הפקודה:")
print(f' mcp__legal-ai__apply_user_edit case_number="{case_number}" '
f'edit_filename="{filename}"')
print()
print(f"(זה ירוץ retrofit שוב idempotent ואז יעדכן את DB)")
return 0
if __name__ == "__main__":
sys.exit(main())