-
Notifications
You must be signed in to change notification settings - Fork 6
fix(byoc): send real capability + model_id for usage attribution #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seanhanca
wants to merge
2
commits into
livepeer:main
Choose a base branch
from
seanhanca:fix/byoc-usage-attribution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+329
−2
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| """ | ||
| Unit tests for BYOC usage attribution (real capability + model_id). | ||
|
|
||
| Root cause being fixed: ``_create_byoc_payment`` used to send only a bare | ||
| ``capability`` field (which the remote signer dropped) and no model id, so the | ||
| signer's ``create_signed_ticket`` metering event recorded | ||
| ``pipeline=live-video-to-video`` / ``model_id=unknown`` for every BYOC job. | ||
|
|
||
| These tests prove the additive wire contract on the gateway side: | ||
| - the ``/generate-live-payment`` body now carries the REAL ``capability`` and | ||
| ``model_id`` for a representative BYOC capability (e.g. ``nano-banana``); | ||
| - ``type`` stays ``"lv2v"`` (fee/pixel routing is unchanged); | ||
| - when no model id can be determined the field is omitted (backward-compatible, | ||
| byte-identical to today so the signer falls back). | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from contextlib import contextmanager | ||
| from unittest.mock import MagicMock, patch | ||
| from urllib.request import Request | ||
|
|
||
| from livepeer_gateway.byoc import ( | ||
| ByocJobRequest, | ||
| _create_byoc_payment, | ||
| _extract_model_id, | ||
| ) | ||
|
|
||
|
|
||
| def _stub_orch_info(): | ||
| info = MagicMock() | ||
| tp = MagicMock() | ||
| tp.face_value = b"\x01\x00" # non-zero → payment required | ||
| info.ticket_params = tp | ||
| info.HasField = lambda field: field == "ticket_params" | ||
| info.SerializeToString = lambda: b"stub-orch-info-protobuf" | ||
| return info | ||
|
|
||
|
|
||
| @contextmanager | ||
| def _mock_signer(*, payment_body=b'{"payment":"TICKETS","segCreds":"SEG"}'): | ||
| """Mock the signer HTTP call and capture the request sent to it.""" | ||
| captured: list[Request] = [] | ||
|
|
||
| class _MockResponse: | ||
| def __init__(self, body: bytes): | ||
| self._body = body | ||
|
|
||
| def read(self): | ||
| return self._body | ||
|
|
||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, *a): | ||
| return False | ||
|
|
||
| def _fake_urlopen(req, *args, **kwargs): | ||
| captured.append(req) | ||
| return _MockResponse(payment_body) | ||
|
|
||
| with patch("livepeer_gateway.byoc.urlopen", side_effect=_fake_urlopen), patch( | ||
| "livepeer_gateway.orch_info.get_orch_info", | ||
| side_effect=lambda *a, **k: _stub_orch_info(), | ||
| ): | ||
| yield captured | ||
|
|
||
|
|
||
| def _signer_body(captured: list[Request]) -> dict: | ||
| signer_reqs = [ | ||
| r for r in captured if "generate-live-payment" in r.full_url | ||
| ] | ||
| assert len(signer_reqs) == 1, f"expected 1 signer call, got {len(signer_reqs)}" | ||
| return json.loads(signer_reqs[0].data.decode("utf-8")) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # _extract_model_id | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_extract_model_id_from_payload_model_id_key(): | ||
| assert _extract_model_id({"model_id": "google/nano-banana"}) == "google/nano-banana" | ||
|
|
||
|
|
||
| def test_extract_model_id_from_payload_model_key(): | ||
| assert _extract_model_id({"prompt": "a cat", "model": "flux-dev"}) == "flux-dev" | ||
|
|
||
|
|
||
| def test_extract_model_id_prefers_payload_over_parameters(): | ||
| assert ( | ||
| _extract_model_id({"model_id": "from-payload"}, {"model_id": "from-params"}) | ||
| == "from-payload" | ||
| ) | ||
|
|
||
|
|
||
| def test_extract_model_id_falls_back_to_parameters(): | ||
| assert _extract_model_id({"prompt": "x"}, {"model": "from-params"}) == "from-params" | ||
|
|
||
|
|
||
| def test_extract_model_id_empty_when_absent(): | ||
| assert _extract_model_id({"prompt": "no model here"}) == "" | ||
| assert _extract_model_id(None, None) == "" | ||
| assert _extract_model_id({"model_id": " "}) == "" # whitespace-only ignored | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # _create_byoc_payment wire contract | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_payment_body_carries_real_capability_and_model_id(): | ||
| """A representative BYOC capability sends the REAL capability + model_id.""" | ||
| with _mock_signer() as captured: | ||
| result = _create_byoc_payment( | ||
| orch_origin="https://orch.test:8936", | ||
| capability="nano-banana", | ||
| livepeer_hdr="", | ||
| signer_url="https://signer.test", | ||
| signer_headers={"Authorization": "Bearer sk_test"}, | ||
| model_id="google/nano-banana", | ||
| ) | ||
|
|
||
| body = _signer_body(captured) | ||
| assert body["capability"] == "nano-banana" | ||
| assert body["model_id"] == "google/nano-banana" | ||
| # type stays lv2v so fee/pixel routing is unchanged | ||
| assert body["type"] == "lv2v" | ||
| assert body["orchestrator"] # base64 orch info present | ||
| # payment headers still returned as before | ||
| assert result["Livepeer-Payment"] == "TICKETS" | ||
| assert result["Livepeer-Segment"] == "SEG" | ||
|
|
||
|
|
||
| def test_payment_body_omits_model_id_when_absent(): | ||
| """Backward-compatible: no model id → field omitted (signer falls back).""" | ||
| with _mock_signer() as captured: | ||
| _create_byoc_payment( | ||
| orch_origin="https://orch.test:8936", | ||
| capability="nano-banana", | ||
| livepeer_hdr="", | ||
| signer_url="https://signer.test", | ||
| signer_headers={"Authorization": "Bearer sk_test"}, | ||
| # model_id omitted → defaults to "" | ||
| ) | ||
|
|
||
| body = _signer_body(captured) | ||
| assert body["capability"] == "nano-banana" | ||
| assert "model_id" not in body, "empty model_id must be omitted for byte-identical body" | ||
| assert body["type"] == "lv2v" | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # End-to-end through submit_byoc_job | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_submit_byoc_job_threads_model_id_from_payload(): | ||
| """submit_byoc_job extracts the model id from the request payload and | ||
| forwards it (and the capability) to the signer payment request.""" | ||
| from livepeer_gateway import byoc as byoc_mod | ||
|
|
||
| captured: list[Request] = [] | ||
|
|
||
| class _MockResponse: | ||
| def __init__(self, body: bytes, status: int = 200): | ||
| self._body = body | ||
| self.status = status | ||
| self.headers = {} | ||
|
|
||
| def read(self): | ||
| return self._body | ||
|
|
||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, *a): | ||
| return False | ||
|
|
||
| def _fake_urlopen(req, *args, **kwargs): | ||
| captured.append(req) | ||
| url = req.full_url if hasattr(req, "full_url") else req.get_full_url() | ||
| if "generate-live-payment" in url: | ||
| return _MockResponse(b'{"payment":"TICKETS","segCreds":"SEG"}') | ||
| if "sign-byoc-job" in url: | ||
| return _MockResponse(b'{"sender":"0xabc","signature":"0xsig"}') | ||
| # orchestrator /process/request/<cap> | ||
| return _MockResponse(b'{"images":[{"url":"https://img"}]}', 200) | ||
|
|
||
| req = ByocJobRequest( | ||
| capability="nano-banana", | ||
| payload={"prompt": "a dragon", "model_id": "google/nano-banana"}, | ||
| ) | ||
|
|
||
| with patch("livepeer_gateway.byoc.urlopen", side_effect=_fake_urlopen), patch( | ||
| "livepeer_gateway.orch_info.get_orch_info", | ||
| side_effect=lambda *a, **k: _stub_orch_info(), | ||
| ): | ||
| byoc_mod.submit_byoc_job( | ||
| req, | ||
| orch_url="https://orch.test:8936", | ||
| signer_url="https://signer.test", | ||
| signer_headers={"Authorization": "Bearer sk_test"}, | ||
| ) | ||
|
|
||
| body = _signer_body(captured) | ||
| assert body["capability"] == "nano-banana" | ||
| assert body["model_id"] == "google/nano-banana" | ||
| assert body["type"] == "lv2v" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 57e9e16.
_create_byoc_paymentnow doesmodel_id = model_id.strip() if isinstance(model_id, str) else ""before theif model_id:guard, so whitespace-only or non-string values are omitted and the wire body stays byte-identical to today when no real model id is present.