Skip to content

Revert 56 feat/coingecko opg price feed#59

Closed
kylexqian wants to merge 15 commits intomainfrom
revert-56-feat/coingecko-opg-price-feed
Closed

Revert 56 feat/coingecko opg price feed#59
kylexqian wants to merge 15 commits intomainfrom
revert-56-feat/coingecko-opg-price-feed

Conversation

@kylexqian
Copy link
Copy Markdown
Collaborator

No description provided.

dixitaniket and others added 15 commits April 1, 2026 18:53
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* feat: Add CoinGecko OPG/USD price feed background service

Replaces the hardcoded mock price (Decimal("1")) with a real CoinGecko
price feed that runs as a background daemon thread.

Key behaviour:
- Fetches OPG/USD price from CoinGecko /simple/token_price/base at startup
  and every 5 minutes thereafter (well within free-tier rate limits)
- Retries up to 3 times per refresh cycle with 10s delay between attempts;
  exits retry loop immediately on 429 (rate-limited) to avoid hammering
- Retains the last known good price on exhausted retries so live traffic
  is not disrupted by a transient CoinGecko outage
- Logs a WARNING when the cached price is older than 2× the refresh
  interval (background loop may be stuck)
- Tracks last_success, last_error, consecutive_failures, total_fetches,
  total_errors via get_status() / get_price_feed_status()
- get_price() raises ValueError when no price has ever been fetched,
  which propagates through dynamic_session_cost_calculator and the
  existing strict _resolve_session_request_cost monkey-patch to return
  HTTP 500 rather than silently charging an incorrect amount

Also adds:
- 29 unit tests (all mocked, no network required) covering the fetch
  helper, retry logic, rate-limit handling, stale warning, stats, and
  module-level singleton functions
- 4 integration tests that hit the live CoinGecko API; OPG-specific
  tests skip gracefully until the token is fully indexed

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor: Reorganize price feed into tee_gateway/price_feed/ package

Moves opg_price_feed.py into a dedicated package matching the layout of
the existing tee_gateway/heartbeat/ package:

  tee_gateway/price_feed/
    __init__.py   — re-exports public API (OPGPriceFeed, PriceFeedConfig,
                    start_price_feed, get_opg_price_usd, get_price_feed_status)
    config.py     — all constants (COINGECKO_BASE_URL, COINGECKO_PLATFORM,
                    FETCH_TIMEOUT, refresh/retry defaults, stale threshold)
                    plus the PriceFeedConfig frozen dataclass
    feed.py       — OPGPriceFeed class, fetch_opg_price(), singleton helpers

Also renames the test files to match:
  test_opg_price_feed.py            -> test_price_feed.py
  test_opg_price_feed_integration.py -> test_price_feed_integration.py

No behaviour changes — all 29 unit tests still pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor: dependency injection for OPG price feed via make_cost_calculator

Replace module-level singleton in price_feed with a factory/closure
pattern. make_cost_calculator(price_feed) in util.py binds an OPGPriceFeed
instance explicitly, eliminating hidden global state. Tests updated:
TestModuleLevelFunctions removed, TestMakeCostCalculator added (10 cases).
Also fix missing Any import in feed.py and unused PriceFeedConfig import.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor: replace make_cost_calculator factory with calculate_session_cost + named function

Remove the factory/closure pattern and _PriceSource Protocol. util.py now
exports calculate_session_cost(context, get_price) — a plain function that
accepts a Callable[[], Decimal]. __main__.py wires it via a named
_session_cost_calculator function that passes _price_feed.get_price directly.
Tests updated to match the new signature.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: include price feed status in /health response

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: verify calculate_session_cost fetches live price on every call

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: update test_pricing.py for calculate_session_cost signature change

Replace dynamic_session_cost_calculator import with calculate_session_cost
and pass _get_price (OPG=$1.00) to all call sites.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: add attr-defined to type: ignore on monkey-patch line

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: address Copilot review comments on price feed

- start() is now non-blocking: initial fetch runs inside the background
  thread instead of blocking the caller
- start() is idempotent: duplicate calls are a no-op if thread is alive
- Clear last_error on successful refresh so /health reflects recovery
- Gate integration tests behind RUN_INTEGRATION_TESTS env var to prevent
  real CoinGecko calls in CI by default
- Fix stale docstring references to make_cost_calculator → calculate_session_cost

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Update .github/workflows/test.yml

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix: update stale dynamic_session_cost_calculator references

Rename test classes and update comment/error message in __main__.py to
match the current calculate_session_cost / _session_cost_calculator names.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: add pre-inference pricing gate and remove ineffective monkey-patch

Replace the _strict_resolve_session_request_cost monkey-patch (which was
patching a method not called in the upto session flow) with a proper
before_request hook that rejects inference requests early if pricing
would fail:
  - 503 if the OPG price feed has no valid price yet
  - 400 if the requested model is not in the registry

_session_cost_calculator now logs CRITICAL with full traceback on any
post-inference cost failure (e.g. missing usage field) so uncharged
requests are never silently missed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: address Copilot review comments (docstrings, defensive coding, thread safety)

- Update start() docstring: replace monkey-patch reference with
  pre-inference gate
- Make fetch_opg_price() validate response.json() is a dict before
  calling .get(), raising ValueError instead of AttributeError on
  unexpected CoinGecko response shapes
- Make start() idempotency check thread-safe under _lock
- Clarify CRITICAL log: provider error, x402 swallows exception so
  client is not charged
- Update calculate_session_cost docstring: replace monkey-patch
  reference with pre-inference gate and CRITICAL log behavior

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: TGE fallback price and CoinGecko sanity checks

Before the TGE cutover (2026-04-21 12:30 UTC) get_price() returns a fixed
$0.10 fallback so inference requests can be priced before OPG is listed on
CoinGecko. After the cutover the live cached price is used. Also adds a
guard in fetch_opg_price() rejecting non-positive or non-finite prices from
the API response.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: expire stale OPG price after 4 hours

get_price() now raises ValueError if the cached price is older than 4 hours,
preventing billing on a price that is too outdated. A warning is still logged
at the existing 2 × refresh_interval threshold (~10 min) as an early signal.
The pre-inference gate in __main__.py will surface this as a 503.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Update tee_gateway/price_feed/feed.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update tee_gateway/util.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@kylexqian
Copy link
Copy Markdown
Collaborator Author

Accident

@kylexqian kylexqian closed this Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants