Files
legal-ai/web/agent_platform_port.py
Chaim 2f094b8d84
All checks were successful
G12 Leak-Guard / leak-guard (pull_request) Successful in 6s
feat(operations): מסך "סוכנים פעילים" + ניהול ריצות (live-runs/log/cancel) (G12/X15, #119)
פאנל ב-/operations שמראה אילו סוכני Paperclip עובדים כעת (רצים+בתור), הפלט החי
שלהם, ושליטה מבוקרת: עצירת ריצה, איפוס session. סוגר את הנקודה-העיוורת שבה drain
מונע-סוכן (למשל ריקון תור הלכות ע"י ה-CEO heartbeat) עוקף את בקרת /operations
שמכירה רק שירותי pm2, והפלט הגולמי נגיש רק ב-Paperclip UI.

מקור-נתונים: Paperclip heartbeat-runs API (אומת חי):
  GET  /api/companies/{cid}/live-runs        — רצים+בתור (agentName/status/issue/outputSilence)
  GET  /api/heartbeat-runs/{id}/log          — NDJSON של פלט הסוכן
  GET  /api/heartbeat-runs/{id}/events        — timeline
  POST /api/heartbeat-runs/{id}/cancel        — עצירה מבוקרת (לא kill — מכבד watchdog+checkpoint)
  POST /api/agents/{id}/runtime-state/reset-session

ארכיטקטורה (G12/INV-PORT1): כל המגע החדש עם Paperclip דרך השער בלבד —
web/paperclip_client.py (shell) → re-export ב-web/agent_platform_port.py →
web/app.py צורך מהשער. leak_guard.py עובר (seam שלם). אסור kill ישיר על
process_pid (עוקף את השער).

Backend:
- paperclip_client: list_live_runs / get_run_log / get_run_events / cancel_run / reset_agent_session
- agent_platform_port: re-export pc_list_live_runs / pc_get_run_log / pc_get_run_events / pc_cancel_run / pc_reset_agent_session
- app.py: GET /api/operations/agents (אגרגציה CMP+CMPA, עמיד לכשל-חברה),
  GET .../runs/{id}/log, GET .../runs/{id}/events, POST .../runs/{id}/cancel,
  POST .../agents/{id}/reset-session

Frontend: פאנל "סוכנים פעילים" ב-/operations (polling 4s) + dialog ללוג חי
(פרסור NDJSON→טקסט קריא) + כפתורי עצור/אפס. הוספת hooks ל-operations.ts.

בטיחות: cancel על דריינר הלכות בטוח — חילוץ checkpointed per-chunk + resumable
+ self-heal לשורות processing.

Invariants: מקיים G12/INV-PORT1 (שער-הפלטפורמה). נוגע X6 (UI↔API).
api:types יורץ אחרי deploy (openapi.json חי).

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

108 lines
4.3 KiB
Python

"""Agent Platform Port (INV-G12 / docs/spec/X15) — the single seam to the platform.
Paperclip is the *agent platform*: a replaceable shell, not the core. This module
is the ONLY place in the web layer that may import the Paperclip client
(``web.paperclip_client`` / ``web.paperclip_api``). ``web/app.py`` — and any future
web code — imports the platform operations from HERE, never from the Paperclip
modules directly. So the dependency points inward (Ports & Adapters / the
Dependency Rule, Cockburn / Martin): swapping the platform means re-implementing
this one module, not touching app.py.
Enforced by the leak-guard (X15 §4 / R4): no file outside this Port imports
``paperclip_client`` / ``paperclip_api``.
Two tiers:
• **Lifecycle side-effects** — archive / restore / create a case's project, and
the case-status notification — are also exposed under domain names
(``archive_case_project`` …). These are the genuine domain events the app
emits over a case's lifecycle; prefer them in new code.
• **Issue / interaction / comment / agent operations** are re-exported under
their existing ``pc_*`` names: they are request/response API calls, not
events, so a faithful facade re-export — rather than an artificial "event"
is the honest treatment. Migrating these to domain verbs is a follow-up that
can happen as call sites are touched; the import seam (the enforceable part
of G12) holds regardless.
"""
from __future__ import annotations
# ── the Paperclip shell — imported ONLY here (the Port is the single seam) ──
from web.paperclip_api import (
emit_case_status_webhook,
pc_request,
require_paperclip_db_url,
)
from web.paperclip_client import (
COMPANIES as PAPERCLIP_COMPANIES,
accept_interaction as pc_accept_interaction,
archive_project as pc_archive_project,
cancel_run as pc_cancel_run,
create_project as pc_create_project,
create_workflow_issue as pc_create_workflow_issue,
get_agents_for_case as pc_get_agents_for_case,
get_agents_for_company as pc_get_agents,
get_case_issues as pc_get_case_issues,
get_issue_comments as pc_get_issue_comments,
get_issue_interactions as pc_get_issue_interactions,
get_project_url,
get_run_events as pc_get_run_events,
get_run_log as pc_get_run_log,
list_live_runs as pc_list_live_runs,
post_comment as pc_post_comment,
reject_interaction as pc_reject_interaction,
reset_agent_session as pc_reset_agent_session,
respond_to_interaction as pc_respond_to_interaction,
restore_project as pc_restore_project,
wake_analyst_for_appraiser_facts as pc_wake_analyst_for_appraiser_facts,
wake_ceo_agent as pc_wake_ceo,
wake_ceo_for_feedback_fold as pc_wake_ceo_for_feedback_fold,
wake_curator_for_final as pc_wake_curator_for_final,
wake_for_precedent_extraction as pc_wake_for_precedent_extraction,
)
# ── domain-named lifecycle aliases (preferred for new call sites) ───────────
archive_case_project = pc_archive_project
restore_case_project = pc_restore_project
create_case_project = pc_create_project
notify_case_status = emit_case_status_webhook
__all__ = [
# platform infrastructure
"PAPERCLIP_COMPANIES",
"pc_request",
"require_paperclip_db_url",
"get_project_url",
# lifecycle — domain names (preferred) + legacy aliases
"archive_case_project",
"restore_case_project",
"create_case_project",
"notify_case_status",
"emit_case_status_webhook",
"pc_archive_project",
"pc_restore_project",
"pc_create_project",
# issues / workflow
"pc_create_workflow_issue",
"pc_get_case_issues",
# agents / wakeups
"pc_get_agents_for_case",
"pc_get_agents",
"pc_wake_ceo",
"pc_wake_ceo_for_feedback_fold",
"pc_wake_curator_for_final",
"pc_wake_for_precedent_extraction",
"pc_wake_analyst_for_appraiser_facts",
# comments / interactions
"pc_post_comment",
"pc_get_issue_comments",
"pc_get_issue_interactions",
"pc_accept_interaction",
"pc_reject_interaction",
"pc_respond_to_interaction",
# agent-run observability + control (live view + smart management)
"pc_list_live_runs",
"pc_get_run_log",
"pc_get_run_events",
"pc_cancel_run",
"pc_reset_agent_session",
]