diagnostics_upload.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """Client for uploading ``hermes debug share`` bundles to Nous-internal S3.
  2. 1. POST {NAS_BASE}/api/diagnostics/upload-url → {uploadUrl, viewUrl, id, ...}; the body carries ``sizeBytes``,
  3. which NAS signs into the presigned URL's ``ContentLength``, so the PUT must send exactly that many bytes.
  4. 2. PUT <uploadUrl> (gzipped bundle, Content-Type application/gzip). NAS is stateless — no confirm step."""
  5. import json
  6. import os
  7. import urllib.request
  8. from hermes_cli.debug_redaction import redact_debug_support_text
  9. # Overridable via env so the feature can be pointed at staging / a local dev NAS instance.
  10. NAS_BASE = os.environ.get("HERMES_DIAGNOSTICS_BASE_URL", "https://portal.nousresearch.com")
  11. _REQUEST_TIMEOUT = 30
  12. _UPLOAD_TIMEOUT = 120 # the PUT carries the gzipped log bundle, so a more generous window
  13. _USER_AGENT = "hermes-agent/debug-share"
  14. def _urlopen_checked(url: str, data: bytes, method: str, headers: dict, *, timeout: int, what: str):
  15. """Send the request; raise ``RuntimeError`` on non-2xx and return the response body bytes."""
  16. req = urllib.request.Request(url, data=data, method=method, headers={**headers, "User-Agent": _USER_AGENT})
  17. with urllib.request.urlopen(req, timeout=timeout) as resp:
  18. status = getattr(resp, "status", None)
  19. status = resp.getcode() if status is None else status
  20. if not (200 <= status < 300):
  21. raise RuntimeError(f"{what} failed: HTTP {status}")
  22. return resp.read()
  23. def request_upload_url(content_type: str = "application/gzip", size_bytes: int | None = None) -> dict:
  24. """Ask NAS to mint a presigned PUT URL for a diagnostics bundle. Returns the parsed JSON, expected to
  25. carry at least ``uploadUrl``, ``viewUrl`` and ``id``. Raises on non-2xx responses or unparseable JSON."""
  26. payload: dict = {"contentType": content_type}
  27. if size_bytes is not None:
  28. payload["sizeBytes"] = int(size_bytes)
  29. body = _urlopen_checked(
  30. f"{NAS_BASE}/api/diagnostics/upload-url", json.dumps(payload).encode("utf-8"), "POST",
  31. {"Content-Type": "application/json", "Accept": "application/json"},
  32. timeout=_REQUEST_TIMEOUT, what="diagnostics upload-url request").decode("utf-8")
  33. try:
  34. result = json.loads(body)
  35. except (ValueError, json.JSONDecodeError) as exc:
  36. safe_body = redact_debug_support_text(body, max_chars=200)
  37. raise RuntimeError(f"diagnostics upload-url returned non-JSON response: {safe_body}") from exc
  38. if not isinstance(result, dict) or not result.get("uploadUrl"):
  39. safe_body = redact_debug_support_text(body, max_chars=200)
  40. raise RuntimeError(f"diagnostics upload-url response missing 'uploadUrl': {safe_body}")
  41. return result
  42. def put_bundle(upload_url: str, data: bytes, content_type: str = "application/gzip") -> None:
  43. """PUT the gzipped *data* bundle to a presigned *upload_url*. Raises on non-2xx.
  44. ``Content-Type`` must match what NAS pinned when signing the URL, otherwise S3 rejects the signature."""
  45. _urlopen_checked(upload_url, data, "PUT", {"Content-Type": content_type}, timeout=_UPLOAD_TIMEOUT,
  46. what="diagnostics bundle PUT")
  47. def share_to_nous(report_bundle: bytes) -> dict:
  48. """Mint a presigned PUT URL (with the exact ``sizeBytes`` NAS signs), then PUT *report_bundle*."""
  49. info = request_upload_url(content_type="application/gzip", size_bytes=len(report_bundle))
  50. put_bundle(info["uploadUrl"], report_bundle, content_type="application/gzip")
  51. return info