Skip to content

Add OpenCode Go collector for agents panel - #8823

Closed
kczimm wants to merge 4 commits into
omacom:quattrofrom
kczimm:add-opencode-go-collector
Closed

kczimm wants to merge 4 commits into
omacom:quattrofrom
kczimm:add-opencode-go-collector

Conversation

@kczimm

@kczimm kczimm commented Aug 28, 2026

Copy link
Copy Markdown

Add OpenCode Go collector for agents panel

Summary

Adds native support for OpenCode Go subscriptions in the agents panel. OpenCode Go is a $10/month subscription providing access to popular open coding models, used by Pi, OpenCode CLI, and other harnesses.

Changes

  • bin/omarchy-agent-usage-opencode-go — New collector that queries OpenCode's official /zen/go/v1/usage API for rolling (5h), weekly, and monthly limit usage with reset timers
  • shell/plugins/agents/assets/opencode-go.svg — Dark mode icon
  • shell/plugins/agents/assets/opencode-go-light.svg — Light mode icon
  • shell/plugins/agents/manifest.json — Add opencode-go to default providers
  • shell/plugins/agents/README.md — Document the new collector and its key discovery logic

Design decisions

Harness-agnostic key discovery

The OpenCode Go API key is the same regardless of which harness (Pi, OpenCode CLI, etc.) connects to the subscription. The collector searches multiple auth locations:

  1. OPENCODE_GO_API_KEY environment variable
  2. ~/.pi/agent/auth.json (Pi harness)
  3. ~/.local/share/opencode/auth.json (OpenCode CLI)

Adding a new harness requires only appending to AUTH_PATHS in the script.

curl instead of urllib

Other collectors use urllib.request directly, but opencode.ai is behind Cloudflare which blocks requests based on TLS fingerprinting (JA3/JA4). Python's ssl module produces a fingerprint Cloudflare flags as non-browser traffic, returning 403. Curl has a browser-like fingerprint and passes through. This deviation is documented in the script.

No local stats

Unlike Claude/Codex collectors that parse local session files, this collector relies solely on the server-side API. OpenCode's local database doesn't reliably represent server-side accounting, especially when using multiple machines. The API provides accurate, real-time limits.

Testing

Verified working on a live OpenCode Go subscription:

  • API returns rolling/weekly/monthly usage percentages and reset timestamps
  • Panel displays correctly with limit meters
  • IPC refresh works: omarchy-shell omarchy.agents refresh

Related

Adds native support for OpenCode Go subscriptions in the agents panel.
Queries OpenCode's official /zen/go/v1/usage API for rolling (5h),
weekly, and monthly limit usage with reset timers.

Key design decisions:
- Harness-agnostic: reads API key from Pi, OpenCode CLI, or env var
- Uses curl instead of urllib to bypass Cloudflare TLS fingerprinting
- Includes light/dark icon variants matching OpenCode brand colors
- Adds test coverage for auth lookup, API parsing, and error handling
Copilot AI balanced review requested due to automatic review settings August 28, 2026 21:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds OpenCode Go subscription usage to the agents panel through a new API collector, provider configuration, documentation, tests, and themed icons.

Changes:

  • Collects rolling, weekly, and monthly limits.
  • Discovers credentials from environment and harness auth files.
  • Enables and documents the provider with light/dark assets.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Reviewed changes

Copilot reviewed 3 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
bin/omarchy-agent-usage-opencode-go Implements credential discovery and usage collection.
test/shell.d/agent-usage-opencode-go-scanner-test.sh Tests collector output and failures.
shell/plugins/agents/manifest.json Enables OpenCode Go by default.
shell/plugins/agents/README.md Documents provider behavior and setup.
shell/plugins/agents/assets/opencode-go.svg Adds the dark-surface icon.
shell/plugins/agents/assets/opencode-go-light.svg Adds the light-surface icon.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread shell/plugins/agents/README.md Resolved
kczimm added 2 commits August 28, 2026 16:57
Adds native support for OpenCode Go subscriptions in the agents panel.
Queries OpenCode's official /zen/go/v1/usage API for rolling (5h),
weekly, and monthly limit usage with reset timers.

Unique features compared to other PRs (omacom#7157, omacom#8065, omacom#6526, omacom#6779, omacom#6464):
- Pi session scanning for local token stats
- Harness-agnostic key discovery (Pi, OpenCode CLI, env var)
- Account-scoped metadata (scope, hasPromptStats)

Key design decisions:
- Uses curl instead of urllib to bypass Cloudflare TLS fingerprinting
- Includes light/dark icon variants matching OpenCode brand colors
- Adds test coverage for auth lookup, API parsing, and error handling
Adds native support for OpenCode Go subscriptions in the agents panel.
Queries OpenCode's official /zen/go/v1/usage API for rolling (5h),
weekly, and monthly limit usage with reset timers.

Features matching codex/claude collectors:
- Limits caching with stale-window detection (limit_window_open)
- Scan caching with file locking (fcntl.flock)
- --force bypasses caches, --limits-only reuses scan cache
- Pi session scanning for local token stats
- OpenCode DB scanning with providerID LIKE narrowing
- Harness-agnostic key discovery (Pi, OpenCode CLI, env var)
- Account-scoped metadata (scope='account', hasPromptStats=false)
- Manual/17-ai.md documentation

Bug fixes from review:
- Fix todayTokensByModel not populated from Pi sessions
- Gate scan cache on DB scan completeness
- Add json_extract guard for providerID in SQL
- Move import hashlib to top

Test coverage: 16 tests covering API parsing, provider detection,
Pi session scanning, todayTokensByModel, and error handling.
@averyrobbins

Copy link
Copy Markdown

Tried this collector on a live OpenCode Go subscription (Omarchy 4.0.2-1, agents plugin 1.0.0). The limits half works great — /zen/go/v1/usage returns rolling/weekly/monthly with reset timers exactly as described. Thanks for building it.

One bug though: local stats are dropped on any day the subscription hasn't been used yet.

In main():

local_stats = cached_local_stats(max_age)
if local_stats.get("todayTotalTokens", 0) > 0:
    record.update(local_stats)

Because the guard keys on today's tokens, a scan that found plenty of history still gets discarded whenever today's total is 0. The panel then renders limit meters above a permanently empty "Tokens by day" and "Tokens by model" — which is the common case first thing in the morning, or for anyone who uses the subscription a few days a week.

Repro

With OpenCode Go usage on disk from previous days but none today:

$ omarchy-agent-usage-opencode-go --force | jq '{totalPrompts, activeDays, modelUsage}'
{
  "totalPrompts": 0,
  "activeDays": 0,
  "modelUsage": {}
}

The scan itself is fine — the data is found and cached correctly, it just never reaches the record:

# same run, calling the scan directly
>>> complete, stats = scan_opencode_sessions()
totalPrompts: 124   activeDays: 2
modelUsage: muse-spark-1.3-contributor, grok-4.6

And the on-disk scan cache holds the right values too:

$ jq '.stats | {totalPrompts, activeDays}' ~/.cache/omarchy/agent-usage/opencode-go-scan-*.json
{ "totalPrompts": 124, "activeDays": 2 }

Suggested fix

Gate on whether the scan produced anything at all rather than on today specifically:

if (local_stats.get("todayTotalTokens", 0) > 0
        or local_stats.get("totalPrompts", 0) > 0
        or local_stats.get("modelUsage")):
    record.update(local_stats)

That keeps the original intent — don't clobber the record with an empty scan — while letting prior-day history through. Running with this change, the same machine reports totalPrompts: 124, activeDays: 2, and both models, and the panel charts populate.

Happy to open a PR against your branch if that's easier.

- Fix local_stats guard: accept prior-day history, not just today's tokens
- Fix shebang to #!/usr/bin/python3 (match other collectors)
- Add runtime_env/find_command for ripgrep PATH discovery (match codex)
- Pass env=ENV to ripgrep subprocess for expanded PATH
- Hide API key from /proc/cmdline via curl -H @file pattern
- Merge totalSessions/todaySessions alongside other counters
- Fix manual JSONL scan fallback when rg is not found
- Fix scan_opencode_sessions return type annotation
- README: accurately describe local data sources in provider table
- Add compact-JSON test, merge logic test, cache test, auth file test
@kczimm

kczimm commented Sep 10, 2026

Copy link
Copy Markdown
Author

@averyrobbins thanks for the thorough review. The guard is now fixed to accept prior-day history.

@bjarneo

bjarneo commented Sep 20, 2026

Copy link
Copy Markdown
Member

Automated duplication check: this pull request looks similar to #8065, which covers the same OpenCode usage collector. I keep that one open and close this one to consolidate review.

If you feel this is the wrong decision, please open the PR again with a note on the difference.

@bjarneo bjarneo closed this Sep 20, 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.

4 participants