diff --git a/mcp-server/src/legal_mcp/services/db.py b/mcp-server/src/legal_mcp/services/db.py index 7428bfb..c827f25 100644 --- a/mcp-server/src/legal_mcp/services/db.py +++ b/mcp-server/src/legal_mcp/services/db.py @@ -1774,19 +1774,41 @@ async def update_case_law(case_law_id: UUID, **fields) -> dict | None: async def set_case_law_extraction_status(case_law_id: UUID, status: str) -> None: + """Set text-extraction status. When transitioning to a terminal state + ('completed'/'failed') we also NULL ``metadata_extraction_requested_at`` + so the local-MCP queue (`process_pending_extractions`, which scans by + ``WHERE *_requested_at IS NOT NULL``) doesn't re-pick the row forever + and leave the row blocked in the UI's `isPrecedentActive` check.""" pool = await get_pool() - await pool.execute( - "UPDATE case_law SET extraction_status = $2 WHERE id = $1", - case_law_id, status, - ) + if status in ("completed", "failed"): + await pool.execute( + "UPDATE case_law SET extraction_status = $2, " + "metadata_extraction_requested_at = NULL WHERE id = $1", + case_law_id, status, + ) + else: + await pool.execute( + "UPDATE case_law SET extraction_status = $2 WHERE id = $1", + case_law_id, status, + ) async def set_case_law_halacha_status(case_law_id: UUID, status: str) -> None: + """Set halacha-extraction status. Mirrors ``set_case_law_extraction_status``: + on terminal states we also clear ``halacha_extraction_requested_at`` so the + queue and UI don't see a stale request flag.""" pool = await get_pool() - await pool.execute( - "UPDATE case_law SET halacha_extraction_status = $2 WHERE id = $1", - case_law_id, status, - ) + if status in ("completed", "failed"): + await pool.execute( + "UPDATE case_law SET halacha_extraction_status = $2, " + "halacha_extraction_requested_at = NULL WHERE id = $1", + case_law_id, status, + ) + else: + await pool.execute( + "UPDATE case_law SET halacha_extraction_status = $2 WHERE id = $1", + case_law_id, status, + ) async def list_external_case_law(