feat(seance): predecessor by-case לצריכת-סוכן דרך ה-plugin (#220 המשך)
מוסיף get_predecessor_for_case(case_number) — האח case-scoped של
get_predecessor_context (by-issue). סוכנים חושבים ב-case_number (לא issue-UUID),
לכן זהו הנתיב שכלי-ה-plugin legal_predecessor_context (PR נפרד ב-plugin-legal-ai)
יקרא. מחזיר מסקנות (summary) של ריצות-heartbeat שהסתיימו על-פני issues של התיק,
newest-first, עם summary בלבד (ריצות cancelled ריקות מסוננות).
- shaper משותף _shape_predecessor_run (G2 — נתיב-shaping יחיד ל-by-issue+by-case).
- join בטוח i.id::text = payload->>'issueId' (לא cast של payload חופשי ל-uuid).
- Port pc_get_predecessor_for_case (read-only); endpoint
GET /api/operations/cases/{case_number}/predecessor.
- 2 בדיקות (shaping+identifier, case-לא-נמצא). אומת חי: 1043-02-26/8125-09-24
מחזירים מסקנות-דיספוזיציה אמיתיות.
Invariants: G12 (fetch מהמעטפת, המגע מהשער; הכלי יחיה ב-plugin=מעטפת מוצהרת, לא
ב-mcp-server), G2 (shaper יחיד). המשך: PR ב-plugin-legal-ai (כלי) + הוראת-HEARTBEAT.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1216,22 +1216,68 @@ async def get_predecessor_context(issue_id: str, limit: int = 3) -> dict:
|
||||
finally:
|
||||
await conn.close()
|
||||
|
||||
runs = [
|
||||
{
|
||||
"run_id": str(r["id"]),
|
||||
"status": r["status"],
|
||||
"started_at": r["started_at"].isoformat() if r["started_at"] else None,
|
||||
"finished_at": r["finished_at"].isoformat() if r["finished_at"] else None,
|
||||
"summary": r["summary"],
|
||||
"error_code": r["error_code"],
|
||||
"session_id": r["session_id_after"],
|
||||
"agent_name": r["agent_name"],
|
||||
}
|
||||
for r in rows
|
||||
]
|
||||
runs = [_shape_predecessor_run(r) for r in rows]
|
||||
return {"ok": True, "issue_id": issue_id, "runs": runs}
|
||||
|
||||
|
||||
def _shape_predecessor_run(r) -> dict:
|
||||
"""Shared row → predecessor-run dict (by-issue and by-case entry points, G2)."""
|
||||
return {
|
||||
"run_id": str(r["id"]),
|
||||
"status": r["status"],
|
||||
"started_at": r["started_at"].isoformat() if r["started_at"] else None,
|
||||
"finished_at": r["finished_at"].isoformat() if r["finished_at"] else None,
|
||||
"summary": r["summary"],
|
||||
"error_code": r["error_code"],
|
||||
"session_id": r["session_id_after"],
|
||||
"agent_name": r["agent_name"],
|
||||
}
|
||||
|
||||
|
||||
async def get_predecessor_for_case(case_number: str, limit: int = 5) -> dict:
|
||||
"""Recent finished runs across a CASE's issues, newest-first (#220, agent-facing).
|
||||
|
||||
The case-scoped sibling of :func:`get_predecessor_context`. The agent knows its
|
||||
``case_number`` (not the Paperclip issue UUID), so the plugin tool
|
||||
``legal_predecessor_context`` calls this. Resolves the case's Paperclip project,
|
||||
then returns finished heartbeat-run summaries across its issues — so a resuming
|
||||
wake reads what prior sessions on the case concluded instead of re-deriving.
|
||||
|
||||
Read-only. The issue join casts the known-valid ``issues.id`` to text (rather
|
||||
than the free-form ``payload->>'issueId'`` to uuid) so a malformed payload
|
||||
can't break the query. ``limit`` clamped 1..10.
|
||||
"""
|
||||
limit = max(1, min(int(limit), 10))
|
||||
conn = await asyncpg.connect(PAPERCLIP_DB_URL)
|
||||
try:
|
||||
project = await conn.fetchrow(
|
||||
"SELECT id FROM projects WHERE name LIKE $1 LIMIT 1", f"%{case_number}%",
|
||||
)
|
||||
if not project:
|
||||
return {"ok": True, "case_number": case_number, "runs": []}
|
||||
rows = await conn.fetch(
|
||||
"""SELECT h.id, h.status, h.started_at, h.finished_at,
|
||||
h.result_json->>'summary' AS summary,
|
||||
h.error_code, h.session_id_after, a.name AS agent_name,
|
||||
i.identifier
|
||||
FROM heartbeat_runs h
|
||||
JOIN agent_wakeup_requests w ON w.id = h.wakeup_request_id
|
||||
JOIN issues i ON i.id::text = w.payload->>'issueId'
|
||||
LEFT JOIN agents a ON a.id = h.agent_id
|
||||
WHERE i.project_id = $1
|
||||
AND h.finished_at IS NOT NULL
|
||||
AND h.result_json->>'summary' IS NOT NULL
|
||||
ORDER BY h.started_at DESC
|
||||
LIMIT $2""",
|
||||
project["id"], limit,
|
||||
)
|
||||
finally:
|
||||
await conn.close()
|
||||
|
||||
runs = [{**_shape_predecessor_run(r), "identifier": r["identifier"]} for r in rows]
|
||||
return {"ok": True, "case_number": case_number, "runs": runs}
|
||||
|
||||
|
||||
# Singleton project for the precedent-library extraction queue. One issue per
|
||||
# uploaded precedent — assigned to the CEO who runs the local-MCP extractor.
|
||||
_LIBRARY_PROJECT_NAME = "ספריית פסיקה — תור חילוץ"
|
||||
|
||||
Reference in New Issue
Block a user