Spaces:
Running
Running
File size: 99,475 Bytes
89e4531 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 | # scikitplot/_externals/_sphinx_ext/_sphinx_ai_assistant/_hf_spaces_proxy/_utils/_storage.py
#
# flake8: noqa: D213
#
# Authors: The scikit-plots developers
# SPDX-License-Identifier: BSD-3-Clause
"""
Provider-neutral record storage for the sphinx AI assistant proxy.
The browser never receives storage credentials. A canonical UTF-8 payload is
written to one primary repository and, optionally, mirrored to additional
repositories. Existing TRAINING_DATASET_REPO/HF_* deployments are synthesized
as a single Hugging Face primary target by :func:`load_storage_targets`.
"""
from __future__ import annotations
import asyncio
import base64
import hashlib
import inspect
import json
import os
import re
import threading
import time
from dataclasses import dataclass, field, replace
from datetime import datetime, timezone
from typing import Any, Literal
from urllib.parse import quote, urlencode, urlsplit
import httpx
Provider = Literal["huggingface", "github", "gitlab", "bitbucket"]
Role = Literal["primary", "mirror"]
_MAX_TARGETS = 8
_MAX_REPO = 240
_MAX_BRANCH = 120
_MAX_PATH = 240
_TOKEN_ENV_RE = re.compile(r"^AI_RECORD_STORAGE_TOKEN_[A-Z0-9_]{1,80}$")
_ID_RE = re.compile(r"^[a-z][a-z0-9_-]{0,47}$")
_SEG_RE = re.compile(r"^[A-Za-z0-9._-]+$")
_ALLOWED_PROVIDERS = {"huggingface", "github", "gitlab", "bitbucket"}
_TRANSIENT_STATUS = {408, 409, 425, 429, 500, 502, 503, 504}
_CONTROL_RESPONSE_DEFAULT = 4 * 1024 * 1024
_CONTROL_RESPONSE_HARD_MAX = 16 * 1024 * 1024
_HF_FACTORY_LOCK = threading.RLock()
class _ProviderResponseTooLarge( # ruff: ignore[error-suffix-on-exception-name]
RuntimeError
):
pass
def _control_response_limit() -> int:
raw = os.environ.get("AI_RECORD_STORAGE_CONTROL_RESPONSE_MAX_BYTES", "").strip()
try:
value = int(raw) if raw else _CONTROL_RESPONSE_DEFAULT
except ValueError:
value = _CONTROL_RESPONSE_DEFAULT
return max(1024, min(_CONTROL_RESPONSE_HARD_MAX, value))
def _safe_gitlab_api_base(value: Any) -> str:
raw = str(value or "").strip().rstrip("/")
if not raw:
return "https://gitlab.com/api/v4"
if any(
ord(ch) < 0x20 # ruff: ignore[magic-value-comparison]
or ord(ch) == 0x7F # ruff: ignore[magic-value-comparison]
for ch in raw
):
raise StorageConfigError("TARGET_API_BASE")
try:
parsed = urlsplit(raw)
except Exception as exc:
raise StorageConfigError("TARGET_API_BASE") from exc
if parsed.scheme.lower() != "https" or not parsed.hostname:
raise StorageConfigError("TARGET_API_BASE")
if (
parsed.username is not None
or parsed.password is not None
or parsed.query
or parsed.fragment
):
raise StorageConfigError("TARGET_API_BASE")
if ".." in [seg for seg in parsed.path.split("/") if seg]:
raise StorageConfigError("TARGET_API_BASE")
if "\\" in parsed.path:
raise StorageConfigError("TARGET_API_BASE")
try:
_ = parsed.port
except ValueError as exc:
raise StorageConfigError("TARGET_API_BASE") from exc
return raw
class _BoundedSyncStream(httpx.SyncByteStream):
def __init__(self, stream: httpx.SyncByteStream, limit: int) -> None:
self._stream = stream
self._limit = limit
self._seen = 0
def __iter__(self):
for chunk in self._stream:
self._seen += len(chunk)
if self._seen > self._limit:
raise _ProviderResponseTooLarge(
"provider control response exceeds configured limit"
)
yield chunk
def close(self) -> None:
self._stream.close()
class _BoundedTransport(httpx.BaseTransport):
def __init__(self, transport: httpx.BaseTransport, limit: int) -> None:
self._transport = transport
self._limit = limit
def handle_request(self, request: httpx.Request) -> httpx.Response:
response = self._transport.handle_request(request)
length = response.headers.get("content-length")
if length and length.isdigit() and int(length) > self._limit:
response.close()
raise _ProviderResponseTooLarge(
"provider control response exceeds configured limit"
)
response.stream = _BoundedSyncStream(response.stream, self._limit)
return response
def close(self) -> None:
self._transport.close()
def _with_bounded_hf_client(call):
"""
Run one huggingface_hub control-plane call with bounded HTTP bodies.
Minimal test doubles and old SDK shims may not expose the factory module;
production is pinned to a version that does. In that compatibility-only
case there is no underlying SDK HTTP client to wrap, so execute directly.
"""
try:
from huggingface_hub.utils import _http as hf_http # noqa: PLC0415
except (ImportError, ModuleNotFoundError):
return call()
with _HF_FACTORY_LOCK:
previous = hf_http._GLOBAL_CLIENT_FACTORY
def factory():
client = previous()
transport = getattr(client, "_transport", None)
if transport is None:
client.close()
raise RuntimeError("HF_CLIENT_TRANSPORT")
client._transport = _BoundedTransport(transport, _control_response_limit())
return client
hf_http.set_client_factory(factory)
try:
return call()
finally:
hf_http.set_client_factory(previous)
class StorageConfigError(ValueError):
"""Raised for invalid server-side storage target configuration."""
class StorageWriteError(RuntimeError):
"""Raised when a storage target cannot persist a record."""
def __init__(self, code: str, *, transient: bool = False) -> None:
super().__init__(code)
self.code = code
self.transient = transient
@dataclass(slots=True)
class StorageTarget:
id: str
label: str
provider: Provider
role: Role
repo: str
branch: str = "main"
feedback_path: str = "feedback"
contributions_path: str = "contributions"
token_env: str = ""
token_type: str = "unknown" # ruff: ignore[hardcoded-password-string]
expose_links: bool = True
api_base: str = ""
@property
def token(self) -> str:
return os.environ.get(self.token_env, "").strip() if self.token_env else ""
def folder_for(self, kind: str) -> str:
return self.feedback_path if kind == "feedback" else self.contributions_path
@dataclass(slots=True)
class TargetRuntimeState:
status: str = "configured"
write_capability: str = "unknown"
failures: int = 0
open_until: float = 0.0
last_error_code: str = ""
last_success_ms: int | None = None
last_failure_ms: int | None = None
pending_retries: int = 0
@dataclass(slots=True)
class StorageReceipt:
accepted: bool
record_id: str
primary: str | None
mirrors: dict[str, str] = field(default_factory=dict)
# Exact logical record path per configured target. Paths are control-plane
# metadata used for best-effort current-view removal after participant
# withdrawal. Removing these files creates another repository commit and
# therefore does NOT imply physical erasure from Git/provider history.
paths: dict[str, str] = field(default_factory=dict)
@dataclass(slots=True)
class ReviewReceipt:
"""Provider-neutral code-review receipt for one quarantined contribution."""
provider: Provider
target_id: str
repo: str
base_branch: str
review_branch: str
review_key: str
review_id: str
review_url: str
status: str
record_id: str
path: str
def storage_metadata(self) -> dict[str, Any]:
return {
"recordId": self.record_id,
"primary": self.review_url or None,
"mirrors": {},
"paths": {self.target_id: self.path},
"review": {
"provider": self.provider,
"targetId": self.target_id,
"repo": self.repo,
"baseBranch": self.base_branch,
"reviewBranch": self.review_branch,
"reviewKey": self.review_key,
"reviewId": self.review_id,
"reviewUrl": self.review_url,
"status": self.status,
},
}
def review_key_for(receipt_id: str) -> str:
"""Return a non-identifying deterministic key safe for refs and titles."""
raw = str(receipt_id or "").encode("utf-8", errors="ignore")
return hashlib.sha256(raw).hexdigest()[:24]
def review_branch_for(receipt_id: str) -> str:
return f"ai-contrib-{review_key_for(receipt_id)}"
def review_title_for(receipt_id: str) -> str:
return f"Dataset contribution {review_key_for(receipt_id)}"
def review_description_for(
review_identity: str,
base_branch: str,
reject_word: str = "close",
*,
review_kind: str = "contribution",
) -> str:
raw = str(review_identity or "")
key = raw if re.fullmatch(r"[0-9a-f]{24}", raw) else review_key_for(raw)
if review_kind == "feedback":
return (
"Automated maintainer feedback review.\n\n"
f"- Review key: `{key}`\n"
f"- Canonical branch: `{base_branch}`\n"
"- Training eligible while this review is open: **No**\n"
"- Merge = approve this Q&A + quality signal for the canonical training view.\n"
"- Repeated quick/detailed updates from the same management receipt update this same review as new commits.\n"
"- Review the latest revision; earlier commits are the feedback edit history.\n"
"- The latest merged revision becomes training eligible; close/reject never does.\n"
f"- {reject_word.capitalize()} without merge = reject the feedback review.\n\n"
"A participant may withdraw the feedback later; provider Git history is not claimed physically erased."
)
return (
"Automated dataset contribution review.\n\n"
f"- Review key: `{key}`\n"
f"- Canonical branch: `{base_branch}`\n"
"- Training eligible while this review is open: **No**\n"
"- Repeated submissions from the same management receipt update this same review as new commits.\n"
"- Merge = approve and make the reviewed record eligible on the canonical branch.\n"
f"- {reject_word.capitalize()} without merge = reject.\n\n"
"Review the latest revision; earlier commits are retained as review history."
)
def feedback_review_key_for(receipt_id: str) -> str:
"""Return a non-identifying stable key for one feedback-review lifecycle."""
raw = ("feedback:" + str(receipt_id or "")).encode("utf-8", errors="ignore")
return hashlib.sha256(raw).hexdigest()[:24]
def feedback_review_branch_for(receipt_id: str) -> str:
return f"ai-feedback-{feedback_review_key_for(receipt_id)}"
def feedback_review_title_for(receipt_id: str) -> str:
return f"Feedback review {feedback_review_key_for(receipt_id)}"
def _safe_id(value: Any, fallback: str) -> str:
s = str(value or "").strip().lower()
if _ID_RE.fullmatch(s):
return s
if fallback and _ID_RE.fullmatch(fallback):
return fallback
raise StorageConfigError("TARGET_ID")
def _safe_repo(value: Any, provider: str = "") -> str:
s = str(value or "").strip().strip("/")
if not s or len(s) > _MAX_REPO:
raise StorageConfigError("TARGET_REPO")
parts = s.split("/")
# GitLab project paths may contain nested groups. The other supported
# providers use an owner/workspace + repository pair.
if provider == "gitlab": # ruff: ignore[if-else-block-instead-of-if-exp]
valid_count = 2 <= len(parts) <= 8 # ruff: ignore[magic-value-comparison]
else:
valid_count = len(parts) == 2 # ruff: ignore[magic-value-comparison]
if not valid_count or any(not p or not _SEG_RE.fullmatch(p) for p in parts):
raise StorageConfigError("TARGET_REPO")
return s
def _safe_branch(value: Any) -> str:
s = str(value or "main").strip()
if not s or len(s) > _MAX_BRANCH or any(c in s for c in "\\\r\n\x00"):
raise StorageConfigError("TARGET_BRANCH")
if s.startswith(("-", "/")) or ".." in s or s.endswith("/"):
raise StorageConfigError("TARGET_BRANCH")
return s
def _safe_folder(value: Any, default: str) -> str:
s = str(value if value is not None else default).strip().strip("/")
if not s or len(s) > _MAX_PATH or "\\" in s or "\x00" in s:
raise StorageConfigError("TARGET_PATH")
parts = s.split("/")
if len(parts) > 12 or any( # ruff: ignore[magic-value-comparison]
p in {"", ".", ".."} or not _SEG_RE.fullmatch(p) for p in parts
):
raise StorageConfigError("TARGET_PATH")
return s
def _safe_token_env(value: Any) -> str:
s = str(value or "").strip()
if not s or not _TOKEN_ENV_RE.fullmatch(s):
raise StorageConfigError("TARGET_TOKEN_ENV")
return s
def _normalize_token_type(value: Any) -> str:
s = str(value or "unknown").strip().lower().replace("_", "-")
if s in {"finegrained", "fine-grained"}:
return "fine-grained"
return s if s in {"read", "write", "unknown"} else "unknown"
def _parse_target(raw: dict[str, Any], index: int) -> StorageTarget:
provider = str(raw.get("provider") or "").strip().lower()
if provider not in _ALLOWED_PROVIDERS:
raise StorageConfigError("TARGET_PROVIDER")
role = (
str(raw.get("role") or ("primary" if index == 0 else "mirror")).strip().lower()
)
if role not in {"primary", "mirror"}:
raise StorageConfigError("TARGET_ROLE")
fallback_id = f"{provider}-{index + 1}"
target_id = _safe_id(raw.get("id"), fallback_id)
label = str(raw.get("label") or target_id).strip()[:96] or target_id
paths = raw.get("paths") if isinstance(raw.get("paths"), dict) else {}
token_env = _safe_token_env(raw.get("token_env"))
token_type = _normalize_token_type(
raw.get("token_type") or os.environ.get(token_env + "_TYPE")
)
raw_api_base = raw.get("api_base")
if provider == "gitlab":
api_base = _safe_gitlab_api_base(raw_api_base)
else:
if str(raw_api_base or "").strip():
raise StorageConfigError("TARGET_API_BASE_UNSUPPORTED")
api_base = ""
return StorageTarget(
id=target_id,
label=label,
provider=provider, # type: ignore[arg-type]
role=role, # type: ignore[arg-type]
repo=_safe_repo(raw.get("repo"), provider),
branch=_safe_branch(raw.get("branch")),
feedback_path=_safe_folder(paths.get("feedback"), "feedback"),
contributions_path=_safe_folder(paths.get("contributions"), "contributions"),
token_env=token_env,
token_type=token_type,
expose_links=raw.get("expose_links", True) is not False,
api_base=api_base,
)
def load_storage_targets(
raw_json: str,
*,
legacy_repo: str = "",
legacy_token: str = "",
legacy_token_type: str = "unknown", # ruff: ignore[hardcoded-password-default]
) -> list[StorageTarget]:
"""Parse configured targets or synthesize the legacy HF target."""
raw_json = (raw_json or "").strip()
targets: list[StorageTarget] = []
if raw_json:
try:
data = json.loads(raw_json)
except Exception as exc: # noqa: BLE001
raise StorageConfigError("TARGETS_JSON") from exc
if not isinstance(data, list) or not 1 <= len(data) <= _MAX_TARGETS:
raise StorageConfigError("TARGETS_COUNT")
seen: set[str] = set()
for i, item in enumerate(data):
if not isinstance(item, dict):
raise StorageConfigError("TARGET_OBJECT")
target = _parse_target(item, i)
if target.id in seen:
raise StorageConfigError("TARGET_DUPLICATE")
seen.add(target.id)
targets.append(target)
primaries = [t for t in targets if t.role == "primary"]
if len(primaries) != 1:
raise StorageConfigError("TARGET_PRIMARY_COUNT")
return targets
# 100% backwards-compatible legacy synthesis. We cannot reference the
# actual token value from a synthetic env name, so mirror it into a private
# process env slot used only by this module.
if legacy_repo:
env_name = "AI_RECORD_STORAGE_TOKEN_LEGACY_HF"
if legacy_token:
os.environ[env_name] = legacy_token
return [
StorageTarget(
id="hf-primary",
label="Hugging Face Dataset",
provider="huggingface",
role="primary",
repo=_safe_repo(legacy_repo, "huggingface"),
token_env=env_name,
token_type=_normalize_token_type(legacy_token_type),
)
]
return []
def _repo_parts(repo: str) -> tuple[str, str]:
return tuple(repo.split("/", 1)) # type: ignore[return-value]
def public_links(target: StorageTarget) -> dict[str, str]:
"""Return public browser links without exposing credentials."""
if not target.expose_links:
return {}
owner, repo = _repo_parts(target.repo)
b = quote(target.branch, safe="")
fp = quote(target.feedback_path, safe="/")
cp = quote(target.contributions_path, safe="/")
if target.provider == "huggingface":
root = f"https://huggingface.co/datasets/{quote(owner)}/{quote(repo)}"
return {
"root": root,
"feedback": f"{root}/tree/{b}/{fp}",
"contributions": f"{root}/tree/{b}/{cp}",
}
if target.provider == "github":
root = f"https://github.com/{quote(owner)}/{quote(repo)}"
return {
"root": root,
"feedback": f"{root}/tree/{b}/{fp}",
"contributions": f"{root}/tree/{b}/{cp}",
}
if target.provider == "gitlab":
# Public links default to gitlab.com even when a custom API base is
# configured; self-managed instances can supply `public_base` in a
# future schema version without exposing credentials.
root = f"https://gitlab.com/{quote(owner)}/{quote(repo)}"
return {
"root": root,
"feedback": f"{root}/-/tree/{b}/{fp}",
"contributions": f"{root}/-/tree/{b}/{cp}",
}
root = f"https://bitbucket.org/{quote(owner)}/{quote(repo)}"
return {
"root": root,
"feedback": f"{root}/src/{b}/{fp}",
"contributions": f"{root}/src/{b}/{cp}",
}
def canonical_record_path(
target: StorageTarget, kind: str, record_id: str, now: float | None = None
) -> str:
dt = datetime.fromtimestamp(now or time.time(), tz=timezone.utc)
folder = target.folder_for(kind)
prefix = "fb" if kind == "feedback" else "ct"
return f"{folder}/{dt:%Y/%m/%d}/{prefix}_{record_id}.jsonl"
def review_record_path(
target: StorageTarget, receipt_id: str, now: float | None = None
) -> str:
"""Return the stable canonical path owned by one contribution review.
Review payloads are mutable until approval. A receipt-derived path lets
subsequent revisions replace the same logical file on the provider review
ref instead of accumulating one content-addressed file per resubmission.
The receipt itself never appears in the path; :func:`review_key_for` hashes it.
"""
dt = datetime.fromtimestamp(now or time.time(), tz=timezone.utc)
folder = target.folder_for("contributions")
return f"{folder}/{dt:%Y/%m/%d}/ct_{review_key_for(receipt_id)}.jsonl"
def feedback_review_record_path(
target: StorageTarget, receipt_id: str, now: float | None = None
) -> str:
"""Return the stable canonical path owned by one reviewable feedback item."""
dt = datetime.fromtimestamp(now or time.time(), tz=timezone.utc)
folder = target.folder_for("feedback")
return f"{folder}/{dt:%Y/%m/%d}/fb_{feedback_review_key_for(receipt_id)}.jsonl"
def record_id_for(content: bytes) -> str:
return hashlib.sha256(content).hexdigest()[:24]
class StorageCoordinator:
"""Persist canonical records to a primary target and optional mirrors."""
def __init__(
self, targets: list[StorageTarget], client: httpx.AsyncClient | None = None
) -> None:
self.targets = targets
self.client = client
self._locks = {t.id: asyncio.Lock() for t in targets}
self._state = {t.id: TargetRuntimeState() for t in targets}
self._max_attempts = 2
self._circuit_seconds = 60.0
self._background_tasks: set[asyncio.Task[Any]] = set()
# A withdrawal must not allow an already-scheduled degraded-mirror retry
# to resurrect the original eligible file after current-view removal.
self._suppressed_retry_record_ids: set[str] = set()
@property
def primary(self) -> StorageTarget | None:
return next((t for t in self.targets if t.role == "primary"), None)
def primary_ready(self) -> bool:
target = self.primary
if target is None or not target.token:
return False
state = self._state[target.id]
return state.write_capability not in {
"missing-token",
"denied",
"denied-read-token",
}
def set_client(self, client: httpx.AsyncClient | None) -> None:
self.client = client
async def initialize(self) -> None:
"""Best-effort capability checks. Never raises at application startup."""
for target in self.targets:
state = self._state[target.id]
if target.provider == "huggingface":
state.write_capability = await self._hf_write_capability(target)
else:
state.write_capability = (
"configured" if target.token else "missing-token"
)
if state.write_capability in {
"missing-token",
"denied",
"denied-read-token",
}:
state.status = "degraded"
async def _hf_write_capability( # ruff: ignore[too-many-return-statements]
self,
target: StorageTarget,
) -> str:
token = target.token
if not token:
return "missing-token"
token_type = _normalize_token_type(target.token_type)
if token_type == "read": # ruff: ignore[hardcoded-password-string]
return "denied-read-token"
# Modern huggingface_hub can verify repo-specific write access without a
# mutation. Keep compatibility with older pinned versions by feature-
# detecting the `write` parameter.
try:
from huggingface_hub import HfApi # noqa: PLC0415
api = HfApi(token=token)
auth_check = getattr(api, "auth_check", None)
if auth_check and "write" in inspect.signature(auth_check).parameters:
await asyncio.to_thread(
_with_bounded_hf_client,
lambda: auth_check(
repo_id=target.repo,
repo_type="dataset",
write=True,
),
)
return "verified"
except Exception as exc: # ruff: ignore[blind-except]
# Permission denial is materially different from a network/version
# uncertainty. Modern huggingface_hub exceptions normally carry an
# HTTP response; classify only the status code and keep every response
# body / exception message private. A 401/403 means the token cannot
# write this repo and should be blocked before any mutation attempt.
status = getattr(getattr(exc, "response", None), "status_code", None)
if status in {401, 403}:
return "denied"
# Older huggingface_hub releases do not support auth_check(write=True),
# and transient network failures are also possible. Preserve
# compatibility by allowing the first real commit to prove capability.
return "unverified"
if token_type == "write": # ruff: ignore[hardcoded-password-string]
return "broad-write"
if token_type == "fine-grained": # ruff: ignore[hardcoded-password-string]
return "unverified"
return "legacy-unverified"
def manifest(self) -> dict[str, Any]:
targets = []
for t in self.targets:
st = self._state[t.id]
targets.append(
{
"id": t.id,
"label": t.label,
"provider": t.provider,
"role": t.role,
"repo": t.repo if t.expose_links else None,
"branch": t.branch,
"paths": {
"feedback": t.feedback_path,
"contributions": t.contributions_path,
},
"capabilities": {
"feedback": True,
"contributions": True,
"native_review": t.role == "primary",
"write": (
bool(t.token)
and st.write_capability
not in {"missing-token", "denied", "denied-read-token"}
),
},
"token": {
"type": (
_normalize_token_type(t.token_type)
if t.provider == "huggingface"
else "server-managed"
),
"write_capability": st.write_capability,
},
"status": (
"circuit-open" if st.open_until > time.time() else st.status
),
"last_success_ms": st.last_success_ms,
"last_failure_ms": st.last_failure_ms,
"pending_retries": st.pending_retries,
"links": public_links(t),
}
)
return {
"schema_version": 1,
"policy": "primary_then_mirrors",
"targets": targets,
}
async def write(
self,
*,
kind: str,
content: bytes,
commit_message: str,
path_timestamp: float | None = None,
) -> StorageReceipt:
primary = self.primary
if primary is None:
raise StorageWriteError("NO_PRIMARY")
rid = record_id_for(content)
# Freeze one logical path timestamp for every target. Retries must write
# the same path rather than drifting across a UTC day boundary, because
# the receipt lifecycle later uses these paths for best-effort current-
# view removal.
# Callers with a durable lifecycle can supply a receipt-stable timestamp
# so a crash/restart replay targets the exact same logical provider path
# instead of creating a second dated file.
path_now = float(path_timestamp) if path_timestamp is not None else time.time()
paths = {
t.id: canonical_record_path(t, kind, rid, now=path_now)
for t in self.targets
}
await self._write_target(
primary, kind, rid, content, commit_message, path=paths[primary.id]
)
mirrors: dict[str, str] = {}
mirror_targets = [t for t in self.targets if t.role == "mirror"]
if mirror_targets:
results = await asyncio.gather(
*(
self._write_target(
t, kind, rid, content, commit_message, path=paths[t.id]
)
for t in mirror_targets
),
return_exceptions=True,
)
for target, result in zip(mirror_targets, results, strict=True):
if not isinstance(result, Exception):
mirrors[target.id] = "ok"
else:
mirrors[target.id] = "degraded"
self._schedule_mirror_retry(
target, kind, rid, content, commit_message, paths[target.id]
)
return StorageReceipt(True, rid, primary.id, mirrors, paths)
def _schedule_mirror_retry( # ruff: ignore[too-many-positional-arguments]
self,
target: StorageTarget,
kind: str,
rid: str,
content: bytes,
message: str,
path: str,
) -> None:
state = self._state[target.id]
state.pending_retries += 1
async def _runner() -> None:
try:
for delay in (2.0, 10.0, 30.0):
await asyncio.sleep(delay)
if rid in self._suppressed_retry_record_ids:
return
# A circuit opened by prior failures is allowed to cool down
# before the next scheduled retry rather than busy-looping.
if self._state[target.id].open_until > time.time():
continue
try:
await self._write_target(
target, kind, rid, content, message, path=path
)
return
except StorageWriteError:
continue
finally:
state.pending_retries = max(0, state.pending_retries - 1)
task = asyncio.create_task(_runner())
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.discard)
async def close(self) -> None:
"""Cancel pending in-memory mirror retries during graceful shutdown."""
tasks = list(self._background_tasks)
for task in tasks:
task.cancel()
if tasks:
await asyncio.gather(*tasks, return_exceptions=True)
self._background_tasks.clear()
self._suppressed_retry_record_ids.clear()
async def _write_target(
self,
target: StorageTarget,
kind: str,
rid: str,
content: bytes,
message: str,
*,
path: str | None = None,
) -> str:
state = self._state[target.id]
now = time.time()
if state.open_until > now:
raise StorageWriteError("CIRCUIT_OPEN", transient=True)
if not target.token:
self._mark_failure(target, "MISSING_TOKEN")
raise StorageWriteError("MISSING_TOKEN")
if (
target.provider == "huggingface"
and _normalize_token_type(target.token_type) == "read"
):
self._mark_failure(target, "READ_TOKEN")
raise StorageWriteError("READ_TOKEN")
async with self._locks[target.id]:
# Re-check withdrawal suppression *after* taking the target lock. A
# degraded-mirror retry may have passed the scheduler's earlier check
# and then waited behind current-view deletion; without this second
# check it could resurrect the withdrawn eligible file after DELETE.
if kind == "contributions" and rid in self._suppressed_retry_record_ids:
raise StorageWriteError("WITHDRAWN")
last: StorageWriteError | None = None
for attempt in range(self._max_attempts):
try:
logical_path = path or canonical_record_path(target, kind, rid)
await self._dispatch(target, logical_path, content, message)
self._mark_success(target)
return logical_path
except StorageWriteError as exc:
last = exc
if not exc.transient or attempt + 1 >= self._max_attempts:
break
await asyncio.sleep(0.25 * (2**attempt))
self._mark_failure(target, last.code if last else "WRITE_FAILED")
raise last or StorageWriteError("WRITE_FAILED")
def _mark_success(self, target: StorageTarget) -> None:
st = self._state[target.id]
st.status = "healthy"
st.failures = 0
st.open_until = 0.0
st.last_error_code = ""
st.last_success_ms = int(time.time() * 1000)
if target.provider == "huggingface" and st.write_capability in {
"unverified",
"legacy-unverified",
"broad-write",
}:
st.write_capability = "verified"
def _mark_failure(self, target: StorageTarget, code: str) -> None:
st = self._state[target.id]
st.failures += 1
st.status = "degraded"
st.last_error_code = code
st.last_failure_ms = int(time.time() * 1000)
if st.failures >= 3: # ruff: ignore[magic-value-comparison]
st.open_until = time.time() + self._circuit_seconds
async def open_contribution_review(
self,
*,
receipt_id: str,
content: bytes,
commit_message: str,
path_timestamp: float | None = None,
) -> ReviewReceipt:
"""
Create or recover a native provider review for one contribution.
The contribution is written to its final canonical path on an isolated
review ref. The configured canonical branch remains the only
training-eligible authority. The operation is idempotent by a
receipt-derived, non-identifying review branch/title.
"""
target = self.primary
if target is None:
raise StorageWriteError("NO_PRIMARY_TARGET")
if not target.token:
raise StorageWriteError("PRIMARY_TOKEN_MISSING")
rid = record_id_for(content)
path = review_record_path(target, receipt_id, path_timestamp)
key = review_key_for(receipt_id)
branch = review_branch_for(receipt_id)
title = review_title_for(receipt_id)
async with self._locks[target.id]:
return await self._open_review_target(
target,
branch=branch,
key=key,
title=title,
path=path,
record_id=rid,
content=content,
message=commit_message,
description=review_description_for(key, target.branch),
)
async def update_contribution_review(
self,
*,
receipt_id: str,
content: bytes,
commit_message: str,
path_timestamp: float | None = None,
review_hint: dict[str, Any] | None = None,
) -> ReviewReceipt:
"""Replace the payload of an existing open provider review.
The provider review identity stays stable. Hugging Face receives a
commit on ``refs/pr/N``; GitHub/GitLab/Bitbucket receive a commit on the
existing source branch. Closed or merged reviews cannot be rewritten.
"""
target = self.primary
if target is None:
raise StorageWriteError("NO_PRIMARY_TARGET")
if not target.token:
raise StorageWriteError("PRIMARY_TOKEN_MISSING")
rid = record_id_for(content)
path = review_record_path(target, receipt_id, path_timestamp)
key = review_key_for(receipt_id)
branch = review_branch_for(receipt_id)
title = review_title_for(receipt_id)
async with self._locks[target.id]:
hinted = self._review_from_hint(
target, branch=branch, key=key, review_hint=review_hint
)
review = (
await self._refresh_review_target(target, hinted)
if hinted is not None
else await self._discover_review_target(
target, branch=branch, key=key, title=title
)
)
if review is None:
raise StorageWriteError("REVIEW_NOT_FOUND")
if review.status == "merged":
raise StorageWriteError("REVIEW_MERGED")
if review.status in {"closed", "rejected"}:
raise StorageWriteError("REVIEW_CLOSED")
await self._update_review_target(
target,
review=review,
path=path,
content=content,
message=commit_message,
)
return replace(review, record_id=rid, path=path)
async def open_feedback_review(
self,
*,
receipt_id: str,
content: bytes,
commit_message: str,
path_timestamp: float | None = None,
) -> ReviewReceipt:
"""Open one provider-native review for a content-bearing feedback item."""
target = self.primary
if target is None:
raise StorageWriteError("NO_PRIMARY_TARGET")
if not target.token:
raise StorageWriteError("PRIMARY_TOKEN_MISSING")
rid = record_id_for(content)
path = feedback_review_record_path(target, receipt_id, path_timestamp)
key = feedback_review_key_for(receipt_id)
branch = feedback_review_branch_for(receipt_id)
title = feedback_review_title_for(receipt_id)
async with self._locks[target.id]:
return await self._open_review_target(
target,
branch=branch,
key=key,
title=title,
path=path,
record_id=rid,
content=content,
message=commit_message,
description=review_description_for(
key, target.branch, review_kind="feedback"
),
)
async def update_feedback_review(
self,
*,
receipt_id: str,
content: bytes,
commit_message: str,
path_timestamp: float | None = None,
review_hint: dict[str, Any] | None = None,
) -> ReviewReceipt:
"""Update the existing feedback PR/MR without opening a duplicate review."""
target = self.primary
if target is None:
raise StorageWriteError("NO_PRIMARY_TARGET")
if not target.token:
raise StorageWriteError("PRIMARY_TOKEN_MISSING")
rid = record_id_for(content)
path = feedback_review_record_path(target, receipt_id, path_timestamp)
key = feedback_review_key_for(receipt_id)
branch = feedback_review_branch_for(receipt_id)
title = feedback_review_title_for(receipt_id)
async with self._locks[target.id]:
hinted = self._review_from_hint(
target, branch=branch, key=key, review_hint=review_hint
)
review = (
await self._refresh_review_target(target, hinted)
if hinted is not None
else await self._discover_review_target(
target, branch=branch, key=key, title=title
)
)
if review is None:
raise StorageWriteError("REVIEW_NOT_FOUND")
if review.status == "merged":
raise StorageWriteError("REVIEW_MERGED")
if review.status in {"closed", "rejected"}:
raise StorageWriteError("REVIEW_CLOSED")
await self._update_review_target(
target,
review=review,
path=path,
content=content,
message=commit_message,
)
return replace(review, record_id=rid, path=path)
async def get_feedback_review(
self, receipt_id: str, *, review_hint: dict[str, Any] | None = None
) -> ReviewReceipt | None:
"""Return current feedback-review state, preferring the persisted locator."""
target = self.primary
if target is None or not target.token:
return None
key = feedback_review_key_for(receipt_id)
branch = feedback_review_branch_for(receipt_id)
title = feedback_review_title_for(receipt_id)
async with self._locks[target.id]:
hinted = self._review_from_hint(
target, branch=branch, key=key, review_hint=review_hint
)
if hinted is not None:
return await self._refresh_review_target(target, hinted)
return await self._discover_review_target(
target, branch=branch, key=key, title=title
)
async def close_feedback_review(
self, receipt_id: str, *, review_hint: dict[str, Any] | None = None
) -> str:
"""Close/reject one pending feedback review and remove its source branch."""
target = self.primary
if target is None or not target.token:
return "not-configured"
key = feedback_review_key_for(receipt_id)
branch = feedback_review_branch_for(receipt_id)
title = feedback_review_title_for(receipt_id)
async with self._locks[target.id]:
hinted = self._review_from_hint(
target, branch=branch, key=key, review_hint=review_hint
)
review = (
await self._refresh_review_target(target, hinted)
if hinted is not None
else await self._discover_review_target(
target, branch=branch, key=key, title=title
)
)
if review is None:
return "already-absent"
if review.status == "merged":
return "already-merged"
if review.status not in {"closed", "rejected"}:
await self._close_review_target(target, review)
await self._delete_review_branch(target, branch)
return "closed"
async def get_contribution_review(
self, receipt_id: str, *, review_hint: dict[str, Any] | None = None
) -> ReviewReceipt | None:
"""Return provider review state, preferring a direct persisted locator.
``review_hint`` is the receipt-scoped metadata captured when the review
was first created. It avoids O(N) repository discussion scans as the
review queue grows; deterministic discovery remains a recovery fallback.
"""
target = self.primary
if target is None or not target.token:
return None
key = review_key_for(receipt_id)
branch = review_branch_for(receipt_id)
title = review_title_for(receipt_id)
async with self._locks[target.id]:
hinted = self._review_from_hint(
target, branch=branch, key=key, review_hint=review_hint
)
if hinted is not None:
return await self._refresh_review_target(target, hinted)
return await self._discover_review_target(
target, branch=branch, key=key, title=title
)
async def close_contribution_review(
self, receipt_id: str, *, review_hint: dict[str, Any] | None = None
) -> str:
"""Close/reject an open provider review and remove its temporary branch."""
target = self.primary
if target is None or not target.token:
return "not-configured"
key = review_key_for(receipt_id)
branch = review_branch_for(receipt_id)
title = review_title_for(receipt_id)
async with self._locks[target.id]:
hinted = self._review_from_hint(
target, branch=branch, key=key, review_hint=review_hint
)
review = (
await self._refresh_review_target(target, hinted)
if hinted is not None
else await self._discover_review_target(
target, branch=branch, key=key, title=title
)
)
if review is None:
return "already-absent"
if review.status == "merged":
return "already-merged"
if review.status not in {"closed", "rejected"}:
await self._close_review_target(target, review)
await self._delete_review_branch(target, branch)
return "closed"
async def merge_contribution_review(
self, receipt_id: str, *, review_hint: dict[str, Any] | None = None
) -> ReviewReceipt:
"""
Merge an existing provider review through the provider API.
Reviewers may instead merge in the native web UI. This method remains
for the authenticated legacy promote endpoint and automation.
"""
target = self.primary
if target is None or not target.token:
raise StorageWriteError("NO_PRIMARY_TARGET")
key = review_key_for(receipt_id)
branch = review_branch_for(receipt_id)
title = review_title_for(receipt_id)
async with self._locks[target.id]:
hinted = self._review_from_hint(
target, branch=branch, key=key, review_hint=review_hint
)
review = (
await self._refresh_review_target(target, hinted)
if hinted is not None
else await self._discover_review_target(
target, branch=branch, key=key, title=title
)
)
if review is None:
raise StorageWriteError("REVIEW_NOT_FOUND")
if review.status == "merged":
return review
if review.status in {"closed", "rejected"}:
raise StorageWriteError("REVIEW_CLOSED")
await self._merge_review_target(target, review)
merged = await self._refresh_review_target(target, review)
if merged is None:
raise StorageWriteError("REVIEW_MERGE_CONFIRM", transient=True)
if merged.status != "merged":
raise StorageWriteError("REVIEW_MERGE_PENDING", transient=True)
await self._delete_review_branch(target, branch)
return merged
@staticmethod
def _review_from_hint( # ruff: ignore[too-many-return-statements]
target: StorageTarget,
*,
branch: str,
key: str,
review_hint: dict[str, Any] | None,
) -> ReviewReceipt | None:
if not isinstance(review_hint, dict):
return None
raw = (
review_hint.get("review")
if isinstance(review_hint.get("review"), dict)
else review_hint
)
if not isinstance(raw, dict):
return None
review_id = str(raw.get("reviewId") or "").strip()
if not re.fullmatch(r"[0-9]{1,20}", review_id):
return None
if str(raw.get("provider") or "") != target.provider:
return None
if str(raw.get("targetId") or "") != target.id:
return None
if str(raw.get("repo") or "") != target.repo:
return None
if str(raw.get("baseBranch") or "") != target.branch:
return None
if str(raw.get("reviewKey") or "") != key:
return None
hinted_branch = str(raw.get("reviewBranch") or branch)
if target.provider != "huggingface" and hinted_branch != branch:
return None
return ReviewReceipt(
provider=target.provider,
target_id=target.id,
repo=target.repo,
base_branch=target.branch,
review_branch=branch,
review_key=key,
review_id=review_id,
review_url=str(raw.get("reviewUrl") or "")[:2048],
status=str(raw.get("status") or "unknown").lower(),
record_id=str(review_hint.get("recordId") or ""),
path=str((review_hint.get("paths") or {}).get(target.id) or ""),
)
async def _refresh_review_target( # ruff: ignore[too-many-branches, too-many-return-statements]
self,
target: StorageTarget,
review: ReviewReceipt,
) -> ReviewReceipt | None:
"""Refresh one known review directly by provider-native review ID."""
if target.provider == "huggingface":
try:
from huggingface_hub import HfApi # noqa: PLC0415
api = HfApi(token=target.token)
details = await asyncio.to_thread(
_with_bounded_hf_client,
lambda: api.get_discussion_details(
target.repo, int(review.review_id), repo_type="dataset"
),
)
if not bool(getattr(details, "is_pull_request", False)):
return None
return replace(
review,
review_url=str(getattr(details, "url", "") or review.review_url)[
:2048
],
status=str(
getattr(details, "status", "unknown") or "unknown"
).lower(),
)
except ValueError:
return None
except Exception as exc: # noqa: BLE001
status = getattr(getattr(exc, "response", None), "status_code", None)
if status == 404: # ruff: ignore[magic-value-comparison]
return None
raise StorageWriteError(
"HF_REVIEW_LOOKUP",
transient=status is None or status in _TRANSIENT_STATUS,
) from exc
if target.provider == "github":
owner, repo = _repo_parts(target.repo)
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
status, data = await self._request_bounded_json(
"GET",
f"https://api.github.com/repos/{quote(owner)}/{quote(repo)}/pulls/{quote(review.review_id)}",
headers=headers,
timeout=15.0,
)
if status == 404: # ruff: ignore[magic-value-comparison]
return None
if status != 200: # ruff: ignore[magic-value-comparison]
raise StorageWriteError(
"GITHUB_REVIEW_LOOKUP", transient=status in _TRANSIENT_STATUS
)
data = data or {}
state = (
"merged"
if data.get("merged_at")
else (
"draft"
if data.get("draft") and data.get("state") == "open"
else str(data.get("state") or "unknown")
)
)
return replace(
review,
review_url=str(data.get("html_url") or review.review_url)[:2048],
status=state,
)
if target.provider == "gitlab":
base = target.api_base or "https://gitlab.com/api/v4"
project = quote(target.repo, safe="")
headers = {"PRIVATE-TOKEN": target.token}
status, data = await self._request_bounded_json(
"GET",
f"{base}/projects/{project}/merge_requests/{quote(review.review_id)}",
headers=headers,
timeout=15.0,
)
if status == 404: # ruff: ignore[magic-value-comparison]
return None
if status != 200: # ruff: ignore[magic-value-comparison]
raise StorageWriteError(
"GITLAB_REVIEW_LOOKUP", transient=status in _TRANSIENT_STATUS
)
data = data or {}
state = str(data.get("state") or "unknown")
if state == "opened":
state = "open"
return replace(
review,
review_url=str(data.get("web_url") or review.review_url)[:2048],
status=state,
)
workspace, repo = _repo_parts(target.repo)
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/json",
}
status, data = await self._request_bounded_json(
"GET",
f"https://api.bitbucket.org/2.0/repositories/{quote(workspace)}/{quote(repo)}/pullrequests/{quote(review.review_id)}",
headers=headers,
timeout=15.0,
)
if status == 404: # ruff: ignore[magic-value-comparison]
return None
if status != 200: # ruff: ignore[magic-value-comparison]
raise StorageWriteError(
"BITBUCKET_REVIEW_LOOKUP", transient=status in _TRANSIENT_STATUS
)
data = data or {}
state = str(data.get("state") or "unknown").upper()
mapped = {
"OPEN": "open",
"MERGED": "merged",
"DECLINED": "closed",
"SUPERSEDED": "closed",
}.get(state, state.lower())
html_url = ((data.get("links") or {}).get("html") or {}).get(
"href"
) or review.review_url
return replace(review, review_url=str(html_url)[:2048], status=mapped)
async def _open_review_target(
self,
target: StorageTarget,
*,
branch: str,
key: str,
title: str,
path: str,
record_id: str,
content: bytes,
message: str,
description: str | None = None,
) -> ReviewReceipt:
existing = await self._discover_review_target(
target, branch=branch, key=key, title=title
)
if existing is not None:
return replace(existing, record_id=record_id, path=path)
if target.provider == "huggingface":
return await self._open_hf_review(
target,
branch,
key,
title,
path,
record_id,
content,
message,
description,
)
if target.provider == "github":
return await self._open_github_review(
target,
branch,
key,
title,
path,
record_id,
content,
message,
description,
)
if target.provider == "gitlab":
return await self._open_gitlab_review(
target,
branch,
key,
title,
path,
record_id,
content,
message,
description,
)
return await self._open_bitbucket_review(
target, branch, key, title, path, record_id, content, message
)
async def _update_review_target(
self,
target: StorageTarget,
*,
review: ReviewReceipt,
path: str,
content: bytes,
message: str,
) -> None:
if target.provider == "huggingface":
await self._write_hf(
replace(target, branch=f"refs/pr/{review.review_id}"),
path,
content,
message,
)
return
if target.provider == "github":
await self._write_github_review_update(
replace(target, branch=review.review_branch), path, content, message
)
return
if target.provider == "gitlab":
await self._write_gitlab_review_update(
replace(target, branch=review.review_branch), path, content, message
)
return
await self._write_bitbucket(
replace(target, branch=review.review_branch), path, content, message
)
async def _discover_review_target(
self,
target: StorageTarget,
*,
branch: str,
key: str,
title: str,
) -> ReviewReceipt | None:
if target.provider == "huggingface":
return await self._discover_hf_review(target, branch, key, title)
if target.provider == "github":
return await self._discover_github_review(target, branch, key, title)
if target.provider == "gitlab":
return await self._discover_gitlab_review(target, branch, key, title)
return await self._discover_bitbucket_review(target, branch, key, title)
@staticmethod
def _review_receipt(
target: StorageTarget,
*,
branch: str,
key: str,
review_id: Any,
review_url: Any,
status: str,
record_id: str = "",
path: str = "",
) -> ReviewReceipt:
return ReviewReceipt(
provider=target.provider,
target_id=target.id,
repo=target.repo,
base_branch=target.branch,
review_branch=branch,
review_key=key,
review_id=str(review_id or ""),
review_url=str(review_url or "")[:2048],
status=str(status or "unknown").lower(),
record_id=record_id,
path=path,
)
async def _open_hf_review( # ruff: ignore[too-many-positional-arguments]
self,
target,
branch,
key,
title,
path,
record_id,
content,
message,
description=None,
):
try:
from huggingface_hub import CommitOperationAdd, HfApi # noqa: PLC0415
api = HfApi(token=target.token)
info = await asyncio.to_thread(
_with_bounded_hf_client,
lambda: api.create_commit(
repo_id=target.repo,
repo_type="dataset",
revision=target.branch,
operations=[
CommitOperationAdd(path_in_repo=path, path_or_fileobj=content)
],
commit_message=f"{title} · revision 1",
commit_description=description
or review_description_for(key, target.branch),
create_pr=True,
),
)
url = str(getattr(info, "pr_url", "") or "")
match = re.search(r"/(?:discussions|pulls?)/(\d+)(?:[/?#]|$)", url)
rid = match.group(1) if match else ""
if not rid:
found = await self._discover_hf_review(target, branch, key, title)
if found is None:
raise StorageWriteError("HF_REVIEW_DISCOVERY", transient=True)
return replace(found, record_id=record_id, path=path)
return self._review_receipt(
target,
branch=branch,
key=key,
review_id=rid,
review_url=url,
status="open",
record_id=record_id,
path=path,
)
except StorageWriteError:
raise
except Exception as exc: # noqa: BLE001
status = getattr(getattr(exc, "response", None), "status_code", None)
raise StorageWriteError(
"HF_REVIEW_OPEN",
transient=status is None or status in _TRANSIENT_STATUS,
) from exc
async def _discover_hf_review(self, target, branch, key, title):
try:
from huggingface_hub import HfApi # noqa: PLC0415
api = HfApi(token=target.token)
def _scan():
out = []
for i, item in enumerate(
api.get_repo_discussions(
target.repo, repo_type="dataset", discussion_type="pull_request"
)
):
# Legacy/unbound receipts only. New reviews persist their
# provider-native review ID and use direct lookup instead.
if i >= 1000: # ruff: ignore[magic-value-comparison]
break
out.append(item)
return out
items = await asyncio.to_thread(_with_bounded_hf_client, _scan)
for item in items:
if getattr(item, "title", "") == title and bool(
getattr(item, "is_pull_request", False)
):
num = int(getattr(item, "num", 0) or 0)
url = f"https://huggingface.co/datasets/{target.repo}/discussions/{num}"
return self._review_receipt(
target,
branch=branch,
key=key,
review_id=num,
review_url=url,
status=str(getattr(item, "status", "unknown")),
)
return None
except Exception as exc: # noqa: BLE001
status = getattr(getattr(exc, "response", None), "status_code", None)
raise StorageWriteError(
"HF_REVIEW_LOOKUP",
transient=status is None or status in _TRANSIENT_STATUS,
) from exc
async def _open_github_review( # ruff: ignore[too-many-positional-arguments]
self,
target,
branch,
key,
title,
path,
record_id,
content,
message,
description=None,
):
owner, repo = _repo_parts(target.repo)
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
ref_url = f"https://api.github.com/repos/{quote(owner)}/{quote(repo)}/git/ref/heads/{quote(target.branch, safe='')}"
status, data = await self._request_bounded_json(
"GET", ref_url, headers=headers, timeout=15.0
)
if status != 200: # ruff: ignore[magic-value-comparison]
raise StorageWriteError(
"GITHUB_REVIEW_BASE", transient=status in _TRANSIENT_STATUS
)
sha = str(((data or {}).get("object") or {}).get("sha") or "")
if not sha:
raise StorageWriteError("GITHUB_REVIEW_BASE_SHA")
create_ref = (
f"https://api.github.com/repos/{quote(owner)}/{quote(repo)}/git/refs"
)
status = await self._request_no_body(
"POST",
create_ref,
headers=headers,
json={"ref": f"refs/heads/{branch}", "sha": sha},
timeout=15.0,
)
if status not in {201, 422}:
raise StorageWriteError(
"GITHUB_REVIEW_BRANCH", transient=status in _TRANSIENT_STATUS
)
await self._write_github(
replace(target, branch=branch), path, content, f"{title} · revision 1"
)
pulls = f"https://api.github.com/repos/{quote(owner)}/{quote(repo)}/pulls"
status, data = await self._request_bounded_json(
"POST",
pulls,
headers=headers,
json={
"title": title,
"head": branch,
"base": target.branch,
"body": description or review_description_for(key, target.branch),
},
timeout=20.0,
)
if status == 201: # ruff: ignore[magic-value-comparison]
return self._review_receipt(
target,
branch=branch,
key=key,
review_id=(data or {}).get("number"),
review_url=(data or {}).get("html_url"),
status="open",
record_id=record_id,
path=path,
)
if status in {409, 422}:
found = await self._discover_github_review(target, branch, key, title)
if found is not None:
return replace(found, record_id=record_id, path=path)
raise StorageWriteError(
"GITHUB_REVIEW_OPEN", transient=status in _TRANSIENT_STATUS
)
async def _discover_github_review(self, target, branch, key, title):
owner, repo = _repo_parts(target.repo)
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
url = f"https://api.github.com/repos/{quote(owner)}/{quote(repo)}/pulls"
status, data = await self._request_bounded_json(
"GET",
url,
headers=headers,
params={
"state": "all",
"head": f"{owner}:{branch}",
"base": target.branch,
"per_page": 10,
},
timeout=15.0,
)
if status != 200: # ruff: ignore[magic-value-comparison]
raise StorageWriteError(
"GITHUB_REVIEW_LOOKUP", transient=status in _TRANSIENT_STATUS
)
for item in data if isinstance(data, list) else []:
if str((item.get("head") or {}).get("ref") or "") != branch:
continue
state = (
"merged"
if item.get("merged_at")
else (
"draft"
if item.get("draft") and item.get("state") == "open"
else str(item.get("state") or "unknown")
)
)
return self._review_receipt(
target,
branch=branch,
key=key,
review_id=item.get("number"),
review_url=item.get("html_url"),
status=state,
)
return None
async def _open_gitlab_review( # ruff: ignore[too-many-positional-arguments]
self,
target,
branch,
key,
title,
path,
record_id,
content,
message,
description=None,
):
base = target.api_base or "https://gitlab.com/api/v4"
project = quote(target.repo, safe="")
headers = {"PRIVATE-TOKEN": target.token}
branch_url = f"{base}/projects/{project}/repository/branches"
status = await self._request_no_body(
"POST",
branch_url,
headers=headers,
params={"branch": branch, "ref": target.branch},
timeout=15.0,
)
if status not in {201, 400}:
raise StorageWriteError(
"GITLAB_REVIEW_BRANCH", transient=status in _TRANSIENT_STATUS
)
await self._write_gitlab(
replace(target, branch=branch), path, content, f"{title} · revision 1"
)
mr_url = f"{base}/projects/{project}/merge_requests"
status, data = await self._request_bounded_json(
"POST",
mr_url,
headers=headers,
json={
"source_branch": branch,
"target_branch": target.branch,
"title": title,
"description": (
description or review_description_for(key, target.branch)
),
"remove_source_branch": True,
},
timeout=20.0,
)
if status == 201: # ruff: ignore[magic-value-comparison]
return self._review_receipt(
target,
branch=branch,
key=key,
review_id=(data or {}).get("iid"),
review_url=(data or {}).get("web_url"),
status="open",
record_id=record_id,
path=path,
)
if status in {400, 409}:
found = await self._discover_gitlab_review(target, branch, key, title)
if found is not None:
return replace(found, record_id=record_id, path=path)
raise StorageWriteError(
"GITLAB_REVIEW_OPEN", transient=status in _TRANSIENT_STATUS
)
async def _discover_gitlab_review(self, target, branch, key, title):
base = target.api_base or "https://gitlab.com/api/v4"
project = quote(target.repo, safe="")
headers = {"PRIVATE-TOKEN": target.token}
url = f"{base}/projects/{project}/merge_requests"
status, data = await self._request_bounded_json(
"GET",
url,
headers=headers,
params={
"scope": "all",
"state": "all",
"source_branch": branch,
"target_branch": target.branch,
"per_page": 20,
},
timeout=15.0,
)
if status != 200: # ruff: ignore[magic-value-comparison]
raise StorageWriteError(
"GITLAB_REVIEW_LOOKUP", transient=status in _TRANSIENT_STATUS
)
for item in data if isinstance(data, list) else []:
if str(item.get("source_branch") or "") != branch:
continue
state = str(item.get("state") or "unknown")
if state == "opened":
state = "open"
return self._review_receipt(
target,
branch=branch,
key=key,
review_id=item.get("iid"),
review_url=item.get("web_url"),
status=state,
)
return None
async def _open_bitbucket_review( # ruff: ignore[too-many-positional-arguments]
self,
target,
branch,
key,
title,
path,
record_id,
content,
message,
description=None,
):
workspace, repo = _repo_parts(target.repo)
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/json",
}
branch_url = f"https://api.bitbucket.org/2.0/repositories/{quote(workspace)}/{quote(repo)}/refs/branches"
status = await self._request_no_body(
"POST",
branch_url,
headers=headers,
json={"name": branch, "target": {"hash": target.branch}},
timeout=15.0,
)
if status not in {201, 400}:
raise StorageWriteError(
"BITBUCKET_REVIEW_BRANCH", transient=status in _TRANSIENT_STATUS
)
await self._write_bitbucket(
replace(target, branch=branch), path, content, f"{title} · revision 1"
)
pr_url = f"https://api.bitbucket.org/2.0/repositories/{quote(workspace)}/{quote(repo)}/pullrequests"
status, data = await self._request_bounded_json(
"POST",
pr_url,
headers=headers,
json={
"title": title,
"source": {"branch": {"name": branch}},
"destination": {"branch": {"name": target.branch}},
"close_source_branch": True,
"description": (
description or review_description_for(key, target.branch, "decline")
),
},
timeout=20.0,
)
if status == 201: # ruff: ignore[magic-value-comparison]
html_url = (((data or {}).get("links") or {}).get("html") or {}).get(
"href"
) or ""
return self._review_receipt(
target,
branch=branch,
key=key,
review_id=(data or {}).get("id"),
review_url=html_url,
status="open",
record_id=record_id,
path=path,
)
if status in {400, 409}:
found = await self._discover_bitbucket_review(target, branch, key, title)
if found is not None:
return replace(found, record_id=record_id, path=path)
raise StorageWriteError(
"BITBUCKET_REVIEW_OPEN", transient=status in _TRANSIENT_STATUS
)
async def _discover_bitbucket_review(self, target, branch, key, title):
workspace, repo = _repo_parts(target.repo)
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/json",
}
url = f"https://api.bitbucket.org/2.0/repositories/{quote(workspace)}/{quote(repo)}/pullrequests"
query = f'source.branch.name="{branch}"'
for state in ("OPEN", "MERGED", "DECLINED", "SUPERSEDED"):
status, data = await self._request_bounded_json(
"GET",
url,
headers=headers,
params={"state": state, "q": query, "pagelen": 10},
timeout=15.0,
)
if status != 200: # ruff: ignore[magic-value-comparison]
raise StorageWriteError(
"BITBUCKET_REVIEW_LOOKUP", transient=status in _TRANSIENT_STATUS
)
for item in (
(data or {}).get("values", []) if isinstance(data, dict) else []
):
if (
str(
((item.get("source") or {}).get("branch") or {}).get("name")
or ""
)
!= branch
):
continue
raw = str(item.get("state") or state).upper()
mapped = {
"OPEN": "open",
"MERGED": "merged",
"DECLINED": "closed",
"SUPERSEDED": "closed",
}.get(raw, "unknown")
html_url = ((item.get("links") or {}).get("html") or {}).get(
"href"
) or ""
return self._review_receipt(
target,
branch=branch,
key=key,
review_id=item.get("id"),
review_url=html_url,
status=mapped,
)
return None
async def _close_review_target(
self, target: StorageTarget, review: ReviewReceipt
) -> None:
if target.provider == "huggingface":
try:
from huggingface_hub import HfApi # noqa: PLC0415
api = HfApi(token=target.token)
await asyncio.to_thread(
_with_bounded_hf_client,
lambda: api.change_discussion_status(
target.repo,
int(review.review_id),
"closed",
repo_type="dataset",
),
)
return
except Exception as exc: # noqa: BLE001
raise StorageWriteError("HF_REVIEW_CLOSE", transient=True) from exc
if target.provider == "github":
owner, repo = _repo_parts(target.repo)
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
url = f"https://api.github.com/repos/{quote(owner)}/{quote(repo)}/pulls/{quote(review.review_id)}"
status = await self._request_no_body(
"PATCH", url, headers=headers, json={"state": "closed"}, timeout=15.0
)
elif target.provider == "gitlab":
base = target.api_base or "https://gitlab.com/api/v4"
project = quote(target.repo, safe="")
headers = {"PRIVATE-TOKEN": target.token}
url = f"{base}/projects/{project}/merge_requests/{quote(review.review_id)}"
status = await self._request_no_body(
"PUT", url, headers=headers, json={"state_event": "close"}, timeout=15.0
)
else:
workspace, repo = _repo_parts(target.repo)
headers = {"Authorization": f"Bearer {target.token}"}
url = f"https://api.bitbucket.org/2.0/repositories/{quote(workspace)}/{quote(repo)}/pullrequests/{quote(review.review_id)}/decline"
status = await self._request_no_body(
"POST", url, headers=headers, timeout=15.0
)
if status not in {200, 201, 204}:
raise StorageWriteError(
f"{target.provider.upper()}_REVIEW_CLOSE",
transient=status in _TRANSIENT_STATUS,
)
async def _merge_review_target(
self, target: StorageTarget, review: ReviewReceipt
) -> None:
if target.provider == "huggingface":
try:
from huggingface_hub import HfApi # noqa: PLC0415
api = HfApi(token=target.token)
await asyncio.to_thread(
_with_bounded_hf_client,
lambda: api.merge_pull_request(
target.repo, int(review.review_id), repo_type="dataset"
),
)
return
except Exception as exc: # noqa: BLE001
raise StorageWriteError("HF_REVIEW_MERGE", transient=True) from exc
if target.provider == "github":
owner, repo = _repo_parts(target.repo)
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
url = f"https://api.github.com/repos/{quote(owner)}/{quote(repo)}/pulls/{quote(review.review_id)}/merge"
status = await self._request_no_body(
"PUT",
url,
headers=headers,
json={
"commit_title": f"Merge dataset contribution {review.review_key}"
},
timeout=20.0,
)
elif target.provider == "gitlab":
base = target.api_base or "https://gitlab.com/api/v4"
project = quote(target.repo, safe="")
headers = {"PRIVATE-TOKEN": target.token}
url = f"{base}/projects/{project}/merge_requests/{quote(review.review_id)}/merge"
status = await self._request_no_body(
"PUT", url, headers=headers, timeout=20.0
)
else:
workspace, repo = _repo_parts(target.repo)
headers = {"Authorization": f"Bearer {target.token}"}
url = f"https://api.bitbucket.org/2.0/repositories/{quote(workspace)}/{quote(repo)}/pullrequests/{quote(review.review_id)}/merge"
status = await self._request_no_body(
"POST", url, headers=headers, timeout=20.0
)
if status not in {200, 201, 202}:
raise StorageWriteError(
f"{target.provider.upper()}_REVIEW_MERGE",
transient=status in _TRANSIENT_STATUS,
)
async def _delete_review_branch(self, target: StorageTarget, branch: str) -> None:
try:
if target.provider == "huggingface":
# HF pull requests use refs/pr/* rather than ordinary source branches.
return
if target.provider == "github":
owner, repo = _repo_parts(target.repo)
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
url = f"https://api.github.com/repos/{quote(owner)}/{quote(repo)}/git/refs/heads/{quote(branch, safe='')}"
elif target.provider == "gitlab":
base = target.api_base or "https://gitlab.com/api/v4"
project = quote(target.repo, safe="")
headers = {"PRIVATE-TOKEN": target.token}
url = f"{base}/projects/{project}/repository/branches/{quote(branch, safe='')}"
else:
workspace, repo = _repo_parts(target.repo)
headers = {"Authorization": f"Bearer {target.token}"}
url = f"https://api.bitbucket.org/2.0/repositories/{quote(workspace)}/{quote(repo)}/refs/branches/{quote(branch, safe='')}"
status = await self._request_no_body(
"DELETE", url, headers=headers, timeout=15.0
)
if status not in {200, 204, 404}:
raise StorageWriteError(
f"{target.provider.upper()}_REVIEW_BRANCH_DELETE",
transient=status in _TRANSIENT_STATUS,
)
except StorageWriteError:
raise
except Exception: # ruff: ignore[blind-except]
# Source-branch cleanup is hygiene, not review-state authority.
return
async def remove_current_view(
self,
paths: dict[str, str],
*,
record_id: str | None = None,
commit_message: str = "Withdraw reviewed contribution from current branch view",
) -> dict[str, str]:
"""
Best-effort remove previously written record files from current branches.
This operation intentionally returns per-target status and never claims
physical erasure. All bundled providers are versioned repositories; a
deletion commit removes the current branch view while prior Git/provider
history may retain the original bytes.
"""
results: dict[str, str] = {}
if record_id:
self._suppressed_retry_record_ids.add(str(record_id))
for target in self.targets:
path = str((paths or {}).get(target.id) or "")
if not path:
results[target.id] = "unknown-path"
continue
try:
results[target.id] = await self._delete_target_current_view(
target, path, commit_message
)
except StorageWriteError:
results[target.id] = "degraded"
return results
async def _delete_target_current_view(
self, target: StorageTarget, path: str, message: str
) -> str:
if not target.token:
raise StorageWriteError("MISSING_TOKEN")
if (
target.provider == "huggingface"
and _normalize_token_type(target.token_type) == "read"
):
raise StorageWriteError("READ_TOKEN")
async with self._locks[target.id]:
if target.provider == "huggingface":
return await self._delete_hf(target, path, message)
if target.provider == "github":
return await self._delete_github(target, path, message)
if target.provider == "gitlab":
return await self._delete_gitlab(target, path, message)
return await self._delete_bitbucket(target, path, message)
async def _delete_hf(self, target: StorageTarget, path: str, message: str) -> str:
try:
from huggingface_hub import CommitOperationDelete, HfApi # noqa: PLC0415
api = HfApi(token=target.token)
await asyncio.to_thread(
_with_bounded_hf_client,
lambda: api.create_commit(
repo_id=target.repo,
repo_type="dataset",
revision=target.branch,
operations=[CommitOperationDelete(path_in_repo=path)],
commit_message=message,
),
)
return "removed-current-view"
except Exception as exc: # noqa: BLE001
status = getattr(getattr(exc, "response", None), "status_code", None)
if status == 404: # ruff: ignore[magic-value-comparison]
return "already-absent"
raise StorageWriteError(
"HF_DELETE", transient=status in _TRANSIENT_STATUS
) from exc
async def _delete_github(
self, target: StorageTarget, path: str, message: str
) -> str:
owner, repo = _repo_parts(target.repo)
url = f"https://api.github.com/repos/{quote(owner)}/{quote(repo)}/contents/{quote(path, safe='/')}"
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
current_status, current_json = await self._request_bounded_json(
"GET", url, headers=headers, params={"ref": target.branch}, timeout=15.0
)
if current_status == 404: # ruff: ignore[magic-value-comparison]
return "already-absent"
if current_status != 200: # ruff: ignore[magic-value-comparison]
raise StorageWriteError(
"GITHUB_DELETE_LOOKUP", transient=current_status in _TRANSIENT_STATUS
)
sha = str((current_json or {}).get("sha") or "")
if not sha:
raise StorageWriteError("GITHUB_DELETE_SHA")
status = await self._request_no_body(
"DELETE",
url,
headers=headers,
json={"message": message, "sha": sha, "branch": target.branch},
timeout=20.0,
)
if status in {200, 204}:
return "removed-current-view"
if status == 404: # ruff: ignore[magic-value-comparison]
return "already-absent"
raise StorageWriteError("GITHUB_DELETE", transient=status in _TRANSIENT_STATUS)
async def _delete_gitlab(
self, target: StorageTarget, path: str, message: str
) -> str:
base = target.api_base or "https://gitlab.com/api/v4"
project = quote(target.repo, safe="")
file_path = quote(path, safe="")
url = f"{base}/projects/{project}/repository/files/{file_path}"
headers = {"PRIVATE-TOKEN": target.token}
status = await self._request_no_body(
"DELETE",
url,
headers=headers,
json={"branch": target.branch, "commit_message": message},
timeout=20.0,
)
if status in {200, 204}:
return "removed-current-view"
if status == 404: # ruff: ignore[magic-value-comparison]
return "already-absent"
raise StorageWriteError("GITLAB_DELETE", transient=status in _TRANSIENT_STATUS)
async def _delete_bitbucket(
self, target: StorageTarget, path: str, message: str
) -> str:
workspace, repo = _repo_parts(target.repo)
read_url = (
f"https://api.bitbucket.org/2.0/repositories/{quote(workspace)}/{quote(repo)}"
f"/src/{quote(target.branch, safe='')}/{quote(path, safe='/')}"
)
headers = {"Authorization": f"Bearer {target.token}"}
current_status = await self._request_no_body(
"GET", read_url, headers=headers, timeout=15.0
)
if current_status == 404: # ruff: ignore[magic-value-comparison]
return "already-absent"
if current_status != 200: # ruff: ignore[magic-value-comparison]
raise StorageWriteError(
"BITBUCKET_DELETE_LOOKUP", transient=current_status in _TRANSIENT_STATUS
)
url = f"https://api.bitbucket.org/2.0/repositories/{quote(workspace)}/{quote(repo)}/src"
form = urlencode(
[("branch", target.branch), ("message", message), ("files", "/" + path)]
)
delete_headers = {
**headers,
"Content-Type": "application/x-www-form-urlencoded",
}
status = await self._request_no_body(
"POST",
url,
headers=delete_headers,
content=form.encode("utf-8"),
timeout=25.0,
)
if status in {200, 201}:
return "removed-current-view"
raise StorageWriteError(
"BITBUCKET_DELETE", transient=status in _TRANSIENT_STATUS
)
async def _dispatch(
self, target: StorageTarget, path: str, content: bytes, message: str
) -> None:
try:
if target.provider == "huggingface":
await self._write_hf(target, path, content, message)
elif target.provider == "github":
await self._write_github(target, path, content, message)
elif target.provider == "gitlab":
await self._write_gitlab(target, path, content, message)
else:
await self._write_bitbucket(target, path, content, message)
except StorageWriteError:
raise
except (httpx.HTTPError, OSError, TimeoutError) as exc:
# A transport failure can happen after the provider accepted the
# mutation but before this process received the response. Treat the
# outcome as ambiguous so the contribution lifecycle fails safe to
# reconciliation instead of reopening quarantine/re-promotion.
raise StorageWriteError(
f"{target.provider.upper()}_TRANSPORT", transient=True
) from exc
async def _write_hf(
self, target: StorageTarget, path: str, content: bytes, message: str
) -> None:
try:
from huggingface_hub import CommitOperationAdd, HfApi # noqa: PLC0415
api = HfApi(token=target.token)
await asyncio.to_thread(
_with_bounded_hf_client,
lambda: api.create_commit(
repo_id=target.repo,
repo_type="dataset",
revision=target.branch,
operations=[
CommitOperationAdd(path_in_repo=path, path_or_fileobj=content)
],
commit_message=message,
),
)
except Exception as exc: # noqa: BLE001
# Keep the exception private. HTTP permission/transient distinction is
# intentionally coarse here because huggingface_hub exception classes
# differ across supported versions.
status = getattr(getattr(exc, "response", None), "status_code", None)
text = str(status or "")
# No response status means the commit outcome is unknown (timeout,
# connection reset, provider client transport failure). Conservatively
# classify that as ambiguous/transient rather than retry-safe failure.
transient = status is None or text in {
"408",
"409",
"425",
"429",
"500",
"502",
"503",
"504",
}
raise StorageWriteError("HF_WRITE", transient=transient) from exc
def _client(self) -> httpx.AsyncClient:
if self.client is None:
raise StorageWriteError("NO_HTTP_CLIENT", transient=True)
return self.client
async def _request_no_body(self, method: str, url: str, **kwargs: Any) -> int:
client = self._client()
request = client.build_request(method, url, **kwargs)
response = await client.send(request, stream=True)
try:
return response.status_code
finally:
await response.aclose()
async def _request_bounded_json(
self, method: str, url: str, **kwargs: Any
) -> tuple[int, Any]:
client = self._client()
request = client.build_request(method, url, **kwargs)
response = await client.send(request, stream=True)
try:
limit = _control_response_limit()
declared = response.headers.get("content-length")
if declared and declared.isdigit() and int(declared) > limit:
raise StorageWriteError("PROVIDER_RESPONSE_TOO_LARGE")
buf = bytearray()
async for chunk in response.aiter_bytes():
buf.extend(chunk)
if len(buf) > limit:
raise StorageWriteError("PROVIDER_RESPONSE_TOO_LARGE")
if not buf:
payload = {}
else:
try:
payload = json.loads(bytes(buf))
except Exception as exc:
raise StorageWriteError("PROVIDER_RESPONSE_JSON") from exc
return response.status_code, payload
finally:
await response.aclose()
async def _write_github(
self, target: StorageTarget, path: str, content: bytes, message: str
) -> None:
owner, repo = _repo_parts(target.repo)
url = f"https://api.github.com/repos/{quote(owner)}/{quote(repo)}/contents/{quote(path, safe='/')}"
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
payload = {
"message": message,
"content": base64.b64encode(content).decode("ascii"),
"branch": target.branch,
}
status = await self._request_no_body(
"PUT", url, headers=headers, json=payload, timeout=20.0
)
if status in {200, 201}:
return
# A retry may encounter an already-created idempotent path. Confirm
# content equality before treating the conflict as success.
if status in {409, 422}:
get_status, get_json = await self._request_bounded_json(
"GET", url, headers=headers, params={"ref": target.branch}, timeout=15.0
)
if get_status == 200: # ruff: ignore[magic-value-comparison]
try:
existing = base64.b64decode(
(get_json.get("content") or "").replace("\n", "")
)
if existing == content:
return
except Exception: # ruff: ignore[blind-except]
pass
raise StorageWriteError("GITHUB_WRITE", transient=status in _TRANSIENT_STATUS)
async def _write_github_review_update(
self, target: StorageTarget, path: str, content: bytes, message: str
) -> None:
owner, repo = _repo_parts(target.repo)
url = f"https://api.github.com/repos/{quote(owner)}/{quote(repo)}/contents/{quote(path, safe='/')}"
headers = {
"Authorization": f"Bearer {target.token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
status, current = await self._request_bounded_json(
"GET", url, headers=headers, params={"ref": target.branch}, timeout=15.0
)
if status not in {200, 404}:
raise StorageWriteError(
"GITHUB_REVIEW_UPDATE_LOOKUP", transient=status in _TRANSIENT_STATUS
)
payload = {
"message": message,
"content": base64.b64encode(content).decode("ascii"),
"branch": target.branch,
}
if status == 200: # ruff: ignore[magic-value-comparison]
sha = str((current or {}).get("sha") or "")
if not sha:
raise StorageWriteError("GITHUB_REVIEW_UPDATE_SHA")
payload["sha"] = sha
write_status = await self._request_no_body(
"PUT", url, headers=headers, json=payload, timeout=20.0
)
if write_status not in {200, 201}:
raise StorageWriteError(
"GITHUB_REVIEW_UPDATE", transient=write_status in _TRANSIENT_STATUS
)
async def _write_gitlab_review_update(
self, target: StorageTarget, path: str, content: bytes, message: str
) -> None:
base = target.api_base or "https://gitlab.com/api/v4"
project = quote(target.repo, safe="")
file_path = quote(path, safe="")
url = f"{base}/projects/{project}/repository/files/{file_path}"
headers = {"PRIVATE-TOKEN": target.token}
get_status, _ = await self._request_bounded_json(
"GET", url, headers=headers, params={"ref": target.branch}, timeout=15.0
)
if get_status not in {200, 404}:
raise StorageWriteError(
"GITLAB_REVIEW_UPDATE_LOOKUP", transient=get_status in _TRANSIENT_STATUS
)
payload = {
"branch": target.branch,
"commit_message": message,
"content": content.decode("utf-8"),
}
method = (
"PUT"
if get_status == 200 # ruff: ignore[magic-value-comparison]
else "POST"
)
write_status = await self._request_no_body(
method, url, headers=headers, json=payload, timeout=20.0
)
if write_status not in {200, 201}:
raise StorageWriteError(
"GITLAB_REVIEW_UPDATE", transient=write_status in _TRANSIENT_STATUS
)
async def _write_gitlab(
self, target: StorageTarget, path: str, content: bytes, message: str
) -> None:
base = target.api_base or "https://gitlab.com/api/v4"
project = quote(target.repo, safe="")
file_path = quote(path, safe="")
url = f"{base}/projects/{project}/repository/files/{file_path}"
headers = {"PRIVATE-TOKEN": target.token}
payload = {
"branch": target.branch,
"commit_message": message,
"content": content.decode("utf-8"),
}
status = await self._request_no_body(
"POST", url, headers=headers, json=payload, timeout=20.0
)
if status in {200, 201}:
return
if status == 400: # ruff: ignore[magic-value-comparison]
get_status, get_json = await self._request_bounded_json(
"GET", url, headers=headers, params={"ref": target.branch}, timeout=15.0
)
if get_status == 200: # ruff: ignore[magic-value-comparison]
try:
if (
get_json.get("content_sha256")
== hashlib.sha256(content).hexdigest()
):
return
except Exception: # ruff: ignore[blind-except]
pass
raise StorageWriteError("GITLAB_WRITE", transient=status in _TRANSIENT_STATUS)
async def _write_bitbucket(
self, target: StorageTarget, path: str, content: bytes, message: str
) -> None:
workspace, repo = _repo_parts(target.repo)
url = f"https://api.bitbucket.org/2.0/repositories/{quote(workspace)}/{quote(repo)}/src"
headers = {"Authorization": f"Bearer {target.token}"}
files = {"/" + path: (path.rsplit("/", 1)[-1], content, "application/x-ndjson")}
data = {"branch": target.branch, "message": message}
status = await self._request_no_body(
"POST", url, headers=headers, data=data, files=files, timeout=25.0
)
if status in {200, 201}:
return
raise StorageWriteError(
"BITBUCKET_WRITE", transient=status in _TRANSIENT_STATUS
)
|