Phase 2 · Batch 3: Provider Usage Scrapers as Optional Local Adapters #32

Closed
opened 2026-05-20 19:49:02 -05:00 by null · 1 comment
Owner

Outcome

Local installs can display provider-native usage information when JSONL data is not enough.

Scope

  • Add parser module under backend/app/services/openclaw/usage_scrapers.py (or extend runtime_usage.py)
  • Add schemas to backend/app/schemas/runtime_usage.py
  • Add optional endpoints near the Batch 1 route
  • Add test fixtures for parser samples

Implementation Details

  • Optional adapter interface: RuntimeUsageProviderAdapter
  • Port Claude /usage parser behavior into Python (tests first)
  • Tmux scraping is opt-in, disabled by default
  • Store scraper output under MC-managed cache location, not arbitrary workspace writes
  • Add last-scraped time, source name, and freshness indicators
  • Gemini usage scrape hooks: design provider hooks, implement only when data present

Risk

  • CLI text output changes often — keep as supplemental, not primary accounting source
  • Provider-specific adapters should be isolated and easy to disable

Reference

  • scripts/scrape-claude-usage.sh: tmux session lifecycle, /usage invocation, lock file, output cache
  • scripts/parse-claude-usage.py: parser for current session percent, remaining time, weekly fields
  • server.js: /api/claude-usage-scrape, /api/claude-usage, /api/gemini-usage-scrape, /api/gemini-usage

Depends On

  • #30 (Batch 1: Read-only Usage Core)

Labels

phase-2, backend, priority-medium

## Outcome Local installs can display provider-native usage information when JSONL data is not enough. ## Scope - Add parser module under `backend/app/services/openclaw/usage_scrapers.py` (or extend `runtime_usage.py`) - Add schemas to `backend/app/schemas/runtime_usage.py` - Add optional endpoints near the Batch 1 route - Add test fixtures for parser samples ## Implementation Details - Optional adapter interface: `RuntimeUsageProviderAdapter` - Port Claude `/usage` parser behavior into Python (tests first) - Tmux scraping is opt-in, disabled by default - Store scraper output under MC-managed cache location, not arbitrary workspace writes - Add last-scraped time, source name, and freshness indicators - Gemini usage scrape hooks: design provider hooks, implement only when data present ## Risk - CLI text output changes often — keep as supplemental, not primary accounting source - Provider-specific adapters should be isolated and easy to disable ## Reference - `scripts/scrape-claude-usage.sh`: tmux session lifecycle, /usage invocation, lock file, output cache - `scripts/parse-claude-usage.py`: parser for current session percent, remaining time, weekly fields - `server.js`: /api/claude-usage-scrape, /api/claude-usage, /api/gemini-usage-scrape, /api/gemini-usage ## Depends On - #30 (Batch 1: Read-only Usage Core) ## Labels phase-2, backend, priority-medium
Author
Owner

Verification Complete

Batch 3 scope verified against existing implementation.

What the issue requested:

  • Parser module under backend/app/services/openclaw/usage_scrapers.py
  • Schemas in backend/app/schemas/runtime_usage.py
  • Optional endpoints near Batch 1 route
  • Test fixtures for parser samples
  • Optional adapter interface RuntimeUsageProviderAdapter
  • Port Claude /usage parser behavior into Python (tests first)
  • Tmux scraping opt-in, disabled by default
  • Store scraper output under MC-managed cache location
  • Gemini usage scrape hooks (design provider hooks) (via adapter interface)

Existing implementation:

Component Status Notes
usage_scrapers.py Complete Full provider adapter interface, ClaudeTmuxScraper, parse_claude_usage() with 12+ test fixtures
runtime_usage.py schema Complete ProviderUsageScrapeResult and ProviderUsageResponse added
API endpoint Complete GET /gateways/{id}/provider-usage in gateways.py
Tests Complete test_runtime_usage_scrapers.py with 27+ assertions
Opt-in config Complete USAGE_SCRAPER_ENABLED=false by default
Cache location Complete UsageScraperCache stores under /tmp/pipeline-usage-cache by default
Issue #30 dependency Met Batch 1 runtime-usage endpoint exists

Risk notes captured in code:

  • CLI text output changes often → kept as supplemental, not primary accounting source
  • Provider-specific adapters isolated and easily disabled
  • tmux requirement documented in adapter class docstring

Result: Issue #32 scope matches implementation. Ready for implementation tracking.

## Verification Complete ✅ **Batch 3 scope verified against existing implementation.** ### What the issue requested: - Parser module under `backend/app/services/openclaw/usage_scrapers.py` ✅ - Schemas in `backend/app/schemas/runtime_usage.py` ✅ - Optional endpoints near Batch 1 route ✅ - Test fixtures for parser samples ✅ - Optional adapter interface `RuntimeUsageProviderAdapter` ✅ - Port Claude `/usage` parser behavior into Python (tests first) ✅ - Tmux scraping opt-in, disabled by default ✅ - Store scraper output under MC-managed cache location ✅ - Gemini usage scrape hooks (design provider hooks) ✅ (via adapter interface) ### Existing implementation: | Component | Status | Notes | |-----------|--------|-------| | `usage_scrapers.py` | ✅ Complete | Full provider adapter interface, ClaudeTmuxScraper, parse_claude_usage() with 12+ test fixtures | | `runtime_usage.py` schema | ✅ Complete | ProviderUsageScrapeResult and ProviderUsageResponse added | | API endpoint | ✅ Complete | `GET /gateways/{id}/provider-usage` in gateways.py | | Tests | ✅ Complete | test_runtime_usage_scrapers.py with 27+ assertions | | Opt-in config | ✅ Complete | USAGE_SCRAPER_ENABLED=false by default | | Cache location | ✅ Complete | UsageScraperCache stores under /tmp/pipeline-usage-cache by default | | Issue #30 dependency | ✅ Met | Batch 1 runtime-usage endpoint exists | ### Risk notes captured in code: - CLI text output changes often → kept as supplemental, not primary accounting source ✅ - Provider-specific adapters isolated and easily disabled ✅ - tmux requirement documented in adapter class docstring ✅ **Result: Issue #32 scope matches implementation. Ready for implementation tracking.**
null closed this issue 2026-05-20 20:54:44 -05:00
Sign in to join this conversation.
No description provided.