You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(usage): list an OpenCode 2 OAuth sign-in as a signed-in account - #4749
OpenCode 2 stores a sign-in made with opencode auth login (device or browser flow) in the credential table of opencode.db as {"type":"oauth","access":…,"expires":…,"metadata":{accountID,email,orgID,server}}. opencodeStore() only accepted {"type":"key"} rows, so these sign-ins read as signed out and the Agents panel said "No accounts signed in".
What changes, all in server/usage.ts:
The active OAuth row counts as a sign-in. Within each integration (opencode-go, then opencode), the row is still picked the old way: the active one, else the newest. A key in either picked row still wins, exactly as before. An OAuth sign-in is used only when neither picked row is a key, and only if its row has active = 1. OpenCode keeps the rows from earlier sign-ins as inactive, so signing in twice doesn't create two accounts.
Account identity is accountID__orgID. Signing in again keeps the same account key. The display name is the sign-in's email, or the row's label (for example "Personal") when the metadata has no email. If the metadata has neither ID, the row ID is used.
Go limits are read the way OpenCode 2.4.1 reads them:GET https://opencode.ai/zen/go/v1/usage with Authorization: Bearer <access> and x-opencode-org-id: <orgID>. The token is only sent to opencode.ai. A sign-in for another console, or one with no workspace, is listed as signed in and no request is made.
An OAuth sign-in without Go is still listed. On a 403 it shows as signed in with no limits ("No limits reported"). A key without Go is still left out, as before.
Still read-only. Tokens are never refreshed, the database is opened read-only, and the refresh token is never read. An access token past expires shows as problem: "expired" through the existing path, and OpenCode refreshes it the next time it runs.
server/usage-login.ts, /api/usage, and the Agents panel needed no changes: they already work from signIns() / read(). The OpenCode row in docs/guide.md's usage table is updated, and there's a changelog.d fragment.
One behavior change: an active OAuth row next to an inactive key row in the same integration used to read as signed out. It now reads as the OAuth sign-in. The inactive key was never read before either.
Validation
bun test server/usage.test.ts: 132 pass, 0 fail. The 7 new OpenCode cases cover: an active OAuth row with Go limits and the request headers, OAuth without Go (403), a key without Go still left out, label fallback and no request when there's no workspace or the console isn't opencode.ai, an expired access token, a key ahead of OAuth with the active row winning within an integration, and an inactive-only OAuth row. Before the fix, 4 of the new cases failed.
bun run typecheck: ok.
bun run check fast: workflow syntax, generated types and themes, typecheck and build all ok. Unit tests: 27931 pass, 3 fail. The same 3 tests also fail on an untouched main on the machine I tested with, so they aren't caused by this change:
scripts/install-binary.test.ts: "gives the installed binary a PATH with ~/.local/bin…"
server/herdr-core/discovery.test.ts: "refuses a group-writable folder whose ACL lets another account write…"
scripts/plugin-launch.test.ts: "exits with 128 + the platform's number of whatever signal ended the child"
I ran signIns() read-only against a real OpenCode 2.4.1 store that had a device sign-in, with the network stubbed out. It returned one sign-in, named by email, with a workspace. Before this change it returned none.
One live request through UsageService against that same store (an account with Go): GET https://opencode.ai/zen/go/v1/usage with the access token and x-opencode-org-id answered 200 with the same {usage:{rolling,weekly,monthly}} shape a key gets. The account was listed by email with plan Go, problem: null and its session, week and month limits. The no-Go path (403) is covered by tests only.
Contract tests that cover /api/usage, on an isolated herdr session (ovis-test-1): server/api.contract.test.ts, server/usage-login.contract.test.ts and server/statusline-switch.contract.test.ts: 245 pass, 0 fail. This includes "returns OpenCode Go windows without exposing its credential", which checks that the /api/usage response never contains the credential.
bun run test:ui (with CHROME_PATH=/usr/bin/chromium): every script passes except one check. checkTerminalCopy in scripts/ui-contexts-regression.ts times out waiting for the terminal prompt to render, and it times out the same way on a fresh build of main (Tests: checkTerminalCopy times out waiting for the fixture prompt on a local run #4763). ui-contexts-layout-regression.ts (61 pass) and sticky-modifiers-regression.ts (2 pass) were run on their own, because the failure stopped the chain before them.
bun run check full as one command stops at the unit step because of the 3 known unit failures above. So I ran its integration and browser stages directly. No screenshots: there's no UI change.
OpenCode 2's device and browser sign-ins are stored as OAuth rows in the
credential table, which opencodeStore() skipped. The OpenCode row then
said "No accounts signed in". The active OAuth row now counts as a
sign-in, and its Go limits are read the way OpenCode reads them.
Fixesdevswha#4683
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
Bug Fixes
OpenCode 2 accounts signed in through the browser or device flow now appear as signed-in accounts instead of “No accounts signed in.”
Accounts with an eligible Go subscription show their usage limits. Accounts without an eligible subscription or workspace remain listed without limits.
Inactive or expired sign-ins are not shown as active accounts, and Go API-key credentials take precedence over OAuth sign-ins.
OpenCode credential discovery now recognizes active OAuth rows in OpenCode 2’s database and derives account and workspace metadata. Eligible tokens can request Go usage. The reader preserves key precedence and reports OAuth sign-ins without eligible usage. Tests and documentation cover these cases.
The store reads active OAuth credentials and retains API-key precedence. OAuth credentials provide account and eligible workspace metadata. Tests cover row labels and key selection.
Usage reporting and documented behavior server/usage.ts, server/usage.test.ts, docs/guide.md, changelog.d/opencode-oauth-sign-in.md
Database sign-ins take precedence over auth.json when the store is recognized. Eligible OAuth sign-ins request usage with the workspace header. Ineligible or expired sign-ins remain listed without usage requests, and HTTP 403 returns a signed-in result. Tests and documentation cover these outcomes.
OpenCode 2 device and browser sign-ins now appear as signed-in accounts instead of "No accounts signed in", with Go limits shown when the account has Go. The change is read-only, keeps API-key precedence, and falls back to "signed in, no limits" if the endpoint rejects the token. It is ready to merge with no known blocking risk. Live acceptance of OAuth tokens by the hosted Go endpoint has not been confirmed.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title "fix(usage): list an OpenCode 2 OAuth sign-in as a signed-in account" names the main change: OpenCode 2 OAuth sign-ins are now listed as signed-in accounts instead of "No accounts signed in"…
Linked Issues check
Issue #4683 asks that an active OpenCode OAuth sign-in appear as a signed-in account, with usage shown or marked unavailable. The whole-PR diff (base 6946dcb to head 7500867) implements this in server…
Out of Scope Changes check
All changes connect to #4683. server/usage.ts holds the OAuth discovery, identity, request, and 403 handling. server/usage.test.ts adds coverage for these paths and extends the opencodeStore test help…
Description check
The description follows the repository template. It has a Change section that explains the problem, the resulting behavior, and the linked issue (#4683). It also has a Validation section that lists th…
Full details: Title check
Explanation
The title "fix(usage): list an OpenCode 2 OAuth sign-in as a signed-in account" names the main change: OpenCode 2 OAuth sign-ins are now listed as signed-in accounts instead of "No accounts signed in". The changeset matches this in server/usage.ts, server/usage.test.ts, docs/guide.md and a changelog fragment. The title is specific and follows a conventional commit style.
Full details: Linked Issues check
Explanation
Issue #4683 asks that an active OpenCode OAuth sign-in appear as a signed-in account, with usage shown or marked unavailable. The whole-PR diff (base 6946dcb to head 7500867) implements this in server/usage.ts. opencodeStore() now reads oauth rows with active = 1 in each picked row (opencode-go, then opencode) and returns them as a SignIn only when neither picked row is a key. Keys still win. opencodeOauth() builds the account from accountID__orgID, falls back to the row ID, and uses the email or row label as the name. The Go request goes only to https://opencode.ai/zen/go/v1/usage with the Bearer token and x-opencode-org-id. A sign-in with no eligible workspace, or with another console, returns a signed-in reading without a request. A 403 returns a signed-in reading with no limits. Keys without Go are still omitted, because read() returns null for keys on 403. Refresh tokens are not read, and the database is opened read-only. Tests and docs cover these cases. I did not run the test suite. The verdict rests on the diff reading.
Full details: Out of Scope Changes check
Explanation
All changes connect to #4683. server/usage.ts holds the OAuth discovery, identity, request, and 403 handling. server/usage.test.ts adds coverage for these paths and extends the opencodeStore test helper with labels. docs/guide.md updates the OpenCode row of the usage table. changelog.d adds a fragment. server/usage-login.ts, /api/usage, and the Agents panel are unchanged, as the PR states. The reader still does not fall back to auth.json when a recognized store holds no sign-in, which matches the pre-change behavior. No unrelated change is evident.
Full details: Description check
Explanation
The description follows the repository template. It has a Change section that explains the problem, the resulting behavior, and the linked issue (#4683). It also has a Validation section that lists the commands run, their results, the known pre-existing failures, and the skipped stages (full check run as separate stages, no screenshots). The description is detailed and specific. Contract-test coverage for the HTTP change is listed, which the template asks for on HTTP changes. The only gap is that the author notes the live-request and check-full results differ from the objectives text, but the description itself is complete.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Reviewing files that changed from the base of the PR and between 22abe50 and 7500867.
Files selected for processing: 4
changelog.d/opencode-oauth-sign-in.md
docs/guide.md
server/usage.test.ts
server/usage.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
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
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.
Fixes #4683
Change
OpenCode 2 stores a sign-in made with
opencode auth login(device or browser flow) in thecredentialtable ofopencode.dbas{"type":"oauth","access":…,"expires":…,"metadata":{accountID,email,orgID,server}}.opencodeStore()only accepted{"type":"key"}rows, so these sign-ins read as signed out and the Agents panel said "No accounts signed in".What changes, all in
server/usage.ts:opencode-go, thenopencode), the row is still picked the old way: the active one, else the newest. A key in either picked row still wins, exactly as before. An OAuth sign-in is used only when neither picked row is a key, and only if its row hasactive = 1. OpenCode keeps the rows from earlier sign-ins as inactive, so signing in twice doesn't create two accounts.accountID__orgID. Signing in again keeps the same account key. The display name is the sign-in's email, or the row'slabel(for example "Personal") when the metadata has no email. If the metadata has neither ID, the row ID is used.GET https://opencode.ai/zen/go/v1/usagewithAuthorization: Bearer <access>andx-opencode-org-id: <orgID>. The token is only sent toopencode.ai. A sign-in for another console, or one with no workspace, is listed as signed in and no request is made.expiresshows asproblem: "expired"through the existing path, and OpenCode refreshes it the next time it runs.server/usage-login.ts,/api/usage, and the Agents panel needed no changes: they already work fromsignIns()/read(). The OpenCode row indocs/guide.md's usage table is updated, and there's achangelog.dfragment.One behavior change: an active OAuth row next to an inactive key row in the same integration used to read as signed out. It now reads as the OAuth sign-in. The inactive key was never read before either.
Validation
bun test server/usage.test.ts: 132 pass, 0 fail. The 7 new OpenCode cases cover: an active OAuth row with Go limits and the request headers, OAuth without Go (403), a key without Go still left out, label fallback and no request when there's no workspace or the console isn't opencode.ai, an expired access token, a key ahead of OAuth with the active row winning within an integration, and an inactive-only OAuth row. Before the fix, 4 of the new cases failed.bun run typecheck: ok.bun run check fast: workflow syntax, generated types and themes, typecheck and build all ok. Unit tests: 27931 pass, 3 fail. The same 3 tests also fail on an untouchedmainon the machine I tested with, so they aren't caused by this change:scripts/install-binary.test.ts: "gives the installed binary a PATH with ~/.local/bin…"server/herdr-core/discovery.test.ts: "refuses a group-writable folder whose ACL lets another account write…"scripts/plugin-launch.test.ts: "exits with 128 + the platform's number of whatever signal ended the child"I ran
signIns()read-only against a real OpenCode 2.4.1 store that had a device sign-in, with the network stubbed out. It returned one sign-in, named by email, with a workspace. Before this change it returned none.One live request through
UsageServiceagainst that same store (an account with Go):GET https://opencode.ai/zen/go/v1/usagewith the access token andx-opencode-org-idanswered 200 with the same{usage:{rolling,weekly,monthly}}shape a key gets. The account was listed by email with plan Go,problem: nulland its session, week and month limits. The no-Go path (403) is covered by tests only.Contract tests that cover
/api/usage, on an isolated herdr session (ovis-test-1):server/api.contract.test.ts,server/usage-login.contract.test.tsandserver/statusline-switch.contract.test.ts: 245 pass, 0 fail. This includes "returns OpenCode Go windows without exposing its credential", which checks that the/api/usageresponse never contains the credential.bun run test:integration(all 177 files, on isolatedovis-test-1to-4): 2730 pass, 2 fail. Both failures are "SIGKILL after durable-revoke" inserver/account/step2-upgrade-crash.contract.test.ts. They fail the same way in a clean clone ofmain(Tests: step2-upgrade-crash durable-revoke cases fail on a PC with its own herdr #4762).bun run test:ui(withCHROME_PATH=/usr/bin/chromium): every script passes except one check.checkTerminalCopyinscripts/ui-contexts-regression.tstimes out waiting for the terminal prompt to render, and it times out the same way on a fresh build ofmain(Tests: checkTerminalCopy times out waiting for the fixture prompt on a local run #4763).ui-contexts-layout-regression.ts(61 pass) andsticky-modifiers-regression.ts(2 pass) were run on their own, because the failure stopped the chain before them.bun run check fullas one command stops at the unit step because of the 3 known unit failures above. So I ran its integration and browser stages directly. No screenshots: there's no UI change.🤖 Generated with Claude Code