PERDYPTO commited on
Update projects/dataset_verification/router.py
Browse files
projects/dataset_verification/router.py
CHANGED
|
@@ -650,6 +650,48 @@ class MemoryStore:
|
|
| 650 |
|
| 651 |
return self._verification_public_locked(verification)
|
| 652 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
def _verification_result_locked(self, verification: dict) -> dict:
|
| 654 |
results = []
|
| 655 |
|
|
@@ -1556,7 +1598,8 @@ def desktop_submit_verification(
|
|
| 1556 |
body: VerificationSubmission,
|
| 1557 |
):
|
| 1558 |
"""
|
| 1559 |
-
Additive logical verification endpoint.
|
|
|
|
| 1560 |
|
| 1561 |
This does NOT replace /desktop/jobs.
|
| 1562 |
|
|
@@ -1586,15 +1629,36 @@ def desktop_submit_verification(
|
|
| 1586 |
"/desktop/verification/{request_id}",
|
| 1587 |
dependencies=[Depends(desktop_auth)],
|
| 1588 |
)
|
| 1589 |
-
def desktop_get_verification(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1590 |
"""
|
| 1591 |
Poll one logical verification request.
|
| 1592 |
|
| 1593 |
-
This lets
|
| 1594 |
-
1. submit once and poll this endpoint
|
|
|
|
| 1595 |
2. submit once with callback_url and receive the POST asynchronously.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1596 |
"""
|
| 1597 |
-
result = store.
|
|
|
|
|
|
|
| 1598 |
|
| 1599 |
if result is None:
|
| 1600 |
raise HTTPException(
|
|
@@ -1665,3 +1729,25 @@ def desktop_set_verification_callback(
|
|
| 1665 |
"status": "callback_configured",
|
| 1666 |
"request_id": request_id,
|
| 1667 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
|
| 651 |
return self._verification_public_locked(verification)
|
| 652 |
|
| 653 |
+
def get_verification_and_maybe_consume(
|
| 654 |
+
self, request_id: str, consume: bool
|
| 655 |
+
) -> Optional[dict]:
|
| 656 |
+
"""
|
| 657 |
+
Same as get_verification, but when consume=True and the request is
|
| 658 |
+
already complete, the verification AND its underlying jobs are
|
| 659 |
+
deleted from memory right after the result is read.
|
| 660 |
+
|
| 661 |
+
This exists so a caller (ChatGPT via the MCP tool) can submit a
|
| 662 |
+
batch, disconnect, and later come back with a single short-lived
|
| 663 |
+
call to fetch-and-clean-up the result instead of holding a
|
| 664 |
+
connection open for the whole run, and without leaving completed
|
| 665 |
+
job/result payloads sitting in memory indefinitely.
|
| 666 |
+
|
| 667 |
+
Only ever deletes when the request is complete. Pending requests
|
| 668 |
+
are left untouched even if consume=True, so an early/mistimed poll
|
| 669 |
+
can never destroy in-flight work.
|
| 670 |
+
"""
|
| 671 |
+
with self.lock:
|
| 672 |
+
verification = self.verifications.get(request_id)
|
| 673 |
+
|
| 674 |
+
if verification is None:
|
| 675 |
+
return None
|
| 676 |
+
|
| 677 |
+
self._finalize_ready_verifications_locked()
|
| 678 |
+
|
| 679 |
+
public = self._verification_public_locked(verification)
|
| 680 |
+
|
| 681 |
+
if consume and verification["status"] == "complete":
|
| 682 |
+
self._delete_verification_locked(request_id)
|
| 683 |
+
|
| 684 |
+
return public
|
| 685 |
+
|
| 686 |
+
def _delete_verification_locked(self, request_id: str) -> None:
|
| 687 |
+
verification = self.verifications.pop(request_id, None)
|
| 688 |
+
|
| 689 |
+
if verification is None:
|
| 690 |
+
return
|
| 691 |
+
|
| 692 |
+
for job_id in verification["job_ids"]:
|
| 693 |
+
self.jobs.pop(job_id, None)
|
| 694 |
+
|
| 695 |
def _verification_result_locked(self, verification: dict) -> dict:
|
| 696 |
results = []
|
| 697 |
|
|
|
|
| 1598 |
body: VerificationSubmission,
|
| 1599 |
):
|
| 1600 |
"""
|
| 1601 |
+
Additive logical verification endpoint. This is the one the ChatGPT/MCP
|
| 1602 |
+
side should use.
|
| 1603 |
|
| 1604 |
This does NOT replace /desktop/jobs.
|
| 1605 |
|
|
|
|
| 1629 |
"/desktop/verification/{request_id}",
|
| 1630 |
dependencies=[Depends(desktop_auth)],
|
| 1631 |
)
|
| 1632 |
+
def desktop_get_verification(
|
| 1633 |
+
request_id: str,
|
| 1634 |
+
consume: bool = Query(
|
| 1635 |
+
False,
|
| 1636 |
+
description=(
|
| 1637 |
+
"If true and the request is already complete, delete the "
|
| 1638 |
+
"verification and its underlying jobs from memory right after "
|
| 1639 |
+
"returning this result. Has no effect while the request is "
|
| 1640 |
+
"still pending, so an early poll can never destroy in-flight "
|
| 1641 |
+
"work. Default is false, which preserves the exact behavior "
|
| 1642 |
+
"existing callers (e.g. the local tester) already depend on."
|
| 1643 |
+
),
|
| 1644 |
+
),
|
| 1645 |
+
):
|
| 1646 |
"""
|
| 1647 |
Poll one logical verification request.
|
| 1648 |
|
| 1649 |
+
This lets a caller either:
|
| 1650 |
+
1. submit once and poll this endpoint (optionally with consume=true
|
| 1651 |
+
to have the coordinator forget it once read), or
|
| 1652 |
2. submit once with callback_url and receive the POST asynchronously.
|
| 1653 |
+
|
| 1654 |
+
This is the intended pattern for ChatGPT / the MCP tool: submit a batch,
|
| 1655 |
+
close the connection, and come back later with a short call using
|
| 1656 |
+
consume=true to fetch results and have them cleaned up in the same
|
| 1657 |
+
request instead of leaving them sitting in memory.
|
| 1658 |
"""
|
| 1659 |
+
result = store.get_verification_and_maybe_consume(
|
| 1660 |
+
request_id, consume
|
| 1661 |
+
)
|
| 1662 |
|
| 1663 |
if result is None:
|
| 1664 |
raise HTTPException(
|
|
|
|
| 1729 |
"status": "callback_configured",
|
| 1730 |
"request_id": request_id,
|
| 1731 |
}
|
| 1732 |
+
|
| 1733 |
+
|
| 1734 |
+
@router.delete(
|
| 1735 |
+
"/desktop/verification/{request_id}",
|
| 1736 |
+
dependencies=[Depends(desktop_auth)],
|
| 1737 |
+
)
|
| 1738 |
+
def desktop_delete_verification(request_id: str):
|
| 1739 |
+
"""
|
| 1740 |
+
Explicit manual cleanup, independent of consume=true on the GET route.
|
| 1741 |
+
Useful if a caller fetched with consume=false and wants to delete
|
| 1742 |
+
afterwards, or wants to abandon a request outright.
|
| 1743 |
+
"""
|
| 1744 |
+
with store.lock:
|
| 1745 |
+
if request_id not in store.verifications:
|
| 1746 |
+
raise HTTPException(
|
| 1747 |
+
status_code=404,
|
| 1748 |
+
detail="Verification request not found",
|
| 1749 |
+
)
|
| 1750 |
+
|
| 1751 |
+
store._delete_verification_locked(request_id)
|
| 1752 |
+
|
| 1753 |
+
return {"status": "deleted", "request_id": request_id}
|