Files
legal-ai/scripts/legal-halacha-drain.config.cjs
Chaim 4fa62db192
All checks were successful
G12 Leak-Guard / leak-guard (pull_request) Successful in 6s
feat(halacha): drain לילי (23:00–05:00) + per-upload חילוץ תיק-בודד דרך ה-CEO (#120)
מפריד בין ריקון-באקלוג המוני לבין חילוץ per-upload, ומסיר את ה"פקק" שגרם
timeout/process_lost ב-heartbeat של ה-CEO.

הבעיה (אבחנה 2026-06-11): לחיצת "חלץ הלכות" על תיק בודד יצרה issue (CMP-165)
שהורה ל-CEO להריץ precedent_process_pending(halacha) — בולען סינכרוני שמרוקן את
כל התור ההיסטורי (147 ממתינים, שעות) בתוך heartbeat שחסום לשעה. תוצאה: timeout
כל שעה → process_lost בפירוק קבוצת-התהליכים → retry → סטורם, והתיק הבודד (FIFO
אחרון) לא טופל. לא OOM, לא קוד שבור — אי-התאמה ארכיטקטונית.

התיקון:
1. per-upload (web/paperclip_client.py, wake_for_precedent_extraction): גוף ה-issue
   + תיאור-הפרויקט מורים כעת להריץ precedent_extract_metadata +
   precedent_extract_halachot ל-case_law_id של ה-issue **בלבד** — עם אזהרה
   מפורשת לא להריץ process_pending. reextract_halachot כבר מנקה requested_at
   ומסמן completed → התיק לא יחזור לתור הלילי.
2. הוראות ה-CEO (.claude/agents/legal-ceo.md): אותו שינוי — חילוץ תיק-בודד, לא
   ריקון-תור. (צריך sync_agents_across_companies.py --apply אחרי מיזוג.)
3. ריקון-באקלוג (scripts/drain_halacha_queue.py): שער חלון-לילה 23:00–05:00 שעון
   ישראל (zoneinfo, DST-safe — המכונה UTC). מחוץ לחלון ===SKIP===; נעצר ===STOP===
   כשהחלון נסגר, השאר ממשיך בלילה הבא (FIFO + per-chunk checkpoint). env:
   HALACHA_DRAIN_WINDOW_START/_END/_TZ.
4. cron (scripts/legal-halacha-drain.config.cjs): UTC band 20:00–03:00 שמכסה את
   חלון-ישראל בשני מצבי-DST; הסקריפט גוזם לחלון המדויק. ירייה שעתית מחדשת
   one-shot שמת (advisory-lock → חפיפה בטוחה).

רשת-ביטחון: request_halacha_extraction עדיין מסמן requested_at, כך שאם wakeup
ל-CEO נכשל — הדריינר הלילי יתפוס את התיק (בלילה, חסום), אך שום נתיב יומי לא
מרוקן את כל התור.

Invariants: מקיים G12/INV-PORT1 (paperclip_client = shell; leak_guard עובר).
נוגע X16 (durability — מתקציב-זמן heartbeat ל-job ייעודי).

בדיקות: py_compile ✓ · window-logic + zoneinfo ✓ (17:00 IDT→False) · leak_guard ✓.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-11 14:02:38 +00:00

55 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* pm2 ecosystem entry for legal-halacha-drain — scheduled drain of the precedent
* halacha-extraction queue. Halacha extraction stays on claude_session (local
* CLI, high reasoning quality for holding/ratio) — unlike metadata which moved
* to Gemini. Extracted halachot land 'pending_review' for the chair's approval
* gate (INV-G10); this drainer only produces them, it never approves.
*
* The drain self-heals orphaned 'processing' rows (precedent_library) and is
* serialised by a global advisory lock, so overlapping ticks are safe.
*
* Pattern: cron_restart fires the script; autorestart:false → one-shot per tick
* (pm2 shows "stopped" between ticks). Cheap no-op when the queue is empty.
*
* NIGHT-WINDOW (23:0005:00 Israel time): backlog extraction is slow (Opus) and
* token-heavy, so it runs only off-hours — never competing with daytime
* interactive work / other agents (avoids the heartbeat timeout+process_lost
* storm we hit when the CEO drained the whole queue mid-day). Single-case
* extraction requested by the chair still runs immediately via the CEO; only
* the bulk backlog is gated to the night.
*
* TIMEZONE: pm2's daemon runs in the host TZ (UTC here), so cron strings are
* UTC. We fire across a UTC *superset* band (20:0003:00 UTC) that covers the
* Israel window in BOTH DST states (UTC+3 summer / UTC+2 winter); the script's
* zoneinfo('Asia/Jerusalem') window-guard then trims to exactly 23:0005:00
* and no-ops on the margin ticks. Firing each hour also re-arms the one-shot if
* a tick died mid-drain — the global advisory lock makes overlap safe.
*
* Requires the local ``claude`` CLI + host ~/.env (POSTGRES_URL, etc.).
*
* Install (once):
* pm2 start /home/chaim/legal-ai/scripts/legal-halacha-drain.config.cjs
* pm2 save
* Run now (manual, ignores window only if env-overridden):
* mcp-server/.venv/bin/python scripts/drain_halacha_queue.py
* Overrides: HALACHA_DRAIN_CRON (UTC cron) · HALACHA_DRAIN_WINDOW_START/_END
* (Israel hours, default 23/5) · HALACHA_DRAIN_TZ.
*/
// UTC band covering Israel 23:0005:00 across DST; script trims to exact window.
const cron = process.env.HALACHA_DRAIN_CRON || "0 20,21,22,23,0,1,2,3 * * *";
module.exports = {
apps: [
{
name: "legal-halacha-drain",
cwd: "/home/chaim/legal-ai",
script: "/home/chaim/legal-ai/mcp-server/.venv/bin/python",
args: "scripts/drain_halacha_queue.py",
env: { HOME: "/home/chaim", PYTHONUNBUFFERED: "1" },
autorestart: false, // one-shot per cron tick
cron_restart: cron,
max_memory_restart: "800M",
},
],
};